-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
changes.go
187 lines (160 loc) · 4.86 KB
/
changes.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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fi
import (
"reflect"
"k8s.io/klog/v2"
)
// An important part of our state synchronization is to compare two tasks, to see what has changed
// Doing so means that the tasks don't have to have this logic, and we can reuse this for dry-run.
// We do so using reflection. We have a custom notion of equality for Resources and for Tasks that implement HasID.
// A task that implements CompareWithID is compared by ID alone.
// BuildChanges compares the values of a & e, and populates differences into changes,
// except that if a value is nil in e, the corresponding value in a is ignored.
// a, e and changes must all be of the same type
// a is the actual object found, e is the expected value
// Note that the ignore-nil-in-e logic therefore implements the idea that nil value in e means "don't care"
// If a is nil, all the non-nil values in e will be copied over to changes, because every field in e must be applied
func BuildChanges(a, e, changes interface{}) bool {
changed := false
vc := reflect.ValueOf(changes)
vc = vc.Elem()
t := vc.Type()
ve := reflect.ValueOf(e)
ve = ve.Elem()
if t != ve.Type() {
panic("mismatched types in BuildChanges")
}
va := reflect.ValueOf(a)
aIsNil := false
if va.IsNil() {
aIsNil = true
}
if !aIsNil {
va = va.Elem()
if t != va.Type() {
panic("mismatched types in BuildChanges")
}
}
for i := 0; i < ve.NumField(); i++ {
if t.Field(i).PkgPath != "" {
// unexported: ignore
continue
}
fve := ve.Field(i)
if fve.Kind() == reflect.Ptr && fve.IsNil() {
// Nil expected value means 'don't care'
continue
}
if !aIsNil {
fva := va.Field(i)
if equalFieldValues(fva, fve) {
continue
}
klog.V(8).Infof("Field changed %q actual=%q expected=%q", t.Field(i).Name, DebugPrint(fva.Interface()), DebugPrint(fve.Interface()))
}
changed = true
vc.Field(i).Set(fve)
}
return changed
}
// equalFieldValues implements our equality checking, with special cases for resources and tasks
func equalFieldValues(a, e reflect.Value) bool {
if !a.IsValid() || !e.IsValid() {
return a.IsValid() == e.IsValid()
}
if a.Kind() == reflect.Map {
return equalMapValues(a, e)
}
if a.Kind() == reflect.Slice {
return equalSlice(a, e)
}
if (a.Kind() == reflect.Ptr || a.Kind() == reflect.Interface) && !a.IsNil() {
aHasID, ok := a.Interface().(CompareWithID)
if ok && (e.Kind() == reflect.Ptr || e.Kind() == reflect.Interface) && !e.IsNil() {
eHasID, ok := e.Interface().(CompareWithID)
if ok {
aID := aHasID.CompareWithID()
eID := eHasID.CompareWithID()
if aID != nil && eID != nil && *aID == *eID {
return true
}
}
}
aResource, ok := a.Interface().(Resource)
if ok && (e.Kind() == reflect.Ptr || e.Kind() == reflect.Interface) && !e.IsNil() {
eResource, ok := e.Interface().(Resource)
if ok {
if hasIsReady, ok := eResource.(HasIsReady); ok {
if !hasIsReady.IsReady() {
return false
}
}
same, err := ResourcesMatch(aResource, eResource)
if err != nil {
klog.Fatalf("error while comparing resources: %v", err)
} else {
return same
}
}
}
}
if reflect.DeepEqual(a.Interface(), e.Interface()) {
return true
}
return false
}
// equalMapValues performs a deep-equality check on a map, but using our custom comparison logic (equalFieldValues)
func equalMapValues(a, e reflect.Value) bool {
if a.IsNil() != e.IsNil() {
return false
}
if a.IsNil() && e.IsNil() {
return true
}
if a.Len() != e.Len() {
return false
}
for _, k := range a.MapKeys() {
valA := a.MapIndex(k)
valE := e.MapIndex(k)
klog.V(10).Infof("comparing maps: %v %v %v", k, valA, valE)
if !equalFieldValues(valA, valE) {
klog.V(4).Infof("unequal map value: %v %v %v", k, valA, valE)
return false
}
}
return true
}
// equalSlice performs a deep-equality check on a slice, but using our custom comparison logic (equalFieldValues)
func equalSlice(a, e reflect.Value) bool {
if a.IsNil() != e.IsNil() {
return false
}
if a.IsNil() && e.IsNil() {
return true
}
if a.Len() != e.Len() {
return false
}
for i := 0; i < a.Len(); i++ {
valA := a.Index(i)
valE := e.Index(i)
klog.V(10).Infof("comparing slices: %d %v %v", i, valA, valE)
if !equalFieldValues(valA, valE) {
klog.V(4).Infof("unequal slice value: %d %v %v", i, valA, valE)
return false
}
}
return true
}