@@ -20,43 +20,44 @@ const (
20
20
21
21
type (
22
22
Config struct {
23
- Metrics []* ConfigMetrics `yaml:"metrics "`
23
+ Resources []* ConfigResource `yaml:"resources "`
24
24
}
25
25
26
- ConfigMetrics struct {
27
- Metric * MetricConfig `yaml:"metric"`
28
- Resource schema.GroupVersionResource `yaml:"resource"`
26
+ ConfigResource struct {
27
+ * schema.GroupVersionResource
29
28
30
29
Selector * metav1.LabelSelector `yaml:"selector"`
31
30
_selector string
32
31
33
- Filters []* MetricFilterConfig `yaml:"filters "`
32
+ Metrics []* ConfigMetric `yaml:"metrics "`
34
33
}
35
34
36
- MetricConfig struct {
35
+ ConfigMetric struct {
37
36
Name string `yaml:"name"`
38
37
Help string `yaml:"help"`
39
- Value * MetricValueConfig `yaml:"value"`
40
- Labels map [string ]* MetricLabelConfig `yaml:"labels"`
38
+ Value * ConfigMetricValue `yaml:"value"`
39
+ Labels map [string ]* ConfigMetricLabel `yaml:"labels"`
40
+
41
+ Filters []* ConfigMetricFilter `yaml:"filters"`
41
42
}
42
43
43
- MetricValueConfig struct {
44
- * MetricPathConfig `yaml:",inline"`
45
- Value * float64 `yaml:"value"`
44
+ ConfigMetricValue struct {
45
+ * ConfigMetricJsonPath `yaml:",inline"`
46
+ Value * float64 `yaml:"value"`
46
47
}
47
48
48
- MetricLabelConfig struct {
49
- * MetricPathConfig `yaml:",inline"`
50
- Value string `yaml:"value"`
49
+ ConfigMetricLabel struct {
50
+ * ConfigMetricJsonPath `yaml:",inline"`
51
+ Value string `yaml:"value"`
51
52
}
52
53
53
- MetricPathConfig struct {
54
+ ConfigMetricJsonPath struct {
54
55
Path string `yaml:"jsonPath" json:"jsonPath"`
55
56
_path * jsonpath.JSONPath
56
57
Convert []* string `yaml:"convert"`
57
58
}
58
59
59
- MetricFilterConfig struct {
60
+ ConfigMetricFilter struct {
60
61
Path string `yaml:"jsonPath" json:"jsonPath"`
61
62
_path * jsonpath.JSONPath
62
63
91
92
)
92
93
93
94
func (m * Config ) Compile () error {
94
- for _ , metric := range m .Metrics {
95
- err := metric .Compile ()
95
+ for _ , row := range m .Resources {
96
+ err := row .Compile ()
96
97
if err != nil {
97
98
return err
98
99
}
@@ -101,19 +102,54 @@ func (m *Config) Compile() error {
101
102
return nil
102
103
}
103
104
104
- func (m * ConfigMetrics ) Compile () error {
105
+ func (m * ConfigResource ) Compile () error {
106
+ if m .Version == "" {
107
+ return fmt .Errorf ("version is required" )
108
+ }
109
+
110
+ if m .Resource == "" {
111
+ return fmt .Errorf ("resource is required" )
112
+ }
113
+
114
+ // selector
115
+ if m .Selector != nil {
116
+ selector := metav1 .FormatLabelSelector (m .Selector )
117
+ if strings .EqualFold (selector , KUBE_SELECTOR_ERROR ) {
118
+ return fmt .Errorf (`unable to compile Kubernetes selector for resource "%s/%s/%s"` , m .Group , m .Version , m .Resource )
119
+ }
120
+
121
+ if ! strings .EqualFold (selector , KUBE_SELECTOR_NONE ) {
122
+ m ._selector = selector
123
+ }
124
+ }
125
+
126
+ for _ , row := range m .Metrics {
127
+ err := row .Compile ()
128
+ if err != nil {
129
+ return err
130
+ }
131
+ }
132
+
133
+ return nil
134
+ }
135
+
136
+ func (m * ConfigMetric ) Compile () error {
137
+ if m .Name == "" {
138
+ return fmt .Errorf ("name is required" )
139
+ }
140
+
105
141
// value path
106
- if m .Metric . Value .MetricPathConfig != nil && m . Metric .Value .Path != "" {
107
- if path , err := compileJsonPath (m .Metric . Value .Path ); err == nil {
108
- m .Metric . Value ._path = path
142
+ if m .Value .ConfigMetricJsonPath != nil && m .Value .Path != "" {
143
+ if path , err := compileJsonPath (m .Value .Path ); err == nil {
144
+ m .Value ._path = path
109
145
} else {
110
146
return err
111
147
}
112
148
}
113
149
114
150
// labels path
115
- for _ , labelConfig := range m .Metric . Labels {
116
- if labelConfig .MetricPathConfig != nil && labelConfig .Path != "" {
151
+ for _ , labelConfig := range m .Labels {
152
+ if labelConfig .ConfigMetricJsonPath != nil && labelConfig .Path != "" {
117
153
if path , err := compileJsonPath (labelConfig .Path ); err == nil {
118
154
labelConfig ._path = path
119
155
} else {
@@ -146,22 +182,10 @@ func (m *ConfigMetrics) Compile() error {
146
182
}
147
183
}
148
184
149
- // selector
150
- if m .Selector != nil {
151
- selector := metav1 .FormatLabelSelector (m .Selector )
152
- if strings .EqualFold (selector , KUBE_SELECTOR_ERROR ) {
153
- return fmt .Errorf (`unable to compile Kubernetes selector for metric "%s"` , m .Metric .Name )
154
- }
155
-
156
- if ! strings .EqualFold (selector , KUBE_SELECTOR_NONE ) {
157
- m ._selector = selector
158
- }
159
- }
160
-
161
185
return nil
162
186
}
163
187
164
- func (m * ConfigMetrics ) KubeMetaListOptions () metav1.ListOptions {
188
+ func (m * ConfigResource ) KubeMetaListOptions () metav1.ListOptions {
165
189
opts := metav1.ListOptions {}
166
190
if m ._selector != "" {
167
191
opts .LabelSelector = m ._selector
@@ -170,15 +194,15 @@ func (m *ConfigMetrics) KubeMetaListOptions() metav1.ListOptions {
170
194
return opts
171
195
}
172
196
173
- func (m * MetricPathConfig ) JsonPath () * jsonpath.JSONPath {
197
+ func (m * ConfigMetricJsonPath ) JsonPath () * jsonpath.JSONPath {
174
198
if m == nil {
175
199
return nil
176
200
}
177
201
178
202
return m ._path
179
203
}
180
204
181
- func (m * MetricPathConfig ) ParseLabel (val interface {}) (ret string ) {
205
+ func (m * ConfigMetricJsonPath ) ParseLabel (val interface {}) (ret string ) {
182
206
// convert type
183
207
switch v := val .(type ) {
184
208
case float64 :
@@ -198,7 +222,7 @@ func (m *MetricPathConfig) ParseLabel(val interface{}) (ret string) {
198
222
return m .DoConvertLabel (ret )
199
223
}
200
224
201
- func (m * MetricPathConfig ) ParseValue (val interface {}) (ret * float64 ) {
225
+ func (m * ConfigMetricJsonPath ) ParseValue (val interface {}) (ret * float64 ) {
202
226
valueString := ""
203
227
switch v := val .(type ) {
204
228
case float64 :
@@ -218,7 +242,7 @@ func (m *MetricPathConfig) ParseValue(val interface{}) (ret *float64) {
218
242
return m .DoConvertValue (valueString )
219
243
}
220
244
221
- func (m * ConfigMetrics ) IsValidObject (object unstructured.Unstructured ) bool {
245
+ func (m * ConfigMetric ) IsValidObject (object unstructured.Unstructured ) bool {
222
246
// no filters = is valid
223
247
if len (m .Filters ) == 0 {
224
248
return true
0 commit comments