sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/image.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package converters 18 19 import ( 20 "fmt" 21 22 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" 23 "github.com/pkg/errors" 24 "k8s.io/utils/ptr" 25 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 26 ) 27 28 // ImageToSDK converts a CAPZ Image (as RawExtension) to a Azure SDK Image Reference. 29 func ImageToSDK(image *infrav1.Image) (*armcompute.ImageReference, error) { 30 if image.ID != nil { 31 return specificImageToSDK(image) 32 } 33 if image.Marketplace != nil { 34 return mpImageToSDK(image) 35 } 36 if image.ComputeGallery != nil || image.SharedGallery != nil { 37 return computeImageToSDK(image) 38 } 39 40 return nil, errors.New("unable to convert image as no options set") 41 } 42 43 func mpImageToSDK(image *infrav1.Image) (*armcompute.ImageReference, error) { 44 return &armcompute.ImageReference{ 45 Publisher: &image.Marketplace.Publisher, 46 Offer: &image.Marketplace.Offer, 47 SKU: &image.Marketplace.SKU, 48 Version: &image.Marketplace.Version, 49 }, nil 50 } 51 52 func computeImageToSDK(image *infrav1.Image) (*armcompute.ImageReference, error) { 53 if image.ComputeGallery == nil && image.SharedGallery == nil { 54 return nil, errors.New("unable to convert compute image to SDK as SharedGallery or ComputeGallery fields are not set") 55 } 56 57 idTemplate := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/galleries/%s/images/%s/versions/%s" 58 if image.SharedGallery != nil { 59 return &armcompute.ImageReference{ 60 ID: ptr.To(fmt.Sprintf(idTemplate, 61 image.SharedGallery.SubscriptionID, 62 image.SharedGallery.ResourceGroup, 63 image.SharedGallery.Gallery, 64 image.SharedGallery.Name, 65 image.SharedGallery.Version, 66 )), 67 }, nil 68 } 69 70 // For private Azure Compute Gallery consumption both resource group and subscription ID must be provided. 71 // If they are not, we assume use of community gallery. 72 if image.ComputeGallery.ResourceGroup != nil && image.ComputeGallery.SubscriptionID != nil { 73 return &armcompute.ImageReference{ 74 ID: ptr.To(fmt.Sprintf(idTemplate, 75 ptr.Deref(image.ComputeGallery.SubscriptionID, ""), 76 ptr.Deref(image.ComputeGallery.ResourceGroup, ""), 77 image.ComputeGallery.Gallery, 78 image.ComputeGallery.Name, 79 image.ComputeGallery.Version, 80 )), 81 }, nil 82 } 83 84 return &armcompute.ImageReference{ 85 CommunityGalleryImageID: ptr.To(fmt.Sprintf("/CommunityGalleries/%s/Images/%s/Versions/%s", 86 image.ComputeGallery.Gallery, 87 image.ComputeGallery.Name, 88 image.ComputeGallery.Version)), 89 }, nil 90 } 91 92 func specificImageToSDK(image *infrav1.Image) (*armcompute.ImageReference, error) { 93 return &armcompute.ImageReference{ 94 ID: image.ID, 95 }, nil 96 } 97 98 // ImageToPlan converts a CAPZ Image to an Azure Compute Plan. 99 func ImageToPlan(image *infrav1.Image) *armcompute.Plan { 100 // Plan is needed when using a Shared Gallery image with Plan details. 101 if image.SharedGallery != nil && image.SharedGallery.Publisher != nil && image.SharedGallery.SKU != nil && image.SharedGallery.Offer != nil { 102 return &armcompute.Plan{ 103 Publisher: image.SharedGallery.Publisher, 104 Name: image.SharedGallery.SKU, 105 Product: image.SharedGallery.Offer, 106 } 107 } 108 109 // Plan is needed for third party Marketplace images. 110 if image.Marketplace != nil && image.Marketplace.ThirdPartyImage { 111 return &armcompute.Plan{ 112 Publisher: ptr.To(image.Marketplace.Publisher), 113 Name: ptr.To(image.Marketplace.SKU), 114 Product: ptr.To(image.Marketplace.Offer), 115 } 116 } 117 118 // Plan is needed when using a Azure Compute Gallery image with Plan details. 119 if image.ComputeGallery != nil && image.ComputeGallery.Plan != nil { 120 return &armcompute.Plan{ 121 Publisher: ptr.To(image.ComputeGallery.Plan.Publisher), 122 Name: ptr.To(image.ComputeGallery.Plan.SKU), 123 Product: ptr.To(image.ComputeGallery.Plan.Offer), 124 } 125 } 126 127 // Otherwise return nil. 128 return nil 129 }