Skip to content

Commit d0aa4a3

Browse files
author
Noah Perks Sloan
committed
refactor: inline config as a flag
This is more flexible than the env variable, as a configuration can still set an env variable and use substitution in the args. e.g., ```yaml args: - --custom-resource-state.config - $(KSM_CUSTOM_RESOURCE_STATE_CONFIG) env: ... ```
1 parent 39f7f0c commit d0aa4a3

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

docs/customresourcestate-metrics.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ registry and running your own build of KSM.
55

66
## Configuration
77

8-
A configuration file is required to describe your custom resources and the fields to turn into metrics.
8+
A YAML configuration file described below is required to define your custom resources and the fields to turn into metrics.
99

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.,
10+
Two flags can be used:
11+
12+
* `--custom-resource-state.config "inline yaml (see example)"` or
13+
* `--custom-resource-state-config /path/to/config.yaml`
14+
15+
If both flags are provided, the inline configuration will take precedence.
1316

1417
```yaml
1518
apiVersion: apps/v1
@@ -22,9 +25,11 @@ spec:
2225
spec:
2326
containers:
2427
- name: kube-state-metrics
25-
env:
26-
- name: KSM_CUSTOM_RESOURCE_METRICS
27-
value: |
28+
args:
29+
- --custom-resource-state.config
30+
# in YAML files, | allows a multi-line string to be passed as a flag value
31+
# see https://yaml-multiline.info
32+
- |
2833
spec:
2934
resources:
3035
- groupVersionKind:
@@ -79,6 +84,8 @@ status:
7984
8085
#### Single Values
8186
87+
The config:
88+
8289
```yaml
8390
kind: CustomResourceStateMetrics
8491
spec:
@@ -153,7 +160,7 @@ kube_myteam_io_v1_Foo_active_count{active="3",custom_metric="yes",foo="bar",name
153160
### Naming
154161

155162
The default metric names are prefixed to avoid collisions with other metrics.
156-
By default a namespace of `kube` and a subsystem based on your custom resource's group+version+kind is used.
163+
By default, a namespace of `kube` and a subsystem based on your custom resource's group+version+kind is used.
157164
You can override these with the namespace and subsystem fields.
158165

159166
```yaml

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ func main() {
6767
}
6868

6969
func resolveCustomResourceConfig(opts *options.Options) (customresourcestate.ConfigDecoder, bool) {
70+
if s := opts.CustomResourceConfig; s != "" {
71+
return yaml.NewDecoder(strings.NewReader(s)), true
72+
}
7073
if file := opts.CustomResourceConfigFile; file != "" {
7174
f, err := os.Open(file)
7275
if err != nil {
7376
klog.Fatal(err)
7477
}
7578
return yaml.NewDecoder(f), true
7679
}
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Options struct {
5454

5555
UseAPIServerCache bool
5656

57+
CustomResourceConfig string
5758
CustomResourceConfigFile string
5859

5960
flags *pflag.FlagSet
@@ -114,7 +115,8 @@ func (o *Options) AddFlags() {
114115
o.flags.BoolVarP(&o.Version, "version", "", false, "kube-state-metrics build version information")
115116
o.flags.BoolVar(&o.EnableGZIPEncoding, "enable-gzip-encoding", false, "Gzip responses when requested by clients via 'Accept-Encoding: gzip' header.")
116117

117-
o.flags.StringVar(&o.CustomResourceConfigFile, "custom-resource-state-config", "", "Path to a Custom Resource State Metrics config file")
118+
o.flags.StringVar(&o.CustomResourceConfig, "custom-resource-state.config", "", "Inline Custom Resource State Metrics config YAML")
119+
o.flags.StringVar(&o.CustomResourceConfigFile, "custom-resource-state.config-file", "", "Path to a Custom Resource State Metrics config file")
118120
}
119121

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

0 commit comments

Comments
 (0)