-
Notifications
You must be signed in to change notification settings - Fork 30
/
policyserver_controller_test.go
358 lines (305 loc) · 12.7 KB
/
policyserver_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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
Copyright 2022.
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 controllers
import (
"fmt"
"time"
. "github.com/onsi/ginkgo/v2" //nolint:revive
. "github.com/onsi/gomega" //nolint:revive
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
k8spoliciesv1 "k8s.io/api/policy/v1"
)
var _ = Describe("PolicyServer controller", func() {
Context("when starting with a new PolicyServer", func() {
policyServerName := newName("policy-server")
BeforeEach(func() {
Expect(
k8sClient.Create(ctx, policyServerFactory(policyServerName)),
).To(haveSucceededOrAlreadyExisted())
// Wait for the Service associated with the PolicyServer to be created
Eventually(func(g Gomega) error {
_, err := getTestPolicyServerService(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
})
Context("with no assigned policies", func() {
It("should get its finalizer removed", func() {
By("deleting the policy server")
Expect(
k8sClient.Delete(ctx, policyServerFactory(policyServerName)),
).To(Succeed())
Eventually(func(g Gomega) (*policiesv1.PolicyServer, error) {
return getTestPolicyServer(policyServerName)
}, timeout, pollInterval).ShouldNot(
HaveField("Finalizers", ContainElement(constants.KubewardenFinalizer)),
)
})
AfterEach(func() {
// It's necessary remove the test finalizer to make the
// BeforeEach work as extected. Otherwise, the policy service
// creation will not work as expected
policyServer, err := getTestPolicyServer(policyServerName)
Expect(err).Should(Succeed())
controllerutil.RemoveFinalizer(policyServer, IntegrationTestsFinalizer)
err = reconciler.Client.Update(ctx, policyServer)
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega) error {
_, err := getTestPolicyServer(policyServerName)
return err
}, timeout, pollInterval).ShouldNot(Succeed())
})
})
Context("with assigned policies", Serial, func() {
policyName := newName("policy")
It("should delete assigned policies", func() {
By("creating a policy and assigning it to the policy server")
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyFactory(policyName, policyServerName, false)),
).To(haveSucceededOrAlreadyExisted())
Expect(
getTestPolicyServerService(policyServerName),
).To(
HaveField("DeletionTimestamp", BeNil()),
)
By("deleting the policy server")
Expect(
k8sClient.Delete(ctx, policyServerFactory(policyServerName)),
).To(Succeed())
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).ShouldNot(
HaveField("DeletionTimestamp", BeNil()),
)
})
It("should not delete its managed resources until all the scheduled policies are gone", func() {
By("having still policies pending deletion")
Expect(
getTestClusterAdmissionPolicy(policyName),
).To(
And(
HaveField("DeletionTimestamp", Not(BeNil())),
HaveField("Finalizers", Not(ContainElement(constants.KubewardenFinalizer))),
HaveField("Finalizers", ContainElement(IntegrationTestsFinalizer)),
),
)
Eventually(func(g Gomega) error {
_, err := getTestPolicyServerService(policyServerName)
return err
}).Should(Succeed())
})
It(fmt.Sprintf("should get its %q finalizer removed", constants.KubewardenFinalizer), func() {
By("not having policies assigned")
policy, err := getTestClusterAdmissionPolicy(policyName)
Expect(err).ToNot(HaveOccurred())
controllerutil.RemoveFinalizer(policy, IntegrationTestsFinalizer)
err = reconciler.Client.Update(ctx, policy)
Expect(err).ToNot(HaveOccurred())
// wait for the reconciliation loop of the ClusterAdmissionPolicy to remove the resource
Eventually(func(g Gomega) error {
_, err := getTestClusterAdmissionPolicy(policyName)
return err
}, timeout, pollInterval).ShouldNot(Succeed())
Eventually(func(g Gomega) error {
_, err := getTestPolicyServerService(policyServerName)
return err
}, timeout, pollInterval).ShouldNot(Succeed())
Eventually(func(g Gomega) (*policiesv1.PolicyServer, error) {
return getTestPolicyServer(policyServerName)
}, timeout, pollInterval).ShouldNot(
HaveField("Finalizers", ContainElement(constants.KubewardenFinalizer)),
)
})
})
})
Context("when starting policy server", func() {
policyServerName := newName("policy-server")
It("with MinAvailable PodDisruptionBudget configuration should create PDB", func() {
minAvailable := intstr.FromInt(2)
policyServer := policyServerFactory(policyServerName)
policyServer.Spec.MinAvailable = &minAvailable
// It's necessary remove the test finalizer to make the
// policy service goes away.
controllerutil.RemoveFinalizer(policyServer, IntegrationTestsFinalizer)
Expect(
k8sClient.Create(ctx, policyServer),
).To(haveSucceededOrAlreadyExisted())
// Wait for the Service associated with the PolicyServer to be created
Eventually(func(g Gomega) error {
_, err := getTestPolicyServer(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
Eventually(func(g Gomega) *k8spoliciesv1.PodDisruptionBudget {
pdb, _ := getPolicyServerPodDisruptionBudget(policyServerName)
return pdb
}, timeout, pollInterval).Should(policyServerPodDisruptionBudgetMatcher(policyServer, &minAvailable, nil))
})
It("with MaxUnavailable PodDisruptionBudget configuration should create PDB", func() {
maxUnavailable := intstr.FromInt(2)
policyServer := policyServerFactory(policyServerName)
policyServer.Spec.MaxUnavailable = &maxUnavailable
// It's necessary remove the test finalizer to make the
// policy service goes away.
controllerutil.RemoveFinalizer(policyServer, IntegrationTestsFinalizer)
Expect(
k8sClient.Create(ctx, policyServer),
).To(haveSucceededOrAlreadyExisted())
// Wait for the Service associated with the PolicyServer to be created
Eventually(func(g Gomega) error {
_, err := getTestPolicyServer(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
Eventually(func(g Gomega) *k8spoliciesv1.PodDisruptionBudget {
pdb, _ := getPolicyServerPodDisruptionBudget(policyServerName)
return pdb
}, timeout, pollInterval).Should(policyServerPodDisruptionBudgetMatcher(policyServer, nil, &maxUnavailable))
})
It("with no PodDisruptionBudget configuration should not create PDB", func() {
policyServer := policyServerFactory(policyServerName)
// It's necessary remove the test finalizer to make the
// policy service goes away.
controllerutil.RemoveFinalizer(policyServer, IntegrationTestsFinalizer)
Expect(
k8sClient.Create(ctx, policyServer),
).To(haveSucceededOrAlreadyExisted())
// Wait for the Service associated with the PolicyServer to be created
Eventually(func(g Gomega) error {
_, err := getTestPolicyServer(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
// Wait for the Service associated with the PolicyServer to be created.
// The service reconciliation is after the PDB reconciliation.
Eventually(func(g Gomega) error {
_, err := getTestPolicyServerService(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
Consistently(func(g Gomega) error {
_, err := getPolicyServerPodDisruptionBudget(policyServerName)
return err
}, 10*time.Second, pollInterval).ShouldNot(Succeed())
})
It("when update policy server PodDisruptionBudget configuration should create PDB", func() {
policyServer := policyServerFactory(policyServerName)
// It's necessary remove the test finalizer to make the
// policy service goes away.
controllerutil.RemoveFinalizer(policyServer, IntegrationTestsFinalizer)
Expect(
k8sClient.Create(ctx, policyServer),
).To(haveSucceededOrAlreadyExisted())
// Wait for the Service associated with the PolicyServer to be created
Eventually(func(g Gomega) error {
_, err := getTestPolicyServer(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
// Wait for the Service associated with the PolicyServer to be created.
// The service reconciliation is after the PDB reconciliation.
Eventually(func(g Gomega) error {
_, err := getTestPolicyServerService(policyServerName)
return err
}, timeout, pollInterval).Should(Succeed())
Consistently(func(g Gomega) error {
_, err := getPolicyServerPodDisruptionBudget(policyServerName)
return err
}, 10*time.Second, pollInterval).ShouldNot(Succeed())
policyServer, err := getTestPolicyServer(policyServerName)
Expect(err).ToNot(HaveOccurred())
maxUnavailable := intstr.FromInt(2)
policyServer.Spec.MaxUnavailable = &maxUnavailable
err = k8sClient.Update(ctx, policyServer)
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega) *k8spoliciesv1.PodDisruptionBudget {
pdb, _ := getPolicyServerPodDisruptionBudget(policyServerName)
return pdb
}, timeout, pollInterval).Should(policyServerPodDisruptionBudgetMatcher(policyServer, nil, &maxUnavailable))
})
AfterEach(func() {
policyServer, err := getTestPolicyServer(policyServerName)
Expect(err).Should(Succeed())
err = reconciler.Client.Delete(ctx, policyServer)
Expect(err).ToNot(HaveOccurred())
Eventually(func(g Gomega) error {
_, err := getTestPolicyServer(policyServerName)
return err
}, timeout, pollInterval).ShouldNot(Succeed())
Eventually(func(g Gomega) error {
_, err := getPolicyServerPodDisruptionBudget(policyServerName)
return err
}, timeout, pollInterval).ShouldNot(Succeed())
})
})
When("creating a PolicyServer with requests and no limits", func() {
policyServerName := newName("policy-server")
policyServer := policyServerFactory(policyServerName)
policyServer.Spec.Limits = corev1.ResourceList{
"cpu": resource.MustParse("100m"),
"memory": resource.MustParse("1Gi"),
}
It("should create the PolicyServer pod with the limits and the requests", func() {
By("creating the PolicyServer")
Expect(
k8sClient.Create(ctx, policyServer),
).To(haveSucceededOrAlreadyExisted())
By("creating a deployment with limits and requests set")
Eventually(func(g Gomega) error {
deployment, err := getTestPolicyServerDeployment(policyServerName)
if err != nil {
return err
}
Expect(deployment.Spec.Template.Spec.Containers[0].Resources.Limits).To(Equal(policyServer.Spec.Limits))
return nil
}, timeout, pollInterval).Should(Succeed())
By("creating a pod with limit and request set")
Eventually(func(g Gomega) error {
pod, err := getTestPolicyServerPod(policyServerName)
if err != nil {
return err
}
Expect(pod.Spec.Containers[0].Resources.Limits).To(Equal(policyServer.Spec.Limits))
By("setting the requests to the same value as the limits")
Expect(pod.Spec.Containers[0].Resources.Requests).To(Equal(policyServer.Spec.Limits))
return nil
}, timeout, pollInterval).Should(Succeed())
})
When("the requests are updated", func() {
It("should update the PolicyServer pod with the new requests", func() {
By("updating the PolicyServer requests")
policyServer, err := getTestPolicyServer(policyServerName)
Expect(err).ToNot(HaveOccurred())
policyServer.Spec.Requests = corev1.ResourceList{
"cpu": resource.MustParse("50m"),
"memory": resource.MustParse("500Mi"),
}
Expect(
k8sClient.Update(ctx, policyServer),
).To(Succeed())
By("updating the pod with the new requests")
Eventually(func(g Gomega) (*corev1.Container, error) {
pod, err := getTestPolicyServerPod(policyServerName)
if err != nil {
return nil, err
}
return &pod.Spec.Containers[0], nil
}, timeout, pollInterval).Should(
And(
HaveField("Resources.Requests", Equal(policyServer.Spec.Requests)),
HaveField("Resources.Limits", Equal(policyServer.Spec.Limits)),
),
)
})
})
})
})