Skip to content

Commit 71f1db2

Browse files
committed
baremetal: implement MAPI/CAPI migration
Assisted-by: claude-4.5-sonnet
1 parent 0a857c4 commit 71f1db2

File tree

7 files changed

+709
-3
lines changed

7 files changed

+709
-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"
@@ -75,6 +76,9 @@ var (
7576
// errAssertingCAPIAWSMachineTemplate is returned when we encounter an issue asserting a client.Object into a AWSMachineTemplate.
7677
errAssertingCAPIAWSMachineTemplate = errors.New("error asserting the CAPI AWSMachineTemplate object")
7778

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

@@ -665,6 +669,20 @@ func (r *MachineSetSyncReconciler) convertCAPIToMAPIMachineSet(capiMachineSet *c
665669
return capi2mapi.FromMachineSetAndAWSMachineTemplateAndAWSCluster( //nolint: wrapcheck
666670
capiMachineSet, machineTemplate, cluster,
667671
).ToMachineSet()
672+
case configv1.BareMetalPlatformType:
673+
machineTemplate, ok := infraMachineTemplate.(*metal3v1.Metal3MachineTemplate)
674+
if !ok {
675+
return nil, nil, fmt.Errorf("%w, expected Metal3MachineTemplate, got %T", errUnexpectedInfraMachineTemplateType, infraMachineTemplate)
676+
}
677+
678+
cluster, ok := infraCluster.(*metal3v1.Metal3Cluster)
679+
if !ok {
680+
return nil, nil, fmt.Errorf("%w, expected Metal3Cluster, got %T", errUnexpectedInfraClusterType, infraCluster)
681+
}
682+
683+
return capi2mapi.FromMachineSetAndMetal3MachineTemplateAndMetal3Cluster( //nolint: wrapcheck
684+
capiMachineSet, machineTemplate, cluster,
685+
).ToMachineSet()
668686
case configv1.OpenStackPlatformType:
669687
machineTemplate, ok := infraMachineTemplate.(*openstackv1.OpenStackMachineTemplate)
670688
if !ok {
@@ -703,6 +721,8 @@ func (r *MachineSetSyncReconciler) convertMAPIToCAPIMachineSet(mapiMachineSet *m
703721
switch r.Platform {
704722
case configv1.AWSPlatformType:
705723
return mapi2capi.FromAWSMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
724+
case configv1.BareMetalPlatformType:
725+
return mapi2capi.FromMetal3MachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
706726
case configv1.OpenStackPlatformType:
707727
return mapi2capi.FromOpenStackMachineSetAndInfra(mapiMachineSet, r.Infra).ToMachineSetAndMachineTemplate() //nolint:wrapcheck
708728
case configv1.PowerVSPlatformType:
@@ -1302,6 +1322,8 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config
13021322
switch platform {
13031323
case configv1.AWSPlatformType:
13041324
return &awsv1.AWSMachineTemplateList{}, &awsv1.AWSClusterList{}, nil
1325+
case configv1.BareMetalPlatformType:
1326+
return &metal3v1.Metal3MachineTemplateList{}, &metal3v1.Metal3ClusterList{}, nil
13051327
case configv1.OpenStackPlatformType:
13061328
return &openstackv1.OpenStackMachineTemplateList{}, &openstackv1.OpenStackClusterList{}, nil
13071329
case configv1.PowerVSPlatformType:
@@ -1313,7 +1335,7 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config
13131335

13141336
// compareCAPIInfraMachineTemplates compares CAPI infra machine templates a and b, and returns a list of differences, or none if there are none.
13151337
//
1316-
//nolint:funlen
1338+
//nolint:funlen,gocognit,cyclop
13171339
func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachineTemplate1, infraMachineTemplate2 client.Object) (map[string]any, error) {
13181340
switch platform {
13191341
case configv1.AWSPlatformType:
@@ -1339,6 +1361,30 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi
13391361

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

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

pkg/controllers/machinesync/machine_sync_controller.go

Lines changed: 18 additions & 0 deletions
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"
@@ -96,6 +97,9 @@ var (
9697
// errAssertingCAPIAWSMachine is returned when we encounter an issue asserting a client.Object into an AWSMachine.
9798
errAssertingCAPIAWSMachine = errors.New("error asserting the Cluster API AWSMachine object")
9899

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

@@ -582,6 +586,8 @@ func (r *MachineSyncReconciler) convertMAPIToCAPIMachine(mapiMachine *mapiv1beta
582586
switch r.Platform {
583587
case configv1.AWSPlatformType:
584588
return mapi2capi.FromAWSMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
589+
case configv1.BareMetalPlatformType:
590+
return mapi2capi.FromMetal3MachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
585591
case configv1.OpenStackPlatformType:
586592
return mapi2capi.FromOpenStackMachineAndInfra(mapiMachine, r.Infra).ToMachineAndInfrastructureMachine() //nolint:wrapcheck
587593
case configv1.PowerVSPlatformType:
@@ -605,6 +611,18 @@ func (r *MachineSyncReconciler) convertCAPIToMAPIMachine(capiMachine *clusterv1.
605611
}
606612

607613
return capi2mapi.FromMachineAndAWSMachineAndAWSCluster(capiMachine, awsMachine, awsCluster).ToMachine() //nolint:wrapcheck
614+
case configv1.BareMetalPlatformType:
615+
metal3Machine, ok := infraMachine.(*metal3v1.Metal3Machine)
616+
if !ok {
617+
return nil, nil, fmt.Errorf("%w, expected Metal3Machine, got %T", errUnexpectedInfraMachineType, infraMachine)
618+
}
619+
620+
metal3Cluster, ok := infraCluster.(*metal3v1.Metal3Cluster)
621+
if !ok {
622+
return nil, nil, fmt.Errorf("%w, expected Metal3Cluster, got %T", errUnexpectedInfraClusterType, infraCluster)
623+
}
624+
625+
return capi2mapi.FromMachineAndMetal3MachineAndMetal3Cluster(capiMachine, metal3Machine, metal3Cluster).ToMachine() //nolint:wrapcheck
608626
case configv1.OpenStackPlatformType:
609627
openStackMachine, ok := infraMachine.(*openstackv1.OpenStackMachine)
610628
if !ok {

pkg/controllers/machinesync/machine_sync_mapi2capi_infrastructure.go

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

2323
"github.com/go-test/deep"
24+
metal3v1 "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
2425
configv1 "github.com/openshift/api/config/v1"
2526
mapiv1beta1 "github.com/openshift/api/machine/v1beta1"
2627
"github.com/openshift/cluster-capi-operator/pkg/util"
@@ -247,7 +248,7 @@ func (r *MachineSyncReconciler) ensureCAPIInfraMachineDeleted(ctx context.Contex
247248

248249
// compareCAPIInfraMachines compares Cluster API infra machines a and b, and returns a list of differences, or none if there are none.
249250
//
250-
//nolint:funlen,gocognit
251+
//nolint:funlen,gocognit,cyclop
251252
func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, infraMachine2 client.Object) (map[string]any, error) {
252253
diff := make(map[string]any)
253254

@@ -293,6 +294,28 @@ func compareCAPIInfraMachines(platform configv1.PlatformType, infraMachine1, inf
293294
diff[".metadata"] = diffMetadata
294295
}
295296

297+
if diffStatus := deep.Equal(typedInfraMachine1.Status, typedinfraMachine2.Status); len(diffStatus) > 0 {
298+
diff[".status"] = diffStatus
299+
}
300+
case configv1.BareMetalPlatformType:
301+
typedInfraMachine1, ok := infraMachine1.(*metal3v1.Metal3Machine)
302+
if !ok {
303+
return nil, errAssertingCAPIMetal3Machine
304+
}
305+
306+
typedinfraMachine2, ok := infraMachine2.(*metal3v1.Metal3Machine)
307+
if !ok {
308+
return nil, errAssertingCAPIMetal3Machine
309+
}
310+
311+
if diffSpec := deep.Equal(typedInfraMachine1.Spec, typedinfraMachine2.Spec); len(diffSpec) > 0 {
312+
diff[".spec"] = diffSpec
313+
}
314+
315+
if diffMetadata := util.ObjectMetaEqual(typedInfraMachine1.ObjectMeta, typedinfraMachine2.ObjectMeta); len(diffMetadata) > 0 {
316+
diff[".metadata"] = diffMetadata
317+
}
318+
296319
if diffStatus := deep.Equal(typedInfraMachine1.Status, typedinfraMachine2.Status); len(diffStatus) > 0 {
297320
diff[".status"] = diffStatus
298321
}

0 commit comments

Comments
 (0)