Skip to content
Closed
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
9 changes: 9 additions & 0 deletions pkg/asset/installconfig/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package aws

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/types/aws"
"github.com/openshift/installer/pkg/types/aws/validation"
"github.com/openshift/installer/pkg/version"
Expand All @@ -25,7 +27,14 @@ import (
func Platform() (*aws.Platform, error) {
longRegions := make([]string, 0, len(validation.Regions))
shortRegions := make([]string, 0, len(validation.Regions))
rhcosRegions, err := rhcos.AMIRegions(context.TODO())
if err != nil {
return nil, err
}
for id, location := range validation.Regions {
if _, ok := rhcosRegions[id]; !ok {
continue
}
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
shortRegions = append(shortRegions, id)
}
Expand Down
27 changes: 26 additions & 1 deletion pkg/rhcos/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rhcos

import (
"context"
"sort"
"strings"

"github.com/pkg/errors"
)
Expand All @@ -15,8 +17,31 @@ func AMI(ctx context.Context, region string) (string, error) {

ami, ok := meta.AMIs[region]
if !ok {
return "", errors.Errorf("no RHCOS AMIs found in %s", region)
regions := make([]string, 0, len(meta.AMIs))
for rgn := range meta.AMIs {
regions = append(regions, rgn)
}
sort.Strings(regions)

return "", errors.Errorf("no RHCOS AMIs found in %q (%s)", region, strings.Join(regions, ", "))
}

return ami.HVM, nil
}

// AMIRegions returns a set of AWS regions with HVM AMIs of the Red
// Hat Enterprise Linux CoreOS release.
func AMIRegions(ctx context.Context) (map[string]struct{}, error) {
meta, err := fetchRHCOSBuild(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch RHCOS metadata")
}

exists := struct{}{}
regions := make(map[string]struct{}, len(meta.AMIs))
for region := range meta.AMIs {
regions[region] = exists
}

return regions, nil
}
41 changes: 24 additions & 17 deletions pkg/types/aws/validation/platform.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package validation

import (
"context"
"sort"

"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/types/aws"
)

Expand All @@ -13,29 +15,29 @@ var (
// the short name of the region. The value of the map is the long
// name of the region.
Regions = map[string]string{
//"ap-east-1": "Hong Kong",
"ap-east-1": "Hong Kong",
"ap-northeast-1": "Tokyo",
"ap-northeast-2": "Seoul",
//"ap-northeast-3": "Osaka-Local",
"ap-northeast-3": "Osaka-Local",
"ap-south-1": "Mumbai",
"ap-southeast-1": "Singapore",
"ap-southeast-2": "Sydney",
"ca-central-1": "Central",
//"cn-north-1": "Beijing",
//"cn-northwest-1": "Ningxia",
"eu-central-1": "Frankfurt",
"eu-north-1": "Stockholm",
"eu-west-1": "Ireland",
"eu-west-2": "London",
"eu-west-3": "Paris",
//"me-south-1": "Bahrain",
"sa-east-1": "São Paulo",
"us-east-1": "N. Virginia",
"us-east-2": "Ohio",
//"us-gov-east-1": "AWS GovCloud (US-East)",
//"us-gov-west-1": "AWS GovCloud (US-West)",
"us-west-1": "N. California",
"us-west-2": "Oregon",
"cn-north-1": "Beijing",
"cn-northwest-1": "Ningxia",
"eu-central-1": "Frankfurt",
"eu-north-1": "Stockholm",
"eu-west-1": "Ireland",
"eu-west-2": "London",
"eu-west-3": "Paris",
"me-south-1": "Bahrain",
"sa-east-1": "São Paulo",
"us-east-1": "N. Virginia",
"us-east-2": "Ohio",
"us-gov-east-1": "AWS GovCloud (US-East)",
"us-gov-west-1": "AWS GovCloud (US-West)",
"us-west-1": "N. California",
"us-west-2": "Oregon",
}

validRegionValues = func() []string {
Expand All @@ -55,6 +57,11 @@ func ValidatePlatform(p *aws.Platform, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if _, ok := Regions[p.Region]; !ok {
allErrs = append(allErrs, field.NotSupported(fldPath.Child("region"), p.Region, validRegionValues))
} else if p.AMIID == "" {
_, err := rhcos.AMI(context.TODO(), p.Region)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("region"), p.Region, err.Error()))
}
}
if p.DefaultMachinePlatform != nil {
allErrs = append(allErrs, ValidateMachinePool(p, p.DefaultMachinePlatform, fldPath.Child("defaultMachinePlatform"))...)
Expand Down
26 changes: 19 additions & 7 deletions pkg/types/aws/validation/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,41 @@ func TestValidatePlatform(t *testing.T) {
cases := []struct {
name string
platform *aws.Platform
valid bool
expected string
}{
{
name: "minimal",
platform: &aws.Platform{
Region: "us-east-1",
},
valid: true,
},
{
name: "invalid region",
platform: &aws.Platform{
Region: "bad-region",
},
valid: false,
expected: "^test-path.region: Unsupported value: \"bad-region\": supported values: .*\"us-east-1\"",
},
{
name: "valid region without RHCOS",
platform: &aws.Platform{
Region: "us-gov-west-1",
},
expected: "^test-path.region: Invalid value: \"us-gov-west-1\": no RHCOS AMIs found in \"us-gov-west-1\" (.*us-east-1.*)$",
},
{
name: "valid region without RHCOS, but with an explicit AMI",
platform: &aws.Platform{
AMIID: "ami-123",
Region: "us-gov-west-1",
},
},
{
name: "valid machine pool",
platform: &aws.Platform{
Region: "us-east-1",
DefaultMachinePlatform: &aws.MachinePool{},
},
valid: true,
},
{
name: "invalid machine pool",
Expand All @@ -47,16 +59,16 @@ func TestValidatePlatform(t *testing.T) {
},
},
},
valid: false,
expected: "^test-path.defaultMachinePlatform.iops: Invalid value: -10: Storage IOPS must be positive$",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := ValidatePlatform(tc.platform, field.NewPath("test-path")).ToAggregate()
if tc.valid {
if tc.expected == "" {
assert.NoError(t, err)
} else {
assert.Error(t, err)
assert.Regexp(t, tc.expected, err)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/validation/installconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestValidateInstallConfig(t *testing.T) {
}
return c
}(),
expectedError: `^platform\.aws\.region: Unsupported value: "": supported values: "ap-northeast-1", "ap-northeast-2", "ap-south-1", "ap-southeast-1", "ap-southeast-2", "ca-central-1", "eu-central-1", "eu-north-1", "eu-west-1", "eu-west-2", "eu-west-3", "sa-east-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"$`,
expectedError: `^platform\.aws\.region: Unsupported value: "": supported values: .*"us-east-1"`,
},
{
name: "valid libvirt platform",
Expand Down