@@ -27,6 +27,7 @@ import (
27
27
"os"
28
28
"path/filepath"
29
29
"strconv"
30
+ "strings"
30
31
"time"
31
32
32
33
"gopkg.in/yaml.v3"
@@ -47,6 +48,7 @@ import (
47
48
"k8s.io/kube-state-metrics/v2/internal/store"
48
49
"k8s.io/kube-state-metrics/v2/pkg/allowdenylist"
49
50
"k8s.io/kube-state-metrics/v2/pkg/customresource"
51
+ "k8s.io/kube-state-metrics/v2/pkg/customresourcestate"
50
52
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
51
53
"k8s.io/kube-state-metrics/v2/pkg/metricshandler"
52
54
"k8s.io/kube-state-metrics/v2/pkg/optin"
@@ -73,8 +75,8 @@ func (pl promLogger) Log(v ...interface{}) error {
73
75
}
74
76
75
77
// RunKubeStateMetricsWrapper runs KSM with context cancellation.
76
- func RunKubeStateMetricsWrapper (ctx context.Context , opts * options.Options , factories ... customresource. RegistryFactory ) error {
77
- err := RunKubeStateMetrics (ctx , opts , factories ... )
78
+ func RunKubeStateMetricsWrapper (ctx context.Context , opts * options.Options ) error {
79
+ err := RunKubeStateMetrics (ctx , opts )
78
80
if ctx .Err () == context .Canceled {
79
81
klog .Infoln ("Restarting: kube-state-metrics, metrics will be reset" )
80
82
return nil
@@ -85,11 +87,10 @@ func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options, fact
85
87
// RunKubeStateMetrics will build and run the kube-state-metrics.
86
88
// Any out-of-tree custom resource metrics could be registered by newing a registry factory
87
89
// which implements customresource.RegistryFactory and pass all factories into this function.
88
- func RunKubeStateMetrics (ctx context.Context , opts * options.Options , factories ... customresource. RegistryFactory ) error {
90
+ func RunKubeStateMetrics (ctx context.Context , opts * options.Options ) error {
89
91
promLogger := promLogger {}
90
92
91
93
storeBuilder := store .NewBuilder ()
92
- storeBuilder .WithCustomResourceStoreFactories (factories ... )
93
94
94
95
ksmMetricsRegistry := prometheus .NewRegistry ()
95
96
ksmMetricsRegistry .MustRegister (version .NewCollector ("kube_state_metrics" ))
@@ -144,6 +145,22 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
144
145
}
145
146
}
146
147
148
+ // Loading custom resource state configuration from cli argument or config file
149
+ config , err := resolveCustomResourceConfig (opts )
150
+ if err != nil {
151
+ return err
152
+ }
153
+
154
+ var factories []customresource.RegistryFactory
155
+
156
+ if config != nil {
157
+ factories , err = customresourcestate .FromConfig (config )
158
+ if err != nil {
159
+ return fmt .Errorf ("Parsing from Custom Resource State Metrics file failed: %v" , err )
160
+ }
161
+ }
162
+ storeBuilder .WithCustomResourceStoreFactories (factories ... )
163
+
147
164
if opts .CustomResourceConfigFile != "" {
148
165
crcFile , err := os .ReadFile (filepath .Clean (opts .CustomResourceConfigFile ))
149
166
if err != nil {
@@ -156,24 +173,22 @@ func RunKubeStateMetrics(ctx context.Context, opts *options.Options, factories .
156
173
157
174
}
158
175
159
- var resources []string
176
+ resources := make ([]string , len (factories ))
177
+
178
+ for i , factory := range factories {
179
+ resources [i ] = factory .Name ()
180
+ }
181
+
160
182
switch {
161
183
case len (opts .Resources ) == 0 && ! opts .CustomResourcesOnly :
184
+ resources = append (resources , options .DefaultResources .AsSlice ()... )
162
185
klog .InfoS ("Used default resources" )
163
- resources = options .DefaultResources .AsSlice ()
164
- // enable custom resource
165
- for _ , factory := range factories {
166
- resources = append (resources , factory .Name ())
167
- }
168
186
case opts .CustomResourcesOnly :
169
187
// enable custom resource only
170
- for _ , factory := range factories {
171
- resources = append (resources , factory .Name ())
172
- }
173
188
klog .InfoS ("Used CRD resources only" , "resources" , resources )
174
189
default :
175
- klog . InfoS ( "Used resources" , " resources" , opts .Resources .String () )
176
- resources = opts . Resources . AsSlice ( )
190
+ resources = append ( resources , opts .Resources .AsSlice () ... )
191
+ klog . InfoS ( "Used resources" , "resources" , resources )
177
192
}
178
193
179
194
if err := storeBuilder .WithEnabledResources (resources ); err != nil {
@@ -419,3 +434,17 @@ func md5HashAsMetricValue(data []byte) float64 {
419
434
copy (bytes , smallSum )
420
435
return float64 (binary .LittleEndian .Uint64 (bytes ))
421
436
}
437
+
438
+ func resolveCustomResourceConfig (opts * options.Options ) (customresourcestate.ConfigDecoder , error ) {
439
+ if s := opts .CustomResourceConfig ; s != "" {
440
+ return yaml .NewDecoder (strings .NewReader (s )), nil
441
+ }
442
+ if file := opts .CustomResourceConfigFile ; file != "" {
443
+ f , err := os .Open (filepath .Clean (file ))
444
+ if err != nil {
445
+ return nil , fmt .Errorf ("Custom Resource State Metrics file could not be opened: %v" , err )
446
+ }
447
+ return yaml .NewDecoder (f ), nil
448
+ }
449
+ return nil , nil
450
+ }
0 commit comments