sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/vm.go (about) 1 /* 2 Copyright 2019 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 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" 21 corev1 "k8s.io/api/core/v1" 22 "k8s.io/utils/ptr" 23 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 24 ) 25 26 // VM describes an Azure virtual machine. 27 type VM struct { 28 ID string `json:"id,omitempty"` 29 Name string `json:"name,omitempty"` 30 AvailabilityZone string `json:"availabilityZone,omitempty"` 31 // Hardware profile 32 VMSize string `json:"vmSize,omitempty"` 33 // Storage profile 34 Image infrav1.Image `json:"image,omitempty"` 35 OSDisk infrav1.OSDisk `json:"osDisk,omitempty"` 36 StartupScript string `json:"startupScript,omitempty"` 37 // State - The provisioning state, which only appears in the response. 38 State infrav1.ProvisioningState `json:"vmState,omitempty"` 39 Identity infrav1.VMIdentity `json:"identity,omitempty"` 40 Tags infrav1.Tags `json:"tags,omitempty"` 41 42 // Addresses contains the addresses associated with the Azure VM. 43 Addresses []corev1.NodeAddress `json:"addresses,omitempty"` 44 45 UserAssignedIdentities []infrav1.UserAssignedIdentity `json:"userAssignedIdentities,omitempty"` 46 } 47 48 // SDKToVM converts an Azure SDK VirtualMachine to the CAPZ VM type. 49 func SDKToVM(v armcompute.VirtualMachine) *VM { 50 vm := &VM{ 51 ID: ptr.Deref(v.ID, ""), 52 Name: ptr.Deref(v.Name, ""), 53 State: infrav1.ProvisioningState(ptr.Deref(v.Properties.ProvisioningState, "")), 54 } 55 56 if v.Properties != nil && v.Properties.HardwareProfile != nil && v.Properties.HardwareProfile.VMSize != nil { 57 vm.VMSize = string(*v.Properties.HardwareProfile.VMSize) 58 } 59 60 if len(v.Zones) > 0 && v.Zones[0] != nil { 61 vm.AvailabilityZone = *v.Zones[0] 62 } 63 64 if len(v.Tags) > 0 { 65 vm.Tags = MapToTags(v.Tags) 66 } 67 68 if v.Identity != nil { 69 for _, identity := range v.Identity.UserAssignedIdentities { 70 if identity != nil && identity.ClientID != nil { 71 vm.UserAssignedIdentities = append(vm.UserAssignedIdentities, infrav1.UserAssignedIdentity{ 72 ProviderID: *identity.ClientID, 73 }) 74 } 75 } 76 } 77 78 return vm 79 }