@@ -2,9 +2,6 @@ package init
22
33import (
44 "context"
5- "errors"
6- "os"
7- "reflect"
85 "time"
96
107 "github.com/aws/aws-sdk-go-v2/aws"
@@ -15,15 +12,12 @@ import (
1512 "k8s.io/utils/strings/slices"
1613
1714 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/api"
18- "github.com/awslabs/amazon-eks-ami/nodeadm/internal/api/bridge"
1915 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/aws/imds"
2016 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/cli"
21- "github.com/awslabs/amazon-eks-ami/nodeadm/internal/configprovider"
2217 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/containerd"
2318 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/daemon"
2419 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/kubelet"
2520 "github.com/awslabs/amazon-eks-ami/nodeadm/internal/system"
26- "github.com/awslabs/amazon-eks-ami/nodeadm/internal/util"
2721)
2822
2923const (
@@ -38,7 +32,7 @@ func NewInitCommand() cli.Command {
3832 c .cmd .Description = "Initialize this instance as a node in an EKS cluster"
3933 c .cmd .StringSlice (& c .daemons , "d" , "daemon" , "specify one or more of `containerd` and `kubelet`. This is intended for testing and should not be used in a production environment." )
4034 c .cmd .StringSlice (& c .skipPhases , "s" , "skip" , "phases of the bootstrap you want to skip" )
41- c . cmd . String ( & c . configCache , "" , "config-cache" , "File path at which to cache the resolved/enriched config. This can make repeated init calls more efficient. JSON encoding will be used." )
35+ cli . RegisterFlagConfigCache ( c . cmd , & c . configCache )
4236 cli .RegisterFlagConfigSources (c .cmd , & c .configSources )
4337 return & c
4438}
@@ -70,7 +64,7 @@ func (c *initCmd) Run(log *zap.Logger, opts *cli.GlobalOptions) error {
7064
7165 log .Info ("Loading configuration.." , zap .Strings ("configSource" , c .configSources ), zap .String ("configCache" , c .configCache ))
7266
73- nodeConfig , isChanged , shouldEnrichConfig , err := c . resolveConfig (log )
67+ nodeConfig , isChanged , shouldEnrichConfig , err := cli . ResolveConfig (log , c . configSources , c . configCache )
7468 if err != nil {
7569 return err
7670 }
@@ -137,7 +131,7 @@ func (c *initCmd) Run(log *zap.Logger, opts *cli.GlobalOptions) error {
137131 }
138132
139133 // this is not fatal, so do not use a blocking error.
140- if err := saveCachedConfig (nodeConfig , c .configCache ); err != nil {
134+ if err := cli . SaveCachedConfig (nodeConfig , c .configCache ); err != nil {
141135 log .Error ("Failed to cache config" , zap .String ("configCache" , c .configCache ), zap .Error (err ))
142136 }
143137 }
@@ -162,52 +156,6 @@ func (c *initCmd) Run(log *zap.Logger, opts *cli.GlobalOptions) error {
162156 return nil
163157}
164158
165- // resolveConfig returns either the cached config or the provided config chain.
166- func (c * initCmd ) resolveConfig (log * zap.Logger ) (cfg * api.NodeConfig , isChanged bool , shouldEnrichConfig bool , err error ) {
167- var cachedConfig * api.NodeConfig
168- shouldEnrichConfig = false
169- if len (c .configCache ) > 0 {
170- config , err := loadCachedConfig (c .configCache )
171- if err != nil {
172- log .Warn ("failed to load cached config" , zap .Error (err ))
173- } else {
174- cachedConfig = config
175- }
176- }
177-
178- provider , err := configprovider .BuildConfigProviderChain (c .configSources )
179- if err != nil {
180- return nil , false , shouldEnrichConfig , err
181- }
182- nodeConfig , err := provider .Provide ()
183- // if the error is just that no config is provided, then attempt to use the
184- // cached config as a fallback. otherwise, treat this as a fatal error.
185- if errors .Is (err , configprovider .ErrNoConfigInChain ) && cachedConfig != nil {
186- log .Warn ("Falling back to cached config..." )
187- return cachedConfig , false , shouldEnrichConfig , nil
188- } else if err != nil {
189- return nil , false , shouldEnrichConfig , err
190- }
191-
192- // if the cached and the provider config specs are the same, we'll just
193- // use the cached spec because it also has the internal NodeConfig
194- // .status information cached.
195- //
196- // if perf of reflect.DeepEqual becomes an issue, look into something like: https://github.com/Wind-River/deepequal-gen
197- if cachedConfig != nil && reflect .DeepEqual (nodeConfig .Spec , cachedConfig .Spec ) {
198- return cachedConfig , false , shouldEnrichConfig , nil
199- }
200-
201- // If the code reaches here it means that either no-config is cached (isChanged = false)
202- // Or the cache exists and the cached spec does not match the node spec (isChanged = true)
203- // In both cases, the config should be enriched
204- shouldEnrichConfig = true
205-
206- // we return the presence of a cache as the `isChanged` value, because if we
207- // had a cache hit and didnt use it, it's because we have a modified config.
208- return nodeConfig , cachedConfig != nil , shouldEnrichConfig , nil
209- }
210-
211159// enrichConfig populates the internal .status portion of the NodeConfig, used
212160// only for internal implementation details.
213161func (* initCmd ) enrichConfig (log * zap.Logger , cfg * api.NodeConfig , opts * cli.GlobalOptions ) error {
@@ -257,24 +205,6 @@ func (*initCmd) enrichConfig(log *zap.Logger, cfg *api.NodeConfig, opts *cli.Glo
257205 return nil
258206}
259207
260- func loadCachedConfig (path string ) (* api.NodeConfig , error ) {
261- // #nosec G304 // intended mechanism to read user-provided config file
262- nodeConfigData , err := os .ReadFile (path )
263- if err != nil {
264- return nil , err
265- }
266- gvk := bridge .InternalGroupVersion .WithKind (api .KindNodeConfig )
267- return bridge .DecodeNodeConfig (nodeConfigData , & gvk )
268- }
269-
270- func saveCachedConfig (cfg * api.NodeConfig , path string ) error {
271- data , err := bridge .EncodeNodeConfig (cfg )
272- if err != nil {
273- return err
274- }
275- return util .WriteFileWithDir (path , data , 0644 )
276- }
277-
278208func (c * initCmd ) configureDaemons (log * zap.Logger , cfg * api.NodeConfig , daemons []daemon.Daemon ) error {
279209 for _ , daemon := range daemons {
280210 if len (c .daemons ) > 0 && ! slices .Contains (c .daemons , daemon .Name ()) {
0 commit comments