Skip to content

Commit 629f160

Browse files
committed
baremetal: implement MAPI/CAPI migration
Assisted-by: claude-4.5-sonnet
1 parent 4bff8cf commit 629f160

File tree

6 files changed

+689
-3
lines changed

6 files changed

+689
-3
lines changed

cmd/machine-api-migration/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"time"
2323

24+
metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
2425
configv1 "github.com/openshift/api/config/v1"
2526
mapiv1alpha1 "github.com/openshift/api/machine/v1alpha1"
2627
mapiv1beta1 "github.com/openshift/api/machine/v1beta1"
@@ -68,6 +69,7 @@ func initScheme(scheme *runtime.Scheme) {
6869
utilruntime.Must(mapiv1beta1.Install(scheme))
6970
utilruntime.Must(configv1.Install(scheme))
7071
utilruntime.Must(awsv1.AddToScheme(scheme))
72+
utilruntime.Must(metal3v1.AddToScheme(scheme))
7173
utilruntime.Must(openstackv1.AddToScheme(scheme))
7274
utilruntime.Must(clusterv1.AddToScheme(scheme))
7375
}
@@ -196,7 +198,7 @@ func main() {
196198

197199
// Currently we only plan to support AWS, so all others are a noop until they're implemented.
198200
switch provider {
199-
case configv1.AWSPlatformType, configv1.OpenStackPlatformType:
201+
case configv1.AWSPlatformType, configv1.BareMetalPlatformType, configv1.OpenStackPlatformType:
200202
klog.Infof("MachineAPIMigration: starting %s controllers", provider)
201203

202204
default:

pkg/controllers/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121

22+
metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
2223
configv1 "github.com/openshift/api/config/v1"
2324
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2425
ibmpowervsv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
@@ -42,6 +43,8 @@ func InitInfraMachineAndInfraClusterFromProvider(platform configv1.PlatformType)
4243
switch platform {
4344
case configv1.AWSPlatformType:
4445
return &awsv1.AWSMachine{}, &awsv1.AWSCluster{}, nil
46+
case configv1.BareMetalPlatformType:
47+
return &metal3v1.Metal3Machine{}, &metal3v1.Metal3Cluster{}, nil
4548
case configv1.OpenStackPlatformType:
4649
return &openstackv1.OpenStackMachine{}, &openstackv1.OpenStackCluster{}, nil
4750
case configv1.PowerVSPlatformType:
@@ -59,6 +62,8 @@ func InitInfraMachineTemplateAndInfraClusterFromProvider(platform configv1.Platf
5962
switch platform {
6063
case configv1.AWSPlatformType:
6164
return &awsv1.AWSMachineTemplate{}, &awsv1.AWSCluster{}, nil
65+
case configv1.BareMetalPlatformType:
66+
return &metal3v1.Metal3MachineTemplate{}, &metal3v1.Metal3Cluster{}, nil
6267
case configv1.OpenStackPlatformType:
6368
return &openstackv1.OpenStackMachineTemplate{}, &openstackv1.OpenStackCluster{}, nil
6469
case configv1.PowerVSPlatformType:

pkg/controllers/machinesetsync/machineset_sync_controller.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
utilerrors "k8s.io/apimachinery/pkg/util/errors"
4141

4242
"github.com/go-test/deep"
43+
metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
4344
machinev1applyconfigs "github.com/openshift/client-go/machine/applyconfigurations/machine/v1beta1"
4445
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4546
"k8s.io/apimachinery/pkg/fields"
@@ -77,6 +78,9 @@ var (
7778
// errAssertingCAPIAWSMachineTemplate is returned when we encounter an issue asserting a client.Object into a AWSMachineTemplate.
7879
errAssertingCAPIAWSMachineTemplate = errors.New("error asserting the CAPI AWSMachineTemplate object")
7980

81+
// errAssertingCAPIMetal3MachineTemplate is returned when we encounter an issue asserting a client.Object into a Metal3MachineTemplate.
82+
errAssertingCAPIMetal3MachineTemplate = errors.New("error asserting the CAPI Metal3MachineTemplate object")
83+
8084
// errAssertingCAPIPowerVSMachineTemplate is returned when we encounter an issue asserting a client.Object into a IBMPowerVSMachineTemplate.
8185
errAssertingCAPIIBMPowerVSMachineTemplate = errors.New("error asserting the CAPI IBMPowerVSMachineTemplate object")
8286

@@ -667,6 +671,20 @@ func (r *MachineSetSyncReconciler) convertCAPIToMAPIMachineSet(capiMachineSet *c
667671
return capi2mapi.FromMachineSetAndAWSMachineTemplateAndAWSCluster( //nolint: wrapcheck
668672
capiMachineSet, machineTemplate, cluster,
669673
).ToMachineSet()
674+
case configv1.BareMetalPlatformType:
675+
machineTemplate, ok := infraMachineTemplate.(*metal3v1.Metal3MachineTemplate)
676+
if !ok {
677+
return nil, nil, fmt.Errorf("%w, expected Metal3MachineTemplate, got %T", errUnexpectedInfraMachineTemplateType, infraMachineTemplate)
678+
}
679+
680+
cluster, ok := infraCluster.(*metal3v1.Metal3Cluster)
681+
if !ok {
682+
return nil, nil, fmt.Errorf("%w, expected Metal3Cluster, got %T", errUnexpectedInfraClusterType, infraCluster)
683+
}
684+
685+
return capi2mapi.FromMachineSetAndMetal3MachineTemplateAndMetal3Cluster( //nolint: wrapcheck
686+
capiMachineSet, machineTemplate, cluster,
687+
).ToMachineSet()
670688
case configv1.OpenStackPlatformType:
671689
machineTemplate, ok := infraMachineTemplate.(*openstackv1.OpenStackMachineTemplate)
672690
if !ok {
@@ -705,6 +723,8 @@ func (r *MachineSetSyncReconciler) convertMAPIToCAPIMachineSet(mapiMachineSet *m
705723
switch r.Platform {
706724
case configv1.AWSPlatformType:
707725
return mapi2capi.FromAWSMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
726+
case configv1.BareMetalPlatformType:
727+
return mapi2capi.FromMetal3MachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
708728
case configv1.OpenStackPlatformType:
709729
return mapi2capi.FromOpenStackMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
710730
case configv1.PowerVSPlatformType:
@@ -1307,6 +1327,8 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config
13071327
switch platform {
13081328
case configv1.AWSPlatformType:
13091329
return &awsv1.AWSMachineTemplateList{}, &awsv1.AWSClusterList{}, nil
1330+
case configv1.BareMetalPlatformType:
1331+
return &metal3v1.Metal3MachineTemplateList{}, &metal3v1.Metal3ClusterList{}, nil
13101332
case configv1.OpenStackPlatformType:
13111333
return &openstackv1.OpenStackMachineTemplateList{}, &openstackv1.OpenStackClusterList{}, nil
13121334
case configv1.PowerVSPlatformType:
@@ -1318,7 +1340,7 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config
13181340

13191341
// compareCAPIInfraMachineTemplates compares CAPI infra machine templates a and b, and returns a list of differences, or none if there are none.
13201342
//
1321-
//nolint:funlen
1343+
//nolint:funlen,gocognit,cyclop
13221344
func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachineTemplate1, infraMachineTemplate2 client.Object) (map[string]any, error) {
13231345
switch platform {
13241346
case configv1.AWSPlatformType:
@@ -1344,6 +1366,30 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi
13441366

13451367
// TODO: Evaluate if we want to add status comparison if needed in the future (e.g. for scale from zero capacity).
13461368

1369+
return diff, nil
1370+
case configv1.BareMetalPlatformType:
1371+
typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*metal3v1.Metal3MachineTemplate)
1372+
if !ok {
1373+
return nil, errAssertingCAPIMetal3MachineTemplate
1374+
}
1375+
1376+
typedinfraMachineTemplate2, ok := infraMachineTemplate2.(*metal3v1.Metal3MachineTemplate)
1377+
if !ok {
1378+
return nil, errAssertingCAPIMetal3MachineTemplate
1379+
}
1380+
1381+
diff := make(map[string]any)
1382+
1383+
if diffSpec := deep.Equal(typedInfraMachineTemplate1.Spec, typedinfraMachineTemplate2.Spec); len(diffSpec) > 0 {
1384+
diff[".spec"] = diffSpec
1385+
}
1386+
1387+
if diffObjectMeta := util.ObjectMetaEqual(typedInfraMachineTemplate1.ObjectMeta, typedinfraMachineTemplate2.ObjectMeta); len(diffObjectMeta) > 0 {
1388+
diff[".metadata"] = diffObjectMeta
1389+
}
1390+
1391+
// TODO: Evaluate if we want to add status comparison if needed in the future (e.g. for scale from zero capacity).
1392+
13471393
return diff, nil
13481394
case configv1.OpenStackPlatformType:
13491395
typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*openstackv1.OpenStackMachineTemplate)

pkg/controllers/machinesync/machine_sync_controller.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/openshift/cluster-capi-operator/pkg/util"
3838

3939
"github.com/go-test/deep"
40+
metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
4041
corev1 "k8s.io/api/core/v1"
4142
apierrors "k8s.io/apimachinery/pkg/api/errors"
4243
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -94,6 +95,9 @@ var (
9495
// errAssertingCAPIAWSMachine is returned when we encounter an issue asserting a client.Object into an AWSMachine.
9596
errAssertingCAPIAWSMachine = errors.New("error asserting the Cluster API AWSMachine object")
9697

98+
// errAssertingCAPIMetal3Machine is returned when we encounter an issue asserting a client.Object into a Metal3Machine.
99+
errAssertingCAPIMetal3Machine = errors.New("error asserting the Cluster API Metal3Machine object")
100+
97101
// errAssertingCAPIOpenStackMachine is returned when we encounter an issue asserting a client.Object into an OpenStackVSMachine.
98102
errAssertingCAPIOpenStackMachine = errors.New("error asserting the Cluster API OpenStackMachine object")
99103

@@ -559,6 +563,8 @@ func (r *MachineSyncReconciler) convertMAPIToCAPIMachine(mapiMachine *machinev1b
559563
switch r.Platform {
560564
case configv1.AWSPlatformType:
561565
return mapi2capi.FromAWSMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
566+
case configv1.BareMetalPlatformType:
567+
return mapi2capi.FromMetal3MachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
562568
case configv1.OpenStackPlatformType:
563569
return mapi2capi.FromOpenStackMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
564570
case configv1.PowerVSPlatformType:
@@ -582,6 +588,18 @@ func (r *MachineSyncReconciler) convertCAPIToMAPIMachine(capiMachine *clusterv1.
582588
}
583589

584590
return capi2mapi.FromMachineAndAWSMachineAndAWSCluster(capiMachine, awsMachine, awsCluster).ToMachine() //nolint:wrapcheck
591+
case configv1.BareMetalPlatformType:
592+
metal3Machine, ok := infraMachine.(*metal3v1.Metal3Machine)
593+
if !ok {
594+
return nil, nil, fmt.Errorf("%w, expected Metal3Machine, got %T", errUnexpectedInfraMachineType, infraMachine)
595+
}
596+
597+
metal3Cluster, ok := infraCluster.(*metal3v1.Metal3Cluster)
598+
if !ok {
599+
return nil, nil, fmt.Errorf("%w, expected Metal3Cluster, got %T", errUnexpectedInfraClusterType, infraCluster)
600+
}
601+
602+
return capi2mapi.FromMachineAndMetal3MachineAndMetal3Cluster(capiMachine, metal3Machine, metal3Cluster).ToMachine() //nolint:wrapcheck
585603
case configv1.OpenStackPlatformType:
586604
openStackMachine, ok := infraMachine.(*openstackv1.OpenStackMachine)
587605
if !ok {
@@ -1343,7 +1361,7 @@ func compareMAPIMachines(a, b *machinev1beta1.Machine) (map[string]any, error) {
13431361

13441362
// compareCAPIInfraMachines compares CAPI infra machines a and b, and returns a list of differences, or none if there are none.
13451363
//
1346-
//nolint:funlen
1364+
//nolint:funlen,gocognit,cyclop
13471365
func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, infraMachine2 client.Object) (map[string]any, error) {
13481366
switch platform {
13491367
case configv1.AWSPlatformType:
@@ -1366,6 +1384,27 @@ func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, inf
13661384
diff[".metadata"] = diffMetadata
13671385
}
13681386

1387+
return diff, nil
1388+
case configv1.BareMetalPlatformType:
1389+
typedInfraMachine1, ok := infraMachine1.(*metal3v1.Metal3Machine)
1390+
if !ok {
1391+
return nil, errAssertingCAPIMetal3Machine
1392+
}
1393+
1394+
typedinfraMachine2, ok := infraMachine2.(*metal3v1.Metal3Machine)
1395+
if !ok {
1396+
return nil, errAssertingCAPIMetal3Machine
1397+
}
1398+
1399+
diff := make(map[string]any)
1400+
if diffSpec := deep.Equal(typedInfraMachine1.Spec, typedinfraMachine2.Spec); len(diffSpec) > 0 {
1401+
diff[".spec"] = diffSpec
1402+
}
1403+
1404+
if diffMetadata := util.ObjectMetaEqual(typedInfraMachine1.ObjectMeta, typedinfraMachine2.ObjectMeta); len(diffMetadata) > 0 {
1405+
diff[".metadata"] = diffMetadata
1406+
}
1407+
13691408
return diff, nil
13701409
case configv1.OpenStackPlatformType:
13711410
typedInfraMachine1, ok := infraMachine1.(*openstackv1.OpenStackMachine)

0 commit comments

Comments
 (0)