/
manage_children.go
279 lines (253 loc) · 10.3 KB
/
manage_children.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
/*
Copyright 2018 Google Inc.
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
https://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 common
import (
"fmt"
"reflect"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/diff"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"metacontroller.app/apis/metacontroller/v1alpha1"
dynamicapply "metacontroller.app/dynamic/apply"
dynamicclientset "metacontroller.app/dynamic/clientset"
k8s "metacontroller.app/third_party/kubernetes"
)
func ApplyUpdate(orig, update *unstructured.Unstructured) (*unstructured.Unstructured, error) {
// The controller only returns a partial object.
// We compute the full updated object in the style of "kubectl apply".
lastApplied, err := dynamicapply.GetLastApplied(orig)
if err != nil {
return nil, err
}
newObj := &unstructured.Unstructured{}
newObj.Object, err = dynamicapply.Merge(orig.UnstructuredContent(), lastApplied, update.UnstructuredContent())
if err != nil {
return nil, err
}
// Revert metadata fields that are known to be read-only, system fields,
// so that attempts to change those fields will never cause a diff to be found
// by DeepEqual, which would cause needless, no-op updates or recreates.
// See: https://github.com/GoogleCloudPlatform/metacontroller/issues/76
if err := revertObjectMetaSystemFields(newObj, orig); err != nil {
return nil, fmt.Errorf("failed to revert ObjectMeta system fields: %v", err)
}
// Revert status because we don't currently support a parent changing status of
// its children, so we need to ensure no diffs on the children involve status.
if err := revertField(newObj, orig, "status"); err != nil {
return nil, fmt.Errorf("failed to revert .status: %v", err)
}
dynamicapply.SetLastApplied(newObj, update.UnstructuredContent())
return newObj, nil
}
// objectMetaSystemFields is a list of JSON field names within ObjectMeta that
// are both read-only and system-populated according to the comments in
// k8s.io/apimachinery/pkg/apis/meta/v1/types.go.
var objectMetaSystemFields = []string{
"selfLink",
"uid",
"resourceVersion",
"generation",
"creationTimestamp",
"deletionTimestamp",
}
// revertObjectMetaSystemFields overwrites the read-only, system-populated
// fields of ObjectMeta in newObj to match what they were in orig.
// If the field existed before, we create it if necessary and set the value.
// If the field was unset before, we delete it if necessary.
func revertObjectMetaSystemFields(newObj, orig *unstructured.Unstructured) error {
for _, fieldName := range objectMetaSystemFields {
if err := revertField(newObj, orig, "metadata", fieldName); err != nil {
return err
}
}
return nil
}
// revertField reverts field in newObj to match what it was in orig.
func revertField(newObj, orig *unstructured.Unstructured, fieldPath ...string) error {
field, found, err := unstructured.NestedFieldNoCopy(orig.UnstructuredContent(), fieldPath...)
if err != nil {
return fmt.Errorf("can't traverse UnstructuredContent to look for field %v: %v", fieldPath, err)
}
if found {
// The original had this field set, so make sure it remains the same.
// SetNestedField will recursively ensure the field and all its parent
// fields exist, and then set the value.
if err := unstructured.SetNestedField(newObj.UnstructuredContent(), field, fieldPath...); err != nil {
return fmt.Errorf("can't revert field %v: %v", fieldPath, err)
}
} else {
// The original had this field unset, so make sure it remains unset.
// RemoveNestedField is a no-op if the field or any of its parents
// don't exist.
unstructured.RemoveNestedField(newObj.UnstructuredContent(), fieldPath...)
}
return nil
}
func MakeControllerRef(parent *unstructured.Unstructured) *metav1.OwnerReference {
return &metav1.OwnerReference{
APIVersion: parent.GetAPIVersion(),
Kind: parent.GetKind(),
Name: parent.GetName(),
UID: parent.GetUID(),
Controller: k8s.BoolPtr(true),
BlockOwnerDeletion: k8s.BoolPtr(true),
}
}
type ChildUpdateStrategy interface {
GetMethod(apiGroup, kind string) v1alpha1.ChildUpdateMethod
}
func ManageChildren(dynClient *dynamicclientset.Clientset, updateStrategy ChildUpdateStrategy, parent *unstructured.Unstructured, observedChildren, desiredChildren ChildMap) error {
// If some operations fail, keep trying others so, for example,
// we don't block recovery (create new Pod) on a failed delete.
var errs []error
// Delete observed, owned objects that are not desired.
for key, objects := range observedChildren {
apiVersion, kind := ParseChildMapKey(key)
client, err := dynClient.Kind(apiVersion, kind)
if err != nil {
errs = append(errs, err)
continue
}
if err := deleteChildren(client, parent, objects, desiredChildren[key]); err != nil {
errs = append(errs, err)
continue
}
}
// Create or update desired objects.
for key, objects := range desiredChildren {
apiVersion, kind := ParseChildMapKey(key)
client, err := dynClient.Kind(apiVersion, kind)
if err != nil {
errs = append(errs, err)
continue
}
if err := updateChildren(client, updateStrategy, parent, observedChildren[key], objects); err != nil {
errs = append(errs, err)
continue
}
}
return utilerrors.NewAggregate(errs)
}
func deleteChildren(client *dynamicclientset.ResourceClient, parent *unstructured.Unstructured, observed, desired map[string]*unstructured.Unstructured) error {
var errs []error
for name, obj := range observed {
if obj.GetDeletionTimestamp() != nil {
// Skip objects that are already pending deletion.
continue
}
if desired == nil || desired[name] == nil {
// This observed object wasn't listed as desired.
glog.Infof("%v: deleting %v", describeObject(parent), describeObject(obj))
uid := obj.GetUID()
// Explicitly request deletion propagation, which is what users expect,
// since some objects default to orphaning for backwards compatibility.
propagation := metav1.DeletePropagationBackground
err := client.Namespace(obj.GetNamespace()).Delete(obj.GetName(), &metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{UID: &uid},
PropagationPolicy: &propagation,
})
if err != nil {
errs = append(errs, fmt.Errorf("can't delete %v: %v", describeObject(obj), err))
continue
}
}
}
return utilerrors.NewAggregate(errs)
}
func updateChildren(client *dynamicclientset.ResourceClient, updateStrategy ChildUpdateStrategy, parent *unstructured.Unstructured, observed, desired map[string]*unstructured.Unstructured) error {
var errs []error
for name, obj := range desired {
ns := obj.GetNamespace()
if ns == "" {
ns = parent.GetNamespace()
}
if oldObj := observed[name]; oldObj != nil {
// Update
newObj, err := ApplyUpdate(oldObj, obj)
if err != nil {
errs = append(errs, err)
continue
}
// Attempt an update, if the 3-way merge resulted in any changes.
if reflect.DeepEqual(newObj.UnstructuredContent(), oldObj.UnstructuredContent()) {
// Nothing changed.
continue
}
if glog.V(5) {
glog.Infof("reflect diff: a=observed, b=desired:\n%s", diff.ObjectReflectDiff(oldObj.UnstructuredContent(), newObj.UnstructuredContent()))
}
// Leave it alone if it's pending deletion.
if oldObj.GetDeletionTimestamp() != nil {
glog.Infof("%v: not updating %v (pending deletion)", describeObject(parent), describeObject(obj))
continue
}
// Check the update strategy for this child kind.
switch method := updateStrategy.GetMethod(client.Group, client.Kind); method {
case v1alpha1.ChildUpdateOnDelete, "":
// This means we don't try to update anything unless it gets deleted
// by someone else (we won't delete it ourselves).
glog.V(5).Infof("%v: not updating %v (OnDelete update strategy)", describeObject(parent), describeObject(obj))
continue
case v1alpha1.ChildUpdateRecreate, v1alpha1.ChildUpdateRollingRecreate:
// Delete the object (now) and recreate it (on the next sync).
glog.Infof("%v: deleting %v for update", describeObject(parent), describeObject(obj))
uid := oldObj.GetUID()
// Explicitly request deletion propagation, which is what users expect,
// since some objects default to orphaning for backwards compatibility.
propagation := metav1.DeletePropagationBackground
err := client.Namespace(ns).Delete(obj.GetName(), &metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{UID: &uid},
PropagationPolicy: &propagation,
})
if err != nil {
errs = append(errs, err)
continue
}
case v1alpha1.ChildUpdateInPlace, v1alpha1.ChildUpdateRollingInPlace:
// Update the object in-place.
glog.Infof("%v: updating %v", describeObject(parent), describeObject(obj))
if _, err := client.Namespace(ns).Update(newObj); err != nil {
errs = append(errs, err)
continue
}
default:
errs = append(errs, fmt.Errorf("invalid update strategy for %v: unknown method %q", client.Kind, method))
continue
}
} else {
// Create
glog.Infof("%v: creating %v", describeObject(parent), describeObject(obj))
// The controller should return a partial object containing only the
// fields it cares about. We save this partial object so we can do
// a 3-way merge upon update, in the style of "kubectl apply".
//
// Make sure this happens before we add anything else to the object.
if err := dynamicapply.SetLastApplied(obj, obj.UnstructuredContent()); err != nil {
errs = append(errs, err)
continue
}
// We always claim everything we create.
controllerRef := MakeControllerRef(parent)
ownerRefs := obj.GetOwnerReferences()
ownerRefs = append(ownerRefs, *controllerRef)
obj.SetOwnerReferences(ownerRefs)
if _, err := client.Namespace(ns).Create(obj); err != nil {
errs = append(errs, err)
continue
}
}
}
return utilerrors.NewAggregate(errs)
}