@@ -181,15 +181,18 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
181
181
}
182
182
183
183
if opts .CustomResourceConfigFile != "" {
184
- crcFile , err := os .ReadFile (filepath .Clean (opts .CustomResourceConfigFile ))
185
- if err != nil {
186
- return fmt .Errorf ("failed to read custom resource config file: %v" , err )
184
+ if _ , err := os .Stat (filepath .Clean (opts .CustomResourceConfigFile )); os .IsNotExist (err ) {
185
+ klog .InfoS ("config file does not exist,ignoring" , "file" , opts .CustomResourceConfigFile )
186
+ } else {
187
+ crcFile , err := os .ReadFile (filepath .Clean (opts .CustomResourceConfigFile ))
188
+ if err != nil {
189
+ return fmt .Errorf ("failed to read custom resource config file: %v" , err )
190
+ }
191
+ configSuccess .WithLabelValues ("customresourceconfig" , filepath .Clean (opts .CustomResourceConfigFile )).Set (1 )
192
+ configSuccessTime .WithLabelValues ("customresourceconfig" , filepath .Clean (opts .CustomResourceConfigFile )).SetToCurrentTime ()
193
+ hash := md5HashAsMetricValue (crcFile )
194
+ configHash .WithLabelValues ("customresourceconfig" , filepath .Clean (opts .CustomResourceConfigFile )).Set (hash )
187
195
}
188
- configSuccess .WithLabelValues ("customresourceconfig" , filepath .Clean (opts .CustomResourceConfigFile )).Set (1 )
189
- configSuccessTime .WithLabelValues ("customresourceconfig" , filepath .Clean (opts .CustomResourceConfigFile )).SetToCurrentTime ()
190
- hash := md5HashAsMetricValue (crcFile )
191
- configHash .WithLabelValues ("customresourceconfig" , filepath .Clean (opts .CustomResourceConfigFile )).Set (hash )
192
-
193
196
}
194
197
195
198
resources := []string {}
@@ -543,11 +546,17 @@ func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.Con
543
546
return yaml .NewDecoder (strings .NewReader (s )), nil
544
547
}
545
548
if file := opts .CustomResourceConfigFile ; file != "" {
546
- f , err := os .Open (filepath .Clean (file ))
547
- if err != nil {
548
- return nil , fmt .Errorf ("unable to open Custom Resource State Metrics file: %v" , err )
549
+ if opts .ContinueWithoutCustomResourceConfigFile {
550
+ if _ , err := os .Stat (filepath .Clean (file )); os .IsNotExist (err ) {
551
+ klog .InfoS ("custom resource config file does not exist,ignoring" , "file" , file )
552
+ }
553
+ } else {
554
+ f , err := os .Open (filepath .Clean (file ))
555
+ if err != nil {
556
+ return nil , fmt .Errorf ("unable to open Custom Resource State Metrics file: %v" , err )
557
+ }
558
+ return yaml .NewDecoder (f ), nil
549
559
}
550
- return yaml .NewDecoder (f ), nil
551
560
}
552
561
return nil , nil
553
562
}
0 commit comments