diff --git a/pkg/asset/installconfig/platform.go b/pkg/asset/installconfig/platform.go index 587e66fd08c..753d463f2e2 100644 --- a/pkg/asset/installconfig/platform.go +++ b/pkg/asset/installconfig/platform.go @@ -20,18 +20,7 @@ import ( "github.com/openshift/installer/pkg/types/openstack" ) -const ( - // AWSPlatformType is used to install on AWS. - AWSPlatformType = "aws" - // OpenStackPlatformType is used to install on OpenStack. - OpenStackPlatformType = "openstack" - // LibvirtPlatformType is used to install of libvirt. - LibvirtPlatformType = "libvirt" -) - var ( - validPlatforms = []string{AWSPlatformType, OpenStackPlatformType, LibvirtPlatformType} - validAWSRegions = map[string]string{ "ap-northeast-1": "Tokyo", "ap-northeast-2": "Seoul", @@ -78,19 +67,19 @@ func (a *platform) Generate(asset.Parents) error { } switch platform { - case AWSPlatformType: + case aws.Name: aws, err := a.awsPlatform() if err != nil { return err } a.AWS = aws - case OpenStackPlatformType: + case openstack.Name: openstack, err := a.openstackPlatform() if err != nil { return err } a.OpenStack = openstack - case LibvirtPlatformType: + case libvirt.Name: libvirt, err := a.libvirtPlatform() if err != nil { return err @@ -109,18 +98,17 @@ func (a *platform) Name() string { } func (a *platform) queryUserForPlatform() (string, error) { - sort.Strings(validPlatforms) return asset.GenerateUserProvidedAsset( "Platform", &survey.Question{ Prompt: &survey.Select{ Message: "Platform", - Options: validPlatforms, + Options: types.PlatformNames, }, Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error { choice := ans.(string) - i := sort.SearchStrings(validPlatforms, choice) - if i == len(validPlatforms) || validPlatforms[i] != choice { + i := sort.SearchStrings(types.PlatformNames, choice) + if i == len(types.PlatformNames) || types.PlatformNames[i] != choice { return errors.Errorf("invalid platform %q", choice) } return nil @@ -259,7 +247,7 @@ func (a *platform) openstackPlatform() (*openstack.Platform, error) { "OPENSHIFT_INSTALL_OPENSTACK_EXTERNAL_NETWORK", ) if err != nil { - return nil, errors.Wrapf(err, "failed to Marshal %s platform", OpenStackPlatformType) + return nil, errors.Wrapf(err, "failed to Marshal %s platform", openstack.Name) } return &openstack.Platform{ diff --git a/pkg/asset/machines/aws/machines.go b/pkg/asset/machines/aws/machines.go index 775794b4510..1c7a9dc419a 100644 --- a/pkg/asset/machines/aws/machines.go +++ b/pkg/asset/machines/aws/machines.go @@ -19,10 +19,10 @@ import ( // Machines returns a list of machines for a machinepool. func Machines(config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]clusterapi.Machine, error) { - if configPlatform := config.Platform.Name(); configPlatform != types.PlatformNameAWS { + if configPlatform := config.Platform.Name(); configPlatform != aws.Name { return nil, fmt.Errorf("non-AWS configuration: %q", configPlatform) } - if poolPlatform := pool.Platform.Name(); poolPlatform != types.PlatformNameAWS { + if poolPlatform := pool.Platform.Name(); poolPlatform != aws.Name { return nil, fmt.Errorf("non-AWS machine-pool: %q", poolPlatform) } clustername := config.ObjectMeta.Name diff --git a/pkg/asset/machines/aws/machinesets.go b/pkg/asset/machines/aws/machinesets.go index 617ff251cdd..65af0057c7c 100644 --- a/pkg/asset/machines/aws/machinesets.go +++ b/pkg/asset/machines/aws/machinesets.go @@ -9,15 +9,16 @@ import ( clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" "github.com/openshift/installer/pkg/types" + "github.com/openshift/installer/pkg/types/aws" "github.com/pkg/errors" ) // MachineSets returns a list of machinesets for a machinepool. func MachineSets(config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]clusterapi.MachineSet, error) { - if configPlatform := config.Platform.Name(); configPlatform != types.PlatformNameAWS { + if configPlatform := config.Platform.Name(); configPlatform != aws.Name { return nil, fmt.Errorf("non-AWS configuration: %q", configPlatform) } - if poolPlatform := pool.Platform.Name(); poolPlatform != types.PlatformNameAWS { + if poolPlatform := pool.Platform.Name(); poolPlatform != aws.Name { return nil, fmt.Errorf("non-AWS machine-pool: %q", poolPlatform) } clustername := config.ObjectMeta.Name diff --git a/pkg/asset/machines/libvirt/machines.go b/pkg/asset/machines/libvirt/machines.go index d2b5a0ae585..e1219358c3b 100644 --- a/pkg/asset/machines/libvirt/machines.go +++ b/pkg/asset/machines/libvirt/machines.go @@ -15,11 +15,11 @@ import ( // Machines returns a list of machines for a machinepool. func Machines(config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]clusterapi.Machine, error) { - if configPlatform := config.Platform.Name(); configPlatform != types.PlatformNameLibvirt { + if configPlatform := config.Platform.Name(); configPlatform != libvirt.Name { return nil, fmt.Errorf("non-Libvirt configuration: %q", configPlatform) } // FIXME: empty is a valid case for Libvirt as we don't use it. - if poolPlatform := pool.Platform.Name(); poolPlatform != "" && poolPlatform != types.PlatformNameLibvirt { + if poolPlatform := pool.Platform.Name(); poolPlatform != "" && poolPlatform != libvirt.Name { return nil, fmt.Errorf("non-Libvirt machine-pool: %q", poolPlatform) } clustername := config.ObjectMeta.Name diff --git a/pkg/asset/machines/libvirt/machinesets.go b/pkg/asset/machines/libvirt/machinesets.go index baea64e0b54..2d8dace7038 100644 --- a/pkg/asset/machines/libvirt/machinesets.go +++ b/pkg/asset/machines/libvirt/machinesets.go @@ -10,15 +10,16 @@ import ( clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" "github.com/openshift/installer/pkg/types" + "github.com/openshift/installer/pkg/types/libvirt" ) // MachineSets returns a list of machinesets for a machinepool. func MachineSets(config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]clusterapi.MachineSet, error) { - if configPlatform := config.Platform.Name(); configPlatform != types.PlatformNameLibvirt { + if configPlatform := config.Platform.Name(); configPlatform != libvirt.Name { return nil, fmt.Errorf("non-Libvirt configuration: %q", configPlatform) } // FIXME: empty is a valid case for Libvirt as we don't use it. - if poolPlatform := pool.Platform.Name(); poolPlatform != "" && poolPlatform != types.PlatformNameLibvirt { + if poolPlatform := pool.Platform.Name(); poolPlatform != "" && poolPlatform != libvirt.Name { return nil, fmt.Errorf("non-Libvirt machine-pool: %q", poolPlatform) } clustername := config.ObjectMeta.Name diff --git a/pkg/types/aws/doc.go b/pkg/types/aws/doc.go index 6bcbe1c6d09..6d494c47651 100644 --- a/pkg/types/aws/doc.go +++ b/pkg/types/aws/doc.go @@ -1,3 +1,6 @@ // Package aws contains AWS-specific structures for installer // configuration and management. package aws + +// Name is name for the AWS platform. +const Name string = "aws" diff --git a/pkg/types/installconfig.go b/pkg/types/installconfig.go index 6515f131e95..5d2391aa23e 100644 --- a/pkg/types/installconfig.go +++ b/pkg/types/installconfig.go @@ -9,13 +9,14 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - // PlatformNameAWS is name for AWS platform. - PlatformNameAWS string = "aws" - // PlatformNameOpenstack is name for Openstack platform. - PlatformNameOpenstack string = "openstack" - // PlatformNameLibvirt is name for Libvirt platform. - PlatformNameLibvirt string = "libvirt" +var ( + // PlatformNames is a slice with all the supported platform names in + // alphabetical order. + PlatformNames = []string{ + aws.Name, + libvirt.Name, + openstack.Name, + } ) // InstallConfig is the configuration for an OpenShift install. @@ -90,13 +91,13 @@ func (p *Platform) Name() string { return "" } if p.AWS != nil { - return PlatformNameAWS + return aws.Name } if p.Libvirt != nil { - return PlatformNameLibvirt + return libvirt.Name } if p.OpenStack != nil { - return PlatformNameOpenstack + return openstack.Name } return "" } diff --git a/pkg/types/installconfig_test.go b/pkg/types/installconfig_test.go new file mode 100644 index 00000000000..01bf6c7b86a --- /dev/null +++ b/pkg/types/installconfig_test.go @@ -0,0 +1,15 @@ +package types + +import ( + "sort" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPlatformNamesSorted(t *testing.T) { + sorted := make([]string, len(PlatformNames)) + copy(sorted, PlatformNames) + sort.Strings(sorted) + assert.Equal(t, sorted, PlatformNames) +} diff --git a/pkg/types/libvirt/doc.go b/pkg/types/libvirt/doc.go index 04affd481bc..0c2ded568f1 100644 --- a/pkg/types/libvirt/doc.go +++ b/pkg/types/libvirt/doc.go @@ -1,3 +1,6 @@ // Package libvirt contains libvirt-specific structures for // installer configuration and management. package libvirt + +// Name is the name for the libvirt platform. +const Name string = "libvirt" diff --git a/pkg/types/machinepools.go b/pkg/types/machinepools.go index 3d1e1459087..89d5a985464 100644 --- a/pkg/types/machinepools.go +++ b/pkg/types/machinepools.go @@ -40,13 +40,13 @@ func (p *MachinePoolPlatform) Name() string { return "" } if p.AWS != nil { - return PlatformNameAWS + return aws.Name } if p.Libvirt != nil { - return PlatformNameLibvirt + return libvirt.Name } if p.OpenStack != nil { - return PlatformNameOpenstack + return openstack.Name } return "" } diff --git a/pkg/types/openstack/doc.go b/pkg/types/openstack/doc.go index 94c5cfe1433..d7fc4d6a88d 100644 --- a/pkg/types/openstack/doc.go +++ b/pkg/types/openstack/doc.go @@ -1,3 +1,6 @@ // Package openstack contains OpenStack-specific structures for // installer configuration and management. package openstack + +// Name is the name for the Openstack platform. +const Name string = "openstack"