Skip to content

Commit 7fc4074

Browse files
authored
Add integration test for the new cluster labelling logics. (#92)
1 parent b4e6d64 commit 7fc4074

File tree

14 files changed

+599
-140
lines changed

14 files changed

+599
-140
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ integration-test: $(GINKGO) $(ETCD)
103103
$(GINKGO) -v controllers/machine -- -enable-integration-tests -enable-unit-tests=false
104104
$(GINKGO) -v controllers/cluster -- -enable-integration-tests -enable-unit-tests=false
105105
$(GINKGO) -v controllers/configmap -- -enable-integration-tests -enable-unit-tests=false
106+
$(GINKGO) -v controllers/tests/cluster_for_akodeploymentconfig/default_adc -- -enable-integration-tests -enable-unit-tests=false -root-dir="../../../.."
107+
$(GINKGO) -v controllers/tests/cluster_for_akodeploymentconfig/default_adc_non_empty_selectors -- -enable-integration-tests -enable-unit-tests=false -root-dir="../../../.."
106108

107109
.PHONY: kind-e2e-test
108110
kind-e2e-test: $(KUSTOMIZE) $(KIND) $(KUBECTL) $(JQ) $(YTT)

controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ import (
99
. "github.com/onsi/ginkgo"
1010
. "github.com/onsi/gomega"
1111
"github.com/pkg/errors"
12-
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
13-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako"
14-
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
15-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
1612
"github.com/vmware/alb-sdk/go/models"
1713
"github.com/vmware/alb-sdk/go/session"
1814
akov1alpha1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1alpha1"
1915
corev1 "k8s.io/api/core/v1"
2016
apierrors "k8s.io/apimachinery/pkg/api/errors"
21-
"sigs.k8s.io/cluster-api/util/conditions"
22-
kcfg "sigs.k8s.io/cluster-api/util/kubeconfig"
23-
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
24-
2517
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2618
"k8s.io/utils/pointer"
2719
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
20+
"sigs.k8s.io/cluster-api/util/conditions"
21+
kcfg "sigs.k8s.io/cluster-api/util/kubeconfig"
2822
"sigs.k8s.io/controller-runtime/pkg/client"
23+
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
24+
25+
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
26+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako"
27+
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
28+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
29+
testutil "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/util"
2930
)
3031

3132
func intgTestAkoDeploymentConfigController() {
@@ -51,56 +52,9 @@ func intgTestAkoDeploymentConfigController() {
5152
userCreateCalled bool
5253
)
5354

54-
staticCluster = &clusterv1.Cluster{
55-
ObjectMeta: metav1.ObjectMeta{
56-
Name: "integration-test-8ed12g",
57-
Namespace: "integration-test-8ed12g",
58-
},
59-
Spec: clusterv1.ClusterSpec{},
60-
}
61-
62-
defaultAkoDeploymentConfigCommonSpec := akoov1alpha1.AKODeploymentConfigSpec{
63-
DataNetwork: akoov1alpha1.DataNetwork{
64-
Name: "integration-test-8ed12g",
65-
CIDR: "10.0.0.0/24",
66-
IPPools: []akoov1alpha1.IPPool{
67-
{
68-
Start: "10.0.0.1",
69-
End: "10.0.0.10",
70-
Type: "V4",
71-
},
72-
},
73-
},
74-
ControlPlaneNetwork: akoov1alpha1.ControlPlaneNetwork{
75-
Name: "integration-test-8ed12g",
76-
CIDR: "10.1.0.0/24",
77-
},
78-
ServiceEngineGroup: "ha-test",
79-
AdminCredentialRef: &akoov1alpha1.SecretRef{
80-
Name: "controller-credentials",
81-
Namespace: "default",
82-
},
83-
CertificateAuthorityRef: &akoov1alpha1.SecretRef{
84-
Name: "controller-ca",
85-
Namespace: "default",
86-
},
87-
}
88-
akoDeploymentConfigCommonSpec := defaultAkoDeploymentConfigCommonSpec.DeepCopy()
89-
akoDeploymentConfigCommonSpec.ClusterSelector = metav1.LabelSelector{
90-
MatchLabels: map[string]string{
91-
"test": "true",
92-
},
93-
}
94-
staticAkoDeploymentConfig = &akoov1alpha1.AKODeploymentConfig{
95-
ObjectMeta: metav1.ObjectMeta{
96-
Name: "ako-deployment-config",
97-
},
98-
Spec: *akoDeploymentConfigCommonSpec,
99-
}
100-
staticDefaultAkoDeploymentConfig = &akoov1alpha1.AKODeploymentConfig{
101-
ObjectMeta: metav1.ObjectMeta{Name: akoov1alpha1.WorkloadClusterAkoDeploymentConfig},
102-
Spec: defaultAkoDeploymentConfigCommonSpec,
103-
}
55+
staticCluster = &testutil.DefaultCluster
56+
staticAkoDeploymentConfig = testutil.GetCustomizedADC(testutil.CustomizedADCLabels)
57+
staticDefaultAkoDeploymentConfig = testutil.GetDefaultADC()
10458

10559
staticControllerCredentials = &corev1.Secret{
10660
ObjectMeta: metav1.ObjectMeta{

controllers/akodeploymentconfig/phases/phases_unit_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
package phases
55

66
import (
7-
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
8-
97
"github.com/go-logr/logr"
108
. "github.com/onsi/ginkgo"
119
. "github.com/onsi/gomega"
12-
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
13-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
1410
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1511
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1612
ctrl "sigs.k8s.io/controller-runtime"
13+
14+
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
15+
ako_operator "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/ako-operator"
16+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
1717
)
1818

1919
func ReconcilePhaseUnitTest() {

controllers/akodeploymentconfig/suite_test.go

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,16 @@ import (
88
"testing"
99

1010
. "github.com/onsi/ginkgo"
11-
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
12-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/controllers/akodeploymentconfig"
13-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/aviclient"
11+
1412
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
13+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/funcs"
1514
testutil "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/util"
16-
akov1alpha1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/apis/ako/v1alpha1"
17-
corev1 "k8s.io/api/core/v1"
18-
"k8s.io/apimachinery/pkg/runtime"
19-
20-
networkv1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
21-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
22-
ctrl "sigs.k8s.io/controller-runtime"
23-
24-
adccluster "github.com/vmware-samples/load-balancer-operator-for-kubernetes/controllers/akodeploymentconfig/cluster"
25-
cluster "github.com/vmware-samples/load-balancer-operator-for-kubernetes/controllers/cluster"
26-
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
2715
)
2816

2917
// suite is used for unit and integration testing this controller.
3018
var suite = builder.NewTestSuiteForController(
31-
func(mgr ctrlmgr.Manager) error {
32-
rec := &akodeploymentconfig.AKODeploymentConfigReconciler{
33-
Client: mgr.GetClient(),
34-
Log: ctrl.Log.WithName("controllers").WithName("AKODeploymentConfig"),
35-
Scheme: mgr.GetScheme(),
36-
}
37-
builder.FakeAvi = aviclient.NewFakeAviClient()
38-
rec.SetAviClient(builder.FakeAvi)
39-
40-
adcClusterReconciler := adccluster.NewReconciler(rec.Client, rec.Log, rec.Scheme)
41-
adcClusterReconciler.GetRemoteClient = adccluster.GetFakeRemoteClient
42-
rec.ClusterReconciler = adcClusterReconciler
43-
44-
if err := rec.SetupWithManager(mgr); err != nil {
45-
return err
46-
}
47-
48-
// involve the cluster controller as well for the resetting skip-default-adc label test
49-
if err := (&cluster.ClusterReconciler{
50-
Client: mgr.GetClient(),
51-
Log: ctrl.Log.WithName("controllers").WithName("Cluster"),
52-
Scheme: mgr.GetScheme(),
53-
}).SetupWithManager(mgr); err != nil {
54-
return err
55-
}
56-
57-
return nil
58-
},
59-
func(scheme *runtime.Scheme) (err error) {
60-
err = networkv1alpha1.AddToScheme(scheme)
61-
if err != nil {
62-
return err
63-
}
64-
err = akoov1alpha1.AddToScheme(scheme)
65-
if err != nil {
66-
return err
67-
}
68-
err = akov1alpha1.AddToScheme(scheme)
69-
if err != nil {
70-
return err
71-
}
72-
err = corev1.AddToScheme(scheme)
73-
if err != nil {
74-
return err
75-
}
76-
err = clusterv1.AddToScheme(scheme)
77-
if err != nil {
78-
return err
79-
}
80-
return nil
81-
},
19+
funcs.AddAKODeploymentConfigAndClusterControllerToMgrFunc,
20+
funcs.AddAllToSchemeFunc,
8221
filepath.Join(testutil.FindModuleDir("sigs.k8s.io/cluster-api"), "config", "crd", "bases"),
8322
filepath.Join(testutil.FindModuleDir("github.com/vmware/load-balancer-and-ingress-services-for-kubernetes"), "helm", "ako", "crds"),
8423
)

controllers/configmap/suite_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ package configmap_test
55

66
import (
77
"context"
8+
"testing"
9+
810
. "github.com/onsi/ginkgo"
9-
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
10-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/controllers/configmap"
11-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/aviclient"
12-
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
13-
testutil "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/util"
11+
corev1 "k8s.io/api/core/v1"
1412
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1513
"k8s.io/apimachinery/pkg/runtime"
1614
"path/filepath"
1715
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1816
ctrl "sigs.k8s.io/controller-runtime"
1917
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
20-
"testing"
2118

22-
corev1 "k8s.io/api/core/v1"
19+
akoov1alpha1 "github.com/vmware-samples/load-balancer-operator-for-kubernetes/api/v1alpha1"
20+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/controllers/configmap"
21+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/aviclient"
22+
"github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/builder"
23+
testutil "github.com/vmware-samples/load-balancer-operator-for-kubernetes/pkg/test/util"
2324
)
2425

2526
// suite is used for unit and integration testing this controller.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)