@@ -21,73 +21,21 @@ import (
21
21
"strings"
22
22
23
23
"github.com/gobuffalo/flect"
24
-
25
24
"k8s.io/klog/v2"
26
- )
27
-
28
- // GroupVersionKind is the Kubernetes group, version, and kind of a resource.
29
- type GroupVersionKind struct {
30
- Group string `yaml:"group" json:"group"`
31
- Version string `yaml:"version" json:"version"`
32
- Kind string `yaml:"kind" json:"kind"`
33
- }
34
-
35
- // MetricPer targets a Path that may be a single value, array, or object. Arrays and objects will generate a metric per element.
36
- type MetricPer struct {
37
- // Path is the path to the value to generate metric(s) for.
38
- Path []string `yaml:"path" json:"path"`
39
- // ValueFrom is the path to a numeric field under Path that will be the metric value.
40
- ValueFrom []string `yaml:"valueFrom" json:"valueFrom"`
41
- // LabelFromKey adds a label with the given name if Path is an object. The label value will be the object key.
42
- LabelFromKey string `yaml:"labelFromKey" json:"labelFromKey"`
43
- // LabelsFromPath adds additional labels where the value of the label is taken from a field under Path.
44
- LabelsFromPath map [string ][]string `yaml:"labelsFromPath" json:"labelsFromPath"`
45
- }
46
25
47
- // Labels is common configuration of labels to add to metrics.
48
- type Labels struct {
49
- // CommonLabels are added to all metrics.
50
- CommonLabels map [string ]string `yaml:"commonLabels" json:"commonLabels"`
51
- // LabelsFromPath adds additional labels where the value is taken from a field in the resource.
52
- LabelsFromPath map [string ][]string `yaml:"labelsFromPath" json:"labelsFromPath"`
53
- }
54
-
55
- // Merge combines the labels from two configs, returning a new config. The other Labels will overwrite keys in this Labels.
56
- func (l Labels ) Merge (other Labels ) Labels {
57
- common := make (map [string ]string )
58
- paths := make (map [string ][]string )
26
+ "k8s.io/kube-state-metrics/v2/pkg/customresource"
27
+ )
59
28
60
- for k , v := range l .CommonLabels {
61
- common [k ] = v
62
- }
63
- for k , v := range l .LabelsFromPath {
64
- paths [k ] = v
65
- }
66
- for k , v := range other .CommonLabels {
67
- common [k ] = v
68
- }
69
- for k , v := range other .LabelsFromPath {
70
- paths [k ] = v
71
- }
72
- return Labels {
73
- CommonLabels : common ,
74
- LabelsFromPath : paths ,
75
- }
29
+ // Metrics is the top level configuration object.
30
+ type Metrics struct {
31
+ Spec MetricsSpec `yaml:"spec" json:"spec"`
76
32
}
77
33
78
- // Generator describes a unique metric name.
79
- type Generator struct {
80
- // Name of the metric. Subject to prefixing based on the configuration of the Resource.
81
- Name string `yaml:"name" json:"name"`
82
- // Help text for the metric.
83
- Help string `yaml:"help" json:"help"`
84
- // Each targets a value or values from the resource.
85
- Each MetricPer `yaml:"each" json:"each"`
86
-
87
- // Labels are added to all metrics. Labels from Each will overwrite these if using the same key.
88
- Labels `yaml:",inline"` // json will inline because it is already tagged
89
- // ErrorLogV defines the verbosity threshold for errors logged for this metric. Must be non-zero to override the resource setting.
90
- ErrorLogV klog.Level `yaml:"errorLogV" json:"errorLogV"`
34
+ // MetricsSpec is the configuration describing the custom resource state metrics to generate.
35
+ type MetricsSpec struct {
36
+ // Resources is the list of custom resources to be monitored. A resource with the same GroupVersionKind may appear
37
+ // multiple times (e.g., to customize the namespace or subsystem,) but will incur additional overhead.
38
+ Resources []Resource `yaml:"resources" json:"resources"`
91
39
}
92
40
93
41
// Resource configures a custom resource for metric generation.
@@ -104,7 +52,7 @@ type Resource struct {
104
52
GroupVersionKind GroupVersionKind `yaml:"groupVersionKind" json:"groupVersionKind"`
105
53
106
54
// Labels are added to all metrics. If the same key is used in a metric, the value from the metric will overwrite the value here.
107
- Labels `yaml:",inline"`
55
+ Labels `yaml:",inline" json:",inline" `
108
56
109
57
// Metrics are the custom resource fields to be collected.
110
58
Metrics []Generator `yaml:"metrics" json:"metrics"`
@@ -150,14 +98,94 @@ func (r Resource) GetResourceName() string {
150
98
return strings .ToLower (flect .Pluralize (r .GroupVersionKind .Kind ))
151
99
}
152
100
153
- // Metrics is the top level configuration object.
154
- type Metrics struct {
155
- Spec MetricsSpec `yaml:"spec" json:"spec"`
101
+ // GroupVersionKind is the Kubernetes group, version, and kind of a resource.
102
+ type GroupVersionKind struct {
103
+ Group string `yaml:"group" json:"group"`
104
+ Version string `yaml:"version" json:"version"`
105
+ Kind string `yaml:"kind" json:"kind"`
156
106
}
157
107
158
- // MetricsSpec is the configuration describing the custom resource state metrics to generate.
159
- type MetricsSpec struct {
160
- // Resources is the list of custom resources to be monitored. A resource with the same GroupVersionKind may appear
161
- // multiple times (e.g., to customize the namespace or subsystem,) but will incur additional overhead.
162
- Resources []Resource `yaml:"resources" json:"resources"`
108
+ // Labels is common configuration of labels to add to metrics.
109
+ type Labels struct {
110
+ // CommonLabels are added to all metrics.
111
+ CommonLabels map [string ]string `yaml:"commonLabels" json:"commonLabels"`
112
+ // LabelsFromPath adds additional labels where the value is taken from a field in the resource.
113
+ LabelsFromPath map [string ][]string `yaml:"labelsFromPath" json:"labelsFromPath"`
114
+ }
115
+
116
+ // Merge combines the labels from two configs, returning a new config. The other Labels will overwrite keys in this Labels.
117
+ func (l Labels ) Merge (other Labels ) Labels {
118
+ common := make (map [string ]string )
119
+ paths := make (map [string ][]string )
120
+
121
+ for k , v := range l .CommonLabels {
122
+ common [k ] = v
123
+ }
124
+ for k , v := range l .LabelsFromPath {
125
+ paths [k ] = v
126
+ }
127
+ for k , v := range other .CommonLabels {
128
+ common [k ] = v
129
+ }
130
+ for k , v := range other .LabelsFromPath {
131
+ paths [k ] = v
132
+ }
133
+ return Labels {
134
+ CommonLabels : common ,
135
+ LabelsFromPath : paths ,
136
+ }
137
+ }
138
+
139
+ // Generator describes a unique metric name.
140
+ type Generator struct {
141
+ // Name of the metric. Subject to prefixing based on the configuration of the Resource.
142
+ Name string `yaml:"name" json:"name"`
143
+ // Help text for the metric.
144
+ Help string `yaml:"help" json:"help"`
145
+ // Each targets a value or values from the resource.
146
+ Each Metric `yaml:"each" json:"each"`
147
+
148
+ // Labels are added to all metrics. Labels from Each will overwrite these if using the same key.
149
+ Labels `yaml:",inline" json:",inline"` // json will inline because it is already tagged
150
+ // ErrorLogV defines the verbosity threshold for errors logged for this metric. Must be non-zero to override the resource setting.
151
+ ErrorLogV klog.Level `yaml:"errorLogV" json:"errorLogV"`
152
+ }
153
+
154
+ // Metric defines a metric to expose.
155
+ // +union
156
+ type Metric struct {
157
+ // Type defines the type of the metric.
158
+ // +unionDiscriminator
159
+ Type MetricType `yaml:"type" json:"type"`
160
+
161
+ // Gauge defines a gauge metric.
162
+ // +optional
163
+ Gauge * MetricGauge `yaml:"gauge" json:"gauge"`
164
+ // StateSet defines a state set metric.
165
+ // +optional
166
+ StateSet * MetricStateSet `yaml:"stateSet" json:"stateSet"`
167
+ // Info defines a info metric.
168
+ // +optional
169
+ Info * MetricInfo `yaml:"info" json:"info"`
170
+ }
171
+
172
+ // ConfigDecoder is for use with FromConfig.
173
+ type ConfigDecoder interface {
174
+ Decode (v interface {}) (err error )
175
+ }
176
+
177
+ // FromConfig decodes a configuration source into a slice of customresource.RegistryFactory that are ready to use.
178
+ func FromConfig (decoder ConfigDecoder ) (factories []customresource.RegistryFactory , err error ) {
179
+ var crconfig Metrics
180
+ if err := decoder .Decode (& crconfig ); err != nil {
181
+ return nil , fmt .Errorf ("failed to parse Custom Resource State metrics: %w" , err )
182
+ }
183
+ for _ , resource := range crconfig .Spec .Resources {
184
+ factory , err := NewCustomResourceMetrics (resource )
185
+ if err != nil {
186
+ return nil , fmt .Errorf ("failed to create metrics factory for %s: %w" , resource .GroupVersionKind , err )
187
+ }
188
+ factories = append (factories , factory )
189
+ }
190
+ return factories , nil
163
191
}
0 commit comments