/
generic.go
178 lines (153 loc) · 5.22 KB
/
generic.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
package generic
import (
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
librarygocontroller "github.com/openshift/library-go/pkg/controller"
hivev1 "github.com/openshift/hive/apis/hive/v1"
k8slabels "github.com/openshift/hive/pkg/util/labels"
"strconv"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// Option defines a function signature for any function that wants to be passed into Build
type Option func(hivev1.MetaRuntimeObject)
// WithName sets the object.Name field when building an object with Build.
func WithName(name string) Option {
return func(meta hivev1.MetaRuntimeObject) {
meta.SetName(name)
}
}
// WithNamePostfix appends the string passed in to the object.Name field when building an with Build.
func WithNamePostfix(postfix string) Option {
return func(meta hivev1.MetaRuntimeObject) {
name := meta.GetName()
meta.SetName(name + "-" + postfix)
}
}
// WithNamespace sets the object.Namespace field when building an object with Build.
func WithNamespace(namespace string) Option {
return func(meta hivev1.MetaRuntimeObject) {
meta.SetNamespace(namespace)
}
}
// WithAnnotationsPopulated ensures that object.Annotations is not nil.
func WithAnnotationsPopulated() Option {
return func(meta hivev1.MetaRuntimeObject) {
annotations := meta.GetAnnotations()
// Only set if Nil (don't wipe out existing)
if annotations == nil {
meta.SetAnnotations(map[string]string{})
}
}
}
// WithAnnotation adds an annotation with the specified key and value to the supplied object.
// If there is already an annotation with the specified key, it will be replaced.
func WithAnnotation(key, value string) Option {
return func(meta hivev1.MetaRuntimeObject) {
annotations := meta.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
annotations[key] = value
meta.SetAnnotations(annotations)
}
}
// WithControllerOwnerReference sets the controller owner reference to the supplied object.
func WithControllerOwnerReference(owner metav1.Object) Option {
return func(meta hivev1.MetaRuntimeObject) {
controllerutil.SetControllerReference(owner, meta, scheme.Scheme)
}
}
// WithOwnerReference sets the owner reference to the supplied object.
// BlockOwnerDeletion is set to true
func WithOwnerReference(owner hivev1.MetaRuntimeObject) Option {
return func(meta hivev1.MetaRuntimeObject) {
ownerRef := metav1.NewControllerRef(owner, owner.GetObjectKind().GroupVersionKind())
ownerRef.Controller = nil
librarygocontroller.EnsureOwnerRef(meta, *ownerRef)
}
}
// WithLabel sets the specified label on the supplied object.
func WithLabel(key, value string) Option {
return func(meta hivev1.MetaRuntimeObject) {
labels := meta.GetLabels()
labels = k8slabels.AddLabel(labels, key, value)
meta.SetLabels(labels)
}
}
// WithResourceVersion sets the specified resource version on the supplied object.
func WithResourceVersion(resourceVersion string) Option {
return func(meta hivev1.MetaRuntimeObject) {
meta.SetResourceVersion(resourceVersion)
}
}
// WithIncrementedResourceVersion increments by one the resource version on the supplied object.
// If the resource version is not an integer, then the new resource version will be set to 1.
func WithIncrementedResourceVersion() Option {
return func(meta hivev1.MetaRuntimeObject) {
rv, err := strconv.Atoi(meta.GetResourceVersion())
if err != nil {
rv = 0
}
meta.SetResourceVersion(strconv.Itoa(rv + 1))
}
}
// WithUID sets the Metadata UID on the supplied object.
func WithUID(uid string) Option {
return func(meta hivev1.MetaRuntimeObject) {
meta.SetUID(types.UID(uid))
}
}
// WithTypeMeta populates the type meta for the object.
func WithTypeMeta(typers ...runtime.ObjectTyper) Option {
return func(meta hivev1.MetaRuntimeObject) {
for _, typer := range append(typers, scheme.Scheme) {
gvks, _, err := typer.ObjectKinds(meta)
if err != nil {
continue
}
if len(gvks) == 0 {
continue
}
meta.GetObjectKind().SetGroupVersionKind(gvks[0])
return
}
}
}
// WithFinalizer adds the specified finalizer to the object.
func WithFinalizer(finalizer string) Option {
return func(meta hivev1.MetaRuntimeObject) {
finalizers := sets.NewString(meta.GetFinalizers()...)
finalizers.Insert(finalizer)
meta.SetFinalizers(finalizers.List())
}
}
// WithoutFinalizer removes the specified finalizer from the object.
func WithoutFinalizer(finalizer string) Option {
return func(meta hivev1.MetaRuntimeObject) {
finalizers := sets.NewString(meta.GetFinalizers()...)
finalizers.Delete(finalizer)
meta.SetFinalizers(finalizers.List())
}
}
// WithCreationTimestamp sets the creation timestamp on the object.
func WithCreationTimestamp(time time.Time) Option {
return func(meta hivev1.MetaRuntimeObject) {
meta.SetCreationTimestamp(metav1.NewTime(time))
}
}
// Deleted sets a deletion timestamp on the object.
func Deleted() Option {
return func(meta hivev1.MetaRuntimeObject) {
now := metav1.Now()
meta.SetDeletionTimestamp(&now)
}
}
func WithGeneration(generation int64) Option {
return func(meta hivev1.MetaRuntimeObject) {
meta.SetGeneration(generation)
}
}