Skip to content

Commit 8e259e4

Browse files
fix errors
1 parent e27489e commit 8e259e4

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

internal/wrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func RunKubeStateMetricsWrapper(opts *options.Options) {
8989
crcViper.SetConfigFile(opts.CustomResourceConfigFile)
9090
if err := crcViper.ReadInConfig(); err != nil {
9191
if errors.Is(err, viper.ConfigFileNotFoundError{}) {
92-
klog.InfoS(err, "Custom resource configuration file not found", "file", opts.CustomResourceConfigFile)
92+
klog.InfoS("Custom resource configuration file not found at startup", "file", opts.CustomResourceConfigFile)
9393
} else if _, err = os.Stat(filepath.Clean(opts.CustomResourceConfigFile)); os.IsNotExist(err) {
9494
// Adding this check in addition to the above since viper.ConfigFileNotFoundError is not working as expected due to this issue -
9595
// https://github.com/spf13/viper/issues/1783

pkg/app/server.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,18 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error {
181181
}
182182

183183
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)
187195
}
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-
193196
}
194197

195198
resources := []string{}
@@ -543,11 +546,17 @@ func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.Con
543546
return yaml.NewDecoder(strings.NewReader(s)), nil
544547
}
545548
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
549559
}
550-
return yaml.NewDecoder(f), nil
551560
}
552561
return nil, nil
553562
}

pkg/options/options.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@ type Options struct {
4747
MetricOptInList MetricSet `yaml:"metric_opt_in_list"`
4848
Resources ResourceSet `yaml:"resources"`
4949

50-
cmd *cobra.Command
51-
Apiserver string `yaml:"apiserver"`
52-
CustomResourceConfig string `yaml:"custom_resource_config"`
53-
CustomResourceConfigFile string `yaml:"custom_resource_config_file"`
54-
Host string `yaml:"host"`
55-
Kubeconfig string `yaml:"kubeconfig"`
56-
Namespace string `yaml:"namespace"`
57-
Node NodeType `yaml:"node"`
58-
Pod string `yaml:"pod"`
59-
TLSConfig string `yaml:"tls_config"`
60-
TelemetryHost string `yaml:"telemetry_host"`
61-
62-
Config string
50+
cmd *cobra.Command
51+
Apiserver string `yaml:"apiserver"`
52+
CustomResourceConfig string `yaml:"custom_resource_config"`
53+
CustomResourceConfigFile string `yaml:"custom_resource_config_file"`
54+
ContinueWithoutCustomResourceConfigFile bool `yaml:"continue_without_custom_resource_config_file"`
55+
Host string `yaml:"host"`
56+
Kubeconfig string `yaml:"kubeconfig"`
57+
Namespace string `yaml:"namespace"`
58+
Node NodeType `yaml:"node"`
59+
Pod string `yaml:"pod"`
60+
TLSConfig string `yaml:"tls_config"`
61+
TelemetryHost string `yaml:"telemetry_host"`
62+
63+
Config string
64+
ContinueWithoutConfig bool `yaml:"continue_without_config"`
6365

6466
Namespaces NamespaceList `yaml:"namespaces"`
6567
NamespacesDenylist NamespaceList `yaml:"namespaces_denylist"`

0 commit comments

Comments
 (0)