|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 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 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | + utilrand "k8s.io/apimachinery/pkg/util/rand" |
| 30 | + |
| 31 | + workv1alpha1 "sigs.k8s.io/work-api/pkg/apis/v1alpha1" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = Describe("Work Status Reconciler", func() { |
| 35 | + var resourceNamespace string |
| 36 | + var workNamespace string |
| 37 | + var work *workv1alpha1.Work |
| 38 | + var cm, cm2 *corev1.ConfigMap |
| 39 | + |
| 40 | + var wns corev1.Namespace |
| 41 | + var rns corev1.Namespace |
| 42 | + |
| 43 | + BeforeEach(func() { |
| 44 | + workNamespace = "cluster-" + utilrand.String(5) |
| 45 | + resourceNamespace = utilrand.String(5) |
| 46 | + |
| 47 | + wns = corev1.Namespace{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: workNamespace, |
| 50 | + }, |
| 51 | + } |
| 52 | + err := k8sClient.Create(context.Background(), &wns) |
| 53 | + Expect(err).ToNot(HaveOccurred()) |
| 54 | + |
| 55 | + rns = corev1.Namespace{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: resourceNamespace, |
| 58 | + }, |
| 59 | + } |
| 60 | + err = k8sClient.Create(context.Background(), &rns) |
| 61 | + Expect(err).ToNot(HaveOccurred()) |
| 62 | + |
| 63 | + // Create the Work object with some type of Manifest resource. |
| 64 | + cm = &corev1.ConfigMap{ |
| 65 | + TypeMeta: metav1.TypeMeta{ |
| 66 | + APIVersion: "v1", |
| 67 | + Kind: "ConfigMap", |
| 68 | + }, |
| 69 | + ObjectMeta: metav1.ObjectMeta{ |
| 70 | + Name: "configmap-" + utilrand.String(5), |
| 71 | + Namespace: resourceNamespace, |
| 72 | + }, |
| 73 | + Data: map[string]string{ |
| 74 | + "test": "test", |
| 75 | + }, |
| 76 | + } |
| 77 | + cm2 = &corev1.ConfigMap{ |
| 78 | + TypeMeta: metav1.TypeMeta{ |
| 79 | + APIVersion: "v1", |
| 80 | + Kind: "ConfigMap", |
| 81 | + }, |
| 82 | + ObjectMeta: metav1.ObjectMeta{ |
| 83 | + Name: "configmap2-" + utilrand.String(5), |
| 84 | + Namespace: resourceNamespace, |
| 85 | + }, |
| 86 | + Data: map[string]string{ |
| 87 | + "test": "test", |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + By("Create work that contains two configMaps") |
| 92 | + work = &workv1alpha1.Work{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "work-" + utilrand.String(5), |
| 95 | + Namespace: workNamespace, |
| 96 | + }, |
| 97 | + Spec: workv1alpha1.WorkSpec{ |
| 98 | + Workload: workv1alpha1.WorkloadTemplate{ |
| 99 | + Manifests: []workv1alpha1.Manifest{ |
| 100 | + { |
| 101 | + RawExtension: runtime.RawExtension{Object: cm}, |
| 102 | + }, |
| 103 | + { |
| 104 | + RawExtension: runtime.RawExtension{Object: cm2}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + AfterEach(func() { |
| 113 | + // TODO: Ensure that all resources are being deleted. |
| 114 | + Expect(k8sClient.Delete(context.Background(), work)).Should(Succeed()) |
| 115 | + Expect(k8sClient.Delete(context.Background(), &wns)).Should(Succeed()) |
| 116 | + Expect(k8sClient.Delete(context.Background(), &rns)).Should(Succeed()) |
| 117 | + }) |
| 118 | + |
| 119 | + It("Should delete the manifest from the member cluster after it is removed from work", func() { |
| 120 | + By("Apply the work") |
| 121 | + Expect(k8sClient.Create(context.Background(), work)).ToNot(HaveOccurred()) |
| 122 | + |
| 123 | + By("Make sure that the work is applied") |
| 124 | + currentWork := waitForWorkToApply(work.Name, workNamespace) |
| 125 | + var appliedWork workv1alpha1.AppliedWork |
| 126 | + Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: work.Name}, &appliedWork)).Should(Succeed()) |
| 127 | + Expect(len(appliedWork.Status.AppliedResources)).Should(Equal(2)) |
| 128 | + |
| 129 | + By("Remove configMap 2 from the work") |
| 130 | + currentWork.Spec.Workload.Manifests = []workv1alpha1.Manifest{ |
| 131 | + { |
| 132 | + RawExtension: runtime.RawExtension{Object: cm}, |
| 133 | + }, |
| 134 | + } |
| 135 | + Expect(k8sClient.Update(context.Background(), currentWork)).Should(Succeed()) |
| 136 | + |
| 137 | + By("Verify that the resource is removed from the cluster") |
| 138 | + Eventually(func() bool { |
| 139 | + var configMap corev1.ConfigMap |
| 140 | + return apierrors.IsNotFound(k8sClient.Get(context.Background(), types.NamespacedName{Name: cm2.Name, Namespace: resourceNamespace}, &configMap)) |
| 141 | + }, timeout, interval).Should(BeTrue()) |
| 142 | + |
| 143 | + By("Verify that the appliedWork status is correct") |
| 144 | + Eventually(func() bool { |
| 145 | + Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: work.Name}, &appliedWork)).Should(Succeed()) |
| 146 | + return len(appliedWork.Status.AppliedResources) == 1 |
| 147 | + }, timeout, interval).Should(BeTrue()) |
| 148 | + Expect(appliedWork.Status.AppliedResources[0].Name).Should(Equal(cm.GetName())) |
| 149 | + Expect(appliedWork.Status.AppliedResources[0].Namespace).Should(Equal(cm.GetNamespace())) |
| 150 | + Expect(appliedWork.Status.AppliedResources[0].Version).Should(Equal(cm.GetObjectKind().GroupVersionKind().Version)) |
| 151 | + Expect(appliedWork.Status.AppliedResources[0].Group).Should(Equal(cm.GetObjectKind().GroupVersionKind().Group)) |
| 152 | + Expect(appliedWork.Status.AppliedResources[0].Kind).Should(Equal(cm.GetObjectKind().GroupVersionKind().Kind)) |
| 153 | + }) |
| 154 | + |
| 155 | + It("Should delete the manifest from the member cluster after it is removed from work", func() { |
| 156 | + By("Create another work that contains configMap 2") |
| 157 | + work2 := work.DeepCopy() |
| 158 | + work2.Name = "work-" + utilrand.String(5) |
| 159 | + Expect(k8sClient.Create(context.Background(), work)).ToNot(HaveOccurred()) |
| 160 | + Expect(k8sClient.Create(context.Background(), work2)).ToNot(HaveOccurred()) |
| 161 | + |
| 162 | + By("Make sure that the appliedWork is updated") |
| 163 | + var appliedWork, appliedWork2 workv1alpha1.AppliedWork |
| 164 | + Eventually(func() bool { |
| 165 | + err := k8sClient.Get(context.Background(), types.NamespacedName{Name: work.Name}, &appliedWork) |
| 166 | + if err != nil { |
| 167 | + return false |
| 168 | + } |
| 169 | + return len(appliedWork.Status.AppliedResources) == 2 |
| 170 | + }, timeout, interval).Should(BeTrue()) |
| 171 | + |
| 172 | + By("Make sure that the appliedWork2 is updated") |
| 173 | + Eventually(func() bool { |
| 174 | + err := k8sClient.Get(context.Background(), types.NamespacedName{Name: work2.Name}, &appliedWork2) |
| 175 | + if err != nil { |
| 176 | + return false |
| 177 | + } |
| 178 | + return len(appliedWork2.Status.AppliedResources) == 2 |
| 179 | + }, timeout, interval).Should(BeTrue()) |
| 180 | + |
| 181 | + By("Remove configMap 2 from the work") |
| 182 | + currentWork := waitForWorkToApply(work.Name, workNamespace) |
| 183 | + currentWork.Spec.Workload.Manifests = []workv1alpha1.Manifest{ |
| 184 | + { |
| 185 | + RawExtension: runtime.RawExtension{Object: cm}, |
| 186 | + }, |
| 187 | + } |
| 188 | + Expect(k8sClient.Update(context.Background(), currentWork)).Should(Succeed()) |
| 189 | + currentWork = waitForWorkToApply(work.Name, workNamespace) |
| 190 | + Expect(len(currentWork.Status.ManifestConditions)).Should(Equal(1)) |
| 191 | + |
| 192 | + By("Verify that configMap 2 is removed from the appliedWork") |
| 193 | + Eventually(func() bool { |
| 194 | + Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: work.Name}, &appliedWork)).Should(Succeed()) |
| 195 | + return len(appliedWork.Status.AppliedResources) == 1 |
| 196 | + }, timeout, interval).Should(BeTrue()) |
| 197 | + |
| 198 | + By("Verify that configMap 2 is not removed from the cluster") |
| 199 | + Consistently(func() bool { |
| 200 | + var configMap corev1.ConfigMap |
| 201 | + return k8sClient.Get(context.Background(), types.NamespacedName{Name: cm2.Name, Namespace: resourceNamespace}, &configMap) == nil |
| 202 | + }, timeout, interval).Should(BeTrue()) |
| 203 | + |
| 204 | + By("Remove configMap 2 from the work2") |
| 205 | + currentWork = waitForWorkToApply(work2.Name, workNamespace) |
| 206 | + currentWork.Spec.Workload.Manifests = []workv1alpha1.Manifest{ |
| 207 | + { |
| 208 | + RawExtension: runtime.RawExtension{Object: cm}, |
| 209 | + }, |
| 210 | + } |
| 211 | + Expect(k8sClient.Update(context.Background(), currentWork)).Should(Succeed()) |
| 212 | + currentWork = waitForWorkToApply(work.Name, workNamespace) |
| 213 | + Expect(len(currentWork.Status.ManifestConditions)).Should(Equal(1)) |
| 214 | + |
| 215 | + By("Verify that the resource is removed from the appliedWork") |
| 216 | + Eventually(func() bool { |
| 217 | + Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: work2.Name}, &appliedWork2)).Should(Succeed()) |
| 218 | + return len(appliedWork2.Status.AppliedResources) == 1 |
| 219 | + }, timeout, interval).Should(BeTrue()) |
| 220 | + |
| 221 | + By("Verify that the cm2 is removed from the cluster") |
| 222 | + Eventually(func() bool { |
| 223 | + var configMap corev1.ConfigMap |
| 224 | + return apierrors.IsNotFound(k8sClient.Get(context.Background(), types.NamespacedName{Name: cm2.Name, Namespace: resourceNamespace}, &configMap)) |
| 225 | + }, timeout, interval).Should(BeTrue()) |
| 226 | + }) |
| 227 | + |
| 228 | +}) |
0 commit comments