Skip to content

Commit f7701d1

Browse files
committed
Prepare for varying SysTypes for new datacenters
Signed-off-by: Hiro Miyamoto <[email protected]>
1 parent 7661345 commit f7701d1

File tree

7 files changed

+150
-5
lines changed

7 files changed

+150
-5
lines changed

pkg/asset/cluster/tfvars.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,17 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
877877
vpcZone = fmt.Sprintf("%s-%d", vpcRegion, rand.Intn(2)+1) //nolint:gosec // we don't need a crypto secure number
878878
}
879879

880+
cpStanza := installConfig.Config.ControlPlane
881+
if cpStanza == nil || cpStanza.Platform.PowerVS == nil || cpStanza.Platform.PowerVS.SysType == "" {
882+
sysTypes, err := powervs.AvailableSysTypes(installConfig.Config.PowerVS.Region)
883+
if err != nil {
884+
return err
885+
}
886+
for i := range masters {
887+
masterConfigs[i].SystemType = sysTypes[0]
888+
}
889+
}
890+
880891
transitGatewayEnabled := powervsconfig.TransitGatewayEnabledZone(installConfig.Config.Platform.PowerVS.Zone)
881892

882893
osImage := strings.SplitN(string(*rhcosImage), "/", 2)

pkg/asset/installconfig/platformprovisioncheck.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ func (a *PlatformProvisionCheck) Generate(dependencies asset.Parents) error {
179179
if err != nil {
180180
return err
181181
}
182+
183+
err = powervsconfig.ValidateSystemTypeForRegion(client, ic.Config)
184+
if err != nil {
185+
return err
186+
}
182187
case external.Name, libvirt.Name, none.Name:
183188
// no special provisioning requirements to check
184189
case nutanix.Name:

pkg/asset/installconfig/powervs/validation.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,26 @@ func ValidateResourceGroup(client API, ic *types.InstallConfig) error {
253253

254254
return nil
255255
}
256+
257+
// ValidateSystemTypeForRegion checks if the specified sysType is available in the target region.
258+
func ValidateSystemTypeForRegion(client API, ic *types.InstallConfig) error {
259+
if ic.ControlPlane == nil || ic.ControlPlane.Platform.PowerVS == nil || ic.ControlPlane.Platform.PowerVS.SysType == "" {
260+
return nil
261+
}
262+
availableOnes, err := powervstypes.AvailableSysTypes(ic.PowerVS.Region)
263+
if err != nil {
264+
return fmt.Errorf("failed to obtain available SysTypes for: %s", ic.PowerVS.Region)
265+
}
266+
requested := ic.ControlPlane.Platform.PowerVS.SysType
267+
found := false
268+
for i := range availableOnes {
269+
if requested == availableOnes[i] {
270+
found = true
271+
break
272+
}
273+
}
274+
if found {
275+
return nil
276+
}
277+
return fmt.Errorf("%s is not available in: %s", requested, ic.PowerVS.Region)
278+
}

pkg/asset/installconfig/powervs/validation_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ var (
130130
"disaster-recover-site": true,
131131
"power-vpn-connections": false,
132132
}
133+
defaultSysType = "s922"
134+
newSysType = "s1022"
135+
invalidRegion = "foo"
133136
)
134137

135138
func validInstallConfig() *types.InstallConfig {
@@ -556,6 +559,89 @@ func TestValidatePERAvailability(t *testing.T) {
556559
}
557560
}
558561

562+
func TestValidateSystemTypeForRegion(t *testing.T) {
563+
cases := []struct {
564+
name string
565+
edits editFunctions
566+
errorMsg string
567+
}{
568+
{
569+
name: "Unknown Region specified",
570+
edits: editFunctions{
571+
func(ic *types.InstallConfig) {
572+
ic.Platform.PowerVS.Region = invalidRegion
573+
ic.ControlPlane.Platform.PowerVS = validMachinePool()
574+
ic.ControlPlane.Platform.PowerVS.SysType = defaultSysType
575+
},
576+
},
577+
errorMsg: fmt.Sprintf("failed to obtain available SysTypes for: %s", invalidRegion),
578+
},
579+
{
580+
name: "No Platform block",
581+
edits: editFunctions{
582+
func(ic *types.InstallConfig) {
583+
ic.ControlPlane.Platform.PowerVS = nil
584+
},
585+
},
586+
errorMsg: "",
587+
},
588+
{
589+
name: "Structure present, but no SysType specified",
590+
edits: editFunctions{
591+
func(ic *types.InstallConfig) {
592+
ic.ControlPlane.Platform.PowerVS = validMachinePool()
593+
},
594+
},
595+
errorMsg: "",
596+
},
597+
{
598+
name: "Unavailable SysType specified for Dallas Region",
599+
edits: editFunctions{
600+
func(ic *types.InstallConfig) {
601+
ic.Platform.PowerVS.Region = validRegion
602+
ic.ControlPlane.Platform.PowerVS = validMachinePool()
603+
ic.ControlPlane.Platform.PowerVS.SysType = newSysType
604+
},
605+
},
606+
errorMsg: fmt.Sprintf("%s is not available in: %s", newSysType, validRegion),
607+
},
608+
{
609+
name: "Good Region/SysType combo specified",
610+
edits: editFunctions{
611+
func(ic *types.InstallConfig) {
612+
ic.Platform.PowerVS.Region = validRegion
613+
ic.ControlPlane.Platform.PowerVS = validMachinePool()
614+
ic.ControlPlane.Platform.PowerVS.SysType = defaultSysType
615+
},
616+
},
617+
errorMsg: "",
618+
},
619+
}
620+
setMockEnvVars()
621+
622+
mockCtrl := gomock.NewController(t)
623+
defer mockCtrl.Finish()
624+
625+
powervsClient := mock.NewMockAPI(mockCtrl)
626+
627+
// Run tests
628+
for _, tc := range cases {
629+
t.Run(tc.name, func(t *testing.T) {
630+
editedInstallConfig := validInstallConfig()
631+
for _, edit := range tc.edits {
632+
edit(editedInstallConfig)
633+
}
634+
635+
aggregatedErrors := powervs.ValidateSystemTypeForRegion(powervsClient, editedInstallConfig)
636+
if tc.errorMsg != "" {
637+
assert.Regexp(t, tc.errorMsg, aggregatedErrors)
638+
} else {
639+
assert.NoError(t, aggregatedErrors)
640+
}
641+
})
642+
}
643+
}
644+
559645
func setMockEnvVars() {
560646
os.Setenv("POWERVS_AUTH_FILEPATH", "./tmp/powervs/config.json")
561647
os.Setenv("IBMID", "foo")

pkg/types/powervs/powervs_regions.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package powervs
22

33
import (
44
"fmt"
5+
6+
"k8s.io/apimachinery/pkg/util/sets"
57
)
68

79
// Since there is no API to query these, we have to hard-code them here.
@@ -13,6 +15,7 @@ type Region struct {
1315
Description string
1416
VPCRegion string
1517
Zones []string
18+
SysTypes []string
1619
}
1720

1821
// Regions holds the regions for IBM Power VS, and descriptions used during the survey.
@@ -21,6 +24,7 @@ var Regions = map[string]Region{
2124
Description: "Dallas, USA",
2225
VPCRegion: "us-south",
2326
Zones: []string{"dal10"},
27+
SysTypes: []string{"s922", "e980"},
2428
},
2529
"mon": {
2630
Description: "Montreal, Canada",
@@ -100,3 +104,21 @@ func RegionFromZone(zone string) string {
100104
}
101105
return ""
102106
}
107+
108+
// AvailableSysTypes returns the default system type for the zone.
109+
func AvailableSysTypes(region string) ([]string, error) {
110+
knownRegion, ok := Regions[region]
111+
if !ok {
112+
return nil, fmt.Errorf("unknown region name provided")
113+
}
114+
return knownRegion.SysTypes, nil
115+
}
116+
117+
// AllKnownSysTypes returns aggregated known system types from all regions.
118+
func AllKnownSysTypes() sets.Set[string] {
119+
sysTypes := sets.New[string]()
120+
for _, region := range Regions {
121+
sysTypes.Insert(region.SysTypes...)
122+
}
123+
return sysTypes
124+
}

pkg/types/powervs/validation/machinepool.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ func ValidateMachinePool(p *powervs.MachinePool, fldPath *field.Path) field.Erro
7272

7373
// Validate SysType
7474
if p.SysType != "" {
75-
const sysTypeRegex = `^(?:e980|s922(-.*|))$`
76-
// Allowing for a staging-only pattern of s922-* but not exposing here
77-
if !regexp.MustCompile(sysTypeRegex).MatchString(p.SysType) {
78-
allErrs = append(allErrs, field.Invalid(fldPath.Child("sysType"), p.SysType, "system type must be one of {e980,s922}"))
75+
if !powervs.AllKnownSysTypes().Has(p.SysType) {
76+
allErrs = append(allErrs, field.Invalid(fldPath.Child("sysType"), p.SysType, "unknown system type specified"))
7977
}
8078
}
8179

pkg/types/powervs/validation/machinepool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestValidateMachinePool(t *testing.T) {
142142
pool: &powervs.MachinePool{
143143
SysType: "p922",
144144
},
145-
expected: `^test-path\.sysType: Invalid value: "p922": system type must be one of {e980,s922}$`,
145+
expected: `^test-path\.sysType: Invalid value: "p922": unknown system type specified$`,
146146
},
147147
}
148148
for _, tc := range cases {

0 commit comments

Comments
 (0)