Skip to content

Commit 4b704fd

Browse files
author
Noah Perks Sloan
committed
fix: use a flag for the configuration file
1 parent ea79557 commit 4b704fd

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

docs/customresourcestate-metrics.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ registry and running your own build of KSM.
55

66
## Configuration
77

8-
The `KSM_CUSTOM_RESOURCE_METRICS` environment variable can be used to configure the metrics for custom resources:
8+
A configuration file is required to describe your custom resources and the fields to turn into metrics.
9+
10+
The contents of this file can be set as an environment variable, `KSM_CUSTOM_RESOURCE_METRICS`, or the
11+
flag `--custom-resource-state-config=/path/to/config.yaml` can be used to specify a file on the filesystem.
12+
The command flag takes precedence over the environment variable. e.g.,
913

1014
```yaml
1115
apiVersion: apps/v1

main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
}
5353

5454
var factories []customresource.RegistryFactory
55-
if config, set := resolveCustomResourceConfig(); set {
55+
if config, set := resolveCustomResourceConfig(opts); set {
5656
crf, err := customresourcestate.FromConfig(config)
5757
if err != nil {
5858
klog.Fatal(err)
@@ -66,16 +66,16 @@ func main() {
6666
}
6767
}
6868

69-
func resolveCustomResourceConfig() (customresourcestate.ConfigDecoder, bool) {
70-
if s, set := os.LookupEnv("KSM_CUSTOM_RESOURCE_METRICS"); set {
71-
return yaml.NewDecoder(strings.NewReader(s)), true
72-
}
73-
if file, set := os.LookupEnv("KSM_CUSTOM_RESOURCE_METRICS_FILE"); set {
69+
func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.ConfigDecoder, bool) {
70+
if file := opts.CustomResourceConfigFile; file != "" {
7471
f, err := os.Open(file)
7572
if err != nil {
7673
klog.Fatal(err)
7774
}
7875
return yaml.NewDecoder(f), true
7976
}
77+
if s, set := os.LookupEnv("KSM_CUSTOM_RESOURCE_METRICS"); set {
78+
return yaml.NewDecoder(strings.NewReader(s)), true
79+
}
8080
return nil, false
8181
}

pkg/options/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type Options struct {
5454

5555
UseAPIServerCache bool
5656

57+
CustomResourceConfigFile string
58+
5759
flags *pflag.FlagSet
5860
}
5961

@@ -111,6 +113,8 @@ func (o *Options) AddFlags() {
111113
o.flags.StringVar(&o.Namespace, "pod-namespace", "", "Name of the namespace of the pod specified by --pod. "+autoshardingNotice)
112114
o.flags.BoolVarP(&o.Version, "version", "", false, "kube-state-metrics build version information")
113115
o.flags.BoolVar(&o.EnableGZIPEncoding, "enable-gzip-encoding", false, "Gzip responses when requested by clients via 'Accept-Encoding: gzip' header.")
116+
117+
o.flags.StringVar(&o.CustomResourceConfigFile, "custom-resource-state-config", "", "Path to a Custom Resource State Metrics config file")
114118
}
115119

116120
// Parse parses the flag definitions from the argument list.

0 commit comments

Comments
 (0)