Skip to content

Commit

Permalink
Make VM extension reconcile async and refactor VMSS extension spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Apr 4, 2022
1 parent 9075098 commit 05653ab
Show file tree
Hide file tree
Showing 22 changed files with 730 additions and 460 deletions.
13 changes: 12 additions & 1 deletion azure/scope/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,20 @@ import (

func specToString(spec azure.ResourceSpecGetter) string {
var sb strings.Builder
sb.WriteString("[ ")
sb.WriteString("{ ")
sb.WriteString(fmt.Sprintf("%+v ", spec))
sb.WriteString("}")
return sb.String()
}

func specArrayToString(specs []azure.ResourceSpecGetter) string {
var sb strings.Builder
sb.WriteString("[\n")
for _, spec := range specs {
sb.WriteString(fmt.Sprintf("\t%+v\n", specToString(spec)))
}
sb.WriteString("]")

return sb.String()
}

Expand Down
17 changes: 11 additions & 6 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachines"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -317,13 +318,17 @@ func (m *MachineScope) HasSystemAssignedIdentity() bool {
return m.AzureMachine.Spec.Identity == infrav1.VMIdentitySystemAssigned
}

// VMExtensionSpecs returns the vm extension specs.
func (m *MachineScope) VMExtensionSpecs() []azure.ExtensionSpec {
var extensionSpecs = []azure.ExtensionSpec{}
extensionSpec := azure.GetBootstrappingVMExtension(m.AzureMachine.Spec.OSDisk.OSType, m.CloudEnvironment(), m.Name())
// VMExtensionSpecs returns the VM extension specs.
func (m *MachineScope) VMExtensionSpecs() []azure.ResourceSpecGetter {
var extensionSpecs = []azure.ResourceSpecGetter{}
bootstrapExtensionSpec := azure.GetBootstrappingVMExtension(m.AzureMachine.Spec.OSDisk.OSType, m.CloudEnvironment(), m.Name())

if extensionSpec != nil {
extensionSpecs = append(extensionSpecs, *extensionSpec)
if bootstrapExtensionSpec != nil {
extensionSpecs = append(extensionSpecs, &vmextensions.VMExtensionSpec{
ExtensionSpec: *bootstrapExtensionSpec,
ResourceGroup: m.ResourceGroup(),
Location: m.Location(),
})
}

return extensionSpecs
Expand Down
117 changes: 82 additions & 35 deletions azure/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package scope

import (
"context"
"fmt"
"reflect"
"strings"
"testing"

autorestazure "github.com/Azure/go-autorest/autorest/azure"
Expand All @@ -36,20 +34,10 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/networkinterfaces"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func specArrayToString(specs []azure.ResourceSpecGetter) string {
var sb strings.Builder
sb.WriteString("[ ")
for _, spec := range specs {
sb.WriteString(fmt.Sprintf("%+v ", spec))
}
sb.WriteString("]")

return sb.String()
}

func TestMachineScope_Name(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -420,6 +408,9 @@ func TestMachineScope_RoleAssignmentSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
Expand Down Expand Up @@ -450,7 +441,7 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
tests := []struct {
name string
machineScope MachineScope
want []azure.ExtensionSpec
want []azure.ResourceSpecGetter
}{
{
name: "If OS type is Linux and cloud is AzurePublicCloud, it returns ExtensionSpec",
Expand All @@ -474,17 +465,29 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ExtensionSpec{
{
Name: "CAPZ.Linux.Bootstrapping",
VMName: "machine-name",
Publisher: "Microsoft.Azure.ContainerUpstream",
Version: "1.0",
ProtectedSettings: map[string]string{
"commandToExecute": azure.LinuxBootstrapExtensionCommand,
want: []azure.ResourceSpecGetter{
&vmextensions.VMExtensionSpec{
ExtensionSpec: azure.ExtensionSpec{
Name: "CAPZ.Linux.Bootstrapping",
VMName: "machine-name",
Publisher: "Microsoft.Azure.ContainerUpstream",
Version: "1.0",
ProtectedSettings: map[string]string{
"commandToExecute": azure.LinuxBootstrapExtensionCommand,
},
},
ResourceGroup: "my-rg",
Location: "westus",
},
},
},
Expand All @@ -510,9 +513,17 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ExtensionSpec{},
want: []azure.ResourceSpecGetter{},
},
{
name: "If OS type is Windows and cloud is AzurePublicCloud, it returns ExtensionSpec",
Expand All @@ -536,17 +547,29 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ExtensionSpec{
{
Name: "CAPZ.Windows.Bootstrapping",
VMName: "machine-name",
Publisher: "Microsoft.Azure.ContainerUpstream",
Version: "1.0",
ProtectedSettings: map[string]string{
"commandToExecute": azure.WindowsBootstrapExtensionCommand,
want: []azure.ResourceSpecGetter{
&vmextensions.VMExtensionSpec{
ExtensionSpec: azure.ExtensionSpec{
Name: "CAPZ.Windows.Bootstrapping",
VMName: "machine-name",
Publisher: "Microsoft.Azure.ContainerUpstream",
Version: "1.0",
ProtectedSettings: map[string]string{
"commandToExecute": azure.WindowsBootstrapExtensionCommand,
},
},
ResourceGroup: "my-rg",
Location: "westus",
},
},
},
Expand All @@ -572,9 +595,17 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ExtensionSpec{},
want: []azure.ResourceSpecGetter{},
},
{
name: "If OS type is not Linux or Windows and cloud is AzurePublicCloud, it returns empty",
Expand All @@ -598,9 +629,17 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ExtensionSpec{},
want: []azure.ResourceSpecGetter{},
},
{
name: "If OS type is not Windows or Linux and cloud is not AzurePublicCloud, it returns empty",
Expand All @@ -624,15 +663,23 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
},
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ExtensionSpec{},
want: []azure.ResourceSpecGetter{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.machineScope.VMExtensionSpecs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("VMExtensionSpecs() = %v, want %v", got, tt.want)
t.Errorf("VMExtensionSpecs() = \n%s, want \n%s", specArrayToString(got), specArrayToString(tt.want))
}
})
}
Expand Down
14 changes: 9 additions & 5 deletions azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure"
machinepool "sigs.k8s.io/cluster-api-provider-azure/azure/scope/strategies/machinepool_deployments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmssextensions"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
Expand Down Expand Up @@ -597,12 +598,15 @@ func (m *MachinePoolScope) HasSystemAssignedIdentity() bool {
}

// VMSSExtensionSpecs returns the vmss extension specs.
func (m *MachinePoolScope) VMSSExtensionSpecs() []azure.ExtensionSpec {
var extensionSpecs = []azure.ExtensionSpec{}
extensionSpec := azure.GetBootstrappingVMExtension(m.AzureMachinePool.Spec.Template.OSDisk.OSType, m.CloudEnvironment(), m.Name())
func (m *MachinePoolScope) VMSSExtensionSpecs() []azure.ResourceSpecGetter {
var extensionSpecs = []azure.ResourceSpecGetter{}
bootstrapExtensionSpec := azure.GetBootstrappingVMExtension(m.AzureMachinePool.Spec.Template.OSDisk.OSType, m.CloudEnvironment(), m.Name())

if extensionSpec != nil {
extensionSpecs = append(extensionSpecs, *extensionSpec)
if bootstrapExtensionSpec != nil {
extensionSpecs = append(extensionSpecs, &vmssextensions.VMSSExtensionSpec{
ExtensionSpec: *bootstrapExtensionSpec,
ResourceGroup: m.ResourceGroup(),
})
}

return extensionSpecs
Expand Down
Loading

0 comments on commit 05653ab

Please sign in to comment.