From c431c8939b70e139f6ec04a0dde4891e353cff66 Mon Sep 17 00:00:00 2001 From: Mark Hamzy Date: Mon, 22 Jan 2024 15:24:40 -0600 Subject: [PATCH] Merge functionality with IPI The IPI installer has a file (pkg/types/powervs/powervs_regions.go) which contains similiar data and functions. We should merge the two and then use this library in IPI. --- .github/workflows/go.yaml | 2 +- go.mod | 6 ++- go.sum | 2 + region.go | 85 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 go.sum diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index b61e411..8fba2f4 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -16,7 +16,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.19 + go-version: 1.21 - name: Build run: go build -v ./... diff --git a/go.mod b/go.mod index c2d457b..1680506 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module github.com/ppc64le-cloud/powervs-utils -go 1.15 +go 1.21 + +toolchain go1.21.4 + +require k8s.io/apimachinery v0.29.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8488d5f --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc= +k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= diff --git a/region.go b/region.go index f6ba24c..c573fc8 100644 --- a/region.go +++ b/region.go @@ -3,6 +3,8 @@ package utils import ( "fmt" "strings" + + "k8s.io/apimachinery/pkg/util/sets" ) func GetRegion(zone string) (region string, err error) { @@ -46,6 +48,7 @@ type Region struct { VPCRegion string COSRegion string Zones []string + SysTypes []string } // Regions provides a mapping between Power VS and IBM Cloud VPC and IBM COS regions. @@ -54,7 +57,11 @@ var Regions = map[string]Region{ Description: "Dallas, USA", VPCRegion: "us-south", COSRegion: "us-south", - Zones: []string{"dal12"}, + Zones: []string{ + "dal10", + "dal12", + }, + SysTypes: []string{"s922", "e980"}, }, "eu-de": { Description: "Frankfurt, Germany", @@ -64,6 +71,7 @@ var Regions = map[string]Region{ "eu-de-1", "eu-de-2", }, + SysTypes: []string{"s922", "e980"}, }, "lon": { Description: "London, UK.", @@ -73,27 +81,31 @@ var Regions = map[string]Region{ "lon04", "lon06", }, + SysTypes: []string{"s922", "e980"}, }, "mad": { Description: "Madrid, Spain", VPCRegion: "eu-es", - COSRegion: "eu-es", + COSRegion: "eu-de", // @HACK - PowerVS says COS not supported in this region Zones: []string{ "mad02", "mad04", }, + SysTypes: []string{"s1022"}, }, "mon": { Description: "Montreal, Canada", VPCRegion: "ca-tor", COSRegion: "ca-tor", Zones: []string{"mon01"}, + SysTypes: []string{"s922", "e980"}, }, "osa": { Description: "Osaka, Japan", VPCRegion: "jp-osa", COSRegion: "jp-osa", Zones: []string{"osa21"}, + SysTypes: []string{"s922", "e980"}, }, "syd": { Description: "Sydney, Australia", @@ -103,6 +115,7 @@ var Regions = map[string]Region{ "syd04", "syd05", }, + SysTypes: []string{"s922", "e980"}, }, "sao": { Description: "São Paulo, Brazil", @@ -112,18 +125,21 @@ var Regions = map[string]Region{ "sao01", "sao04", }, + SysTypes: []string{"s922", "e980"}, }, "tok": { Description: "Tokyo, Japan", VPCRegion: "jp-tok", COSRegion: "jp-tok", Zones: []string{"tok04"}, + SysTypes: []string{"s922", "e980"}, }, "us-east": { Description: "Washington DC, USA", VPCRegion: "us-east", COSRegion: "us-east", Zones: []string{"us-east"}, + SysTypes: []string{}, // Missing }, "wdc": { Description: "Washington DC, USA", @@ -133,9 +149,21 @@ var Regions = map[string]Region{ "wdc06", "wdc07", }, + SysTypes: []string{"s922", "e980"}, }, } +// COSRegionForVPCRegion returns the corresponding COS region for the given VPC region +func COSRegionForVPCRegion(vpcRegion string) (string, error) { + for r := range Regions { + if vpcRegion == Regions[r].VPCRegion { + return Regions[r].COSRegion, nil + } + } + + return "", fmt.Errorf("COS region corresponding to a VPC region %s not found ", vpcRegion) +} + // VPCRegionForPowerVSRegion returns the VPC region for the specified PowerVS region. func VPCRegionForPowerVSRegion(region string) (string, error) { if r, ok := Regions[region]; ok { @@ -182,3 +210,56 @@ func RegionShortNames() []string { } return keys } + +// ValidateZone validates that the given zone is known/tested. +func ValidateZone(zone string) bool { + for r := range Regions { + for z := range Regions[r].Zones { + if zone == Regions[r].Zones[z] { + return true + } + } + } + return false +} + +// ZoneNames returns the list of zone names. +func ZoneNames() []string { + zones := []string{} + for r := range Regions { + for z := range Regions[r].Zones { + zones = append(zones, Regions[r].Zones[z]) + } + } + return zones +} + +// RegionFromZone returns the region name for a given zone name. +func RegionFromZone(zone string) string { + for r := range Regions { + for z := range Regions[r].Zones { + if zone == Regions[r].Zones[z] { + return r + } + } + } + return "" +} + +// AvailableSysTypes returns the default system type for the zone. +func AvailableSysTypes(region string) ([]string, error) { + knownRegion, ok := Regions[region] + if !ok { + return nil, fmt.Errorf("unknown region name provided") + } + return knownRegion.SysTypes, nil +} + +// AllKnownSysTypes returns aggregated known system types from all regions. +func AllKnownSysTypes() sets.Set[string] { + sysTypes := sets.New[string]() + for _, region := range Regions { + sysTypes.Insert(region.SysTypes...) + } + return sysTypes +}