-
Notifications
You must be signed in to change notification settings - Fork 277
/
apiobject.go
67 lines (54 loc) 路 1.78 KB
/
apiobject.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
package yamlutil
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// APIObject represents a kubernetes API object.
type APIObject interface {
runtime.Object
GetName() string
}
// ObjectLookup allows to search APIObjects by a unique key composed of apiVersion, kind, and name.
type ObjectLookup map[string]APIObject
// GetFromRef searches in a ObjectLookup for an APIObject referenced by a corev1.ObjectReference.
func (o ObjectLookup) GetFromRef(ref corev1.ObjectReference) APIObject {
return o[keyForRef(ref)]
}
func (o ObjectLookup) add(obj APIObject) {
o[keyForObject(obj)] = obj
}
func NewObjectLookupBuilder() *ObjectLookupBuilder {
return &ObjectLookupBuilder{
lookup: ObjectLookup{},
}
}
// ObjectLookupBuilder allows to construct an ObjectLookup and add APIObjects to it.
type ObjectLookupBuilder struct {
lookup ObjectLookup
}
// Add acumulates an API object that will be included in the built ObjectLookup.
func (o *ObjectLookupBuilder) Add(objs ...APIObject) *ObjectLookupBuilder {
for _, obj := range objs {
o.lookup.add(obj)
}
return o
}
// Build constructs and returns an ObjectLookup
// After this method is called, the builder is reset and loses track
// of all previously added objects.
func (o *ObjectLookupBuilder) Build() ObjectLookup {
l := o.lookup
o.lookup = ObjectLookup{}
return l
}
func keyForRef(ref corev1.ObjectReference) string {
return key(ref.APIVersion, ref.Kind, ref.Name)
}
func key(apiVersion, kind, name string) string {
// this assumes we don't allow to have objects in multiple namespaces
return fmt.Sprintf("%s%s%s", apiVersion, kind, name)
}
func keyForObject(o APIObject) string {
return key(o.GetObjectKind().GroupVersionKind().GroupVersion().String(), o.GetObjectKind().GroupVersionKind().Kind, o.GetName())
}