Fix Azure zone number format passed to VMSS API

This commit is contained in:
Peter Rifel 2021-07-14 19:06:28 -04:00
parent 9552b25050
commit affbeb3c5b
No known key found for this signature in database
GPG Key ID: BC6469E5B16DB2B6
3 changed files with 61 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import (
"k8s.io/kops/pkg/model"
"k8s.io/kops/pkg/model/defaults"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
"k8s.io/kops/upup/pkg/fi/cloudup/azuretasks"
)
@ -70,6 +71,14 @@ func (b *VMScaleSetModelBuilder) buildVMScaleSetTask(
name string,
ig *kops.InstanceGroup,
) (*azuretasks.VMScaleSet, error) {
var azNumbers []string
for _, zone := range ig.Spec.Zones {
az, err := azure.ZoneToAvailabilityZoneNumber(zone)
if err != nil {
return nil, err
}
azNumbers = append(azNumbers, az)
}
t := &azuretasks.VMScaleSet{
Name: fi.String(name),
Lifecycle: b.Lifecycle,
@ -78,7 +87,7 @@ func (b *VMScaleSetModelBuilder) buildVMScaleSetTask(
SKUName: fi.String(ig.Spec.MachineType),
ComputerNamePrefix: fi.String(ig.Name),
AdminUser: fi.String(b.Cluster.Spec.CloudConfig.Azure.AdminUser),
Zones: ig.Spec.Zones,
Zones: azNumbers,
}
var err error

View File

@ -30,3 +30,13 @@ func ZoneToLocation(zone string) (string, error) {
}
return l[0], nil
}
// ZoneToAvailabilityZoneNumber extracts the availability zone number from a zone of the
// form <location>-<available-zone-number>..
func ZoneToAvailabilityZoneNumber(zone string) (string, error) {
l := strings.Split(zone, "-")
if len(l) != 2 {
return "", fmt.Errorf("invalid Azure zone: %q ", zone)
}
return l[1], nil
}

View File

@ -61,3 +61,44 @@ func TestZoneToLocation(t *testing.T) {
})
}
}
func TestZoneToAvailabilityZoneNumber(t *testing.T) {
testCases := []struct {
zone string
success bool
azNumber string
}{
{
zone: "eastus-1",
success: true,
azNumber: "1",
},
{
zone: "eastus",
success: false,
},
{
zone: "eastus-1-2",
success: false,
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
azNum, err := ZoneToAvailabilityZoneNumber(tc.zone)
if !tc.success {
if err == nil {
t.Fatalf("unexpected success")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if azNum != tc.azNumber {
t.Errorf("expected %s but got %s", tc.azNumber, azNum)
}
})
}
}