Skip to content

Commit 50f77fb

Browse files
committed
always compare client.Object
1 parent 12db253 commit 50f77fb

File tree

9 files changed

+358
-465
lines changed

9 files changed

+358
-465
lines changed

pkg/controllers/machinesetsync/machineset_sync_controller.go

Lines changed: 35 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"slices"
24-
"sort"
2524

2625
"github.com/go-logr/logr"
2726
configv1 "github.com/openshift/api/config/v1"
@@ -740,7 +739,7 @@ func (r *MachineSetSyncReconciler) createOrUpdateCAPIInfraMachineTemplate(ctx co
740739
return updateErr
741740
}
742741

743-
if len(capiInfraMachineTemplatesDiff) == 0 {
742+
if capiInfraMachineTemplatesDiff.HasChanges() {
744743
logger.Info("No changes detected for CAPI infra machine template")
745744
return nil
746745
}
@@ -841,11 +840,11 @@ func (r *MachineSetSyncReconciler) ensureCAPIMachineSet(ctx context.Context, sou
841840
}
842841

843842
// ensureCAPIMachineSetSpecUpdated updates the CAPI machine set if changes are detected.
844-
func (r *MachineSetSyncReconciler) ensureCAPIMachineSetSpecUpdated(ctx context.Context, mapiMachineSet *mapiv1beta1.MachineSet, capiMachineSetsDiff map[string]any, updatedOrCreatedCAPIMachineSet *clusterv1.MachineSet) (bool, error) {
843+
func (r *MachineSetSyncReconciler) ensureCAPIMachineSetSpecUpdated(ctx context.Context, mapiMachineSet *mapiv1beta1.MachineSet, capiMachineSetsDiff util.DiffResult, updatedOrCreatedCAPIMachineSet *clusterv1.MachineSet) (bool, error) {
845844
logger := logf.FromContext(ctx)
846845

847846
// If there are no spec changes, return early.
848-
if !hasSpecOrMetadataOrProviderSpecChanges(capiMachineSetsDiff) {
847+
if !(capiMachineSetsDiff.HasMetadataChanges() || capiMachineSetsDiff.HasSpecChanges() || capiMachineSetsDiff.HasProviderSpecChanges()) {
849848
return false, nil
850849
}
851850

@@ -867,11 +866,11 @@ func (r *MachineSetSyncReconciler) ensureCAPIMachineSetSpecUpdated(ctx context.C
867866
}
868867

869868
// ensureCAPIMachineSetStatusUpdated updates the CAPI machine set status if changes are detected and conditions are met.
870-
func (r *MachineSetSyncReconciler) ensureCAPIMachineSetStatusUpdated(ctx context.Context, mapiMachineSet *mapiv1beta1.MachineSet, existingCAPIMachineSet *clusterv1.MachineSet, convertedCAPIMachineSet *clusterv1.MachineSet, updatedOrCreatedCAPIMachineSet *clusterv1.MachineSet, capiMachineSetsDiff map[string]any, specUpdated bool) (bool, error) {
869+
func (r *MachineSetSyncReconciler) ensureCAPIMachineSetStatusUpdated(ctx context.Context, mapiMachineSet *mapiv1beta1.MachineSet, existingCAPIMachineSet *clusterv1.MachineSet, convertedCAPIMachineSet *clusterv1.MachineSet, updatedOrCreatedCAPIMachineSet *clusterv1.MachineSet, capiMachineSetsDiff util.DiffResult, specUpdated bool) (bool, error) {
871870
logger := logf.FromContext(ctx)
872871

873872
// If there are no status changes and the spec has not been updated, return early.
874-
if !hasStatusChanges(capiMachineSetsDiff) && !specUpdated {
873+
if !capiMachineSetsDiff.HasStatusChanges() && !specUpdated {
875874
return false, nil
876875
}
877876

@@ -918,11 +917,11 @@ func (r *MachineSetSyncReconciler) ensureCAPIMachineSetStatusUpdated(ctx context
918917
}
919918

920919
// ensureMAPIMachineSetSpecUpdated updates the MAPI machine set if changes are detected.
921-
func (r *MachineSetSyncReconciler) ensureMAPIMachineSetSpecUpdated(ctx context.Context, mapiMachineSet *mapiv1beta1.MachineSet, mapiMachineSetsDiff map[string]any, updatedMAPIMachineSet *mapiv1beta1.MachineSet) (bool, error) {
920+
func (r *MachineSetSyncReconciler) ensureMAPIMachineSetSpecUpdated(ctx context.Context, mapiMachineSet *mapiv1beta1.MachineSet, mapiMachineSetsDiff util.DiffResult, updatedMAPIMachineSet *mapiv1beta1.MachineSet) (bool, error) {
922921
logger := logf.FromContext(ctx)
923922

924923
// If there are no spec changes, return early.
925-
if !hasSpecOrMetadataOrProviderSpecChanges(mapiMachineSetsDiff) {
924+
if !(mapiMachineSetsDiff.HasMetadataChanges() || mapiMachineSetsDiff.HasSpecChanges() || mapiMachineSetsDiff.HasProviderSpecChanges()) {
926925
return false, nil
927926
}
928927

@@ -945,11 +944,11 @@ func (r *MachineSetSyncReconciler) ensureMAPIMachineSetSpecUpdated(ctx context.C
945944
}
946945

947946
// ensureMAPIMachineSetStatusUpdated updates the MAPI machine set status if changes are detected and conditions are met.
948-
func (r *MachineSetSyncReconciler) ensureMAPIMachineSetStatusUpdated(ctx context.Context, existingMAPIMachineSet *mapiv1beta1.MachineSet, convertedMAPIMachineSet *mapiv1beta1.MachineSet, updatedMAPIMachineSet *mapiv1beta1.MachineSet, sourceCAPIMachineSet *clusterv1.MachineSet, mapiMachineSetsDiff map[string]any, specUpdated bool) (bool, error) {
947+
func (r *MachineSetSyncReconciler) ensureMAPIMachineSetStatusUpdated(ctx context.Context, existingMAPIMachineSet *mapiv1beta1.MachineSet, convertedMAPIMachineSet *mapiv1beta1.MachineSet, updatedMAPIMachineSet *mapiv1beta1.MachineSet, sourceCAPIMachineSet *clusterv1.MachineSet, mapiMachineSetsDiff util.DiffResult, specUpdated bool) (bool, error) {
949948
logger := logf.FromContext(ctx)
950949

951950
// If there are no status changes and the spec has not been updated, return early.
952-
if !hasStatusChanges(mapiMachineSetsDiff) && !specUpdated {
951+
if !mapiMachineSetsDiff.HasStatusChanges() && !specUpdated {
953952
return false, nil
954953
}
955954

@@ -1019,7 +1018,7 @@ func (r *MachineSetSyncReconciler) updateMAPIMachineSet(ctx context.Context, exi
10191018

10201019
// Here we always assume the existingMAPIMachineSet already exists, so we don't need to create it.
10211020

1022-
mapiMachineSetsDiff, err := compareMAPIMachineSets(existingMAPIMachineSet, convertedMAPIMachineSet)
1021+
mapiMachineSetsDiff, err := compareMAPIMachineSets(r.Platform, existingMAPIMachineSet, convertedMAPIMachineSet)
10231022
if err != nil {
10241023
return fmt.Errorf("unable to compare MAPI machine sets: %w", err)
10251024
}
@@ -1315,14 +1314,8 @@ func initInfraMachineTemplateListAndInfraClusterListFromProvider(platform config
13151314
}
13161315

13171316
// compareCAPIInfraMachineTemplates compares CAPI infra machine templates a and b, and returns a list of differences, or none if there are none.
1318-
//
1319-
//nolint:funlen
1320-
func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachineTemplate1, infraMachineTemplate2 client.Object) (map[string]any, error) {
1321-
diff := make(map[string]any)
1322-
1323-
var metadata1, metadata2 metav1.ObjectMeta
1324-
1325-
var spec1, spec2, status1, status2 any
1317+
func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachineTemplate1, infraMachineTemplate2 client.Object) (util.DiffResult, error) {
1318+
var obj1, obj2 client.Object
13261319

13271320
switch platform {
13281321
case configv1.AWSPlatformType:
@@ -1336,12 +1329,8 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi
13361329
return nil, errAssertingCAPIAWSMachineTemplate
13371330
}
13381331

1339-
spec1 = typedInfraMachineTemplate1.Spec
1340-
spec2 = typedinfraMachineTemplate2.Spec
1341-
metadata1 = typedInfraMachineTemplate1.ObjectMeta
1342-
metadata2 = typedinfraMachineTemplate2.ObjectMeta
1343-
status1 = typedInfraMachineTemplate1.Status
1344-
status2 = typedinfraMachineTemplate2.Status
1332+
obj1 = typedInfraMachineTemplate1
1333+
obj2 = typedinfraMachineTemplate2
13451334
case configv1.OpenStackPlatformType:
13461335
typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*openstackv1.OpenStackMachineTemplate)
13471336
if !ok {
@@ -1353,10 +1342,8 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi
13531342
return nil, errAssertingCAPIOpenStackMachineTemplate
13541343
}
13551344

1356-
spec1 = typedInfraMachineTemplate1.Spec
1357-
spec2 = typedinfraMachineTemplate2.Spec
1358-
metadata1 = typedInfraMachineTemplate1.ObjectMeta
1359-
metadata2 = typedinfraMachineTemplate2.ObjectMeta
1345+
obj1 = typedInfraMachineTemplate1
1346+
obj2 = typedinfraMachineTemplate2
13601347
case configv1.PowerVSPlatformType:
13611348
typedInfraMachineTemplate1, ok := infraMachineTemplate1.(*ibmpowervsv1.IBMPowerVSMachineTemplate)
13621349
if !ok {
@@ -1368,124 +1355,44 @@ func compareCAPIInfraMachineTemplates(platform configv1.PlatformType, infraMachi
13681355
return nil, errAssertingCAPIIBMPowerVSMachineTemplate
13691356
}
13701357

1371-
spec1 = typedInfraMachineTemplate1.Spec
1372-
spec2 = typedinfraMachineTemplate2.Spec
1373-
metadata1 = typedInfraMachineTemplate1.ObjectMeta
1374-
metadata2 = typedinfraMachineTemplate2.ObjectMeta
1375-
status1 = typedInfraMachineTemplate1.Status
1376-
status2 = typedinfraMachineTemplate2.Status
1358+
obj1 = typedInfraMachineTemplate1
1359+
obj2 = typedinfraMachineTemplate2
13771360
default:
13781361
return nil, fmt.Errorf("%w: %s", errPlatformNotSupported, platform)
13791362
}
13801363

1381-
// Compare metadata
1382-
if diffMetadata, err := util.ObjectMetaEqual(metadata1, metadata2); err != nil {
1383-
return nil, fmt.Errorf("failed to compare Cluster API Infrastructure machine metadata: %w", err)
1384-
} else if diffMetadata.Changed() {
1385-
diff[".metadata"] = diffMetadata.String()
1386-
}
1387-
1388-
// Compare spec
1389-
if diffSpec, err := util.NewDiffer().Diff(spec1, spec2); err != nil {
1390-
return nil, fmt.Errorf("failed to compare Cluster API Infrastructure machine spec: %w", err)
1391-
} else if diffSpec.Changed() {
1392-
diff[".spec"] = diffSpec.String()
1393-
}
1394-
1395-
// Compare status
1396-
if diffStatus, err := util.NewDiffer(util.WithIgnoreConditionsLastTransitionTime()).Diff(status1, status2); err != nil {
1397-
return nil, fmt.Errorf("failed to compare Cluster API Infrastructure machine status: %w", err)
1398-
} else if diffStatus.Changed() {
1399-
diff[".status"] = diffStatus.String()
1364+
diff, err := util.NewDefaultDiffer().Diff(obj1, obj2)
1365+
if err != nil {
1366+
return nil, fmt.Errorf("failed to compare Cluster API infrastructure machine templates: %w", err)
14001367
}
14011368

14021369
return diff, nil
14031370
}
14041371

14051372
// compareCAPIMachineSets compares CAPI machineSets a and b, and returns a list of differences, or none if there are none.
1406-
func compareCAPIMachineSets(capiMachineSet1, capiMachineSet2 *clusterv1.MachineSet) (map[string]any, error) {
1407-
diff := make(map[string]any)
1408-
1409-
// Compare metadata
1410-
if diffMetadata, err := util.ObjectMetaEqual(capiMachineSet1.ObjectMeta, capiMachineSet2.ObjectMeta); err != nil {
1411-
return nil, fmt.Errorf("failed to compare Cluster sAPI machine set metadata: %w", err)
1412-
} else if diffMetadata.Changed() {
1413-
diff[".metadata"] = diffMetadata.String()
1414-
}
1415-
1416-
// Compare spec
1417-
if diffSpec, err := util.NewDiffer().Diff(capiMachineSet1.Spec, capiMachineSet2.Spec); err != nil {
1418-
return nil, fmt.Errorf("failed to compare Cluster API machine spec: %w", err)
1419-
} else if diffSpec.Changed() {
1420-
diff[".spec"] = diffSpec.String()
1421-
}
1422-
1423-
// Compare status
1424-
if diffStatus, err := util.CAPIMachineSetStatusEqual(capiMachineSet1.Status, capiMachineSet2.Status); err != nil {
1425-
return nil, fmt.Errorf("failed to compare Cluster API machine set status: %w", err)
1426-
} else if diffStatus.Changed() {
1427-
diff[".status"] = diffStatus.String()
1373+
func compareCAPIMachineSets(capiMachineSet1, capiMachineSet2 *clusterv1.MachineSet) (util.DiffResult, error) {
1374+
diff, err := util.NewDefaultDiffer().Diff(capiMachineSet1, capiMachineSet2)
1375+
if err != nil {
1376+
return nil, fmt.Errorf("failed to compare Cluster API machinesets: %w", err)
14281377
}
14291378

14301379
return diff, nil
14311380
}
14321381

14331382
// compareMAPIMachineSets compares MAPI machineSets a and b, and returns a list of differences, or none if there are none.
1434-
func compareMAPIMachineSets(a, b *mapiv1beta1.MachineSet) (map[string]any, error) {
1435-
diff := make(map[string]any)
1383+
func compareMAPIMachineSets(platform configv1.PlatformType, a, b *mapiv1beta1.MachineSet) (util.DiffResult, error) {
1384+
diff, err := util.NewDefaultDiffer(
1385+
util.WithProviderSpec(platform, []string{"spec", "template", "providerSpec", "value"}, mapi2capi.ProviderSpecFromRawExtension),
14361386

1437-
ps1, err := mapi2capi.AWSProviderSpecFromRawExtension(a.Spec.Template.Spec.ProviderSpec.Value)
1438-
if err != nil {
1439-
return nil, fmt.Errorf("unable to parse first MAPI machine set providerSpec: %w", err)
1440-
}
1387+
// Other status fields to ignore
1388+
util.WithIgnoreField("status", "replicas"),
1389+
util.WithIgnoreField("status", "observedGeneration"),
1390+
util.WithIgnoreField("status", "authoritativeAPI"),
1391+
util.WithIgnoreField("status", "synchronizedGeneration"),
1392+
).Diff(a, b)
14411393

1442-
ps2, err := mapi2capi.AWSProviderSpecFromRawExtension(b.Spec.Template.Spec.ProviderSpec.Value)
14431394
if err != nil {
1444-
return nil, fmt.Errorf("unable to parse second MAPI machine set providerSpec: %w", err)
1445-
}
1446-
1447-
// Sort the tags by name to ensure consistent ordering.
1448-
// On the CAPI side these tags are in a map,
1449-
// so the order is not guaranteed when converting back from a CAPI map to a MAPI slice.
1450-
sort.Slice(ps1.Tags, func(i, j int) bool {
1451-
return ps1.Tags[i].Name < ps1.Tags[j].Name
1452-
})
1453-
1454-
// Sort the tags by name to ensure consistent ordering.
1455-
// On the CAPI side these tags are in a map,
1456-
// so the order is not guaranteed when converting back from a CAPI map to a MAPI slice.
1457-
sort.Slice(ps2.Tags, func(i, j int) bool {
1458-
return ps2.Tags[i].Name < ps2.Tags[j].Name
1459-
})
1460-
1461-
// Compare providerSpec
1462-
if diffProviderSpec, err := util.NewDiffer().Diff(ps1, ps2); err != nil {
1463-
return nil, fmt.Errorf("failed to compare Machine API machine spec: %w", err)
1464-
} else if diffProviderSpec.Changed() {
1465-
diff[".providerSpec"] = diffProviderSpec.String()
1466-
}
1467-
1468-
// Compare metadata
1469-
if diffMetadata, err := util.ObjectMetaEqual(a.ObjectMeta, b.ObjectMeta); err != nil {
1470-
return nil, fmt.Errorf("failed to compare Machine API machine set metadata: %w", err)
1471-
} else if diffMetadata.Changed() {
1472-
diff[".metadata"] = diffMetadata.String()
1473-
}
1474-
1475-
// Compare spec
1476-
if diffSpec, err := util.NewDiffer(
1477-
util.WithIgnoreField("template", "providerSpec", "value"),
1478-
).Diff(a.Spec, b.Spec); err != nil {
1479-
return nil, fmt.Errorf("failed to compare Machine API machine spec: %w", err)
1480-
} else if diffSpec.Changed() {
1481-
diff[".spec"] = diffSpec.String()
1482-
}
1483-
1484-
// Compare status
1485-
if diffStatus, err := util.MAPIMachineSetStatusEqual(a.Status, b.Status); err != nil {
1486-
return nil, fmt.Errorf("failed to compare Machine API machine set status: %w", err)
1487-
} else if diffStatus.Changed() {
1488-
diff[".status"] = diffStatus.String()
1395+
return nil, fmt.Errorf("failed to compare Machine API machinesets: %w", err)
14891396
}
14901397

14911398
return diff, nil
@@ -1542,19 +1449,3 @@ func restoreMAPIFields(existingMAPIMachineSet, convertedMAPIMachineSet *mapiv1be
15421449
// Restore finalizers.
15431450
convertedMAPIMachineSet.SetFinalizers(existingMAPIMachineSet.GetFinalizers())
15441451
}
1545-
1546-
// hasStatusChanges returns true if there are changes to the status.
1547-
func hasStatusChanges(diff map[string]any) bool {
1548-
_, ok := diff[".status"]
1549-
1550-
return ok
1551-
}
1552-
1553-
// hasSpecOrMetadataOrProviderSpecChanges returns true if there are changes to the spec, metadata, or providerSpec.
1554-
func hasSpecOrMetadataOrProviderSpecChanges(diff map[string]any) bool {
1555-
_, ok1 := diff[".spec"]
1556-
_, ok2 := diff[".metadata"]
1557-
_, ok3 := diff[".providerSpec"]
1558-
1559-
return ok1 || ok2 || ok3
1560-
}

pkg/controllers/machinesetsync/machineset_sync_controller_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,18 +1174,17 @@ var _ = Describe("compareMAPIMachineSets", func() {
11741174

11751175
Context("when comparing MachineSets with different instance types", func() {
11761176
It("should detect differences in providerSpec", func() {
1177-
diff, err := compareMAPIMachineSets(mapiMachineSet1, mapiMachineSet2)
1177+
diff, err := compareMAPIMachineSets(configv1.AWSPlatformType, mapiMachineSet1, mapiMachineSet2)
11781178
Expect(err).NotTo(HaveOccurred())
1179-
Expect(diff).To(HaveKey(".providerSpec"))
1180-
Expect(diff[".providerSpec"]).NotTo(BeEmpty())
1179+
Expect(diff.HasProviderSpecChanges()).To(BeTrue())
11811180
})
11821181
})
11831182

11841183
Context("when comparing identical MachineSets", func() {
11851184
It("should detect no differences", func() {
1186-
diff, err := compareMAPIMachineSets(mapiMachineSet1, mapiMachineSet1)
1185+
diff, err := compareMAPIMachineSets(configv1.AWSPlatformType, mapiMachineSet1, mapiMachineSet1)
11871186
Expect(err).NotTo(HaveOccurred())
1188-
Expect(diff).To(BeEmpty())
1187+
Expect(diff.HasChanges()).To(BeFalse())
11891188
})
11901189
})
11911190
})

0 commit comments

Comments
 (0)