diff --git a/cloud/scope/cluster.go b/cloud/scope/cluster.go index d17cd9186..e7092a274 100644 --- a/cloud/scope/cluster.go +++ b/cloud/scope/cluster.go @@ -421,7 +421,7 @@ func (s *ClusterScope) CreateLoadBalancer() (*vpcv1.LoadBalancer, error) { options.SetListeners([]vpcv1.LoadBalancerListenerPrototypeLoadBalancerContext{ { Protocol: core.StringPtr("tcp"), - Port: core.Int64Ptr(6443), + Port: core.Int64Ptr(int64(s.APIServerPort())), DefaultPool: &vpcv1.LoadBalancerPoolIdentityByName{ Name: core.StringPtr(s.IBMVPCCluster.Spec.ControlPlaneLoadBalancer.Name + "-pool"), }, @@ -524,3 +524,11 @@ func (s *ClusterScope) PatchObject() error { func (s *ClusterScope) Close() error { return s.PatchObject() } + +// APIServerPort returns the APIServerPort to use when creating the ControlPlaneEndpoint. +func (s *ClusterScope) APIServerPort() int32 { + if s.Cluster.Spec.ClusterNetwork != nil && s.Cluster.Spec.ClusterNetwork.APIServerPort != nil { + return *s.Cluster.Spec.ClusterNetwork.APIServerPort + } + return 6443 +} diff --git a/controllers/ibmvpccluster_controller.go b/controllers/ibmvpccluster_controller.go index c837f2aa0..f222fa0cf 100644 --- a/controllers/ibmvpccluster_controller.go +++ b/controllers/ibmvpccluster_controller.go @@ -129,7 +129,7 @@ func (r *IBMVPCClusterReconciler) reconcile(clusterScope *scope.ClusterScope) (c if fip != nil { clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint = capiv1beta1.APIEndpoint{ Host: *fip.Address, - Port: 6443, + Port: clusterScope.APIServerPort(), } clusterScope.IBMVPCCluster.Status.VPCEndpoint = infrav1beta1.VPCEndpoint{ @@ -169,8 +169,7 @@ func (r *IBMVPCClusterReconciler) reconcile(clusterScope *scope.ClusterScope) (c clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint = capiv1beta1.APIEndpoint{ Host: *loadBalancer.Hostname, - // TODO: Support usage of user supplied port. - Port: 6443, + Port: clusterScope.APIServerPort(), } clusterScope.IBMVPCCluster.Status.VPCEndpoint = infrav1beta1.VPCEndpoint{ diff --git a/controllers/ibmvpccluster_controller_test.go b/controllers/ibmvpccluster_controller_test.go index 6ac5ed2f4..b00266292 100644 --- a/controllers/ibmvpccluster_controller_test.go +++ b/controllers/ibmvpccluster_controller_test.go @@ -150,6 +150,7 @@ func TestIBMVPCClusterReconciler_reconcile(t *testing.T) { } clusterScope = &scope.ClusterScope{ IBMVPCClient: mockvpc, + Cluster: &capiv1beta1.Cluster{}, Logger: klogr.New(), IBMVPCCluster: &infrav1beta1.IBMVPCCluster{ ObjectMeta: metav1.ObjectMeta{ @@ -250,6 +251,36 @@ func TestIBMVPCClusterReconciler_reconcile(t *testing.T) { g.Expect(clusterScope.IBMVPCCluster.Finalizers).To(ContainElement(infrav1beta1.ClusterFinalizer)) g.Expect(clusterScope.IBMVPCCluster.Status.Ready).To(Equal(true)) }) + t.Run("Should use the user supplied port for the apiserver", func(t *testing.T) { + g := NewWithT(t) + setup(t) + t.Cleanup(teardown) + clusterScope.IBMVPCCluster.Finalizers = []string{infrav1beta1.ClusterFinalizer} + port := int32(412) + clusterScope.Cluster.Spec.ClusterNetwork = &capiv1beta1.ClusterNetwork{APIServerPort: &port} + mockvpc.EXPECT().ListVpcs(listVpcsOptions).Return(vpclist, response, nil) + mockvpc.EXPECT().ListFloatingIps(listFloatingIpsOptions).Return(fips, response, nil) + mockvpc.EXPECT().ListSubnets(options).Return(subnets, response, nil) + _, err := reconciler.reconcile(clusterScope) + g.Expect(err).To(BeNil()) + g.Expect(clusterScope.IBMVPCCluster.Finalizers).To(ContainElement(infrav1beta1.ClusterFinalizer)) + g.Expect(clusterScope.IBMVPCCluster.Status.Ready).To(Equal(true)) + g.Expect(clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint.Port).To(Equal(port)) + }) + t.Run("Should use the default port for the apiserver if not specified", func(t *testing.T) { + g := NewWithT(t) + setup(t) + t.Cleanup(teardown) + clusterScope.IBMVPCCluster.Finalizers = []string{infrav1beta1.ClusterFinalizer} + mockvpc.EXPECT().ListVpcs(listVpcsOptions).Return(vpclist, response, nil) + mockvpc.EXPECT().ListFloatingIps(listFloatingIpsOptions).Return(fips, response, nil) + mockvpc.EXPECT().ListSubnets(options).Return(subnets, response, nil) + _, err := reconciler.reconcile(clusterScope) + g.Expect(err).To(BeNil()) + g.Expect(clusterScope.IBMVPCCluster.Finalizers).To(ContainElement(infrav1beta1.ClusterFinalizer)) + g.Expect(clusterScope.IBMVPCCluster.Status.Ready).To(Equal(true)) + g.Expect(clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint.Port).To(Equal(int32(6443))) + }) }) }