AB#2287 support community image IDs (#9)

* support community image IDs
Signed-off-by: Fabian Kammel <fk@edgeless.systems>
This commit is contained in:
Fabian Kammel 2022-08-30 15:15:51 +02:00 committed by GitHub
parent e0a457b6ff
commit 778952e07c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 175 additions and 41 deletions

View file

@ -21,10 +21,14 @@ 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.ID == nil && resp.Properties.StorageProfile.ImageReference.CommunityGalleryImageID == nil {
return "", fmt.Errorf("node %q does not have valid image reference", providerID)
}
return *resp.Properties.StorageProfile.ImageReference.ID, nil
if resp.Properties.StorageProfile.ImageReference.ID != nil {
return *resp.Properties.StorageProfile.ImageReference.ID, nil
} else {
return *resp.Properties.StorageProfile.ImageReference.CommunityGalleryImageID, nil
}
}
// GetScalingGroupID returns the scaling group ID of the node.

View file

@ -34,6 +34,19 @@ func TestGetNodeImage(t *testing.T) {
},
wantImage: "/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/images/image-name",
},
"getting community node image works": {
providerID: "azure:///subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name/virtualMachines/instance-id",
vm: armcomputev2.VirtualMachineScaleSetVM{
Properties: &armcomputev2.VirtualMachineScaleSetVMProperties{
StorageProfile: &armcomputev2.StorageProfile{
ImageReference: &armcomputev2.ImageReference{
CommunityGalleryImageID: to.Ptr("/CommunityGalleries/gallery-name/Images/image-name/Versions/1.2.3"),
},
},
},
},
wantImage: "/CommunityGalleries/gallery-name/Images/image-name/Versions/1.2.3",
},
"splitting providerID fails": {
providerID: "invalid",
wantErr: true,

View file

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v2"
)
@ -22,10 +23,14 @@ func (c *Client) GetScalingGroupImage(ctx context.Context, scalingGroupID string
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.ID == nil && res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID == nil {
return "", fmt.Errorf("scalet set %q does not have valid image reference", scalingGroupID)
}
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID, nil
if res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID != nil {
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID, nil
} else {
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID, nil
}
}
// SetScalingGroupImage sets the image URI of the scaling group.
@ -38,9 +43,7 @@ func (c *Client) SetScalingGroupImage(ctx context.Context, scalingGroupID, image
Properties: &armcompute.VirtualMachineScaleSetUpdateProperties{
VirtualMachineProfile: &armcompute.VirtualMachineScaleSetUpdateVMProfile{
StorageProfile: &armcompute.VirtualMachineScaleSetUpdateStorageProfile{
ImageReference: &armcompute.ImageReference{
ID: &imageURI,
},
ImageReference: imageReferenceFromImage(imageURI),
},
},
},
@ -82,3 +85,15 @@ func (c *Client) ListScalingGroups(ctx context.Context, uid string) (controlPlan
}
return controlPlaneGroupIDs, workerGroupIDs, nil
}
func imageReferenceFromImage(img string) *armcompute.ImageReference {
ref := &armcompute.ImageReference{}
if strings.HasPrefix(img, "/CommunityGalleries") {
ref.CommunityGalleryImageID = to.Ptr(img)
} else {
ref.ID = to.Ptr(img)
}
return ref
}

View file

@ -34,6 +34,21 @@ func TestGetScalingGroupImage(t *testing.T) {
},
wantImage: "/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/images/image-name",
},
"getting community image works": {
scalingGroupID: "/subscriptions/subscription-id/resourceGroups/resource-group/providers/Microsoft.Compute/virtualMachineScaleSets/scale-set-name",
scaleSet: armcomputev2.VirtualMachineScaleSet{
Properties: &armcomputev2.VirtualMachineScaleSetProperties{
VirtualMachineProfile: &armcomputev2.VirtualMachineScaleSetVMProfile{
StorageProfile: &armcomputev2.VirtualMachineScaleSetStorageProfile{
ImageReference: &armcomputev2.ImageReference{
CommunityGalleryImageID: to.Ptr("/CommunityGalleries/gallery-name/Images/image-name/Versions/1.2.3"),
},
},
},
},
},
wantImage: "/CommunityGalleries/gallery-name/Images/image-name/Versions/1.2.3",
},
"splitting scalingGroupID fails": {
scalingGroupID: "invalid",
wantErr: true,
@ -217,3 +232,33 @@ func TestListScalingGroups(t *testing.T) {
})
}
}
func TestImageReferenceFromImage(t *testing.T) {
testCases := map[string]struct {
img string
wantID *string
wantCommunityID *string
}{
"ID": {
img: "/subscriptions/0d202bbb-4fa7-4af8-8125-58c269a05435/resourceGroups/constellation-images/providers/Microsoft.Compute/galleries/Constellation/images/constellation/versions/1.5.0",
wantID: to.Ptr("/subscriptions/0d202bbb-4fa7-4af8-8125-58c269a05435/resourceGroups/constellation-images/providers/Microsoft.Compute/galleries/Constellation/images/constellation/versions/1.5.0"),
wantCommunityID: nil,
},
"Community": {
img: "/CommunityGalleries/ConstellationCVM-728bd310-e898-4450-a1ed-21cf2fb0d735/Images/feat-azure-cvm-sharing/Versions/2022.0826.084922",
wantID: nil,
wantCommunityID: to.Ptr("/CommunityGalleries/ConstellationCVM-728bd310-e898-4450-a1ed-21cf2fb0d735/Images/feat-azure-cvm-sharing/Versions/2022.0826.084922"),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
ref := imageReferenceFromImage(tc.img)
assert.Equal(tc.wantID, ref.ID)
assert.Equal(tc.wantCommunityID, ref.CommunityGalleryImageID)
})
}
}