Skip to content

Commit 550478a

Browse files
authored
🌱 supervisor: create the VirtualMachineService for the control plane without a name suffix (#3415)
* supervisor: create the VirtualMachineService for the control plane without a name suffix This allows creating clusters on supervisor having a name > 41 characters. Before this change the VirtualMachineService was exceeding the name length limit of 63 and thus never got created. The code falls back to the legacy name if there is no VirtualMachineService found. * add test coverage * fix
1 parent 1cd14f2 commit 550478a

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

‎pkg/services/vmoperator/control_plane_endpoint.go‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,14 @@ func (s *CPService) ReconcileControlPlaneEndpointService(ctx context.Context, cl
154154
return cpEndpoint, nil
155155
}
156156

157-
func controlPlaneVMServiceName(ctx *vmware.ClusterContext) string {
158-
return fmt.Sprintf("%s-control-plane-service", ctx.Cluster.Name)
157+
func controlPlaneVMServiceName(clusterName string) string {
158+
return clusterName
159+
}
160+
161+
// legacyControlPlaneVMServiceName was used for creating the ControlPlane VirtualMachineService prior to
162+
// v1.13.0. It resulted in limiting the name of a Cluster to 41 characters (besides other places).
163+
func legacyControlPlaneVMServiceName(clusterName string) string {
164+
return fmt.Sprintf("%s-control-plane-service", clusterName)
159165
}
160166

161167
// ClusterRoleVMLabels returns labels applied to a VirtualMachine in the cluster. The Control Plane
@@ -179,7 +185,7 @@ func clusterRoleVMLabels(ctx *vmware.ClusterContext, controlPlane bool) map[stri
179185
func newVirtualMachineService(ctx *vmware.ClusterContext) *vmoprv1.VirtualMachineService {
180186
return &vmoprv1.VirtualMachineService{
181187
ObjectMeta: metav1.ObjectMeta{
182-
Name: controlPlaneVMServiceName(ctx),
188+
Name: controlPlaneVMServiceName(ctx.Cluster.Name),
183189
Namespace: ctx.Cluster.Namespace,
184190
},
185191
TypeMeta: metav1.TypeMeta{
@@ -247,15 +253,26 @@ func (s *CPService) getVMControlPlaneService(ctx context.Context, clusterCtx *vm
247253
vmService := &vmoprv1.VirtualMachineService{}
248254
vmServiceKey := client.ObjectKey{
249255
Namespace: clusterCtx.Cluster.Namespace,
250-
Name: controlPlaneVMServiceName(clusterCtx),
256+
Name: controlPlaneVMServiceName(clusterCtx.Cluster.Name),
251257
}
252258
if err := s.Client.Get(ctx, vmServiceKey, vmService); err != nil {
253259
if !apierrors.IsNotFound(err) {
254260
return nil, fmt.Errorf("failed to get VirtualMachineService %s: %v", vmServiceKey.Name, err)
255261
}
256262

257-
log.Info("VirtualMachineService was not found", "VirtualMachineService", klog.KRef(vmServiceKey.Namespace, vmServiceKey.Name))
258-
return nil, err
263+
// In case of not finding the ControlPlane VirtualMachineService: fallback to try the legacy name.
264+
fallbackVMServiceKey := client.ObjectKey{
265+
Namespace: vmServiceKey.Namespace,
266+
Name: legacyControlPlaneVMServiceName(clusterCtx.Cluster.Name),
267+
}
268+
if fallbackErr := s.Client.Get(ctx, fallbackVMServiceKey, vmService); fallbackErr != nil {
269+
if !apierrors.IsNotFound(fallbackErr) {
270+
return nil, fmt.Errorf("failed to get VirtualMachineService %s: %v", fallbackVMServiceKey.Name, fallbackErr)
271+
}
272+
273+
log.Info("VirtualMachineService was not found", "VirtualMachineService", klog.KRef(vmServiceKey.Namespace, vmServiceKey.Name))
274+
return nil, err
275+
}
259276
}
260277

261278
return vmService, nil

‎pkg/services/vmoperator/control_plane_endpoint_test.go‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
corev1 "k8s.io/api/core/v1"
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30-
"k8s.io/apimachinery/pkg/types"
3130
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3231
"sigs.k8s.io/cluster-api/util/conditions"
3332
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -39,17 +38,12 @@ import (
3938
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
4039
)
4140

42-
func getVirtualMachineService(ctx context.Context, clusterCtx *vmware.ClusterContext, c ctrlclient.Client, _ CPService) *vmoprv1.VirtualMachineService {
43-
vms := newVirtualMachineService(clusterCtx)
44-
nsname := types.NamespacedName{
45-
Namespace: vms.Namespace,
46-
Name: vms.Name,
47-
}
48-
err := c.Get(ctx, nsname, vms)
41+
func getVirtualMachineService(ctx context.Context, clusterCtx *vmware.ClusterContext, _ ctrlclient.Client, cpService CPService) *vmoprv1.VirtualMachineService {
42+
vms, err := cpService.getVMControlPlaneService(ctx, clusterCtx)
4943
if apierrors.IsNotFound(err) {
5044
return nil
5145
}
52-
Expect(err).ShouldNot(HaveOccurred())
46+
Expect(err).ToNot(HaveOccurred())
5347
return vms
5448
}
5549

@@ -184,10 +178,33 @@ var _ = Describe("ControlPlaneEndpoint Tests", func() {
184178

185179
Specify("DummyLBNetworkProvider has a LoadBalancer", func() {
186180
expectReconcileError = true // VirtualMachineService LB does not yet have VIP assigned
181+
expectAPIEndpoint = false
187182
expectVMS = true
188183
expectedType = vmoprv1.VirtualMachineServiceTypeLoadBalancer
189184
apiEndpoint, err = cpService.ReconcileControlPlaneEndpointService(ctx, clusterCtx, network.DummyLBNetworkProvider())
190185
verifyOutput()
186+
187+
// Set a VIP and reconcile again.
188+
expectReconcileError = false
189+
expectAPIEndpoint = true
190+
updateVMServiceWithVIP(ctx, clusterCtx, c, cpService, vip)
191+
expectedPort = defaultAPIBindPort
192+
expectedHost = vip
193+
apiEndpoint, err = cpService.ReconcileControlPlaneEndpointService(ctx, clusterCtx, network.DummyLBNetworkProvider())
194+
verifyOutput()
195+
vmService := getVirtualMachineService(ctx, clusterCtx, c, cpService)
196+
Expect(vmService.Name).To(Equal(controlPlaneVMServiceName(clusterCtx.Cluster.Name)))
197+
198+
// Delete the apiservice and recreate using the legacy name and reconcile again.
199+
Expect(c.Delete(ctx, vmService.DeepCopy())).To(Succeed())
200+
vmService.Name = legacyControlPlaneVMServiceName(clusterCtx.Cluster.Name)
201+
vmService.ResourceVersion = ""
202+
Expect(c.Create(ctx, vmService)).To(Succeed())
203+
updateVMServiceWithVIP(ctx, clusterCtx, c, cpService, vip)
204+
apiEndpoint, err = cpService.ReconcileControlPlaneEndpointService(ctx, clusterCtx, network.DummyLBNetworkProvider())
205+
verifyOutput()
206+
vmService = getVirtualMachineService(ctx, clusterCtx, c, cpService)
207+
Expect(vmService.Name).To(Equal(legacyControlPlaneVMServiceName(clusterCtx.Cluster.Name)))
191208
})
192209

193210
Specify("Reconcile VirtualMachineService for NetOp", func() {

0 commit comments

Comments
 (0)