|
6 | 6 | "os" |
7 | 7 | "strings" |
8 | 8 | "sync" |
| 9 | + "time" |
9 | 10 |
|
10 | 11 | admv1 "k8s.io/api/admissionregistration/v1" |
11 | 12 | appsv1 "k8s.io/api/apps/v1" |
@@ -38,15 +39,7 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { |
38 | 39 |
|
39 | 40 | BeforeAll(func() { |
40 | 41 | By("Create SriovOperatorConfig controller k8s objs") |
41 | | - config := &sriovnetworkv1.SriovOperatorConfig{} |
42 | | - config.SetNamespace(testNamespace) |
43 | | - config.SetName(consts.DefaultConfigName) |
44 | | - config.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ |
45 | | - EnableInjector: true, |
46 | | - EnableOperatorWebhook: true, |
47 | | - ConfigDaemonNodeSelector: map[string]string{}, |
48 | | - LogLevel: 2, |
49 | | - } |
| 42 | + config := makeDefaultSriovOpConfig() |
50 | 43 | Expect(k8sClient.Create(context.Background(), config)).Should(Succeed()) |
51 | 44 | DeferCleanup(func() { |
52 | 45 | err := k8sClient.Delete(context.Background(), config) |
@@ -224,6 +217,29 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { |
224 | 217 | Expect(err).NotTo(HaveOccurred()) |
225 | 218 | }) |
226 | 219 |
|
| 220 | + // Namespaced resources are deleted via the `.ObjectMeta.OwnerReference` field. That logic can't be tested here because testenv doesn't have built-in controllers |
| 221 | + // (See https://book.kubebuilder.io/reference/envtest#testing-considerations). Since Service and DaemonSet are deleted when default/SriovOperatorConfig is no longer |
| 222 | + // present, it's important that webhook configurations are deleted as well. |
| 223 | + It("should delete the webhooks when SriovOperatorConfig/default is deleted", func() { |
| 224 | + DeferCleanup(k8sClient.Create, context.Background(), makeDefaultSriovOpConfig()) |
| 225 | + |
| 226 | + err := k8sClient.Delete(context.Background(), &sriovnetworkv1.SriovOperatorConfig{ |
| 227 | + ObjectMeta: metav1.ObjectMeta{Namespace: testNamespace, Name: "default"}, |
| 228 | + }) |
| 229 | + Expect(err).NotTo(HaveOccurred()) |
| 230 | + |
| 231 | + assertResourceDoesNotExist( |
| 232 | + schema.GroupVersionKind{Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration", Version: "v1"}, |
| 233 | + client.ObjectKey{Name: "sriov-operator-webhook-config"}) |
| 234 | + assertResourceDoesNotExist( |
| 235 | + schema.GroupVersionKind{Group: "admissionregistration.k8s.io", Kind: "ValidatingWebhookConfiguration", Version: "v1"}, |
| 236 | + client.ObjectKey{Name: "sriov-operator-webhook-config"}) |
| 237 | + |
| 238 | + assertResourceDoesNotExist( |
| 239 | + schema.GroupVersionKind{Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration", Version: "v1"}, |
| 240 | + client.ObjectKey{Name: "network-resources-injector-config"}) |
| 241 | + }) |
| 242 | + |
227 | 243 | It("should be able to update the node selector of sriov-network-config-daemon", func() { |
228 | 244 | By("specify the configDaemonNodeSelector") |
229 | 245 | nodeSelector := map[string]string{"node-role.kubernetes.io/worker": ""} |
@@ -517,13 +533,40 @@ var _ = Describe("SriovOperatorConfig controller", Ordered, func() { |
517 | 533 | }) |
518 | 534 | }) |
519 | 535 |
|
| 536 | +func makeDefaultSriovOpConfig() *sriovnetworkv1.SriovOperatorConfig { |
| 537 | + config := &sriovnetworkv1.SriovOperatorConfig{} |
| 538 | + config.SetNamespace(testNamespace) |
| 539 | + config.SetName(consts.DefaultConfigName) |
| 540 | + config.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ |
| 541 | + EnableInjector: true, |
| 542 | + EnableOperatorWebhook: true, |
| 543 | + ConfigDaemonNodeSelector: map[string]string{}, |
| 544 | + LogLevel: 2, |
| 545 | + } |
| 546 | + return config |
| 547 | +} |
| 548 | + |
520 | 549 | func assertResourceExists(gvk schema.GroupVersionKind, key client.ObjectKey) { |
521 | 550 | u := &unstructured.Unstructured{} |
522 | 551 | u.SetGroupVersionKind(gvk) |
523 | 552 | err := k8sClient.Get(context.Background(), key, u) |
524 | 553 | Expect(err).NotTo(HaveOccurred()) |
525 | 554 | } |
526 | 555 |
|
| 556 | +func assertResourceDoesNotExist(gvk schema.GroupVersionKind, key client.ObjectKey) { |
| 557 | + Eventually(func(g Gomega) { |
| 558 | + u := &unstructured.Unstructured{} |
| 559 | + u.SetGroupVersionKind(gvk) |
| 560 | + err := k8sClient.Get(context.Background(), key, u) |
| 561 | + g.Expect(err).To(HaveOccurred()) |
| 562 | + g.Expect(errors.IsNotFound(err)).To(BeTrue()) |
| 563 | + }). |
| 564 | + WithOffset(1). |
| 565 | + WithPolling(100*time.Millisecond). |
| 566 | + WithTimeout(2*time.Second). |
| 567 | + Should(Succeed(), "Resource type[%s] name[%s] still present in the cluster", gvk.String(), key.String()) |
| 568 | +} |
| 569 | + |
527 | 570 | func updateConfigDaemonNodeSelector(newValue map[string]string) func() { |
528 | 571 | config := &sriovnetworkv1.SriovOperatorConfig{} |
529 | 572 | err := k8sClient.Get(context.Background(), types.NamespacedName{Namespace: testNamespace, Name: "default"}, config) |
|
0 commit comments