Skip to content

Commit

Permalink
Use new naming scheme for SKUs and versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed May 23, 2022
1 parent 0298a7d commit 4f8c87a
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 358 deletions.
85 changes: 0 additions & 85 deletions azure/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/blang/semver"
"github.com/pkg/errors"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
"sigs.k8s.io/cluster-api-provider-azure/version"
)
Expand Down Expand Up @@ -282,88 +279,6 @@ func AvailabilitySetID(subscriptionID, resourceGroup, availabilitySetName string
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", subscriptionID, resourceGroup, availabilitySetName)
}

// GetDefaultImageSKUID gets the SKU ID of the image to use for the provided version of Kubernetes.
// note: osAndVersion is expected to be in the format of {os}-{version} (ex: unbuntu-2004 or windows-2022)
func getDefaultImageSKUID(k8sVersion, osAndVersion string) (string, error) {
version, err := semver.ParseTolerant(k8sVersion)
if err != nil {
return "", errors.Wrapf(err, "unable to parse Kubernetes version \"%s\" in spec, expected valid SemVer string", k8sVersion)
}
return fmt.Sprintf("k8s-%ddot%ddot%d-%s", version.Major, version.Minor, version.Patch, osAndVersion), nil
}

// GetDefaultUbuntuImage returns the default image spec for Ubuntu.
func GetDefaultUbuntuImage(k8sVersion string) (*infrav1.Image, error) {
v, err := semver.ParseTolerant(k8sVersion)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse Kubernetes version \"%s\"", k8sVersion)
}
// Default to Ubuntu 20.04 LTS, except for k8s versions which have only 18.04 reference images.
osVersion := "2004"
if (v.Major == 1 && v.Minor == 21 && v.Patch < 2) ||
(v.Major == 1 && v.Minor == 20 && v.Patch < 8) ||
(v.Major == 1 && v.Minor == 19 && v.Patch < 12) ||
(v.Major == 1 && v.Minor == 18 && v.Patch < 20) ||
(v.Major == 1 && v.Minor < 18) {
osVersion = "1804"
}

skuID, err := getDefaultImageSKUID(k8sVersion, fmt.Sprintf("ubuntu-%s", osVersion))
if err != nil {
return nil, errors.Wrap(err, "failed to get default image")
}

defaultImage := &infrav1.Image{
Marketplace: &infrav1.AzureMarketplaceImage{
Publisher: DefaultImagePublisherID,
Offer: DefaultImageOfferID,
SKU: skuID,
Version: LatestVersion,
},
}

return defaultImage, nil
}

// GetDefaultWindowsImage returns the default image spec for Windows.
func GetDefaultWindowsImage(k8sVersion, runtime, osAndVersion string) (*infrav1.Image, error) {
v122 := semver.MustParse("1.22.0")
v, err := semver.ParseTolerant(k8sVersion)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse Kubernetes version \"%s\"", k8sVersion)
}

// If containerd is specified we don't currently support less than 1.22
if v.LE(v122) && runtime == "containerd" {
return nil, errors.New("containerd image only supported in 1.22+")
}

if osAndVersion == "" {
osAndVersion = DefaultWindowsOsAndVersion
}

skuID, err := getDefaultImageSKUID(k8sVersion, osAndVersion)
if err != nil {
return nil, errors.Wrap(err, "failed to get default image")
}

// Starting with 1.22 we default to containerd for Windows unless the runtime flag is set.
if v.GTE(v122) && runtime != "dockershim" {
skuID += "-containerd"
}

defaultImage := &infrav1.Image{
Marketplace: &infrav1.AzureMarketplaceImage{
Publisher: DefaultImagePublisherID,
Offer: DefaultWindowsImageOfferID,
SKU: skuID,
Version: LatestVersion,
},
}

return defaultImage, nil
}

// GetBootstrappingVMExtension returns the CAPZ Bootstrapping VM extension.
// The CAPZ Bootstrapping extension is a simple clone of https://github.com/Azure/custom-script-extension-linux for Linux or
// https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows for Windows.
Expand Down
251 changes: 0 additions & 251 deletions azure/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,108 +29,6 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

func TestGetDefaultImageSKUID(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
k8sVersion string
osAndVersion string
expectedResult string
expectedError bool
}{
{
k8sVersion: "v1.14.9",
expectedResult: "k8s-1dot14dot9-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "v1.14.10",
expectedResult: "k8s-1dot14dot10-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "v1.15.6",
expectedResult: "k8s-1dot15dot6-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "v1.15.7",
expectedResult: "k8s-1dot15dot7-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "v1.16.3",
expectedResult: "k8s-1dot16dot3-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "v1.16.4",
expectedResult: "k8s-1dot16dot4-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "1.12.0",
expectedResult: "k8s-1dot12dot0-ubuntu-1804",
expectedError: false,
osAndVersion: "ubuntu-1804",
},
{
k8sVersion: "1.1.notvalid.semver",
expectedResult: "",
expectedError: true,
},
{
k8sVersion: "v1.19.3",
expectedResult: "k8s-1dot19dot3-windows-2019",
expectedError: false,
osAndVersion: "windows-2019",
},
{
k8sVersion: "v1.20.8",
expectedResult: "k8s-1dot20dot8-windows-2019",
expectedError: false,
osAndVersion: "windows-2019",
},
{
k8sVersion: "v1.21.2",
expectedResult: "k8s-1dot21dot2-windows-2019",
expectedError: false,
osAndVersion: "windows-2019",
},
{
k8sVersion: "v1.20.8",
expectedResult: "k8s-1dot20dot8-ubuntu-2004",
expectedError: false,
osAndVersion: "ubuntu-2004",
},
{
k8sVersion: "v1.21.2",
expectedResult: "k8s-1dot21dot2-ubuntu-2004",
expectedError: false,
osAndVersion: "ubuntu-2004",
},
}

for _, test := range tests {
t.Run(test.k8sVersion, func(t *testing.T) {
id, err := getDefaultImageSKUID(test.k8sVersion, test.osAndVersion)

if test.expectedError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
g.Expect(id).To(Equal(test.expectedResult))
})
}
}

func TestAutoRestClientAppendUserAgent(t *testing.T) {
g := NewWithT(t)
userAgent := "cluster-api-provider-azure/2.29.2"
Expand Down Expand Up @@ -170,64 +68,6 @@ func TestAutoRestClientAppendUserAgent(t *testing.T) {
}
}

func TestGetDefaultUbuntuImage(t *testing.T) {
g := NewWithT(t)

tests := []struct {
k8sVersion string
expectedSKU string
}{
{
k8sVersion: "v1.15.6",
expectedSKU: "k8s-1dot15dot6-ubuntu-1804",
},
{
k8sVersion: "v1.17.11",
expectedSKU: "k8s-1dot17dot11-ubuntu-1804",
},
{
k8sVersion: "v1.18.19",
expectedSKU: "k8s-1dot18dot19-ubuntu-1804",
},
{
k8sVersion: "v1.18.20",
expectedSKU: "k8s-1dot18dot20-ubuntu-2004",
},
{
k8sVersion: "v1.19.11",
expectedSKU: "k8s-1dot19dot11-ubuntu-1804",
},
{
k8sVersion: "v1.19.12",
expectedSKU: "k8s-1dot19dot12-ubuntu-2004",
},
{
k8sVersion: "v1.21.1",
expectedSKU: "k8s-1dot21dot1-ubuntu-1804",
},
{
k8sVersion: "v1.21.2",
expectedSKU: "k8s-1dot21dot2-ubuntu-2004",
},
{
k8sVersion: "v1.22.0",
expectedSKU: "k8s-1dot22dot0-ubuntu-2004",
},
{
k8sVersion: "v1.23.6",
expectedSKU: "k8s-1dot23dot6-ubuntu-2004",
},
}

for _, test := range tests {
t.Run(test.k8sVersion, func(t *testing.T) {
image, err := GetDefaultUbuntuImage(test.k8sVersion)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(image.Marketplace.SKU).To(Equal(test.expectedSKU))
})
}
}

func TestMSCorrelationIDSendDecorator(t *testing.T) {
g := NewWithT(t)
const corrID tele.CorrID = "TestMSCorrelationIDSendDecoratorCorrID"
Expand Down Expand Up @@ -276,94 +116,3 @@ func TestMSCorrelationIDSendDecorator(t *testing.T) {
receivedReq.Header.Get(string(tele.CorrIDKeyVal)),
).To(Equal(string(corrID)))
}

func TestGetDefaultWindowsImage(t *testing.T) {
g := NewWithT(t)

var tests = []struct {
name string
k8sVersion string
runtime string
osVersion string
expectedSKU string
expectedError bool
}{
{
name: "no k8sVersion",
k8sVersion: "1.1.1.1.1.1",
runtime: "",
osVersion: "",
expectedSKU: "",
expectedError: true,
},
{
name: "1.21.* - default runtime - default osVersion",
k8sVersion: "v1.21.4",
runtime: "",
osVersion: "",
expectedSKU: "k8s-1dot21dot4-windows-2019",
expectedError: false,
},
{
name: "1.21.* - dockershim runtime - default osVersion",
k8sVersion: "v1.21.4",
runtime: "dockershim",
osVersion: "",
expectedSKU: "k8s-1dot21dot4-windows-2019",
expectedError: false,
},
{
name: "1.21.* - containerd runtime - default osVersion",
k8sVersion: "v1.21.4",
runtime: "containerd",
osVersion: "",
expectedSKU: "",
expectedError: true,
},
{
name: "1.23.* - containerd runtime - default osVersion",
k8sVersion: "v1.23.2",
runtime: "containerd",
osVersion: "",
expectedSKU: "k8s-1dot23dot2-windows-2019-containerd",
expectedError: false,
},
{
name: "1.23.* - default runtime - 2019 osVersion",
k8sVersion: "v1.23.2",
runtime: "",
osVersion: "windows-2019",
expectedSKU: "k8s-1dot23dot2-windows-2019-containerd",
expectedError: false,
},
{
name: "1.23.* - default runtime - 2022 osVersion",
k8sVersion: "v1.23.2",
runtime: "",
osVersion: "windows-2022",
expectedSKU: "k8s-1dot23dot2-windows-2022-containerd",
expectedError: false,
},
{
name: "1.23.* - containerd runtime - 2022 osVersion",
k8sVersion: "v1.23.2",
runtime: "containerd",
osVersion: "windows-2022",
expectedSKU: "k8s-1dot23dot2-windows-2022-containerd",
expectedError: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
image, err := GetDefaultWindowsImage(test.k8sVersion, test.runtime, test.osVersion)

if test.expectedError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(image.Marketplace.SKU).To(Equal(test.expectedSKU))
}
})
}
}
7 changes: 4 additions & 3 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/networkinterfaces"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachineimages"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachines"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
Expand Down Expand Up @@ -623,7 +624,7 @@ func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {

// GetVMImage returns the image from the machine configuration, or a default one.
func (m *MachineScope) GetVMImage(ctx context.Context) (*infrav1.Image, error) {
_, log, done := tele.StartSpanWithLogger(ctx, "scope.MachineScope.GetVMImage")
ctx, log, done := tele.StartSpanWithLogger(ctx, "scope.MachineScope.GetVMImage")
defer done()

// Use custom Marketplace image, Image ID or a Shared Image Gallery image if provided
Expand All @@ -635,11 +636,11 @@ func (m *MachineScope) GetVMImage(ctx context.Context) (*infrav1.Image, error) {
runtime := m.AzureMachine.Annotations["runtime"]
windowsServerVersion := m.AzureMachine.Annotations["windowsServerVersion"]
log.Info("No image specified for machine, using default Windows Image", "machine", m.AzureMachine.GetName(), "runtime", runtime, "windowsServerVersion", windowsServerVersion)
return azure.GetDefaultWindowsImage(to.String(m.Machine.Spec.Version), runtime, windowsServerVersion)
return virtualmachineimages.GetDefaultWindowsImage(ctx, m, m.Location(), to.String(m.Machine.Spec.Version), runtime, windowsServerVersion)
}

log.Info("No image specified for machine, using default Linux Image", "machine", m.AzureMachine.GetName())
return azure.GetDefaultUbuntuImage(to.String(m.Machine.Spec.Version))
return virtualmachineimages.GetDefaultUbuntuImage(ctx, m, m.Location(), to.String(m.Machine.Spec.Version))
}

// SetSubnetName defaults the AzureMachine subnet name to the name of one the subnets with the machine role when there is only one of them.
Expand Down
Loading

0 comments on commit 4f8c87a

Please sign in to comment.