forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator_controller_test.go
263 lines (224 loc) · 8.38 KB
/
operator_controller_test.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
package operators
import (
"context"
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/testobj"
)
var _ = Describe("Operator Controller", func() {
var (
ctx context.Context
operator *operatorsv1.Operator
name types.NamespacedName
expectedKey string
expectedComponentLabelSelector *metav1.LabelSelector
)
BeforeEach(func() {
ctx = context.Background()
operator = newOperator(genName("ghost-")).Operator
name = types.NamespacedName{Name: operator.GetName()}
expectedKey = decorators.ComponentLabelKeyPrefix + operator.GetName()
expectedComponentLabelSelector = &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: expectedKey,
Operator: metav1.LabelSelectorOpExists,
},
},
}
Expect(k8sClient.Create(ctx, operator)).To(Succeed())
})
Describe("operator deletion", func() {
var originalUID types.UID
JustBeforeEach(func() {
originalUID = operator.GetUID()
Expect(k8sClient.Delete(ctx, operator)).To(Succeed())
})
Context("with components bearing its label", func() {
var (
objs []runtime.Object
namespace string
)
BeforeEach(func() {
namespace = genName("ns-")
objs = testobj.WithLabel(expectedKey, "",
testobj.WithName(namespace, &corev1.Namespace{}),
)
for _, obj := range objs {
Expect(k8sClient.Create(ctx, obj.(client.Object))).To(Succeed())
}
})
AfterEach(func() {
for _, obj := range objs {
Expect(k8sClient.Delete(ctx, obj.(client.Object), deleteOpts)).To(Succeed())
}
Expect(k8sClient.Delete(ctx, operator, deleteOpts)).To(Succeed())
})
It("should re-create it", func() {
// There's a race condition between this test and the controller. By the time,
// this function is running, we may be in one of three states.
// 1. The original deletion in the test setup has not yet finished, so the original
// operator resource still exists.
// 2. The operator doesn't exist, and the controller has not yet re-created it.
// 3. The operator has already been deleted and re-created.
//
// To solve this problem, we simply compare the UIDs and expect to eventually see a
// a different UID.
Eventually(func() (types.UID, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.GetUID(), err
}, timeout, interval).ShouldNot(Equal(originalUID))
})
})
Context("with no components bearing its label", func() {
It("should not re-create it", func() {
// We expect the operator deletion to eventually complete, and then we
// expect the operator to consistently never be found.
Eventually(func() bool {
return apierrors.IsNotFound(k8sClient.Get(ctx, name, operator))
}, timeout, interval).Should(BeTrue())
Consistently(func() bool {
return apierrors.IsNotFound(k8sClient.Get(ctx, name, operator))
}, timeout, interval).Should(BeTrue())
})
})
})
Describe("component selection", func() {
BeforeEach(func() {
Eventually(func() (*operatorsv1.Components, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components, err
}, timeout, interval).ShouldNot(BeNil())
Eventually(func() (*metav1.LabelSelector, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components.LabelSelector, err
}, timeout, interval).Should(Equal(expectedComponentLabelSelector))
})
AfterEach(func() {
Expect(k8sClient.Delete(ctx, operator, deleteOpts)).To(Succeed())
})
Context("with no components bearing its label", func() {
Specify("a status containing no component references", func() {
Consistently(func() ([]operatorsv1.RichReference, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components.Refs, err
}, timeout, interval).Should(BeEmpty())
})
})
Context("with components bearing its label", func() {
var (
objs []runtime.Object
expectedRefs []operatorsv1.RichReference
namespace string
)
BeforeEach(func() {
namespace = genName("ns-")
objs = testobj.WithLabel(expectedKey, "",
testobj.WithName(namespace, &corev1.Namespace{}),
)
for _, obj := range objs {
Expect(k8sClient.Create(ctx, obj.(client.Object))).To(Succeed())
}
expectedRefs = toRefs(scheme, objs...)
})
AfterEach(func() {
for _, obj := range objs {
Expect(k8sClient.Delete(ctx, obj.(client.Object), deleteOpts)).To(Succeed())
}
})
Specify("a status containing its component references", func() {
Eventually(func() ([]operatorsv1.RichReference, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components.Refs, err
}, timeout, interval).Should(ConsistOf(expectedRefs))
})
Context("when new components are labelled", func() {
BeforeEach(func() {
saName := &types.NamespacedName{Namespace: namespace, Name: genName("sa-")}
newObjs := testobj.WithLabel(expectedKey, "",
testobj.WithNamespacedName(saName, &corev1.ServiceAccount{}),
testobj.WithName(genName("sa-admin-"), &rbacv1.ClusterRoleBinding{
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: saName.Name,
Namespace: saName.Namespace,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "ClusterRole",
Name: "cluster-admin",
},
}),
)
for _, obj := range newObjs {
Expect(k8sClient.Create(ctx, obj.(client.Object))).To(Succeed())
}
objs = append(objs, newObjs...)
expectedRefs = append(expectedRefs, toRefs(scheme, newObjs...)...)
})
It("should add the component references", func() {
Eventually(func() ([]operatorsv1.RichReference, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components.Refs, err
}, timeout, interval).Should(ConsistOf(expectedRefs))
})
})
Context("when multiple types of a gvk are labeled", func() {
BeforeEach(func() {
newObjs := make([]runtime.Object, 9)
// Create objects in reverse order to ensure they are eventually ordered alphabetically in the status of the operator.
for i := 8; i >= 0; i-- {
newObjs[8-i] = testobj.WithLabels(map[string]string{expectedKey: ""}, testobj.WithNamespacedName(
&types.NamespacedName{Namespace: namespace, Name: fmt.Sprintf("sa-%d", i)}, &corev1.ServiceAccount{},
))
}
for _, obj := range newObjs {
Expect(k8sClient.Create(ctx, obj.(client.Object))).To(Succeed())
}
objs = append(objs, newObjs...)
expectedRefs = append(expectedRefs, toRefs(scheme, newObjs...)...)
})
It("should list each of the component references in alphabetical order by namespace and name", func() {
Eventually(func() ([]operatorsv1.RichReference, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components.Refs, err
}, timeout, interval).Should(ConsistOf(expectedRefs))
serviceAccountCount := 0
for _, ref := range operator.Status.Components.Refs {
if ref.Kind != rbacv1.ServiceAccountKind {
continue
}
Expect(ref.Name).Should(Equal(fmt.Sprintf("sa-%d", serviceAccountCount)))
serviceAccountCount++
}
Expect(serviceAccountCount).To(Equal(9))
})
})
Context("when component labels are removed", func() {
BeforeEach(func() {
for _, obj := range testobj.StripLabel(expectedKey, objs...) {
Expect(k8sClient.Update(ctx, obj.(client.Object))).To(Succeed())
}
})
It("should remove the component references", func() {
Eventually(func() ([]operatorsv1.RichReference, error) {
err := k8sClient.Get(ctx, name, operator)
return operator.Status.Components.Refs, err
}, timeout, interval).Should(BeEmpty())
})
})
})
})
})