Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator: fix get-handling of Azure marketplace images #2846

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/edgelesssys/constellation/v2/internal/mpimage"
)

// GetNodeImage returns the image name of the node.
Expand All @@ -27,12 +28,34 @@ func (c *Client) GetNodeImage(ctx context.Context, providerID string) (string, e
if resp.Properties == nil ||
resp.Properties.StorageProfile == nil ||
resp.Properties.StorageProfile.ImageReference == nil ||
resp.Properties.StorageProfile.ImageReference.ID == nil && resp.Properties.StorageProfile.ImageReference.CommunityGalleryImageID == nil {
resp.Properties.StorageProfile.ImageReference.ID == nil &&
resp.Properties.StorageProfile.ImageReference.CommunityGalleryImageID == nil &&
resp.Properties.StorageProfile.ImageReference.Publisher == nil &&
resp.Properties.StorageProfile.ImageReference.Offer == nil &&
resp.Properties.StorageProfile.ImageReference.SKU == nil &&
resp.Properties.StorageProfile.ImageReference.Version == nil {
msanft marked this conversation as resolved.
Show resolved Hide resolved
return "", fmt.Errorf("node %q does not have valid image reference", providerID)
}

// Marketplace Image is used, format it to an URI and return it.
if resp.Properties.StorageProfile.ImageReference.Publisher != nil &&
resp.Properties.StorageProfile.ImageReference.Offer != nil &&
resp.Properties.StorageProfile.ImageReference.SKU != nil &&
resp.Properties.StorageProfile.ImageReference.Version != nil {
return mpimage.AzureMarketplaceImage{
msanft marked this conversation as resolved.
Show resolved Hide resolved
Publisher: *resp.Properties.StorageProfile.ImageReference.Publisher,
Offer: *resp.Properties.StorageProfile.ImageReference.Offer,
SKU: *resp.Properties.StorageProfile.ImageReference.SKU,
Version: *resp.Properties.StorageProfile.ImageReference.Version,
}.URI(), nil
}

// Image ID is set, return it.
if resp.Properties.StorageProfile.ImageReference.ID != nil {
return *resp.Properties.StorageProfile.ImageReference.ID, nil
}

// Community Gallery image ID is set otherwise.
return *resp.Properties.StorageProfile.ImageReference.CommunityGalleryImageID, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ func TestGetNodeImage(t *testing.T) {
},
wantImage: "/CommunityGalleries/gallery-name/Images/image-name/Versions/1.2.3",
},
"getting marketplace image works": {
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
vm: armcompute.VirtualMachineScaleSetVM{
Properties: &armcompute.VirtualMachineScaleSetVMProperties{
StorageProfile: &armcompute.StorageProfile{
ImageReference: &armcompute.ImageReference{
Publisher: to.Ptr("edgelesssystems"),
Offer: to.Ptr("constellation"),
SKU: to.Ptr("constellation"),
Version: to.Ptr("2.14.2"),
},
},
},
},
wantImage: "constellation-marketplace-image://Azure?offer=constellation&publisher=edgelesssystems&sku=constellation&version=2.14.2",
},
"splitting providerID fails": {
providerID: "invalid",
wantErr: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,39 @@ func (c *Client) GetScalingGroupImage(ctx context.Context, scalingGroupID string
if err != nil {
return "", err
}

if res.Properties == nil ||
res.Properties.VirtualMachineProfile == nil ||
res.Properties.VirtualMachineProfile.StorageProfile == nil ||
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference == nil ||
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID == nil && res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID == nil {
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID == nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID == nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Publisher == nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Offer == nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.SKU == nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Version == nil {
return "", fmt.Errorf("scalet set %q does not have valid image reference", scalingGroupID)
}

// Marketplace Image is used, format it to an URI and return it.
if res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Publisher != nil &&
msanft marked this conversation as resolved.
Show resolved Hide resolved
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Offer != nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.SKU != nil &&
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Version != nil {
return mpimage.AzureMarketplaceImage{
Publisher: *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Publisher,
Offer: *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Offer,
SKU: *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.SKU,
Version: *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.Version,
}.URI(), nil
}

// Image ID is set, return it.
if res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID != nil {
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID, nil
}

// Community Gallery Image ID is set otherwise.
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ func TestGetScalingGroupImage(t *testing.T) {
},
wantImage: "/communityGalleries/gallery-name/Images/image-name/Versions/1.2.3",
},
"getting marketplace image works": {
scalingGroupID: "/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name",
scaleSet: armcompute.VirtualMachineScaleSet{
Properties: &armcompute.VirtualMachineScaleSetProperties{
VirtualMachineProfile: &armcompute.VirtualMachineScaleSetVMProfile{
StorageProfile: &armcompute.VirtualMachineScaleSetStorageProfile{
ImageReference: &armcompute.ImageReference{
Publisher: to.Ptr("edgelesssystems"),
Offer: to.Ptr("constellation"),
SKU: to.Ptr("constellation"),
Version: to.Ptr("2.14.2"),
},
},
},
},
},
wantImage: "constellation-marketplace-image://Azure?offer=constellation&publisher=edgelesssystems&sku=constellation&version=2.14.2",
},
"splitting scalingGroupID fails": {
scalingGroupID: "invalid",
wantErr: true,
Expand Down
Loading