-
Notifications
You must be signed in to change notification settings - Fork 94
/
owner.go
130 lines (107 loc) · 3.9 KB
/
owner.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
/*
Copyright 2019 Banzai Cloud.
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 k8sutil
import (
"github.com/goph/emperror"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/banzaicloud/istio-operator/pkg/util"
)
type OwnerReferenceMatcher struct {
owner runtime.Object
ownerMeta metav1.Object
ownerTypeGroupKind schema.GroupKind
isController bool
scheme *runtime.Scheme
}
// NewOwnerReferenceMatcher initializes a new owner reference matcher
func NewOwnerReferenceMatcher(owner runtime.Object, ctrl bool, scheme *runtime.Scheme) *OwnerReferenceMatcher {
m := &OwnerReferenceMatcher{
owner: owner,
isController: ctrl,
scheme: scheme,
}
meta, _ := meta.Accessor(owner)
m.ownerMeta = meta
m.setOwnerTypeGroupKind()
return m
}
// Match matches if an object is owned by the initialised owner
func (e *OwnerReferenceMatcher) Match(object runtime.Object) (bool, metav1.Object, error) {
o, err := meta.Accessor(object)
if err != nil {
return false, o, emperror.WrapWith(err, "could not access object meta", "kind", object.GetObjectKind())
}
for _, owner := range e.getOwnersReferences(o) {
groupVersion, err := schema.ParseGroupVersion(owner.APIVersion)
if err != nil {
return false, o, emperror.WrapWith(err, "could not parse api version", "apiVersion", owner.APIVersion)
}
if (e.ownerMeta.GetUID() == "" || (e.ownerMeta.GetUID() != "" && owner.UID == e.ownerMeta.GetUID())) && owner.Kind == e.ownerTypeGroupKind.Kind && groupVersion.Group == e.ownerTypeGroupKind.Group {
return true, o, nil
}
}
return false, o, nil
}
func (e *OwnerReferenceMatcher) getOwnersReferences(object metav1.Object) []metav1.OwnerReference {
if object == nil {
return nil
}
if !e.isController {
return object.GetOwnerReferences()
}
if ownerRef := metav1.GetControllerOf(object); ownerRef != nil {
return []metav1.OwnerReference{*ownerRef}
}
return nil
}
func (e *OwnerReferenceMatcher) setOwnerTypeGroupKind() error {
kinds, _, err := e.scheme.ObjectKinds(e.owner)
if err != nil || len(kinds) < 1 {
return emperror.WrapWith(err, "could not get object kinds", "owner", e.owner)
}
e.ownerTypeGroupKind = schema.GroupKind{Group: kinds[0].Group, Kind: kinds[0].Kind}
return nil
}
func SetOwnerReferenceToObject(obj runtime.Object, owner runtime.Object) ([]metav1.OwnerReference, error) {
object, err := meta.Accessor(obj)
if err != nil {
return nil, emperror.WrapWith(err, "could not access object meta", "kind", obj.GetObjectKind())
}
own, err := meta.Accessor(owner)
if err != nil {
return nil, emperror.WrapWith(err, "could not access object meta", "kind", owner.GetObjectKind())
}
refs := object.GetOwnerReferences()
found := false
for _, ref := range refs {
if ref.Kind == owner.GetObjectKind().GroupVersionKind().Kind && ref.APIVersion == owner.GetObjectKind().GroupVersionKind().GroupVersion().String() && ref.UID == own.GetUID() {
found = true
break
}
}
if !found {
gvk := owner.GetObjectKind().GroupVersionKind()
refs = append(refs, metav1.OwnerReference{
APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: own.GetName(),
UID: own.GetUID(),
Controller: util.BoolPointer(true),
BlockOwnerDeletion: util.BoolPointer(true),
})
}
return refs, nil
}