diff --git a/pkg/operator/render.go b/pkg/operator/render.go index ddab42002e..41b058d429 100644 --- a/pkg/operator/render.go +++ b/pkg/operator/render.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net" + "strings" "text/template" "github.com/Masterminds/sprig" @@ -67,23 +68,19 @@ func createDiscoveredControllerConfigSpec(infra *configv1.Infrastructure, networ return nil, err } + infraPlatformString := "" + // The PlatformStatus field is set in cluster versions >= 4.2 + // Otherwise use the Platform field + if infra.Status.PlatformStatus != nil { + infraPlatformString = string(infra.Status.PlatformStatus.Type) + } else { + //nolint:staticcheck + infraPlatformString = string(infra.Status.Platform) + } + platform := "none" - //nolint:staticcheck - switch infra.Status.Platform { - case configv1.AWSPlatformType: - platform = "aws" - case configv1.AzurePlatformType: - platform = "azure" - case configv1.BareMetalPlatformType: - platform = "baremetal" - case configv1.GCPPlatformType: - platform = "gcp" - case configv1.OpenStackPlatformType: - platform = "openstack" - case configv1.LibvirtPlatformType: - platform = "libvirt" - case configv1.VSpherePlatformType: - platform = "vsphere" + if infraPlatformString != "" { + platform = strings.ToLower(infraPlatformString) } ccSpec := &mcfgv1.ControllerConfigSpec{ diff --git a/pkg/operator/render_test.go b/pkg/operator/render_test.go index d01bea10ed..4cd0b3e35f 100644 --- a/pkg/operator/render_test.go +++ b/pkg/operator/render_test.go @@ -2,6 +2,7 @@ package operator import ( "fmt" + configv1 "github.com/openshift/api/config/v1" "strings" "testing" ) @@ -102,3 +103,98 @@ func TestRenderAsset(t *testing.T) { }) } } + +func TestCreateDiscoveredControllerConfigSpec(t *testing.T) { + tests := []struct { + Infra *configv1.Infrastructure + Network *configv1.Network + Proxy *configv1.Proxy + Error bool + }{{ + Infra: &configv1.Infrastructure{ + Status: configv1.InfrastructureStatus{ + PlatformStatus: &configv1.PlatformStatus{ + Type: configv1.AWSPlatformType, + }, + EtcdDiscoveryDomain: "tt.testing", + }}, + Network: &configv1.Network{ + Spec: configv1.NetworkSpec{ServiceNetwork: []string{"192.168.1.1/24"}}}, + Proxy: &configv1.Proxy{ + Status: configv1.ProxyStatus{ + HTTPProxy: "test.proxy"}}, + }, { + Infra: &configv1.Infrastructure{ + Status: configv1.InfrastructureStatus{ + PlatformStatus: &configv1.PlatformStatus{ + Type: configv1.AWSPlatformType, + }, + EtcdDiscoveryDomain: "tt.testing", + }}, + Network: &configv1.Network{ + Spec: configv1.NetworkSpec{ServiceNetwork: []string{"192.168.1.1/99999999"}}}, + Error: true, + }, { + Infra: &configv1.Infrastructure{ + Status: configv1.InfrastructureStatus{ + PlatformStatus: &configv1.PlatformStatus{}, + EtcdDiscoveryDomain: "tt.testing", + }}, + Network: &configv1.Network{ + Spec: configv1.NetworkSpec{ServiceNetwork: []string{"192.168.1.1/24"}}}, + }, { + Infra: &configv1.Infrastructure{ + Status: configv1.InfrastructureStatus{ + PlatformStatus: &configv1.PlatformStatus{ + Type: configv1.AWSPlatformType, + }, + EtcdDiscoveryDomain: "tt.testing", + }}, + Network: &configv1.Network{ + Spec: configv1.NetworkSpec{ServiceNetwork: []string{}}}, + Error: true, + }, { + // Test old Platform field instead of PlatformStatus + Infra: &configv1.Infrastructure{ + Status: configv1.InfrastructureStatus{ + Platform: configv1.AWSPlatformType, + EtcdDiscoveryDomain: "tt.testing", + }, + }, + Network: &configv1.Network{ + Spec: configv1.NetworkSpec{ServiceNetwork: []string{"192.168.1.1/24"}}}, + }} + + for idx, test := range tests { + t.Run(fmt.Sprintf("case#%d", idx), func(t *testing.T) { + desc := fmt.Sprintf("Infra(%#v), Network(%#v)", test.Infra, test.Network) + controllerConfigSpec, err := createDiscoveredControllerConfigSpec(test.Infra, test.Network, test.Proxy) + if err != nil { + if !test.Error { + t.Fatalf("%s failed: %s", desc, err.Error()) + } else { + // If Err flag is true and err is found, stop testing + return + } + } + if controllerConfigSpec == nil { + t.Fatalf("Controller config spec did not get initialized") + } else if controllerConfigSpec.Platform == "" { + t.Fatalf("Error setting controller config platform") + } + etcdDomain := controllerConfigSpec.EtcdDiscoveryDomain + testDomain := test.Infra.Status.EtcdDiscoveryDomain + if etcdDomain != testDomain { + t.Fatalf("%s failed: got = %s want = %s", desc, etcdDomain, testDomain) + } + if test.Proxy != nil { + testURL := test.Proxy.Status.HTTPProxy + controllerURL := controllerConfigSpec.Proxy.HTTPProxy + if controllerURL != testURL { + t.Fatalf("%s failed: got = %s want = %s", desc, controllerURL, testURL) + } + } + }) + } + +}