Skip to content

Commit 416bed6

Browse files
authored
User defined port for the control plane (#854)
1 parent 17ba756 commit 416bed6

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

cloud/scope/cluster.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func (s *ClusterScope) CreateLoadBalancer() (*vpcv1.LoadBalancer, error) {
421421
options.SetListeners([]vpcv1.LoadBalancerListenerPrototypeLoadBalancerContext{
422422
{
423423
Protocol: core.StringPtr("tcp"),
424-
Port: core.Int64Ptr(6443),
424+
Port: core.Int64Ptr(int64(s.APIServerPort())),
425425
DefaultPool: &vpcv1.LoadBalancerPoolIdentityByName{
426426
Name: core.StringPtr(s.IBMVPCCluster.Spec.ControlPlaneLoadBalancer.Name + "-pool"),
427427
},
@@ -524,3 +524,11 @@ func (s *ClusterScope) PatchObject() error {
524524
func (s *ClusterScope) Close() error {
525525
return s.PatchObject()
526526
}
527+
528+
// APIServerPort returns the APIServerPort to use when creating the ControlPlaneEndpoint.
529+
func (s *ClusterScope) APIServerPort() int32 {
530+
if s.Cluster.Spec.ClusterNetwork != nil && s.Cluster.Spec.ClusterNetwork.APIServerPort != nil {
531+
return *s.Cluster.Spec.ClusterNetwork.APIServerPort
532+
}
533+
return 6443
534+
}

controllers/ibmvpccluster_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (r *IBMVPCClusterReconciler) reconcile(clusterScope *scope.ClusterScope) (c
129129
if fip != nil {
130130
clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint = capiv1beta1.APIEndpoint{
131131
Host: *fip.Address,
132-
Port: 6443,
132+
Port: clusterScope.APIServerPort(),
133133
}
134134

135135
clusterScope.IBMVPCCluster.Status.VPCEndpoint = infrav1beta1.VPCEndpoint{
@@ -169,8 +169,7 @@ func (r *IBMVPCClusterReconciler) reconcile(clusterScope *scope.ClusterScope) (c
169169

170170
clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint = capiv1beta1.APIEndpoint{
171171
Host: *loadBalancer.Hostname,
172-
// TODO: Support usage of user supplied port.
173-
Port: 6443,
172+
Port: clusterScope.APIServerPort(),
174173
}
175174

176175
clusterScope.IBMVPCCluster.Status.VPCEndpoint = infrav1beta1.VPCEndpoint{

controllers/ibmvpccluster_controller_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func TestIBMVPCClusterReconciler_reconcile(t *testing.T) {
150150
}
151151
clusterScope = &scope.ClusterScope{
152152
IBMVPCClient: mockvpc,
153+
Cluster: &capiv1beta1.Cluster{},
153154
Logger: klogr.New(),
154155
IBMVPCCluster: &infrav1beta1.IBMVPCCluster{
155156
ObjectMeta: metav1.ObjectMeta{
@@ -250,6 +251,36 @@ func TestIBMVPCClusterReconciler_reconcile(t *testing.T) {
250251
g.Expect(clusterScope.IBMVPCCluster.Finalizers).To(ContainElement(infrav1beta1.ClusterFinalizer))
251252
g.Expect(clusterScope.IBMVPCCluster.Status.Ready).To(Equal(true))
252253
})
254+
t.Run("Should use the user supplied port for the apiserver", func(t *testing.T) {
255+
g := NewWithT(t)
256+
setup(t)
257+
t.Cleanup(teardown)
258+
clusterScope.IBMVPCCluster.Finalizers = []string{infrav1beta1.ClusterFinalizer}
259+
port := int32(412)
260+
clusterScope.Cluster.Spec.ClusterNetwork = &capiv1beta1.ClusterNetwork{APIServerPort: &port}
261+
mockvpc.EXPECT().ListVpcs(listVpcsOptions).Return(vpclist, response, nil)
262+
mockvpc.EXPECT().ListFloatingIps(listFloatingIpsOptions).Return(fips, response, nil)
263+
mockvpc.EXPECT().ListSubnets(options).Return(subnets, response, nil)
264+
_, err := reconciler.reconcile(clusterScope)
265+
g.Expect(err).To(BeNil())
266+
g.Expect(clusterScope.IBMVPCCluster.Finalizers).To(ContainElement(infrav1beta1.ClusterFinalizer))
267+
g.Expect(clusterScope.IBMVPCCluster.Status.Ready).To(Equal(true))
268+
g.Expect(clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint.Port).To(Equal(port))
269+
})
270+
t.Run("Should use the default port for the apiserver if not specified", func(t *testing.T) {
271+
g := NewWithT(t)
272+
setup(t)
273+
t.Cleanup(teardown)
274+
clusterScope.IBMVPCCluster.Finalizers = []string{infrav1beta1.ClusterFinalizer}
275+
mockvpc.EXPECT().ListVpcs(listVpcsOptions).Return(vpclist, response, nil)
276+
mockvpc.EXPECT().ListFloatingIps(listFloatingIpsOptions).Return(fips, response, nil)
277+
mockvpc.EXPECT().ListSubnets(options).Return(subnets, response, nil)
278+
_, err := reconciler.reconcile(clusterScope)
279+
g.Expect(err).To(BeNil())
280+
g.Expect(clusterScope.IBMVPCCluster.Finalizers).To(ContainElement(infrav1beta1.ClusterFinalizer))
281+
g.Expect(clusterScope.IBMVPCCluster.Status.Ready).To(Equal(true))
282+
g.Expect(clusterScope.IBMVPCCluster.Spec.ControlPlaneEndpoint.Port).To(Equal(int32(6443)))
283+
})
253284
})
254285
}
255286

0 commit comments

Comments
 (0)