Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion data/data/openstack/bootstrap/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ variable "ignition" {

variable "flavor_name" {
type = "string"
default = "m1.medium"
description = "The Nova flavor for the bootstrap node."
}

Expand Down
1 change: 0 additions & 1 deletion data/data/openstack/variables-openstack.tf
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ EOF

variable "openstack_master_flavor_name" {
type = "string"
default = "m1.medium"
description = "Instance size for the master node(s). Example: `m1.medium`."
}

Expand Down
30 changes: 29 additions & 1 deletion pkg/asset/installconfig/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,34 @@ func Platform() (*openstack.Platform, error) {
},
}, &extNet)
if err != nil {
return nil, errors.Wrapf(err, "failed to Marshal %s platform", openstack.Name)
return nil, err
}

flavorNames, err := validValuesFetcher.GetFlavorNames(cloud)
if err != nil {
return nil, err
}
sort.Strings(flavorNames)
var flavor string
err = survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "FlavorName",
Help: "The OpenStack compute flavor to use for servers. A flavor with at least 4 GB RAM is recommended.",
Options: flavorNames,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
value := ans.(string)
i := sort.SearchStrings(flavorNames, value)
if i == len(flavorNames) || flavorNames[i] != value {
return errors.Errorf("invalid flavor name %q, should be one of %+v", value, strings.Join(flavorNames, ", "))
}
return nil
}),
},
}, &flavor)
if err != nil {
return nil, err
}

return &openstack.Platform{
Expand All @@ -138,5 +165,6 @@ func Platform() (*openstack.Platform, error) {
BaseImage: image,
Cloud: cloud,
ExternalNetwork: extNet,
FlavorName: flavor,
}, nil
}
2 changes: 1 addition & 1 deletion pkg/asset/machines/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *Master) Generate(dependencies asset.Parents) error {
Instances: instances,
Image: ic.Platform.OpenStack.BaseImage,
Region: ic.Platform.OpenStack.Region,
Machine: defaultOpenStackMachinePoolPlatform(),
Machine: defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName),
}

tags := map[string]string{
Expand Down
6 changes: 3 additions & 3 deletions pkg/asset/machines/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func defaultAWSMachinePoolPlatform() awstypes.MachinePool {
}
}

func defaultOpenStackMachinePoolPlatform() openstacktypes.MachinePool {
func defaultOpenStackMachinePoolPlatform(flavor string) openstacktypes.MachinePool {
return openstacktypes.MachinePool{
FlavorName: "m1.medium",
FlavorName: flavor,
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
Replicas: numOfWorkers,
Image: ic.Platform.OpenStack.BaseImage,
Region: ic.Platform.OpenStack.Region,
Machine: defaultOpenStackMachinePoolPlatform(),
Machine: defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName),
}

tags := map[string]string{
Expand Down
4 changes: 4 additions & 0 deletions pkg/asset/mock/filefetcher_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/tfvars/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TFVars(cfg *types.InstallConfig, bootstrapIgn, masterIgn string) ([]byte, e
}
config.OpenStack.Credentials.Cloud = cfg.Platform.OpenStack.Cloud
config.OpenStack.ExternalNetwork = cfg.Platform.OpenStack.ExternalNetwork
config.OpenStack.Master.FlavorName = cfg.Platform.OpenStack.FlavorName
}

return json.MarshalIndent(config, "", " ")
Expand Down
4 changes: 4 additions & 0 deletions pkg/types/openstack/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ type Platform struct {
// ExternalNetwork
// The OpenStack external network to be used for installation.
ExternalNetwork string `json:"externalNetwork"`

// FlavorName
// The OpenStack compute flavor to use for servers.
FlavorName string `json:"computeFlavor"`
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/types/openstack/validation/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func ValidatePlatform(p *openstack.Platform, fldPath *field.Path, fetcher ValidV
} else if !isValidValue(p.ExternalNetwork, validNetworks) {
allErrs = append(allErrs, field.NotSupported(fldPath.Child("externalNetwork"), p.ExternalNetwork, validNetworks))
}
validFlavors, err := fetcher.GetFlavorNames(p.Cloud)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath.Child("computeFlavor"), errors.New("could not retrieve valid flavors")))
} else if !isValidValue(p.FlavorName, validFlavors) {
allErrs = append(allErrs, field.NotSupported(fldPath.Child("computeFlavor"), p.FlavorName, validFlavors))
}
}
if p.DefaultMachinePlatform != nil {
allErrs = append(allErrs, ValidateMachinePool(p.DefaultMachinePlatform, fldPath.Child("defaultMachinePlatform"))...)
Expand Down
17 changes: 17 additions & 0 deletions pkg/types/openstack/validation/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func validPlatform() *openstack.Platform {
BaseImage: "test-image",
Cloud: "test-cloud",
ExternalNetwork: "test-network",
FlavorName: "test-flavor",
}
}

Expand All @@ -31,6 +32,7 @@ func TestValidatePlatform(t *testing.T) {
noRegions bool
noImages bool
noNetworks bool
noFlavors bool
valid bool
}{
{
Expand Down Expand Up @@ -107,6 +109,12 @@ func TestValidatePlatform(t *testing.T) {
noNetworks: true,
valid: false,
},
{
name: "flavors fetch failure",
platform: validPlatform(),
noFlavors: true,
valid: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -148,6 +156,15 @@ func TestValidatePlatform(t *testing.T) {
Return([]string{"test-network"}, nil).
MaxTimes(1)
}
if tc.noFlavors {
fetcher.EXPECT().GetFlavorNames(tc.platform.Cloud).
Return(nil, errors.New("no flavors")).
MaxTimes(1)
} else {
fetcher.EXPECT().GetFlavorNames(tc.platform.Cloud).
Return([]string{"test-flavor"}, nil).
MaxTimes(1)
}
err := ValidatePlatform(tc.platform, field.NewPath("test-path"), fetcher).ToAggregate()
if tc.valid {
assert.NoError(t, err)
Expand Down
31 changes: 31 additions & 0 deletions pkg/types/openstack/validation/realvalidvaluesfetcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validation

import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/identity/v3/regions"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
Expand Down Expand Up @@ -118,3 +119,33 @@ func (f realValidValuesFetcher) GetNetworkNames(cloud string) ([]string, error)

return networkNames, nil
}

// GetFlavorNames gets a list of valid flavor names.
func (f realValidValuesFetcher) GetFlavorNames(cloud string) ([]string, error) {
opts := &clientconfig.ClientOpts{
Cloud: cloud,
}

conn, err := clientconfig.NewServiceClient("compute", opts)
if err != nil {
return nil, err
}

listOpts := flavors.ListOpts{}
allPages, err := flavors.ListDetail(conn, listOpts).AllPages()
if err != nil {
return nil, err
}

allFlavors, err := flavors.ExtractFlavors(allPages)
if err != nil {
return nil, err
}

flavorNames := make([]string, len(allFlavors))
for i, flavor := range allFlavors {
flavorNames[i] = flavor.Name
}

return flavorNames, nil
}
2 changes: 2 additions & 0 deletions pkg/types/openstack/validation/validvaluesfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ type ValidValuesFetcher interface {
GetImageNames(cloud string) ([]string, error)
// GetNetworkNames gets the valid network names.
GetNetworkNames(cloud string) ([]string, error)
// GetFlavorNames gets the valid flavor names.
GetFlavorNames(cloud string) ([]string, error)
}
2 changes: 2 additions & 0 deletions pkg/types/validation/installconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func TestValidateInstallConfig(t *testing.T) {
BaseImage: "test-image",
Cloud: "test-cloud",
ExternalNetwork: "test-network",
FlavorName: "test-flavor",
},
}
return c
Expand Down Expand Up @@ -287,6 +288,7 @@ func TestValidateInstallConfig(t *testing.T) {
fetcher.EXPECT().GetRegionNames(gomock.Any()).Return([]string{"test-region"}, nil).AnyTimes()
fetcher.EXPECT().GetImageNames(gomock.Any()).Return([]string{"test-image"}, nil).AnyTimes()
fetcher.EXPECT().GetNetworkNames(gomock.Any()).Return([]string{"test-network"}, nil).AnyTimes()
fetcher.EXPECT().GetFlavorNames(gomock.Any()).Return([]string{"test-flavor"}, nil).AnyTimes()

err := ValidateInstallConfig(tc.installConfig, fetcher).ToAggregate()
if tc.valid {
Expand Down