@@ -19,14 +19,13 @@ package scope
1919import (
2020 "context"
2121 "encoding/base64"
22- "k8s.io/apimachinery/pkg/util/uuid"
23-
22+ "github.com/Azure/go-autorest/autorest/to"
2423 "github.com/go-logr/logr"
2524 "github.com/pkg/errors"
2625 corev1 "k8s.io/api/core/v1"
2726 "k8s.io/apimachinery/pkg/types"
27+ "k8s.io/apimachinery/pkg/util/uuid"
2828 "k8s.io/klog/klogr"
29- "k8s.io/utils/pointer"
3029 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
3130 azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
3231 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
@@ -87,6 +86,25 @@ type MachineScope struct {
8786 AzureMachine * infrav1.AzureMachine
8887}
8988
89+ // VMSpecs returns the VM specs.
90+ func (m * MachineScope ) VMSpecs () []azure.VMSpec {
91+ return []azure.VMSpec {
92+ {
93+ Name : m .Name (),
94+ Role : m .Role (),
95+ NICNames : m .NICNames (),
96+ SSHKeyData : m .AzureMachine .Spec .SSHPublicKey ,
97+ Size : m .AzureMachine .Spec .VMSize ,
98+ OSDisk : m .AzureMachine .Spec .OSDisk ,
99+ DataDisks : m .AzureMachine .Spec .DataDisks ,
100+ Zone : m .AvailabilityZone (),
101+ Identity : m .AzureMachine .Spec .Identity ,
102+ UserAssignedIdentities : m .AzureMachine .Spec .UserAssignedIdentities ,
103+ SpotVMOptions : m .AzureMachine .Spec .SpotVMOptions ,
104+ },
105+ }
106+ }
107+
90108// PublicIPSpec returns the public IP specs.
91109func (m * MachineScope ) PublicIPSpecs () []azure.PublicIPSpec {
92110 var spec []azure.PublicIPSpec
@@ -147,6 +165,14 @@ func (m *MachineScope) NICSpecs() []azure.NICSpec {
147165 return specs
148166}
149167
168+ func (m * MachineScope ) NICNames () []string {
169+ nicNames := make ([]string , len (m .NICSpecs ()))
170+ for i , nic := range m .NICSpecs () {
171+ nicNames [i ] = nic .Name
172+ }
173+ return nicNames
174+ }
175+
150176// DiskSpecs returns the disk specs.
151177func (m * MachineScope ) DiskSpecs () []azure.DiskSpec {
152178 spec := azure.DiskSpec {
@@ -180,13 +206,14 @@ func (m *MachineScope) Subnet() *infrav1.SubnetSpec {
180206// AvailabilityZone returns the AzureMachine Availability Zone.
181207// Priority for selecting the AZ is
182208// 1) Machine.Spec.FailureDomain
183- // 2) AzureMachine.Spec.FailureDomain
209+ // 2) AzureMachine.Spec.FailureDomain (This is to support deprecated AZ)
184210// 3) AzureMachine.Spec.AvailabilityZone.ID (This is DEPRECATED)
185211// 4) No AZ
186212func (m * MachineScope ) AvailabilityZone () string {
187213 if m .Machine .Spec .FailureDomain != nil {
188214 return * m .Machine .Spec .FailureDomain
189215 }
216+ // DEPRECATED: to support old clients
190217 if m .AzureMachine .Spec .FailureDomain != nil {
191218 return * m .AzureMachine .Spec .FailureDomain
192219 }
@@ -221,12 +248,12 @@ func (m *MachineScope) Role() string {
221248}
222249
223250// GetVMID returns the AzureMachine instance id by parsing Spec.ProviderID.
224- func (m * MachineScope ) GetVMID () * string {
251+ func (m * MachineScope ) GetVMID () string {
225252 parsed , err := noderefutil .NewProviderID (m .GetProviderID ())
226253 if err != nil {
227- return nil
254+ return ""
228255 }
229- return pointer . StringPtr ( parsed .ID () )
256+ return parsed .ID ()
230257}
231258
232259// GetProviderID returns the AzureMachine providerID from the spec.
@@ -239,12 +266,15 @@ func (m *MachineScope) GetProviderID() string {
239266
240267// SetProviderID sets the AzureMachine providerID in spec.
241268func (m * MachineScope ) SetProviderID (v string ) {
242- m .AzureMachine .Spec .ProviderID = pointer .StringPtr (v )
269+ m .AzureMachine .Spec .ProviderID = to .StringPtr (v )
243270}
244271
245272// GetVMState returns the AzureMachine VM state.
246- func (m * MachineScope ) GetVMState () * infrav1.VMState {
247- return m .AzureMachine .Status .VMState
273+ func (m * MachineScope ) GetVMState () infrav1.VMState {
274+ if m .AzureMachine .Status .VMState != nil {
275+ return * m .AzureMachine .Status .VMState
276+ }
277+ return ""
248278}
249279
250280// SetVMState sets the AzureMachine VM state.
@@ -264,7 +294,7 @@ func (m *MachineScope) SetNotReady() {
264294
265295// SetFailureMessage sets the AzureMachine status failure message.
266296func (m * MachineScope ) SetFailureMessage (v error ) {
267- m .AzureMachine .Status .FailureMessage = pointer .StringPtr (v .Error ())
297+ m .AzureMachine .Status .FailureMessage = to .StringPtr (v .Error ())
268298}
269299
270300// SetFailureReason sets the AzureMachine status failure reason.
@@ -299,11 +329,12 @@ func (m *MachineScope) Close(ctx context.Context) error {
299329// the value from AzureMachine takes precedence.
300330func (m * MachineScope ) AdditionalTags () infrav1.Tags {
301331 tags := make (infrav1.Tags )
302-
303332 // Start with the cluster-wide tags...
304333 tags .Merge (m .ClusterDescriber .AdditionalTags ())
305334 // ... and merge in the Machine's
306335 tags .Merge (m .AzureMachine .Spec .AdditionalTags )
336+ // Set the cloud provider tag
337+ tags [infrav1 .ClusterAzureCloudProviderTagKey (m .ClusterName ())] = string (infrav1 .ResourceLifecycleOwned )
307338
308339 return tags
309340}
@@ -325,3 +356,13 @@ func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
325356 }
326357 return base64 .StdEncoding .EncodeToString (value ), nil
327358}
359+
360+ // Pick image from the machine configuration, or use a default one.
361+ func (m * MachineScope ) GetVMImage () (* infrav1.Image , error ) {
362+ // Use custom Marketplace image, Image ID or a Shared Image Gallery image if provided
363+ if m .AzureMachine .Spec .Image != nil {
364+ return m .AzureMachine .Spec .Image , nil
365+ }
366+ m .Info ("No image specified for machine, using default" , "machine" , m .AzureMachine .GetName ())
367+ return azure .GetDefaultUbuntuImage (to .String (m .Machine .Spec .Version ))
368+ }
0 commit comments