-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathvmss.go
More file actions
280 lines (237 loc) · 9.35 KB
/
vmss.go
File metadata and controls
280 lines (237 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package converters
import (
"regexp"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"k8s.io/utils/ptr"
azprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
)
const (
// RegExpStrCommunityGalleryID is a regexp string used for matching community gallery IDs and capturing specific values.
RegExpStrCommunityGalleryID = `/CommunityGalleries/(?P<gallery>.*)/Images/(?P<name>.*)/Versions/(?P<version>.*)`
// RegExpStrComputeGalleryID is a regexp string used for matching compute gallery IDs and capturing specific values.
RegExpStrComputeGalleryID = `/subscriptions/(?P<subID>.*)/resourceGroups/(?P<rg>.*)/providers/Microsoft.Compute/galleries/(?P<gallery>.*)/images/(?P<name>.*)/versions/(?P<version>.*)`
)
// SDKToVMSS converts an Azure SDK VirtualMachineScaleSet to the AzureMachinePool type.
func SDKToVMSS(sdkvmss armcompute.VirtualMachineScaleSet, sdkinstances []armcompute.VirtualMachineScaleSetVM) azure.VMSS {
vmss := azure.VMSS{
ID: ptr.Deref(sdkvmss.ID, ""),
Name: ptr.Deref(sdkvmss.Name, ""),
State: infrav1.ProvisioningState(ptr.Deref(sdkvmss.Properties.ProvisioningState, "")),
}
if sdkvmss.SKU != nil {
vmss.Sku = ptr.Deref(sdkvmss.SKU.Name, "")
vmss.Capacity = ptr.Deref[int64](sdkvmss.SKU.Capacity, 0)
}
for _, zone := range sdkvmss.Zones {
vmss.Zones = append(vmss.Zones, *zone)
}
if len(sdkvmss.Tags) > 0 {
vmss.Tags = MapToTags(sdkvmss.Tags)
}
if len(sdkinstances) > 0 {
vmss.Instances = make([]azure.VMSSVM, len(sdkinstances))
orchestrationMode := ptr.Deref(sdkvmss.Properties.OrchestrationMode, "")
for i, vm := range sdkinstances {
vmss.Instances[i] = *SDKToVMSSVM(vm)
vmss.Instances[i].OrchestrationMode = infrav1.OrchestrationModeType(orchestrationMode)
}
}
if sdkvmss.Properties.VirtualMachineProfile != nil &&
sdkvmss.Properties.VirtualMachineProfile.StorageProfile != nil &&
sdkvmss.Properties.VirtualMachineProfile.StorageProfile.ImageReference != nil {
imageRef := sdkvmss.Properties.VirtualMachineProfile.StorageProfile.ImageReference
vmss.Image = SDKImageToImage(imageRef, sdkvmss.Plan)
}
return vmss
}
// SDKVMToVMSSVM converts an Azure SDK VM to a VMSS VM.
func SDKVMToVMSSVM(sdkInstance armcompute.VirtualMachine, mode infrav1.OrchestrationModeType) *azure.VMSSVM {
instance := azure.VMSSVM{
ID: ptr.Deref(sdkInstance.ID, ""),
}
if sdkInstance.Properties == nil {
return &instance
}
instance.State = infrav1.Creating
if sdkInstance.Properties.ProvisioningState != nil {
instance.State = infrav1.ProvisioningState(ptr.Deref(sdkInstance.Properties.ProvisioningState, ""))
}
if sdkInstance.Properties.OSProfile != nil && sdkInstance.Properties.OSProfile.ComputerName != nil {
instance.Name = *sdkInstance.Properties.OSProfile.ComputerName
}
if sdkInstance.Properties.StorageProfile != nil && sdkInstance.Properties.StorageProfile.ImageReference != nil {
imageRef := sdkInstance.Properties.StorageProfile.ImageReference
instance.Image = SDKImageToImage(imageRef, sdkInstance.Plan)
}
if len(sdkInstance.Zones) > 0 {
// An instance should have only 1 zone, so use the first item of the slice.
instance.AvailabilityZone = *sdkInstance.Zones[0]
}
instance.OrchestrationMode = mode
return &instance
}
// SDKToVMSSVM converts an Azure SDK VirtualMachineScaleSetVM into an infrav1exp.VMSSVM.
func SDKToVMSSVM(sdkInstance armcompute.VirtualMachineScaleSetVM) *azure.VMSSVM {
// Convert resourceGroup Name ID ( ProviderID in capz objects )
var convertedID string
convertedID, err := azprovider.ConvertResourceGroupNameToLower(ptr.Deref(sdkInstance.ID, ""))
if err != nil {
convertedID = ptr.Deref(sdkInstance.ID, "")
}
instance := azure.VMSSVM{
ID: convertedID,
InstanceID: ptr.Deref(sdkInstance.InstanceID, ""),
}
if sdkInstance.Properties == nil {
return &instance
}
instance.State = infrav1.Creating
if sdkInstance.Properties.ProvisioningState != nil {
instance.State = infrav1.ProvisioningState(ptr.Deref(sdkInstance.Properties.ProvisioningState, ""))
}
if sdkInstance.Properties.OSProfile != nil && sdkInstance.Properties.OSProfile.ComputerName != nil {
instance.Name = *sdkInstance.Properties.OSProfile.ComputerName
}
if sdkInstance.Resources != nil {
for _, r := range sdkInstance.Resources {
if r.Properties.ProvisioningState != nil && r.Name != nil &&
(*r.Name == azure.BootstrappingExtensionLinux || *r.Name == azure.BootstrappingExtensionWindows) {
instance.BootstrappingState = infrav1.ProvisioningState(ptr.Deref(r.Properties.ProvisioningState, ""))
break
}
}
}
if sdkInstance.Properties.StorageProfile != nil && sdkInstance.Properties.StorageProfile.ImageReference != nil {
imageRef := sdkInstance.Properties.StorageProfile.ImageReference
instance.Image = SDKImageToImage(imageRef, sdkInstance.Plan)
}
if len(sdkInstance.Zones) > 0 {
// an instance should only have 1 zone, so we select the first item of the slice
instance.AvailabilityZone = *sdkInstance.Zones[0]
}
return &instance
}
// SDKImageToImage converts a SDK image reference to infrav1.Image.
func SDKImageToImage(sdkImageRef *armcompute.ImageReference, sdkPlan *armcompute.Plan) infrav1.Image {
var image infrav1.Image
switch {
case sdkImageRef.ID != nil:
image = IDImageRefToImage(*sdkImageRef.ID)
case sdkImageRef.CommunityGalleryImageID != nil:
image = cgImageRefToImage(*sdkImageRef.CommunityGalleryImageID)
case sdkImageRef.SharedGalleryImageID != nil:
image = sgImageRefToImage(*sdkImageRef.SharedGalleryImageID)
default:
image = mpImageRefToImage(sdkImageRef, sdkPlan != nil)
}
if sdkPlan != nil {
if image.ComputeGallery != nil {
image.ComputeGallery.Plan = &infrav1.ImagePlan{
Publisher: ptr.Deref(sdkPlan.Publisher, ""),
Offer: ptr.Deref(sdkPlan.Product, ""),
SKU: ptr.Deref(sdkPlan.Name, ""),
}
} else if image.SharedGallery != nil {
image.SharedGallery.Publisher = sdkPlan.Publisher
image.SharedGallery.Offer = sdkPlan.Product
image.SharedGallery.SKU = sdkPlan.Name
}
}
return image
}
// GetOrchestrationMode returns the compute.OrchestrationMode for the given infrav1.OrchestrationModeType.
func GetOrchestrationMode(modeType infrav1.OrchestrationModeType) armcompute.OrchestrationMode {
if modeType == infrav1.FlexibleOrchestrationMode {
return armcompute.OrchestrationModeFlexible
}
return armcompute.OrchestrationModeUniform
}
// IDImageRefToImage converts an ID to a infrav1.Image with ComputeGallery set or ID, depending on the structure of the ID.
func IDImageRefToImage(id string) infrav1.Image {
// compute gallery image
if ok, params := getParams(RegExpStrComputeGalleryID, id); ok {
return infrav1.Image{
ComputeGallery: &infrav1.AzureComputeGalleryImage{
Gallery: params["gallery"],
Name: params["name"],
Version: params["version"],
SubscriptionID: ptr.To(params["subID"]),
ResourceGroup: ptr.To(params["rg"]),
},
}
}
// specific image
return infrav1.Image{
ID: &id,
}
}
// mpImageRefToImage converts a marketplace gallery ImageReference to an infrav1.Image.
func mpImageRefToImage(sdkImageRef *armcompute.ImageReference, isThirdPartyImage bool) infrav1.Image {
return infrav1.Image{
Marketplace: &infrav1.AzureMarketplaceImage{
ImagePlan: infrav1.ImagePlan{
Publisher: ptr.Deref(sdkImageRef.Publisher, ""),
Offer: ptr.Deref(sdkImageRef.Offer, ""),
SKU: ptr.Deref(sdkImageRef.SKU, ""),
},
Version: ptr.Deref(sdkImageRef.Version, ""),
ThirdPartyImage: isThirdPartyImage,
},
}
}
// cgImageRefToImage converts a community gallery ImageReference to an infrav1.Image.
func cgImageRefToImage(id string) infrav1.Image {
if ok, params := getParams(RegExpStrCommunityGalleryID, id); ok {
return infrav1.Image{
ComputeGallery: &infrav1.AzureComputeGalleryImage{
Gallery: params["gallery"],
Name: params["name"],
Version: params["version"],
},
}
}
return infrav1.Image{}
}
// sgImageRefToImage converts a shared gallery ImageReference to an infrav1.Image.
func sgImageRefToImage(id string) infrav1.Image {
if ok, params := getParams(RegExpStrComputeGalleryID, id); ok {
return infrav1.Image{
SharedGallery: &infrav1.AzureSharedGalleryImage{
SubscriptionID: params["subID"],
ResourceGroup: params["rg"],
Gallery: params["gallery"],
Name: params["name"],
Version: params["version"],
},
}
}
return infrav1.Image{}
}
func getParams(regStr, str string) (matched bool, params map[string]string) {
re := regexp.MustCompile(regStr)
match := re.FindAllStringSubmatch(str, -1)
if len(match) == 1 {
params = make(map[string]string)
for i, name := range re.SubexpNames() {
if i > 0 && i <= len(match[0]) {
params[name] = match[0][i]
}
}
matched = true
}
return matched, params
}