|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + functionsdevv1alpha1 "github.com/functions-dev/func-operator/api/v1alpha1" |
| 5 | + . "github.com/onsi/ginkgo/v2" |
| 6 | + . "github.com/onsi/gomega" |
| 7 | + v1 "k8s.io/api/core/v1" |
| 8 | + rbacv1 "k8s.io/api/rbac/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/types" |
| 11 | + "k8s.io/apimachinery/pkg/util/rand" |
| 12 | + "k8s.io/client-go/tools/events" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = Describe("Function RBAC", func() { |
| 16 | + Context("ensureDeployFunctionRoleBinding", func() { |
| 17 | + var reconciler *FunctionReconciler |
| 18 | + var testNamespace string |
| 19 | + |
| 20 | + BeforeEach(func() { |
| 21 | + testNamespace = "rbac-test-" + rand.String(6) |
| 22 | + ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}} |
| 23 | + Expect(k8sClient.Create(ctx, ns)).To(Succeed()) |
| 24 | + |
| 25 | + reconciler = &FunctionReconciler{ |
| 26 | + Client: k8sClient, |
| 27 | + Scheme: k8sClient.Scheme(), |
| 28 | + Recorder: &events.FakeRecorder{}, |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + It("should add owner references for multiple Functions without overwriting", func() { |
| 33 | + functionA := &functionsdevv1alpha1.Function{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: "function-a", |
| 36 | + Namespace: testNamespace, |
| 37 | + }, |
| 38 | + Spec: functionsdevv1alpha1.FunctionSpec{ |
| 39 | + Repository: functionsdevv1alpha1.FunctionSpecRepository{ |
| 40 | + URL: "https://github.com/foo/bar", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + Expect(k8sClient.Create(ctx, functionA)).To(Succeed()) |
| 45 | + |
| 46 | + functionB := &functionsdevv1alpha1.Function{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Name: "function-b", |
| 49 | + Namespace: testNamespace, |
| 50 | + }, |
| 51 | + Spec: functionsdevv1alpha1.FunctionSpec{ |
| 52 | + Repository: functionsdevv1alpha1.FunctionSpecRepository{ |
| 53 | + URL: "https://github.com/foo/baz", |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + Expect(k8sClient.Create(ctx, functionB)).To(Succeed()) |
| 58 | + |
| 59 | + By("Reconciling the RoleBinding for Function A") |
| 60 | + Expect(reconciler.ensureDeployFunctionRoleBinding(ctx, functionA)).To(Succeed()) |
| 61 | + |
| 62 | + By("Verifying RoleBinding was created with Function A as owner") |
| 63 | + rb := &rbacv1.RoleBinding{} |
| 64 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 65 | + Name: deployFunctionRoleBindingName, |
| 66 | + Namespace: testNamespace, |
| 67 | + }, rb)).To(Succeed()) |
| 68 | + Expect(rb.OwnerReferences).To(HaveLen(1)) |
| 69 | + Expect(rb.OwnerReferences[0].Name).To(Equal("function-a")) |
| 70 | + |
| 71 | + By("Reconciling the RoleBinding for Function B") |
| 72 | + Expect(reconciler.ensureDeployFunctionRoleBinding(ctx, functionB)).To(Succeed()) |
| 73 | + |
| 74 | + By("Verifying RoleBinding now has both Functions as owners") |
| 75 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 76 | + Name: deployFunctionRoleBindingName, |
| 77 | + Namespace: testNamespace, |
| 78 | + }, rb)).To(Succeed()) |
| 79 | + Expect(rb.OwnerReferences).To(HaveLen(2)) |
| 80 | + |
| 81 | + ownerNames := []string{rb.OwnerReferences[0].Name, rb.OwnerReferences[1].Name} |
| 82 | + Expect(ownerNames).To(ConsistOf("function-a", "function-b")) |
| 83 | + |
| 84 | + By("Verifying no owner is marked as controller") |
| 85 | + for _, ref := range rb.OwnerReferences { |
| 86 | + if ref.Controller != nil { |
| 87 | + Expect(*ref.Controller).To(BeFalse()) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + By("Reconciling Function A again should be idempotent") |
| 92 | + Expect(reconciler.ensureDeployFunctionRoleBinding(ctx, functionA)).To(Succeed()) |
| 93 | + |
| 94 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 95 | + Name: deployFunctionRoleBindingName, |
| 96 | + Namespace: testNamespace, |
| 97 | + }, rb)).To(Succeed()) |
| 98 | + Expect(rb.OwnerReferences).To(HaveLen(2)) |
| 99 | + }) |
| 100 | + |
| 101 | + It("should set the expected Subjects and RoleRef", func() { |
| 102 | + function := &functionsdevv1alpha1.Function{ |
| 103 | + ObjectMeta: metav1.ObjectMeta{ |
| 104 | + Name: "function-rbac-check", |
| 105 | + Namespace: testNamespace, |
| 106 | + }, |
| 107 | + Spec: functionsdevv1alpha1.FunctionSpec{ |
| 108 | + Repository: functionsdevv1alpha1.FunctionSpecRepository{ |
| 109 | + URL: "https://github.com/foo/bar", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + Expect(k8sClient.Create(ctx, function)).To(Succeed()) |
| 114 | + |
| 115 | + Expect(reconciler.ensureDeployFunctionRoleBinding(ctx, function)).To(Succeed()) |
| 116 | + |
| 117 | + rb := &rbacv1.RoleBinding{} |
| 118 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 119 | + Name: deployFunctionRoleBindingName, |
| 120 | + Namespace: testNamespace, |
| 121 | + }, rb)).To(Succeed()) |
| 122 | + |
| 123 | + Expect(rb.Subjects).To(HaveLen(1)) |
| 124 | + Expect(rb.Subjects[0].Kind).To(Equal("ServiceAccount")) |
| 125 | + Expect(rb.Subjects[0].Name).To(Equal("default")) |
| 126 | + |
| 127 | + Expect(rb.RoleRef.APIGroup).To(Equal("rbac.authorization.k8s.io")) |
| 128 | + Expect(rb.RoleRef.Kind).To(Equal("Role")) |
| 129 | + Expect(rb.RoleRef.Name).To(Equal(deployFunctionRoleName)) |
| 130 | + }) |
| 131 | + }) |
| 132 | +}) |
0 commit comments