|
| 1 | +// Copyright 2022 VMware, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package default_adc_test |
| 5 | + |
| 6 | +import ( |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + v1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + |
| 14 | + akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1" |
| 15 | + "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder" |
| 16 | + testutil "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/util" |
| 17 | +) |
| 18 | + |
| 19 | +func intgTestClusterDisableAVIWithoutAnyADC() { |
| 20 | + var ( |
| 21 | + ctx *builder.IntegrationTestContext |
| 22 | + staticCluster *clusterv1.Cluster |
| 23 | + ) |
| 24 | + |
| 25 | + BeforeEach(func() { |
| 26 | + ctx = suite.NewIntegrationTestContext() |
| 27 | + staticCluster = testutil.GetDefaultCluster() |
| 28 | + }) |
| 29 | + |
| 30 | + When("there is no ADC and a cluster is created", func() { |
| 31 | + BeforeEach(func() { |
| 32 | + testutil.CreateObjects(ctx, staticCluster.DeepCopy()) |
| 33 | + }) |
| 34 | + AfterEach(func() { |
| 35 | + testutil.DeleteObjects(ctx, staticCluster.DeepCopy()) |
| 36 | + testutil.EnsureRuntimeObjectMatchExpectation(ctx, client.ObjectKey{ |
| 37 | + Name: staticCluster.Name, |
| 38 | + Namespace: staticCluster.Namespace, |
| 39 | + }, &clusterv1.Cluster{}, testutil.NOTFOUND) |
| 40 | + }) |
| 41 | + It("shouldn't have 'networking.tkg.tanzu.vmware.com/avi'", func() { |
| 42 | + testutil.EnsureClusterAviLabelExists(ctx, client.ObjectKey{ |
| 43 | + Name: staticCluster.Name, |
| 44 | + Namespace: staticCluster.Namespace, |
| 45 | + }, akoov1alpha1.AviClusterLabel, false) |
| 46 | + }) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func intgTestClusterCanBeSelectedByADC() { |
| 51 | + var ( |
| 52 | + ctx *builder.IntegrationTestContext |
| 53 | + |
| 54 | + staticCluster *clusterv1.Cluster |
| 55 | + staticAkoDeploymentConfig *akoov1alpha1.AKODeploymentConfig |
| 56 | + staticDefaultAkoDeploymentConfig *akoov1alpha1.AKODeploymentConfig |
| 57 | + |
| 58 | + staticManagementNamespace *v1.Namespace |
| 59 | + staticManagementCluster *clusterv1.Cluster |
| 60 | + staticManagementAkoDeploymentConfig *akoov1alpha1.AKODeploymentConfig |
| 61 | + ) |
| 62 | + |
| 63 | + BeforeEach(func() { |
| 64 | + ctx = suite.NewIntegrationTestContext() |
| 65 | + staticCluster = testutil.GetDefaultCluster() |
| 66 | + staticAkoDeploymentConfig = testutil.GetCustomizedADC(testutil.CustomizedADCLabels) |
| 67 | + staticDefaultAkoDeploymentConfig = testutil.GetDefaultADC() |
| 68 | + |
| 69 | + staticManagementNamespace = &v1.Namespace{ |
| 70 | + ObjectMeta: metav1.ObjectMeta{Name: "tkg-system"}, |
| 71 | + } |
| 72 | + staticManagementCluster = testutil.GetManagementCluster() |
| 73 | + staticManagementAkoDeploymentConfig = testutil.GetManagementADC() |
| 74 | + }) |
| 75 | + |
| 76 | + When("both default and customized ADC exist", func() { |
| 77 | + |
| 78 | + BeforeEach(func() { |
| 79 | + testutil.CreateObjects(ctx, staticAkoDeploymentConfig.DeepCopy()) |
| 80 | + testutil.CreateObjects(ctx, staticDefaultAkoDeploymentConfig.DeepCopy()) |
| 81 | + testutil.CreateObjects(ctx, staticCluster.DeepCopy()) |
| 82 | + }) |
| 83 | + |
| 84 | + It("labels the cluster dynamically", func() { |
| 85 | + By("labels with 'networking.tkg.tanzu.vmware.com/avi: install-ako-for-all'", func() { |
| 86 | + testutil.EnsureClusterAviLabelMatchExpectation(ctx, client.ObjectKey{ |
| 87 | + Name: staticCluster.Name, |
| 88 | + Namespace: staticCluster.Namespace, |
| 89 | + }, akoov1alpha1.AviClusterLabel, staticDefaultAkoDeploymentConfig.Name) |
| 90 | + }) |
| 91 | + |
| 92 | + By("add cluster label to use customized adc") |
| 93 | + testutil.UpdateObjectLabels(ctx, client.ObjectKey{ |
| 94 | + Name: staticCluster.Name, |
| 95 | + Namespace: staticCluster.Namespace, |
| 96 | + }, testutil.CustomizedADCLabels) |
| 97 | + |
| 98 | + By("labels with 'networking.tkg.tanzu.vmware.com/avi: ako-deployment-config'", func() { |
| 99 | + testutil.EnsureClusterAviLabelMatchExpectation(ctx, client.ObjectKey{ |
| 100 | + Name: staticCluster.Name, |
| 101 | + Namespace: staticCluster.Namespace, |
| 102 | + }, akoov1alpha1.AviClusterLabel, staticAkoDeploymentConfig.Name) |
| 103 | + }) |
| 104 | + |
| 105 | + By("create another customized ako-deployment-config2") |
| 106 | + anotherAkoDeploymentConfig := staticAkoDeploymentConfig.DeepCopy() |
| 107 | + anotherAkoDeploymentConfig.Name = "ako-deployment-config-2" |
| 108 | + testutil.CreateObjects(ctx, anotherAkoDeploymentConfig.DeepCopy()) |
| 109 | + |
| 110 | + By("cluster should keep its label, even through another custom ADC matches the name. a.k.a it won't override", func() { |
| 111 | + Consistently(func() bool { |
| 112 | + obj := &clusterv1.Cluster{} |
| 113 | + err := ctx.Client.Get(ctx.Context, client.ObjectKey{ |
| 114 | + Name: staticCluster.Name, |
| 115 | + Namespace: staticCluster.Namespace, |
| 116 | + }, obj) |
| 117 | + if err != nil { |
| 118 | + return false |
| 119 | + } |
| 120 | + val, ok := obj.Labels[akoov1alpha1.AviClusterLabel] |
| 121 | + return ok && val == staticAkoDeploymentConfig.Name |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + By("unset cluster label to use default adc") |
| 126 | + testutil.UpdateObjectLabels(ctx, client.ObjectKey{ |
| 127 | + Name: staticCluster.Name, |
| 128 | + Namespace: staticCluster.Namespace, |
| 129 | + }, map[string]string{}) |
| 130 | + |
| 131 | + By("labels with 'networking.tkg.tanzu.vmware.com/avi: install-ako-for-all'", func() { |
| 132 | + testutil.EnsureClusterAviLabelMatchExpectation(ctx, client.ObjectKey{ |
| 133 | + Name: staticCluster.Name, |
| 134 | + Namespace: staticCluster.Namespace, |
| 135 | + }, akoov1alpha1.AviClusterLabel, staticDefaultAkoDeploymentConfig.Name) |
| 136 | + }) |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + When("management ADC exists", func() { |
| 141 | + BeforeEach(func() { |
| 142 | + testutil.CreateObjects(ctx, staticManagementNamespace.DeepCopy()) |
| 143 | + testutil.CreateObjects(ctx, staticManagementAkoDeploymentConfig.DeepCopy()) |
| 144 | + testutil.CreateObjects(ctx, staticManagementCluster.DeepCopy()) |
| 145 | + }) |
| 146 | + |
| 147 | + It("labels the management cluster", func() { |
| 148 | + By("labels with 'networking.tkg.tanzu.vmware.com/avi: install-ako-for-management-cluster'", func() { |
| 149 | + testutil.EnsureClusterAviLabelMatchExpectation(ctx, client.ObjectKey{ |
| 150 | + Name: staticManagementCluster.Name, |
| 151 | + Namespace: staticManagementCluster.Namespace, |
| 152 | + }, akoov1alpha1.AviClusterLabel, staticManagementAkoDeploymentConfig.Name) |
| 153 | + }) |
| 154 | + }) |
| 155 | + }) |
| 156 | +} |
0 commit comments