-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompare.go
393 lines (377 loc) · 9.67 KB
/
compare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
package kube
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"
"github.com/samber/lo"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// genericCondition contains fields that are (mostly) common to many Condition
// objects and that we wish to match against.
type genericCondition struct {
Type string
Status string
Reason string
}
// conditionFound returns a delta describing the differences found between a
// supplied resource's Conditions and the expected Conditions
func compareConditions(
res *unstructured.Unstructured,
expected map[string]*ConditionMatch,
) *delta {
d := &delta{differences: []string{}}
conds, found, err := unstructured.NestedSlice(res.Object, "status", "conditions")
if found && err != nil {
// this means the resource's Status.Conditions is not a slice of
// something... which is weird and unexpected, so just panic.
msg := fmt.Sprintf(
"found a resource %q with a non-slice Status.Conditions field",
res.GetKind(),
)
panic(msg)
}
if (!found || len(conds) == 0) && len(expected) != 0 {
for condType := range expected {
d.Add(fmt.Sprintf("no condition with type %q found", condType))
}
return d
}
// construct a map, keyed by condition type, of the condition fields from
// the resource so we can do type-based lookups easier.
gcs := map[string]genericCondition{}
for _, condAny := range conds {
condMap, ok := condAny.(map[string]interface{})
if !ok {
// this means the resource's Status.Conditions is not a slice of
// map[string]interface... which is also weird and unexpected, so
// just panic.
msg := fmt.Sprintf(
"found a resource %q with a non-map[string]interface{} "+
"Status.Conditions member type: %T",
res.GetKind(), condAny,
)
panic(msg)
}
gc := genericCondition{}
for k, v := range condMap {
klow := strings.ToLower(k)
switch klow {
case "type":
gc.Type = strings.ToLower(v.(string))
case "reason":
gc.Reason = v.(string)
case "status":
gc.Status = strings.ToLower(v.(string))
}
}
gcs[gc.Type] = gc
}
for condType, condMatch := range expected {
ctlow := strings.ToLower(condType)
gc, found := gcs[ctlow]
if !found {
d.Add(fmt.Sprintf("no condition with type %q found", condType))
continue
}
if condMatch.Status != nil {
statusValues := condMatch.Status.Values()
if gc.Status == "" {
msg := fmt.Sprintf(
"condition %q had no status. "+
"expected status to be one of %s",
condType, statusValues,
)
d.Add(msg)
continue
}
svlow := []string{}
for _, sv := range statusValues {
svlow = append(svlow, strings.ToLower(sv))
}
if !lo.Contains(svlow, strings.ToLower(gc.Status)) {
msg := fmt.Sprintf(
"condition %q had status of %q. "+
"expected status to be one of %s",
condType, gc.Status, statusValues,
)
d.Add(msg)
continue
}
}
if condMatch.Reason != "" {
if gc.Reason != condMatch.Reason {
msg := fmt.Sprintf(
"condition %q had reason of %q. "+
"expected reason to be %q",
condType, gc.Reason, condMatch.Reason,
)
d.Add(msg)
continue
}
}
}
return d
}
// matchObjectFromAny returns a map[string]interface{} given any of a filepath,
// an inline YAML string or a map[string]interface{}. The returned
// map[string]interface{} is the collection of resource fields that we will
// match against.
func matchObjectFromAny(m interface{}) map[string]interface{} {
switch m := m.(type) {
case string:
var err error
var b []byte
v := m
if probablyFilePath(v) {
b, err = os.ReadFile(v)
if err != nil {
// NOTE(jaypipes): We already validated that the file exists at
// parse time. If we get an error here, just panic cuz there's
// nothing we can really do.
panic(err)
}
} else {
b = []byte(v)
}
var obj map[string]interface{}
if err = yaml.Unmarshal(b, &obj); err != nil {
// NOTE(jaypipes): We already validated that the content could be
// unmarshaled at parse time. If we get an error here, just panic
// cuz there's nothing we can really do.
panic(err)
}
return obj
case map[string]interface{}:
return m
}
return map[string]interface{}{}
}
// delta collects differences between two objects.
type delta struct {
differences []string
}
func (d *delta) Add(diff string) {
d.differences = append(d.differences, diff)
}
func (d *delta) Empty() bool {
return len(d.differences) == 0
}
func (d *delta) Differences() []string {
return d.differences
}
// compareResourceToMatchObject returns a delta object containing and
// differences between the supplied resource and the match object.
func compareResourceToMatchObject(
res *unstructured.Unstructured,
match map[string]interface{},
) *delta {
d := &delta{differences: []string{}}
collectFieldDifferences("$", match, res.Object, d)
return d
}
// collectFieldDifferences compares two things and adds any differences between
// them to a supplied set of differences.
func collectFieldDifferences(
fp string, // the "field path" to the field we are comparing...
match interface{},
subject interface{},
delta *delta,
) {
if !typesComparable(match, subject) {
diff := fmt.Sprintf(
"%s non-comparable types: %T and %T.",
fp, match, subject,
)
delta.Add(diff)
return
}
switch match.(type) {
case map[string]interface{}:
matchmap := match.(map[string]interface{})
subjectmap := subject.(map[string]interface{})
for matchk, matchv := range matchmap {
subjectv, ok := subjectmap[matchk]
newfp := fp + "." + matchk
if !ok {
diff := fmt.Sprintf("%s not present in subject", newfp)
delta.Add(diff)
continue
}
collectFieldDifferences(newfp, matchv, subjectv, delta)
}
return
case []interface{}:
matchlist := match.([]interface{})
subjectlist := subject.([]interface{})
if len(matchlist) != len(subjectlist) {
diff := fmt.Sprintf(
"%s had different lengths. expected %d but found %d",
fp, len(matchlist), len(subjectlist),
)
delta.Add(diff)
return
}
// Sort order currently matters, unfortunately...
for x, matchv := range matchlist {
subjectv := subjectlist[x]
newfp := fmt.Sprintf("%s[%d]", fp, x)
collectFieldDifferences(newfp, matchv, subjectv, delta)
}
return
case int, int8, int16, int32, int64:
switch subject := subject.(type) {
case int, int8, int16, int32, int64:
mv := toInt64(match)
sv := toInt64(subject)
if mv != sv {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
}
case uint, uint8, uint16, uint32, uint64:
mv := toUint64(match)
sv := toUint64(subject)
if mv != sv {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
}
case string:
mv := toInt64(match)
sv, err := strconv.Atoi(subject)
if err != nil {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
return
}
if mv != int64(sv) {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
}
}
return
case string:
switch subject.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64:
mv := match.(string)
si := subject.(int)
sv := strconv.Itoa(si)
if mv != sv {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
}
case string:
mv, _ := match.(string)
if mv != subject {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
}
}
return
}
if !reflect.DeepEqual(match, subject) {
diff := fmt.Sprintf(
"%s had different values. expected %v but found %v",
fp, match, subject,
)
delta.Add(diff)
}
}
// typesComparable returns true if the two supplied things are comparable,
// false otherwise
func typesComparable(a, b interface{}) bool {
av := reflect.ValueOf(a)
bv := reflect.ValueOf(b)
at := av.Kind()
bt := bv.Kind()
switch at {
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
switch bt {
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64,
reflect.String:
return true
default:
return false
}
case reflect.Uint, reflect.Uint8, reflect.Uint32, reflect.Uint64:
switch bt {
case reflect.Uint, reflect.Uint8, reflect.Uint32,
reflect.Uint64, reflect.String:
return true
default:
return false
}
case reflect.Complex64, reflect.Complex128:
switch bt {
case reflect.Complex64, reflect.Complex128, reflect.String:
return true
default:
return false
}
case reflect.String:
switch bt {
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint32, reflect.Uint64,
reflect.Complex64, reflect.Complex128, reflect.String:
return true
default:
return false
}
}
return reflect.TypeOf(a) == reflect.TypeOf(b)
}
// toUint64 takes an interface and returns a uint64
func toUint64(v interface{}) uint64 {
switch v := v.(type) {
case uint64:
return v
case uint8:
return uint64(v)
case uint16:
return uint64(v)
case uint32:
return uint64(v)
case uint:
return uint64(v)
}
return 0
}
// toInt64 takes an interface and returns an int64
func toInt64(v interface{}) int64 {
switch v := v.(type) {
case int64:
return v
case int8:
return int64(v)
case int16:
return int64(v)
case int32:
return int64(v)
case int:
return int64(v)
}
return 0
}