|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + v1 "k8s.io/api/core/v1" |
| 22 | + "math/rand/v2" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | + "time" |
| 25 | + |
| 26 | + templatesv1alpha1 "github.com/kluctl/template-controller/api/v1alpha1" |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = Describe("ObjectTemplate controller", func() { |
| 33 | + const ( |
| 34 | + timeout = time.Second * 1000 |
| 35 | + duration = time.Second * 10 |
| 36 | + interval = time.Millisecond * 250 |
| 37 | + ) |
| 38 | + |
| 39 | + Context("Template without permissions to write object", func() { |
| 40 | + ns := fmt.Sprintf("test-%d", rand.Int64()) |
| 41 | + |
| 42 | + key := client.ObjectKey{Name: "t1", Namespace: ns} |
| 43 | + cmKey := client.ObjectKey{Name: "cm1", Namespace: ns} |
| 44 | + |
| 45 | + t := buildObjectTemplate(key.Name, key.Namespace, |
| 46 | + []templatesv1alpha1.MatrixEntry{buildMatrixListEntry("m1")}, |
| 47 | + []templatesv1alpha1.Template{ |
| 48 | + {Object: buildTestConfigMap(cmKey.Name, cmKey.Namespace, map[string]string{ |
| 49 | + "k1": `{{ matrix.m1.k1 + matrix.m1.k2 }}`, |
| 50 | + })}, |
| 51 | + }) |
| 52 | + |
| 53 | + It("Should fail initially", func() { |
| 54 | + createNamespace(ns) |
| 55 | + createServiceAccount("default", ns) |
| 56 | + |
| 57 | + Expect(k8sClient.Create(ctx, t)).Should(Succeed()) |
| 58 | + waitUntiReconciled(key, timeout) |
| 59 | + |
| 60 | + Consistently(func() error { |
| 61 | + return k8sClient.Get(ctx, cmKey, &v1.ConfigMap{}) |
| 62 | + }, "2s").Should(MatchError("configmaps \"cm1\" not found")) |
| 63 | + |
| 64 | + assertFailedConfigMaps(key, cmKey) |
| 65 | + }) |
| 66 | + It("Should succeed when RBAC is added", func() { |
| 67 | + createRoleWithBinding("default", ns, []string{"configmaps"}) |
| 68 | + |
| 69 | + triggerReconcile(key) |
| 70 | + waitUntiReconciled(key, timeout) |
| 71 | + |
| 72 | + assertAppliedConfigMaps(key, cmKey) |
| 73 | + |
| 74 | + var cm v1.ConfigMap |
| 75 | + err := k8sClient.Get(ctx, cmKey, &cm) |
| 76 | + Expect(err).To(Succeed()) |
| 77 | + |
| 78 | + Expect(cm.Data).To(Equal(map[string]string{ |
| 79 | + "k1": "3", |
| 80 | + })) |
| 81 | + }) |
| 82 | + It("Should fail with non-existing SA", func() { |
| 83 | + updateObjectTemplate(key, func(t *templatesv1alpha1.ObjectTemplate) { |
| 84 | + t.Spec.ServiceAccountName = "non-existent" |
| 85 | + }) |
| 86 | + waitUntiReconciled(key, timeout) |
| 87 | + |
| 88 | + assertFailedConfigMap(key, cmKey, "configmaps \"cm1\" is forbidden") |
| 89 | + }) |
| 90 | + It("Should succeed after the SA is being created", func() { |
| 91 | + createServiceAccount("non-existent", ns) |
| 92 | + createRoleWithBinding("non-existent", ns, []string{"configmaps"}) |
| 93 | + triggerReconcile(key) |
| 94 | + waitUntiReconciled(key, timeout) |
| 95 | + assertAppliedConfigMaps(key, cmKey) |
| 96 | + }) |
| 97 | + }) |
| 98 | + Context("Template without permissions to read matrix object", func() { |
| 99 | + ns := fmt.Sprintf("test-%d", rand.Int64()) |
| 100 | + |
| 101 | + key := client.ObjectKey{Name: "t1", Namespace: ns} |
| 102 | + cmKey := client.ObjectKey{Name: "cm1", Namespace: ns} |
| 103 | + |
| 104 | + It("Should fail initially", func() { |
| 105 | + createNamespace(ns) |
| 106 | + createServiceAccount("default", ns) |
| 107 | + createRoleWithBinding("default", ns, []string{"configmaps"}) |
| 108 | + |
| 109 | + t := buildObjectTemplate(key.Name, key.Namespace, |
| 110 | + []templatesv1alpha1.MatrixEntry{ |
| 111 | + buildMatrixObjectEntry("m1", "m1", ns, "Secret", "", false), |
| 112 | + }, |
| 113 | + []templatesv1alpha1.Template{ |
| 114 | + {Object: buildTestConfigMap(cmKey.Name, cmKey.Namespace, map[string]string{ |
| 115 | + "k1": `{{ matrix.m1.k1 + matrix.m1.k2 }}`, |
| 116 | + })}, |
| 117 | + }) |
| 118 | + |
| 119 | + err := k8sClient.Create(ctx, buildTestSecret("m1", ns, map[string]string{ |
| 120 | + "k1": "1", |
| 121 | + "k2": "2", |
| 122 | + })) |
| 123 | + Expect(err).To(Succeed()) |
| 124 | + |
| 125 | + Expect(k8sClient.Create(ctx, t)).Should(Succeed()) |
| 126 | + waitUntiReconciled(key, timeout) |
| 127 | + |
| 128 | + t2 := getObjectTemplate(key) |
| 129 | + c := getReadyCondition(t2.GetConditions()) |
| 130 | + Expect(c.Status).To(Equal(metav1.ConditionFalse)) |
| 131 | + Expect(c.Message).To(ContainSubstring("secrets \"m1\" is forbidden")) |
| 132 | + }) |
| 133 | + It("Should succeed when RBAC is created", func() { |
| 134 | + createRoleWithBinding("default", ns, []string{"secrets"}) |
| 135 | + triggerReconcile(key) |
| 136 | + waitUntiReconciled(key, timeout) |
| 137 | + assertAppliedConfigMaps(key, cmKey) |
| 138 | + }) |
| 139 | + It("Should fail with non-existing SA", func() { |
| 140 | + updateObjectTemplate(key, func(t *templatesv1alpha1.ObjectTemplate) { |
| 141 | + t.Spec.ServiceAccountName = "non-existent" |
| 142 | + }) |
| 143 | + waitUntiReconciled(key, timeout) |
| 144 | + |
| 145 | + t2 := getObjectTemplate(key) |
| 146 | + c := getReadyCondition(t2.GetConditions()) |
| 147 | + Expect(c.Status).To(Equal(metav1.ConditionFalse)) |
| 148 | + Expect(c.Message).To(ContainSubstring("secrets \"m1\" is forbidden")) |
| 149 | + }) |
| 150 | + It("Should succeed after the SA is being created", func() { |
| 151 | + createServiceAccount("non-existent", ns) |
| 152 | + createRoleWithBinding("non-existent", ns, []string{"configmaps"}) |
| 153 | + triggerReconcile(key) |
| 154 | + waitUntiReconciled(key, timeout) |
| 155 | + assertAppliedConfigMaps(key, cmKey) |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
0 commit comments