From 316eb5a02c039b856eb96c8435b04304972c69c0 Mon Sep 17 00:00:00 2001 From: Edwin Xie Date: Thu, 7 Dec 2023 00:01:18 +0000 Subject: [PATCH 1/2] Ensure GetControlPlaneEndpoint can still return an IP Even if the cluster-controlplane-endpoint annotation or ClusterClass topology variable for apiServerEndpoint are not set, it can still retrieve the control plane endpoint from the cluster.Spec.ControlPlaneEndpoint.Host. Co-authored-by: Aidan Obley --- pkg/ako-operator/lib.go | 4 ++++ pkg/ako-operator/lib_test.go | 10 ++++++++++ pkg/haprovider/haprovider_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/pkg/ako-operator/lib.go b/pkg/ako-operator/lib.go index 64cf57ce..06b18498 100644 --- a/pkg/ako-operator/lib.go +++ b/pkg/ako-operator/lib.go @@ -110,6 +110,10 @@ func GetControlPlaneEndpoint(cluster *clusterv1.Cluster) (string, error) { } } } + if apiServerEndpoint == "" { + apiServerEndpoint = cluster.Spec.ControlPlaneEndpoint.Host + } + return apiServerEndpoint, nil } diff --git a/pkg/ako-operator/lib_test.go b/pkg/ako-operator/lib_test.go index 8d7f0145..6ea069e4 100644 --- a/pkg/ako-operator/lib_test.go +++ b/pkg/ako-operator/lib_test.go @@ -353,6 +353,16 @@ var _ = Describe("AKO Operator lib unit test", func() { Expect(endpoint).Should(Equal("")) Expect(err).ShouldNot(HaveOccurred()) }) + When("ControlPlaneEndpoint.Hosts is set", func() { + BeforeEach(func() { + cluster.Spec.ControlPlaneEndpoint.Host = "10.1.10.1" + }) + It("returns that value for the endpoint", func() { + endpoint, err := GetControlPlaneEndpoint(cluster) + Expect(endpoint).Should(Equal("10.1.10.1")) + Expect(err).ShouldNot(HaveOccurred()) + }) + }) }) When("invalid input ", func() { var cluster *clusterv1.Cluster diff --git a/pkg/haprovider/haprovider_test.go b/pkg/haprovider/haprovider_test.go index 47d4f73c..9656f833 100644 --- a/pkg/haprovider/haprovider_test.go +++ b/pkg/haprovider/haprovider_test.go @@ -172,6 +172,31 @@ var _ = Describe("Control Plane HA provider", func() { }) }) + When("cluster has ControlPlaneEndpoint.Host set", func() { + BeforeEach(func() { + cluster = &clusterv1.Cluster{ + ObjectMeta: v1.ObjectMeta{ + Name: "test-cluster", + Namespace: "default", + Annotations: map[string]string{"tkg.tanzu.vmware.com/cluster-controlplane-endpoint": "2.2.2.2"}, + }, + Spec: clusterv1.ClusterSpec{ + ControlPlaneEndpoint: clusterv1.APIEndpoint{ + Host: "2.2.2.2", + Port: 6443, + }, + }, + } + }) + It("should create service successfully", func() { + svc, err = haProvider.createService(ctx, cluster) + Expect(err).ShouldNot(HaveOccurred()) + Expect(svc.Spec.LoadBalancerIP).Should(Equal("2.2.2.2")) + Expect(svc.Annotations[akoov1alpha1.AkoPreferredIPAnnotation]).Should(Equal("2.2.2.2")) + Expect(haProvider.Client.Delete(ctx, svc)).ShouldNot(HaveOccurred()) + }) + }) + When("cluster has valid FQDN control plane endpoint", func() { BeforeEach(func() { cluster = &clusterv1.Cluster{ From 7f72fde258ef6eebb6e92b955e07144e8f83cf4b Mon Sep 17 00:00:00 2001 From: Edwin Xie Date: Thu, 7 Dec 2023 23:57:24 +0000 Subject: [PATCH 2/2] Fix integration test flakes - Modifying the cluster object directly in Golang also somehow modifies it on the fake API side --- .../akodeploymentconfig_controller_intg_test.go | 2 -- .../machine/machine_controller_intg_test.go | 16 +++++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go b/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go index 94f3f0e2..c92793f1 100644 --- a/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go +++ b/controllers/akodeploymentconfig/akodeploymentconfig_controller_intg_test.go @@ -309,7 +309,6 @@ func intgTestAkoDeploymentConfigController() { latestCluster := &clusterv1.Cluster{} if err := getCluster(latestCluster, cluster.Name, cluster.Namespace); err == nil { latestCluster.Finalizers = nil - updateObjects(latestCluster) deleteObjects(latestCluster) ensureRuntimeObjectMatchExpectation(client.ObjectKey{ Name: cluster.Name, @@ -378,7 +377,6 @@ func intgTestAkoDeploymentConfigController() { latestCluster := &clusterv1.Cluster{} if err := getCluster(latestCluster, cluster.Name, cluster.Namespace); err == nil { latestCluster.Finalizers = nil - updateObjects(latestCluster) deleteObjects(latestCluster) ensureRuntimeObjectMatchExpectation(client.ObjectKey{ Name: cluster.Name, diff --git a/controllers/machine/machine_controller_intg_test.go b/controllers/machine/machine_controller_intg_test.go index e3979e3f..eb70a0e4 100644 --- a/controllers/machine/machine_controller_intg_test.go +++ b/controllers/machine/machine_controller_intg_test.go @@ -93,13 +93,15 @@ func intgTestMachineController() { }) It("Corresponding Endpoints should be created", func() { ep := &corev1.Endpoints{} - Eventually(func() bool { - err := ctx.Client.Get(ctx.Context, client.ObjectKey{Name: cluster.Namespace + "-" + cluster.Name + "-control-plane", Namespace: cluster.Namespace}, ep) - return err == nil - }).Should(BeTrue()) - Expect(ep.Subsets).ShouldNot(BeNil()) - Expect(ep.Subsets[0].Addresses).ShouldNot(BeNil()) - Expect(len(ep.Subsets[0].Addresses)).Should(Equal(1)) + Eventually(func() error { + return ctx.Client.Get(ctx.Context, client.ObjectKey{Name: cluster.Namespace + "-" + cluster.Name + "-control-plane", Namespace: cluster.Namespace}, ep) + }).Should(Succeed()) + Eventually(func() int { + if len(ep.Subsets) == 0 { + return 0 + } + return len(ep.Subsets[0].Addresses) + }).Should(Equal(1)) Expect(ep.Subsets[0].Addresses[0].IP).Should(Equal("1.1.1.1")) }) It("Should add one more machine", func() {