Skip to content

Commit e27489e

Browse files
add flags for backcompat
1 parent 54a6806 commit e27489e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

internal/wrapper.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func RunKubeStateMetricsWrapper(opts *options.Options) {
5858
klog.InfoS("Options configuration file not found at startup", "file", file)
5959
} else {
6060
klog.ErrorS(err, "Error reading options configuration file", "file", file)
61+
}
62+
if !opts.ContinueWithoutConfig {
6163
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
6264
}
6365
}
@@ -87,11 +89,17 @@ func RunKubeStateMetricsWrapper(opts *options.Options) {
8789
crcViper.SetConfigFile(opts.CustomResourceConfigFile)
8890
if err := crcViper.ReadInConfig(); err != nil {
8991
if errors.Is(err, viper.ConfigFileNotFoundError{}) {
90-
klog.ErrorS(err, "Custom resource configuration file not found", "file", opts.CustomResourceConfigFile)
92+
klog.InfoS(err, "Custom resource configuration file not found", "file", opts.CustomResourceConfigFile)
93+
} else if _, err = os.Stat(filepath.Clean(opts.CustomResourceConfigFile)); os.IsNotExist(err) {
94+
// Adding this check in addition to the above since viper.ConfigFileNotFoundError is not working as expected due to this issue -
95+
// https://github.com/spf13/viper/issues/1783
96+
klog.InfoS("Custom resource configuration file not found at startup", "file", opts.CustomResourceConfigFile)
9197
} else {
9298
klog.ErrorS(err, "Error reading Custom resource configuration file", "file", opts.CustomResourceConfigFile)
9399
}
94-
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
100+
if !opts.ContinueWithoutCustomResourceConfigFile {
101+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
102+
}
95103
}
96104
crcViper.OnConfigChange(func(e fsnotify.Event) {
97105
klog.InfoS("Changes detected", "name", e.Name)

pkg/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ func (o *Options) AddFlags(cmd *cobra.Command) {
156156
o.cmd.Flags().Float64Var(&o.AutoGoMemlimitRatio, "auto-gomemlimit-ratio", float64(0.9), "The ratio of reserved GOMEMLIMIT memory to the detected maximum container or system memory. (experimental)")
157157
o.cmd.Flags().StringVar(&o.CustomResourceConfig, "custom-resource-state-config", "", "Inline Custom Resource State Metrics config YAML (experimental)")
158158
o.cmd.Flags().StringVar(&o.CustomResourceConfigFile, "custom-resource-state-config-file", "", "Path to a Custom Resource State Metrics config file (experimental)")
159+
o.cmd.Flags().BoolVar(&o.ContinueWithoutCustomResourceConfigFile, "continue-without-cr-config-file", false, "Continue running even if the config file specified by --custom-resource-state-config-file is not present. This is useful for scenarios where config file is not provided at startup but is provided later ex - via configmap. If this flag is set to true, kube-state-metrics will not exit with an error if the custom-resource-state-config file is not found, instead watches and reloads when it is created")
159160
o.cmd.Flags().StringVar(&o.Host, "host", "::", `Host to expose metrics on.`)
160161
o.cmd.Flags().StringVar(&o.Kubeconfig, "kubeconfig", "", "Absolute path to the kubeconfig file")
161162
o.cmd.Flags().StringVar(&o.Namespace, "pod-namespace", "", "Name of the namespace of the pod specified by --pod. "+autoshardingNotice)
162163
o.cmd.Flags().StringVar(&o.Pod, "pod", "", "Name of the pod that contains the kube-state-metrics container. "+autoshardingNotice)
163164
o.cmd.Flags().StringVar(&o.TLSConfig, "tls-config", "", "Path to the TLS configuration file")
164165
o.cmd.Flags().StringVar(&o.TelemetryHost, "telemetry-host", "::", `Host to expose kube-state-metrics self metrics on.`)
165166
o.cmd.Flags().StringVar(&o.Config, "config", "", "Path to the kube-state-metrics options config file")
167+
o.cmd.Flags().BoolVar(&o.ContinueWithoutConfig, "continue-without-config", false, "Continue running even if the config file specified by --config is not present. This is useful for scenarios where config file is not provided at startup but is provided later ex - via configmap. If this flag is set to true, kube-state-metrics will not exit with an error if the config file is not found, instead watches and reloads when it is created")
166168
o.cmd.Flags().StringVar((*string)(&o.Node), "node", "", "Name of the node that contains the kube-state-metrics pod. Most likely it should be passed via the downward API. This is used for daemonset sharding. Only available for resources (pod metrics) that support spec.nodeName fieldSelector. This is experimental.")
167169
o.cmd.Flags().Var(&o.AnnotationsAllowList, "metric-annotations-allowlist", "Comma-separated list of Kubernetes annotations keys that will be used in the resource' labels metric. By default the annotations metrics are not exposed. To include them, provide a list of resource names in their plural form and Kubernetes annotation keys you would like to allow for them (Example: '=namespaces=[kubernetes.io/team,...],pods=[kubernetes.io/team],...)'. A single '*' can be provided per resource instead to allow any annotations, but that has severe performance implications (Example: '=pods=[*]').")
168170
o.cmd.Flags().Var(&o.LabelsAllowList, "metric-labels-allowlist", "Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the labels metrics are not exposed. To include them, provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'. A single '*' can be provided per resource instead to allow any labels, but that has severe performance implications (Example: '=pods=[*]'). Additionally, an asterisk (*) can be provided as a key, which will resolve to all resources, i.e., assuming '--resources=deployments,pods', '=*=[*]' will resolve to '=deployments=[*],pods=[*]'.")

0 commit comments

Comments
 (0)