diff --git a/pkg/controller/machinepool/ibmcloudactuator_test.go b/pkg/controller/machinepool/ibmcloudactuator_test.go index ed784eb5cc..9ae1f32519 100644 --- a/pkg/controller/machinepool/ibmcloudactuator_test.go +++ b/pkg/controller/machinepool/ibmcloudactuator_test.go @@ -10,7 +10,11 @@ import ( "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + configv1 "github.com/openshift/api/config/v1" + machinev1beta1 "github.com/openshift/api/machine/v1beta1" ibmcloudprovider "github.com/openshift/machine-api-provider-ibmcloud/pkg/apis/ibmcloudprovider/v1" hivev1 "github.com/openshift/hive/apis/hive/v1" @@ -168,6 +172,94 @@ func TestIBMCloudActuator(t *testing.T) { } } +func TestIBMCloudMatchMachineSets(t *testing.T) { + tests := []struct { + name string + generated *machinev1beta1.MachineSet + remote *machinev1beta1.MachineSet + expectMatch bool + expectErr bool + }{ + { + name: "Zones match", + generated: testIBMCloudMachineSet("1"), + remote: testIBMCloudMachineSet("1"), + expectMatch: true, + }, + { + name: "Zones do not match", + generated: testIBMCloudMachineSet("1"), + remote: testIBMCloudMachineSet("2"), + }, + { + name: "bogus generated mset", + generated: &machinev1beta1.MachineSet{ + ObjectMeta: v1.ObjectMeta{ + Labels: map[string]string{ + machinePoolNameLabel: "a-pool", + }, + }, + }, + remote: testIBMCloudMachineSet("2"), + expectErr: true, + }, + { + name: "bogus remote mset", + generated: testIBMCloudMachineSet("2"), + remote: &machinev1beta1.MachineSet{ + ObjectMeta: v1.ObjectMeta{ + Labels: map[string]string{ + machinePoolNameLabel: "a-pool", + }, + }, + }, + expectErr: true, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + infra := &configv1.Infrastructure{ + Spec: configv1.InfrastructureSpec{ + PlatformSpec: configv1.PlatformSpec{ + Type: configv1.IBMCloudPlatformType, + }, + }, + } + match, err := matchMachineSets(test.generated, *test.remote, infra, log.New()) + if test.expectErr { + assert.Error(t, err, "expected error for test case") + } else { + assert.NoError(t, err, "unexpected error for test case") + assert.Equal(t, test.expectMatch, match, "unexpected match result") + } + }) + } +} + +// This is super simple and fit-for-purpose right now. +func testIBMCloudMachineSet(zone string) *machinev1beta1.MachineSet { + return &machinev1beta1.MachineSet{ + ObjectMeta: v1.ObjectMeta{ + Labels: map[string]string{ + machinePoolNameLabel: "a-pool", + }, + }, + Spec: machinev1beta1.MachineSetSpec{ + Template: machinev1beta1.MachineTemplateSpec{ + Spec: machinev1beta1.MachineSpec{ + ProviderSpec: machinev1beta1.ProviderSpec{ + Value: &runtime.RawExtension{ + Object: &ibmcloudprovider.IBMCloudMachineProviderSpec{ + Zone: zone, + }, + }, + }, + }, + }, + }, + } +} + func testIBMCloudPool() *hivev1.MachinePool { p := testMachinePool() p.Spec.Platform = hivev1.MachinePoolPlatform{ diff --git a/pkg/controller/machinepool/machinepool_controller.go b/pkg/controller/machinepool/machinepool_controller.go index 1787746ec6..bd8cfda998 100644 --- a/pkg/controller/machinepool/machinepool_controller.go +++ b/pkg/controller/machinepool/machinepool_controller.go @@ -38,6 +38,7 @@ import ( cpms "github.com/openshift/cluster-control-plane-machine-set-operator/pkg/machineproviders/providers/openshift/machine/v1beta1/providerconfig" installertypes "github.com/openshift/installer/pkg/types" "github.com/openshift/library-go/pkg/operator/resource/resourcemerge" + ibmcloudprovider "github.com/openshift/machine-api-provider-ibmcloud/pkg/apis/ibmcloudprovider/v1" hivev1 "github.com/openshift/hive/apis/hive/v1" "github.com/openshift/hive/pkg/awsclient" @@ -685,11 +686,25 @@ func matchFailureDomains(gMS *machineapi.MachineSet, rMS machineapi.MachineSet, if gSpec.ProviderSpec.Value.Raw == nil { gSpec.ProviderSpec.Value.Raw, err = json.Marshal(gMS.Spec.Template.Spec.ProviderSpec.Value.Object) if err != nil { - logger.WithError(err).Error("unable to marshal generate ms provider spec object to raw value") + logger.WithError(err).Error("unable to marshal generated ms provider spec object to raw value") return false, err } } + // SNOWFLAKE! CPMS does not support IBMCloud, so we have to roll our own matcher. HIVE-3002. + if infrastructure.Spec.PlatformSpec.Type == configv1.IBMCloudPlatformType { + rPS := &ibmcloudprovider.IBMCloudMachineProviderSpec{} + gPS := &ibmcloudprovider.IBMCloudMachineProviderSpec{} + // Non-strict + if err := json.Unmarshal(rSpec.ProviderSpec.Value.Raw, rPS); err != nil { + return false, errors.Wrap(err, "failed to unmarshal providerSpec for remote MachineSet") + } + if err := json.Unmarshal(gSpec.ProviderSpec.Value.Raw, gPS); err != nil { + return false, errors.Wrap(err, "failed to unmarshal providerSpec for generated MachineSet") + } + return rPS.Zone == gPS.Zone, nil + } + // - The provider config funcs take a different kind of logger. Convert. logr := logrus.NewLogr(logger) rMS_providerconfig, err := cpms.NewProviderConfigFromMachineSpec(logr, rSpec, infrastructure)