Skip to content

Commit 9b35375

Browse files
committed
vsphere conversion
1 parent 82bcb80 commit 9b35375

File tree

6 files changed

+807
-8
lines changed

6 files changed

+807
-8
lines changed

cmd/machine-api-migration/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import (
2121
"os"
2222
"time"
2323

24+
"k8s.io/utils/clock"
25+
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
26+
openstackv1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
27+
vspherev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
29+
2430
configv1 "github.com/openshift/api/config/v1"
2531
mapiv1alpha1 "github.com/openshift/api/machine/v1alpha1"
2632
mapiv1beta1 "github.com/openshift/api/machine/v1beta1"
@@ -32,14 +38,7 @@ import (
3238
"github.com/openshift/cluster-capi-operator/pkg/controllers/machinesetsync"
3339
"github.com/openshift/cluster-capi-operator/pkg/controllers/machinesync"
3440
"github.com/openshift/cluster-capi-operator/pkg/util"
35-
"k8s.io/utils/clock"
36-
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
37-
openstackv1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
38-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3941

40-
"github.com/openshift/api/features"
41-
featuregates "github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
42-
"github.com/openshift/library-go/pkg/operator/events"
4342
"github.com/spf13/pflag"
4443
"k8s.io/apimachinery/pkg/runtime"
4544
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -52,6 +51,10 @@ import (
5251
"sigs.k8s.io/controller-runtime/pkg/cache"
5352
"sigs.k8s.io/controller-runtime/pkg/client"
5453
"sigs.k8s.io/controller-runtime/pkg/healthz"
54+
55+
"github.com/openshift/api/features"
56+
featuregates "github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
57+
"github.com/openshift/library-go/pkg/operator/events"
5558
)
5659

5760
var (
@@ -70,6 +73,7 @@ func initScheme(scheme *runtime.Scheme) {
7073
utilruntime.Must(awsv1.AddToScheme(scheme))
7174
utilruntime.Must(openstackv1.AddToScheme(scheme))
7275
utilruntime.Must(clusterv1.AddToScheme(scheme))
76+
utilruntime.Must(vspherev1.AddToScheme(scheme))
7377
}
7478

7579
//nolint:funlen
@@ -196,7 +200,7 @@ func main() {
196200

197201
// Currently we only plan to support AWS, so all others are a noop until they're implemented.
198202
switch provider {
199-
case configv1.AWSPlatformType, configv1.OpenStackPlatformType:
203+
case configv1.AWSPlatformType, configv1.OpenStackPlatformType, configv1.VSpherePlatformType:
200204
klog.Infof("MachineAPIMigration: starting %s controllers", provider)
201205

202206
default:

pkg/controllers/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2424
ibmpowervsv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
2525
openstackv1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
26+
vspherev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
)
2829

@@ -46,6 +47,8 @@ func InitInfraMachineAndInfraClusterFromProvider(platform configv1.PlatformType)
4647
return &openstackv1.OpenStackMachine{}, &openstackv1.OpenStackCluster{}, nil
4748
case configv1.PowerVSPlatformType:
4849
return &ibmpowervsv1.IBMPowerVSMachine{}, &ibmpowervsv1.IBMPowerVSCluster{}, nil
50+
case configv1.VSpherePlatformType:
51+
return &vspherev1.VSphereMachine{}, &vspherev1.VSphereCluster{}, nil
4952
default:
5053
return nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, platform)
5154
}
@@ -63,6 +66,8 @@ func InitInfraMachineTemplateAndInfraClusterFromProvider(platform configv1.Platf
6366
return &openstackv1.OpenStackMachineTemplate{}, &openstackv1.OpenStackCluster{}, nil
6467
case configv1.PowerVSPlatformType:
6568
return &ibmpowervsv1.IBMPowerVSMachineTemplate{}, &ibmpowervsv1.IBMPowerVSCluster{}, nil
69+
case configv1.VSpherePlatformType:
70+
return &vspherev1.VSphereMachineTemplate{}, &vspherev1.VSphereCluster{}, nil
6671
default:
6772
return nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, platform)
6873
}

pkg/controllers/machinesetsync/machineset_sync_controller.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
4949
ibmpowervsv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
5050
openstackv1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
51+
vspherev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
5152
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
5253
"sigs.k8s.io/cluster-api/util/annotations"
5354
ctrl "sigs.k8s.io/controller-runtime"
@@ -80,6 +81,9 @@ var (
8081
// errAssertingCAPIOpenStackMachineTemplate is returned when we encounter an issue asserting a client.Object into a OpenStackMachineTemplate.
8182
errAssertingCAPIOpenStackMachineTemplate = errors.New("error asserting the CAPI OpenStackMachineTemplate object")
8283

84+
// errAssertingCAPIVSphereMachineTemplate is returned when we encounter an issue asserting a client.Object into a VSphereMachineTemplate.
85+
errAssertingCAPIVSphereMachineTemplate = errors.New("error asserting the CAPI VSphereMachineTemplate object")
86+
8387
// errUnsuportedOwnerKindForConversion is returned when the owner kind is not supported for conversion.
8488
errUnsuportedOwnerKindForConversion = errors.New("unsupported owner kind for conversion")
8589

@@ -714,6 +718,20 @@ func (r *MachineSetSyncReconciler) convertCAPIToMAPIMachineSet(capiMachineSet *c
714718
return capi2mapi.FromMachineSetAndPowerVSMachineTemplateAndPowerVSCluster( //nolint: wrapcheck
715719
capiMachineSet, machineTemplate, cluster,
716720
).ToMachineSet()
721+
case configv1.VSpherePlatformType:
722+
machineTemplate, ok := infraMachineTemplate.(*vspherev1.VSphereMachineTemplate)
723+
if !ok {
724+
return nil, nil, fmt.Errorf("%w, expected VSphereMachineTemplate, got %T", errUnexpectedInfraMachineTemplateType, infraMachineTemplate)
725+
}
726+
727+
cluster, ok := infraCluster.(*vspherev1.VSphereCluster)
728+
if !ok {
729+
return nil, nil, fmt.Errorf("%w, expected VSphereCluster, got %T", errUnexpectedInfraClusterType, infraCluster)
730+
}
731+
732+
return capi2mapi.FromMachineSetAndVSphereMachineTemplateAndVSphereCluster( //nolint: wrapcheck
733+
capiMachineSet, machineTemplate, cluster,
734+
).ToMachineSet()
717735
default:
718736
return nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, r.Platform)
719737
}
@@ -728,6 +746,8 @@ func (r *MachineSetSyncReconciler) convertMAPIToCAPIMachineSet(mapiMachineSet *m
728746
return mapi2capi.FromOpenStackMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
729747
case configv1.PowerVSPlatformType:
730748
return mapi2capi.FromPowerVSMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
749+
case configv1.VSpherePlatformType:
750+
return mapi2capi.FromVSphereMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
731751
default:
732752
return nil, nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, r.Platform)
733753
}
@@ -1071,6 +1091,8 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config
10711091
return &openstackv1.OpenStackMachineTemplateList{}, &openstackv1.OpenStackClusterList{}, nil
10721092
case configv1.PowerVSPlatformType:
10731093
return &ibmpowervsv1.IBMPowerVSMachineTemplateList{}, &ibmpowervsv1.IBMPowerVSClusterList{}, nil
1094+
case configv1.VSpherePlatformType:
1095+
return &vspherev1.VSphereMachineTemplateList{}, &vspherev1.VSphereClusterList{}, nil
10741096
default:
10751097
return nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, platform)
10761098
}
@@ -1146,6 +1168,28 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi
11461168
diff[".metadata"] = diffObjectMeta
11471169
}
11481170

1171+
return diff, nil
1172+
case configv1.VSpherePlatformType:
1173+
typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*vspherev1.VSphereMachineTemplate)
1174+
if !ok {
1175+
return nil, errAssertingCAPIVSphereMachineTemplate
1176+
}
1177+
1178+
typedinfraMachineTemplate2, ok := infraMachineTemplate2.(*vspherev1.VSphereMachineTemplate)
1179+
if !ok {
1180+
return nil, errAssertingCAPIVSphereMachineTemplate
1181+
}
1182+
1183+
diff := make(map[string]any)
1184+
1185+
if diffSpec := deep.Equal(typedInfraMachineTemplate1.Spec, typedinfraMachineTemplate2.Spec); len(diffSpec) > 0 {
1186+
diff[".spec"] = diffSpec
1187+
}
1188+
1189+
if diffObjectMeta := util.ObjectMetaEqual(typedInfraMachineTemplate1.ObjectMeta, typedinfraMachineTemplate2.ObjectMeta); len(diffObjectMeta) > 0 {
1190+
diff[".metadata"] = diffObjectMeta
1191+
}
1192+
11491193
return diff, nil
11501194
default:
11511195
return nil, fmt.Errorf("%w: %s", errPlatformNotSupported, platform)

pkg/controllers/machinesync/machine_sync_controller.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
4949
ibmpowervsv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
5050
openstackv1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
51+
vspherev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
5152
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
5253
"sigs.k8s.io/cluster-api/util/annotations"
5354
"sigs.k8s.io/cluster-api/util/labels/format"
@@ -99,6 +100,9 @@ var (
99100
// errAssertingCAPIBMPowerVSMachine is returned when we encounter an issue asserting a client.Object into an IBMPowerVSMachine.
100101
errAssertingCAPIIBMPowerVSMachine = errors.New("error asserting the Cluster API IBMPowerVSMachine object")
101102

103+
// errAssertingCAPIVSphereMachine is returned when we encounter an issue asserting a client.Object into a VSphereMachine.
104+
errAssertingCAPIVSphereMachine = errors.New("error asserting the Cluster API VSphereMachine object")
105+
102106
// errCAPIMachineNotFound is returned when the AuthoritativeAPI is set to CAPI on the MAPI machine,
103107
// but we can't find the CAPI machine.
104108
//lint:ignore ST1005 Cluster API is a name.
@@ -562,6 +566,8 @@ func (r *MachineSyncReconciler) convertMAPIToCAPIMachine(mapiMachine *machinev1b
562566
return mapi2capi.FromOpenStackMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
563567
case configv1.PowerVSPlatformType:
564568
return mapi2capi.FromPowerVSMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
569+
case configv1.VSpherePlatformType:
570+
return mapi2capi.FromVSphereMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
565571
default:
566572
return nil, nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, r.Platform)
567573
}
@@ -593,6 +599,18 @@ func (r *MachineSyncReconciler) convertCAPIToMAPIMachine(capiMachine *clusterv1.
593599
}
594600

595601
return capi2mapi.FromMachineAndOpenStackMachineAndOpenStackCluster(capiMachine, openStackMachine, openStackCluster).ToMachine() //nolint:wrapcheck
602+
case configv1.VSpherePlatformType:
603+
vsphereMachine, ok := infraMachine.(*vspherev1.VSphereMachine)
604+
if !ok {
605+
return nil, nil, fmt.Errorf("%w, expected VSphereMachine, got %T", errUnexpectedInfraMachineType, infraMachine)
606+
}
607+
608+
vsphereCluster, ok := infraCluster.(*vspherev1.VSphereCluster)
609+
if !ok {
610+
return nil, nil, fmt.Errorf("%w, expected VSphereCluster, got %T", errUnexpectedInfraClusterType, infraCluster)
611+
}
612+
613+
return capi2mapi.FromMachineAndVSphereMachineAndVSphereCluster(capiMachine, vsphereMachine, vsphereCluster).ToMachine() //nolint:wrapcheck
596614
default:
597615
return nil, nil, fmt.Errorf("%w: %s", errPlatformNotSupported, r.Platform)
598616
}
@@ -1362,6 +1380,27 @@ func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, inf
13621380
diff[".metadata"] = diffMetadata
13631381
}
13641382

1383+
return diff, nil
1384+
case configv1.VSpherePlatformType:
1385+
typedInfraMachine1, ok := infraMachine1.(*vspherev1.VSphereMachine)
1386+
if !ok {
1387+
return nil, errAssertingCAPIVSphereMachine
1388+
}
1389+
1390+
typedinfraMachine2, ok := infraMachine2.(*vspherev1.VSphereMachine)
1391+
if !ok {
1392+
return nil, errAssertingCAPIVSphereMachine
1393+
}
1394+
1395+
diff := make(map[string]any)
1396+
if diffSpec := deep.Equal(typedInfraMachine1.Spec, typedinfraMachine2.Spec); len(diffSpec) > 0 {
1397+
diff[".spec"] = diffSpec
1398+
}
1399+
1400+
if diffMetadata := util.ObjectMetaEqual(typedInfraMachine1.ObjectMeta, typedinfraMachine2.ObjectMeta); len(diffMetadata) > 0 {
1401+
diff[".metadata"] = diffMetadata
1402+
}
1403+
13651404
return diff, nil
13661405
default:
13671406
return nil, fmt.Errorf("%w: %s", errPlatformNotSupported, platform)

0 commit comments

Comments
 (0)