This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
unstructured.go
120 lines (104 loc) · 4.18 KB
/
unstructured.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
/*
Copyright 2017 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 controllerref
import (
"fmt"
"github.com/golang/glog"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
dynamicclientset "k8s.io/metacontroller/dynamic/clientset"
k8s "k8s.io/metacontroller/third_party/kubernetes"
)
type UnstructuredManager struct {
k8s.BaseControllerRefManager
parentKind schema.GroupVersionKind
childKind schema.GroupVersionKind
client *dynamicclientset.ResourceClient
}
func NewUnstructuredManager(client *dynamicclientset.ResourceClient, parent metav1.Object, selector labels.Selector, parentKind, childKind schema.GroupVersionKind, canAdopt func() error) *UnstructuredManager {
return &UnstructuredManager{
BaseControllerRefManager: k8s.BaseControllerRefManager{
Controller: parent,
Selector: selector,
CanAdoptFunc: canAdopt,
},
parentKind: parentKind,
childKind: childKind,
client: client,
}
}
func (m *UnstructuredManager) ClaimChildren(children []*unstructured.Unstructured) ([]*unstructured.Unstructured, error) {
var claimed []*unstructured.Unstructured
var errlist []error
match := func(obj metav1.Object) bool {
return m.Selector.Matches(labels.Set(obj.GetLabels()))
}
adopt := func(obj metav1.Object) error {
return m.adoptChild(obj.(*unstructured.Unstructured))
}
release := func(obj metav1.Object) error {
return m.releaseChild(obj.(*unstructured.Unstructured))
}
for _, child := range children {
ok, err := m.ClaimObject(child, match, adopt, release)
if err != nil {
errlist = append(errlist, err)
continue
}
if ok {
claimed = append(claimed, child)
}
}
return claimed, utilerrors.NewAggregate(errlist)
}
func (m *UnstructuredManager) adoptChild(obj *unstructured.Unstructured) error {
if err := m.CanAdopt(); err != nil {
return fmt.Errorf("can't adopt %v %v/%v (%v): %v", m.childKind.Kind, obj.GetNamespace(), obj.GetName(), obj.GetUID(), err)
}
glog.Infof("%v %v/%v: adopting %v %v", m.parentKind.Kind, m.Controller.GetNamespace(), m.Controller.GetName(), m.childKind.Kind, obj.GetName())
controllerRef := metav1.OwnerReference{
APIVersion: m.parentKind.GroupVersion().String(),
Kind: m.parentKind.Kind,
Name: m.Controller.GetName(),
UID: m.Controller.GetUID(),
Controller: k8s.BoolPtr(true),
BlockOwnerDeletion: k8s.BoolPtr(true),
}
// We can't use strategic merge patch because we want this to work with custom resources.
// We can't use merge patch because that would replace the whole list.
// We can't use JSON patch ops because that wouldn't be idempotent.
// The only option is GET/PUT with ResourceVersion.
_, err := m.client.UpdateWithRetries(obj, func(obj *unstructured.Unstructured) bool {
ownerRefs := addOwnerReference(obj.GetOwnerReferences(), controllerRef)
obj.SetOwnerReferences(ownerRefs)
return true
})
return err
}
func (m *UnstructuredManager) releaseChild(obj *unstructured.Unstructured) error {
glog.Infof("%v %v/%v: releasing %v %v", m.parentKind.Kind, m.Controller.GetNamespace(), m.Controller.GetName(), m.childKind.Kind, obj.GetName())
_, err := m.client.UpdateWithRetries(obj, func(obj *unstructured.Unstructured) bool {
ownerRefs := removeOwnerReference(obj.GetOwnerReferences(), m.Controller.GetUID())
obj.SetOwnerReferences(ownerRefs)
return true
})
if apierrors.IsNotFound(err) || apierrors.IsGone(err) {
// If the original object is gone, that's fine because we're giving up on this child anyway.
return nil
}
return err
}