|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + operatorsv1 "github.com/operator-framework/api/pkg/operators/v1" |
| 10 | + operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 11 | + "github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + "k8s.io/apimachinery/pkg/api/meta" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/fields" |
| 17 | + k8slabels "k8s.io/apimachinery/pkg/labels" |
| 18 | + "k8s.io/apimachinery/pkg/selection" |
| 19 | + apitypes "k8s.io/apimachinery/pkg/types" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = Describe("Disabling copied CSVs", func() { |
| 24 | + var ( |
| 25 | + ns corev1.Namespace |
| 26 | + csv operatorsv1alpha1.ClusterServiceVersion |
| 27 | + nonTerminatingNamespaceSelector = fields.ParseSelectorOrDie("status.phase!=Terminating") |
| 28 | + ) |
| 29 | + |
| 30 | + BeforeEach(func() { |
| 31 | + nsname := genName("csv-toggle-test-") |
| 32 | + og := operatorsv1.OperatorGroup{ |
| 33 | + ObjectMeta: metav1.ObjectMeta{ |
| 34 | + Name: fmt.Sprintf("%s-operatorgroup", nsname), |
| 35 | + Namespace: nsname, |
| 36 | + }, |
| 37 | + } |
| 38 | + ns = SetupGeneratedTestNamespaceWithOperatorGroup(nsname, og) |
| 39 | + |
| 40 | + csv = operatorsv1alpha1.ClusterServiceVersion{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: genName("csv-toggle-test-"), |
| 43 | + Namespace: nsname, |
| 44 | + }, |
| 45 | + Spec: operatorsv1alpha1.ClusterServiceVersionSpec{ |
| 46 | + InstallStrategy: newNginxInstallStrategy(genName("csv-toggle-test-"), nil, nil), |
| 47 | + InstallModes: []operatorsv1alpha1.InstallMode{ |
| 48 | + { |
| 49 | + Type: operatorsv1alpha1.InstallModeTypeAllNamespaces, |
| 50 | + Supported: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + err := ctx.Ctx().Client().Create(context.Background(), &csv) |
| 56 | + Expect(err).ShouldNot(HaveOccurred()) |
| 57 | + }) |
| 58 | + |
| 59 | + AfterEach(func() { |
| 60 | + Eventually(func() error { |
| 61 | + err := ctx.Ctx().Client().Delete(context.Background(), &csv) |
| 62 | + if err != nil && k8serrors.IsNotFound(err) { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | + }).Should(Succeed()) |
| 68 | + TeardownNamespace(ns.GetName()) |
| 69 | + }) |
| 70 | + |
| 71 | + When("an operator is installed in AllNamespace mode", func() { |
| 72 | + It("should have Copied CSVs in all other namespaces", func() { |
| 73 | + Eventually(func() error { |
| 74 | + requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()}) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList |
| 80 | + err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{ |
| 81 | + LabelSelector: k8slabels.NewSelector().Add(*requirement), |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + var namespaces corev1.NamespaceList |
| 88 | + if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{ |
| 89 | + FieldSelector: nonTerminatingNamespaceSelector, |
| 90 | + }); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + if len(namespaces.Items)-1 != len(copiedCSVs.Items) { |
| 95 | + return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + }).Should(Succeed()) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + When("Copied CSVs are disabled", func() { |
| 104 | + BeforeEach(func() { |
| 105 | + Eventually(func() error { |
| 106 | + var olmConfig operatorsv1.OLMConfig |
| 107 | + if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil { |
| 108 | + ctx.Ctx().Logf("Error getting olmConfig %v", err) |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + // Exit early if copied CSVs are disabled. |
| 113 | + if !olmConfig.CopiedCSVsAreEnabled() { |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + olmConfig.Spec = operatorsv1.OLMConfigSpec{ |
| 118 | + Features: &operatorsv1.Features{ |
| 119 | + DisableCopiedCSVs: getPointer(true), |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil { |
| 124 | + ctx.Ctx().Logf("Error setting olmConfig %v", err) |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | + }).Should(Succeed()) |
| 130 | + }) |
| 131 | + |
| 132 | + It("should not have any copied CSVs", func() { |
| 133 | + Eventually(func() error { |
| 134 | + requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()}) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList |
| 140 | + err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{ |
| 141 | + LabelSelector: k8slabels.NewSelector().Add(*requirement), |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + if numCSVs := len(copiedCSVs.Items); numCSVs != 0 { |
| 148 | + return fmt.Errorf("Found %d copied CSVs, should be 0", numCSVs) |
| 149 | + } |
| 150 | + return nil |
| 151 | + }).Should(Succeed()) |
| 152 | + }) |
| 153 | + |
| 154 | + It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() { |
| 155 | + Eventually(func() error { |
| 156 | + var olmConfig operatorsv1.OLMConfig |
| 157 | + if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType) |
| 162 | + if foundCondition == nil { |
| 163 | + return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType) |
| 164 | + } |
| 165 | + |
| 166 | + expectedCondition := metav1.Condition{ |
| 167 | + Reason: "NoCopiedCSVsFound", |
| 168 | + Message: "Copied CSVs are disabled and none were found for operators installed in AllNamespace mode", |
| 169 | + Status: metav1.ConditionTrue, |
| 170 | + } |
| 171 | + |
| 172 | + if foundCondition.Reason != expectedCondition.Reason || |
| 173 | + foundCondition.Message != expectedCondition.Message || |
| 174 | + foundCondition.Status != expectedCondition.Status { |
| 175 | + return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition) |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | + }).Should(Succeed()) |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + When("Copied CSVs are toggled back on", func() { |
| 184 | + BeforeEach(func() { |
| 185 | + Eventually(func() error { |
| 186 | + var olmConfig operatorsv1.OLMConfig |
| 187 | + if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + // Exit early if copied CSVs are enabled. |
| 192 | + if olmConfig.CopiedCSVsAreEnabled() { |
| 193 | + return nil |
| 194 | + } |
| 195 | + |
| 196 | + olmConfig.Spec = operatorsv1.OLMConfigSpec{ |
| 197 | + Features: &operatorsv1.Features{ |
| 198 | + DisableCopiedCSVs: getPointer(false), |
| 199 | + }, |
| 200 | + } |
| 201 | + |
| 202 | + if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + |
| 206 | + return nil |
| 207 | + }).Should(Succeed()) |
| 208 | + }) |
| 209 | + |
| 210 | + It("should have copied CSVs in all other Namespaces", func() { |
| 211 | + Eventually(func() error { |
| 212 | + // find copied csvs... |
| 213 | + requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()}) |
| 214 | + if err != nil { |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList |
| 219 | + err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{ |
| 220 | + LabelSelector: k8slabels.NewSelector().Add(*requirement), |
| 221 | + }) |
| 222 | + if err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + |
| 226 | + var namespaces corev1.NamespaceList |
| 227 | + if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{FieldSelector: nonTerminatingNamespaceSelector}); err != nil { |
| 228 | + return err |
| 229 | + } |
| 230 | + |
| 231 | + if len(namespaces.Items)-1 != len(copiedCSVs.Items) { |
| 232 | + return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1) |
| 233 | + } |
| 234 | + |
| 235 | + return nil |
| 236 | + }).Should(Succeed()) |
| 237 | + }) |
| 238 | + |
| 239 | + It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() { |
| 240 | + Eventually(func() error { |
| 241 | + var olmConfig operatorsv1.OLMConfig |
| 242 | + if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil { |
| 243 | + return err |
| 244 | + } |
| 245 | + foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType) |
| 246 | + if foundCondition == nil { |
| 247 | + return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType) |
| 248 | + } |
| 249 | + |
| 250 | + expectedCondition := metav1.Condition{ |
| 251 | + Reason: "CopiedCSVsEnabled", |
| 252 | + Message: "Copied CSVs are enabled and present across the cluster", |
| 253 | + Status: metav1.ConditionFalse, |
| 254 | + } |
| 255 | + |
| 256 | + if foundCondition.Reason != expectedCondition.Reason || |
| 257 | + foundCondition.Message != expectedCondition.Message || |
| 258 | + foundCondition.Status != expectedCondition.Status { |
| 259 | + return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition) |
| 260 | + } |
| 261 | + |
| 262 | + return nil |
| 263 | + }).Should(Succeed()) |
| 264 | + }) |
| 265 | + }) |
| 266 | +}) |
0 commit comments