|
| 1 | +package controller_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/neuvector/runtime-enforcer/api/v1alpha1" |
| 7 | + "github.com/neuvector/runtime-enforcer/internal/controller" |
| 8 | + "github.com/neuvector/runtime-enforcer/internal/policygenerator" |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("WorkloadPolicy Controller", func() { |
| 19 | + Context("When reconciling a resource", func() { |
| 20 | + const policyName = "test-policy" |
| 21 | + const testNamespace = "default" |
| 22 | + |
| 23 | + ctx = context.Background() |
| 24 | + |
| 25 | + typeNamespacedName := types.NamespacedName{ |
| 26 | + Name: policyName, |
| 27 | + Namespace: testNamespace, |
| 28 | + } |
| 29 | + policy := &v1alpha1.WorkloadPolicy{} |
| 30 | + |
| 31 | + BeforeEach(func() { |
| 32 | + By("Creating a new WorkloadPolicy that is ") |
| 33 | + err := k8sClient.Get(ctx, typeNamespacedName, policy) |
| 34 | + if err != nil && errors.IsNotFound(err) { |
| 35 | + resource := |
| 36 | + &v1alpha1.WorkloadPolicy{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: policyName, |
| 39 | + Namespace: testNamespace, |
| 40 | + Finalizers: []string{v1alpha1.WorkloadPolicyFinalizer}, |
| 41 | + }, |
| 42 | + Spec: v1alpha1.WorkloadPolicySpec{ |
| 43 | + Mode: "monitor", |
| 44 | + RulesByContainer: map[string]*v1alpha1.WorkloadPolicyRules{ |
| 45 | + "main": { |
| 46 | + Executables: v1alpha1.WorkloadPolicyExecutables{ |
| 47 | + Allowed: []string{"/usr/bin/sleep"}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + Severity: 10, |
| 52 | + Tags: []string{ |
| 53 | + "tag", |
| 54 | + }, |
| 55 | + Message: "TEST_RULE", |
| 56 | + }, |
| 57 | + } |
| 58 | + Expect(k8sClient.Create(ctx, resource)).To(Succeed()) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + AfterEach(func() { |
| 63 | + resource := &v1alpha1.WorkloadPolicy{} |
| 64 | + err := k8sClient.Get(ctx, typeNamespacedName, resource) |
| 65 | + if errors.IsNotFound(err) { |
| 66 | + // Resource already deleted, nothing to clean up |
| 67 | + return |
| 68 | + } |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + |
| 71 | + By("Cleanup the specific resource instance WorkloadPolicy") |
| 72 | + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) |
| 73 | + }) |
| 74 | + |
| 75 | + It("Should delete a WorkloadPolicy that is not referenced by any Pod", func() { |
| 76 | + By("Deleting the created WorkloadPolicy") |
| 77 | + policy = &v1alpha1.WorkloadPolicy{ |
| 78 | + ObjectMeta: metav1.ObjectMeta{ |
| 79 | + Name: policyName, |
| 80 | + Namespace: testNamespace, |
| 81 | + }, |
| 82 | + } |
| 83 | + Expect(k8sClient.Delete(ctx, policy)).To(Succeed()) |
| 84 | + |
| 85 | + By("Reconciling the created resource") |
| 86 | + controllerReconciler := &controller.WorkloadPolicyReconciler{ |
| 87 | + Client: k8sClient, |
| 88 | + Scheme: k8sClient.Scheme(), |
| 89 | + } |
| 90 | + |
| 91 | + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ |
| 92 | + NamespacedName: typeNamespacedName, |
| 93 | + }) |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + |
| 96 | + By("Verifying the WorkloadPolicy has been deleted") |
| 97 | + err = k8sClient.Get(ctx, typeNamespacedName, policy) |
| 98 | + Expect(errors.IsNotFound(err)).To(BeTrue()) |
| 99 | + }) |
| 100 | + |
| 101 | + It("Should not delete a WorkloadPolicy that is referenced by a Pod", func() { |
| 102 | + By("Associating the WorkloadPolicy with a Pod") |
| 103 | + podName := "test-pod" |
| 104 | + pod := &corev1.Pod{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{ |
| 106 | + Name: podName, |
| 107 | + Namespace: testNamespace, |
| 108 | + Labels: map[string]string{ |
| 109 | + policygenerator.PolicyLabelKey: policyName, |
| 110 | + }, |
| 111 | + }, |
| 112 | + Spec: corev1.PodSpec{ |
| 113 | + Containers: []corev1.Container{ |
| 114 | + { |
| 115 | + Name: "pause", |
| 116 | + Image: "registry.k8s.io/pause", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + Expect(k8sClient.Create(ctx, pod)).To(Succeed()) |
| 122 | + |
| 123 | + By("Deleting the referenced WorkloadPolicy") |
| 124 | + policy = &v1alpha1.WorkloadPolicy{ |
| 125 | + ObjectMeta: metav1.ObjectMeta{ |
| 126 | + Name: policyName, |
| 127 | + Namespace: testNamespace, |
| 128 | + }, |
| 129 | + } |
| 130 | + Expect(k8sClient.Delete(ctx, policy)).To(Succeed()) |
| 131 | + |
| 132 | + By("Reconciling the deleted WorkloadPolicy") |
| 133 | + controllerReconciler := &controller.WorkloadPolicyReconciler{ |
| 134 | + Client: k8sClient, |
| 135 | + Scheme: k8sClient.Scheme(), |
| 136 | + } |
| 137 | + |
| 138 | + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ |
| 139 | + NamespacedName: typeNamespacedName, |
| 140 | + }) |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + |
| 143 | + By("Verifying the WorkloadPolicy has not been deleted") |
| 144 | + err = k8sClient.Get(ctx, typeNamespacedName, policy) |
| 145 | + Expect(err).NotTo(HaveOccurred()) |
| 146 | + |
| 147 | + By("Cleaning up the created Pod") |
| 148 | + Expect(k8sClient.Delete(ctx, pod)).To(Succeed()) |
| 149 | + |
| 150 | + By("Reconciling the WorkloadPolicy deletion again") |
| 151 | + _, err = controllerReconciler.Reconcile(ctx, reconcile.Request{ |
| 152 | + NamespacedName: typeNamespacedName, |
| 153 | + }) |
| 154 | + Expect(err).NotTo(HaveOccurred()) |
| 155 | + |
| 156 | + By("Verifying the WorkloadPolicy has been deleted after Pod removal") |
| 157 | + err = k8sClient.Get(ctx, typeNamespacedName, policy) |
| 158 | + Expect(errors.IsNotFound(err)).To(BeTrue()) |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | +}) |
0 commit comments