@@ -48,32 +48,21 @@ type Components struct {
4848 FaultRemediationReconciler reconciler.FaultRemediationReconciler
4949}
5050
51- // nolint: cyclop // todo
5251func InitializeAll (
5352 ctx context.Context ,
5453 params InitializationParams ,
5554 ctrlruntimeClient ctrlruntimeClient.Client ,
5655) (* Components , error ) {
5756 slog .Info ("Starting fault remediation module initialization" )
5857
59- tokenConfig , err := storeconfig . TokenConfigFromEnv ( "fault-remediation" )
58+ tokenConfig , tomlConfig , err := loadTokenAndTomlConfig ( params )
6059 if err != nil {
61- return nil , fmt . Errorf ( "failed to load token configuration: %w" , err )
60+ return nil , err
6261 }
6362
6463 builder := client .GetPipelineBuilder ()
6564 pipeline := builder .BuildQuarantinedAndDrainedNodesPipeline ()
6665
67- var tomlConfig config.TomlConfig
68- if err := configmanager .LoadTOMLConfig (params .TomlConfigPath , & tomlConfig ); err != nil {
69- return nil , fmt .Errorf ("error while loading the toml Config: %w" , err )
70- }
71-
72- // Validate the configuration for consistency
73- if err := tomlConfig .Validate (); err != nil {
74- return nil , fmt .Errorf ("configuration validation failed: %w" , err )
75- }
76-
7766 if params .DryRun {
7867 slog .Info ("Running in dry-run mode" )
7968 }
@@ -82,56 +71,25 @@ func InitializeAll(
8271 slog .Info ("Log collector enabled" )
8372 }
8473
85- remediationClient , err := remediation .NewRemediationClient (
86- ctrlruntimeClient ,
87- params .DryRun ,
88- tomlConfig ,
89- )
74+ remediationClient , stateManager , err := initRemediationAndStateManager (params .Config , ctrlruntimeClient ,
75+ params .DryRun , tomlConfig )
9076 if err != nil {
91- return nil , fmt . Errorf ( "error while initializing remediation client: %w" , err )
77+ return nil , err
9278 }
9379
94- kubeClient , err := kubernetes .NewForConfig (params .Config )
95- if err != nil {
96- return nil , fmt .Errorf ("error init kube client for state manager: %w" , err )
97- }
98-
99- stateManager := statemanager .NewStateManager (kubeClient )
100-
10180 slog .Info ("Successfully initialized client" )
10281
103- // Load datastore configuration
104- datastoreConfig , err := datastore .LoadDatastoreConfig ()
82+ ds , watcherInstance , healthEventStore , datastoreConfig , err := initDatastoreAndWatcher (ctx , pipeline )
10583 if err != nil {
106- return nil , fmt . Errorf ( "failed to load datastore configuration: %w" , err )
84+ return nil , err
10785 }
10886
109- // Convert store Config types to the types expected by reconciler
11087 clientTokenConfig := client.TokenConfig {
11188 ClientName : tokenConfig .ClientName ,
11289 TokenDatabase : tokenConfig .TokenDatabase ,
11390 TokenCollection : tokenConfig .TokenCollection ,
11491 }
11592
116- ds , err := datastore .NewDataStore (ctx , * datastoreConfig )
117- if err != nil {
118- return nil , fmt .Errorf ("error initializing datastore: %w" , err )
119- }
120-
121- // Create watcher using the factory pattern
122- watcherConfig := watcher.WatcherConfig {
123- Pipeline : pipeline ,
124- CollectionName : "HealthEvents" ,
125- }
126-
127- watcherInstance , err := watcher .CreateChangeStreamWatcher (ctx , ds , watcherConfig )
128- if err != nil {
129- return nil , fmt .Errorf ("error initializing change stream watcher: %w" , err )
130- }
131-
132- // Get the HealthEventStore for document operations
133- healthEventStore := ds .HealthEventStore ()
134-
13593 reconcilerCfg := reconciler.ReconcilerConfig {
13694 DataStoreConfig : * datastoreConfig ,
13795 TokenConfig : clientTokenConfig ,
@@ -150,3 +108,71 @@ func InitializeAll(
150108 ds , watcherInstance , healthEventStore , reconcilerCfg , params .DryRun ),
151109 }, nil
152110}
111+
112+ func loadTokenAndTomlConfig (params InitializationParams ) (* storeconfig.TokenConfig , * config.TomlConfig , error ) {
113+ tokenConfig , err := storeconfig .TokenConfigFromEnv ("fault-remediation" )
114+ if err != nil {
115+ return nil , nil , fmt .Errorf ("failed to load token configuration: %w" , err )
116+ }
117+
118+ var tomlConfig config.TomlConfig
119+ if err := configmanager .LoadTOMLConfig (params .TomlConfigPath , & tomlConfig ); err != nil {
120+ return nil , nil , fmt .Errorf ("error while loading the toml Config: %w" , err )
121+ }
122+
123+ if err := tomlConfig .Validate (); err != nil {
124+ return nil , nil , fmt .Errorf ("configuration validation failed: %w" , err )
125+ }
126+
127+ return & tokenConfig , & tomlConfig , nil
128+ }
129+
130+ func initRemediationAndStateManager (
131+ restConfig * rest.Config ,
132+ ctrlruntimeClient ctrlruntimeClient.Client ,
133+ dryRun bool ,
134+ tomlConfig * config.TomlConfig ,
135+ ) (* remediation.FaultRemediationClient , statemanager.StateManager , error ) {
136+ remediationClient , err := remediation .NewRemediationClient (ctrlruntimeClient , dryRun , * tomlConfig )
137+ if err != nil {
138+ return nil , nil , fmt .Errorf ("error while initializing remediation client: %w" , err )
139+ }
140+
141+ kubeClient , err := kubernetes .NewForConfig (restConfig )
142+ if err != nil {
143+ return nil , nil , fmt .Errorf ("error init kube client for state manager: %w" , err )
144+ }
145+
146+ stateManager := statemanager .NewStateManager (kubeClient )
147+
148+ return remediationClient , stateManager , nil
149+ }
150+
151+ func initDatastoreAndWatcher (
152+ ctx context.Context ,
153+ pipeline datastore.Pipeline ,
154+ ) (datastore.DataStore , datastore.ChangeStreamWatcher , datastore.HealthEventStore , * datastore.DataStoreConfig , error ) {
155+ datastoreConfig , err := datastore .LoadDatastoreConfig ()
156+ if err != nil {
157+ return nil , nil , nil , nil , fmt .Errorf ("failed to load datastore configuration: %w" , err )
158+ }
159+
160+ ds , err := datastore .NewDataStore (ctx , * datastoreConfig )
161+ if err != nil {
162+ return nil , nil , nil , nil , fmt .Errorf ("error initializing datastore: %w" , err )
163+ }
164+
165+ watcherConfig := watcher.WatcherConfig {
166+ Pipeline : pipeline ,
167+ CollectionName : "HealthEvents" ,
168+ }
169+
170+ watcherInstance , err := watcher .CreateChangeStreamWatcher (ctx , ds , watcherConfig )
171+ if err != nil {
172+ return nil , nil , nil , nil , fmt .Errorf ("error initializing change stream watcher: %w" , err )
173+ }
174+
175+ healthEventStore := ds .HealthEventStore ()
176+
177+ return ds , watcherInstance , healthEventStore , datastoreConfig , nil
178+ }
0 commit comments