Skip to content

Commit 3b2f7fd

Browse files
authored
Merge pull request #827 from CecileRobertMichon/vm-refactor
πŸ’Ž Refactor VM service spec
2 parents b431f06 + d2f4b77 commit 3b2f7fd

File tree

22 files changed

+1122
-1290
lines changed

22 files changed

+1122
-1290
lines changed

β€Žcloud/converters/vmss.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package converters
1818

1919
import (
20-
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
20+
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
2121
"github.com/Azure/go-autorest/autorest/to"
2222

2323
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"

β€Žcloud/converters/vmss_test.goβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"testing"
2222

23-
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
23+
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
2424
"github.com/Azure/go-autorest/autorest/to"
2525
"github.com/onsi/gomega"
2626

β€Žcloud/defaults.goβ€Ž

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package azure
1818

1919
import (
2020
"fmt"
21-
2221
"github.com/blang/semver"
2322
"github.com/pkg/errors"
2423
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
@@ -41,27 +40,6 @@ const (
4140
LatestVersion = "latest"
4241
)
4342

44-
// SupportedAvailabilityZoneLocations is a slice of the locations where Availability Zones are supported.
45-
// This is used to validate whether a virtual machine should leverage an Availability Zone.
46-
// Based on the Availability Zones listed in https://docs.microsoft.com/en-us/azure/availability-zones/az-overview
47-
var SupportedAvailabilityZoneLocations = []string{
48-
// Americas
49-
"centralus",
50-
"eastus",
51-
"eastus2",
52-
"westus2",
53-
54-
// Europe
55-
"francecentral",
56-
"northeurope",
57-
"uksouth",
58-
"westeurope",
59-
60-
// Asia Pacific
61-
"japaneast",
62-
"southeastasia",
63-
}
64-
6543
// GenerateInternalLBName generates a internal load balancer name, based on the cluster name.
6644
func GenerateInternalLBName(clusterName string) string {
6745
return fmt.Sprintf("%s-%s", clusterName, "internal-lb")

β€Žcloud/scope/machine.goβ€Ž

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ package scope
1919
import (
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.
91109
func (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.
151177
func (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
186212
func (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.
241268
func (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.
266296
func (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.
300330
func (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+
}

β€Žcloud/scope/machinepool.goβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package scope
1919
import (
2020
"context"
2121
"encoding/base64"
22+
"github.com/Azure/go-autorest/autorest/to"
2223
"github.com/go-logr/logr"
2324
"github.com/pkg/errors"
2425
corev1 "k8s.io/api/core/v1"
@@ -177,3 +178,13 @@ func (m *MachinePoolScope) GetBootstrapData(ctx context.Context) (string, error)
177178
}
178179
return base64.StdEncoding.EncodeToString(value), nil
179180
}
181+
182+
// Pick image from the machine configuration, or use a default one.
183+
func (m *MachinePoolScope) GetVMImage() (*infrav1.Image, error) {
184+
// Use custom Marketplace image, Image ID or a Shared Image Gallery image if provided
185+
if m.AzureMachinePool.Spec.Template.Image != nil {
186+
return m.AzureMachinePool.Spec.Template.Image, nil
187+
}
188+
m.Info("No image specified for machine, using default", "machine", m.MachinePool.GetName())
189+
return azure.GetDefaultUbuntuImage(to.String(m.MachinePool.Spec.Template.Spec.Version))
190+
}

β€Žcloud/services/networkinterfaces/networkinterfaces_test.goβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import (
3737
"sigs.k8s.io/cluster-api-provider-azure/cloud/services/resourceskus"
3838
"sigs.k8s.io/cluster-api-provider-azure/cloud/services/subnets/mock_subnets"
3939

40-
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
41-
network "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
40+
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
41+
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
4242
"k8s.io/klog/klogr"
4343
"k8s.io/utils/pointer"
4444
)

β€Žcloud/services/virtualmachineextensions/client.goβ€Ž

Lines changed: 0 additions & 86 deletions
This file was deleted.

β€Žcloud/services/virtualmachineextensions/mock_virtualmachineextensions/doc.goβ€Ž

Lines changed: 0 additions & 20 deletions
This file was deleted.

β€Žcloud/services/virtualmachineextensions/service.goβ€Ž

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
Β (0)