Skip to content

Commit

Permalink
Default to Azure Linux images
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed May 10, 2024
1 parent 5fefbdb commit 8b5fb2d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ func (m *MachineScope) GetVMImage(ctx context.Context) (*infrav1.Image, error) {
}

log.Info("No image specified for machine, using default Linux Image", "machine", m.AzureMachine.GetName())
return svc.GetDefaultUbuntuImage(ctx, m.Location(), ptr.Deref(m.Machine.Spec.Version, ""))
return svc.GetDefaultLinuxImage(ctx, m.Location(), ptr.Deref(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
2 changes: 1 addition & 1 deletion azure/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ func TestMachineScope_GetVMImage(t *testing.T) {
ClusterScoper: clusterMock,
},
want: func() *infrav1.Image {
image, _ := svc.GetDefaultUbuntuImage(context.TODO(), "", "1.20.1")
image, _ := svc.GetDefaultLinuxImage(context.TODO(), "", "1.20.1")
return image
}(),
expectedErr: "",
Expand Down
2 changes: 1 addition & 1 deletion azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func (m *MachinePoolScope) GetVMImage(ctx context.Context) (*infrav1.Image, erro
log.V(4).Info("No image specified for machine, using default Windows Image", "machine", m.MachinePool.GetName(), "runtime", runtime, "windowsServerVersion", windowsServerVersion)
defaultImage, err = svc.GetDefaultWindowsImage(ctx, m.Location(), ptr.Deref(m.MachinePool.Spec.Template.Spec.Version, ""), runtime, windowsServerVersion)
} else {
defaultImage, err = svc.GetDefaultUbuntuImage(ctx, m.Location(), ptr.Deref(m.MachinePool.Spec.Template.Spec.Version, ""))
defaultImage, err = svc.GetDefaultLinuxImage(ctx, m.Location(), ptr.Deref(m.MachinePool.Spec.Template.Spec.Version, ""))
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion azure/scope/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestMachinePoolScope_GetVMImage(t *testing.T) {
ImagePlan: infrav1.ImagePlan{
Publisher: "cncf-upstream",
Offer: "capi",
SKU: "k8s-1dot19dot11-ubuntu-1804",
SKU: "k8s-1dot19dot11-azurelinux-3",
},
Version: "latest",
ThirdPartyImage: false,
Expand Down
53 changes: 52 additions & 1 deletion azure/services/virtualmachineimages/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,59 @@ func New(auth azure.Authorizer) (*Service, error) {
}, nil
}

// GetDefaultLinuxImage returns the default image spec for a Linux node.
func (s *Service) GetDefaultLinuxImage(ctx context.Context, location, k8sVersion string) (*infrav1.Image, error) {
ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.Service.GetDefaultLinuxImage")
defer done()

Check warning on line 53 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L51-L53

Added lines #L51 - L53 were not covered by tests

// First try Azure Linux, then Ubuntu.
defaultImage, err := s.GetDefaultAzureLinuxImage(ctx, location, k8sVersion)
if err != nil {
log.V(4).Info("Failed to get default Azure Linux image, trying default Ubuntu image", "error", err)
return s.GetDefaultUbuntuImage(ctx, location, k8sVersion)

Check warning on line 59 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L56-L59

Added lines #L56 - L59 were not covered by tests
}

return defaultImage, nil

Check warning on line 62 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L62

Added line #L62 was not covered by tests
}

// GetDefaultAzureLinuxImage returns the default image spec for Azure Linux.
func (s *Service) GetDefaultAzureLinuxImage(ctx context.Context, location, k8sVersion string) (*infrav1.Image, error) {
ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.Service.GetDefaultAzureLinuxImage")
defer done()

Check warning on line 68 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L66-L68

Added lines #L66 - L68 were not covered by tests

// First try Azure Linux 3.
publisher, offer := azure.DefaultImagePublisherID, azure.DefaultImageOfferID
skuID, version, err := s.getSKUAndVersion(
ctx, location, publisher, offer, k8sVersion, "azurelinux-3")
if err != nil {

Check warning on line 74 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L71-L74

Added lines #L71 - L74 were not covered by tests
// If that fails, log the error and try Azure Linux 2.
log.V(4).Info("Failed to get default image for Azure Linux 3, trying Azure Linux 2", "error", err)
skuID, version, err = s.getSKUAndVersion(
ctx, location, publisher, offer, k8sVersion, "mariner-2") //TODO: switch to "azurelinux-2"
if err != nil {
return nil, errors.Wrap(err, "failed to get default image")

Check warning on line 80 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L76-L80

Added lines #L76 - L80 were not covered by tests
}
}

defaultImage := &infrav1.Image{
Marketplace: &infrav1.AzureMarketplaceImage{
ImagePlan: infrav1.ImagePlan{
Publisher: publisher,
Offer: offer,
SKU: skuID,
},
Version: version,
},

Check warning on line 92 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L84-L92

Added lines #L84 - L92 were not covered by tests
}

return defaultImage, nil

Check warning on line 95 in azure/services/virtualmachineimages/images.go

View check run for this annotation

Codecov / codecov/patch

azure/services/virtualmachineimages/images.go#L95

Added line #L95 was not covered by tests
}

// GetDefaultUbuntuImage returns the default image spec for Ubuntu.
func (s *Service) GetDefaultUbuntuImage(ctx context.Context, location, k8sVersion string) (*infrav1.Image, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.Service.GetDefaultAzureLinuxImage")
defer done()

v, err := semver.ParseTolerant(k8sVersion)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse Kubernetes version \"%s\"", k8sVersion)
Expand Down Expand Up @@ -121,7 +172,7 @@ func (s *Service) GetDefaultWindowsImage(ctx context.Context, location, k8sVersi

// getSKUAndVersion gets the SKU ID and version of the image to use for the provided version of Kubernetes.
// note: osAndVersion is expected to be in the format of {os}-{version} (ex: ubuntu-2004 or windows-2022)
func (s *Service) getSKUAndVersion(ctx context.Context, location, publisher, offer, k8sVersion, osAndVersion string) (skuID string, imageVersion string, err error) {
func (s *Service) getSKUAndVersion(ctx context.Context, location, publisher, offer, k8sVersion, osAndVersion string) (skuID string, imageVersion string, err error) { //nolint:unparam // Keep "publisher" in the function signature.
ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.Service.getSKUAndVersion")
defer done()

Expand Down

0 comments on commit 8b5fb2d

Please sign in to comment.