Skip to content

Commit

Permalink
Refactor VMSSExtension to use ResourceSpecGetter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Mar 28, 2022
1 parent c00ba97 commit 9891e80
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 235 deletions.
37 changes: 0 additions & 37 deletions azure/converters/vmextensions.go

This file was deleted.

4 changes: 1 addition & 3 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ func (m *MachineScope) VMExtensionSpecs() []azure.ResourceSpecGetter {
bootstrapExtensionSpec := azure.GetBootstrappingVMExtension(m.AzureMachine.Spec.OSDisk.OSType, m.CloudEnvironment(), m.Name())

if bootstrapExtensionSpec != nil {
// extensionSpec := converters.BootstrapingExtensionSpecToVMExtensionSpec(*bootstrapExtensionSpec, m.ResourceGroup(), m.Location())
// extensionSpecs = append(extensionSpecs, &extensionSpec)
extensionSpecs = append(extensionSpecs, &vmextensions.ExtensionSpec{
extensionSpecs = append(extensionSpecs, &vmextensions.VMExtensionSpec{
Name: bootstrapExtensionSpec.Name,
ResourceGroup: m.ResourceGroup(),
VMName: bootstrapExtensionSpec.VMName,
Expand Down
4 changes: 2 additions & 2 deletions azure/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
want: []azure.ResourceSpecGetter{
&vmextensions.ExtensionSpec{
&vmextensions.VMExtensionSpec{
Name: "CAPZ.Linux.Bootstrapping",
ResourceGroup: "my-rg",
VMName: "machine-name",
Expand Down Expand Up @@ -556,7 +556,7 @@ func TestMachineScope_VMExtensionSpecs(t *testing.T) {
},
},
want: []azure.ResourceSpecGetter{
&vmextensions.ExtensionSpec{
&vmextensions.VMExtensionSpec{
Name: "CAPZ.Windows.Bootstrapping",
ResourceGroup: "my-rg",
VMName: "machine-name",
Expand Down
6 changes: 2 additions & 4 deletions azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +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/vmextensions"
"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 @@ -603,15 +603,13 @@ func (m *MachinePoolScope) VMSSExtensionSpecs() []azure.ResourceSpecGetter {
bootstrapExtensionSpec := azure.GetBootstrappingVMExtension(m.AzureMachinePool.Spec.Template.OSDisk.OSType, m.CloudEnvironment(), m.Name())

if bootstrapExtensionSpec != nil {
// extensionSpec := converters.BootstrapingExtensionSpecToVMExtensionSpec(*bootstrapExtensionSpec, m.ResourceGroup(), m.Location())
extensionSpecs = append(extensionSpecs, &vmextensions.ExtensionSpec{
extensionSpecs = append(extensionSpecs, &vmssextensions.VMSSExtensionSpec{
Name: bootstrapExtensionSpec.Name,
ResourceGroup: m.ResourceGroup(),
VMName: bootstrapExtensionSpec.VMName,
Publisher: bootstrapExtensionSpec.Publisher,
Version: bootstrapExtensionSpec.Version,
ProtectedSettings: bootstrapExtensionSpec.ProtectedSettings,
Location: m.Location(),
})
}

Expand Down
26 changes: 3 additions & 23 deletions azure/scope/machinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmssextensions"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
Expand Down Expand Up @@ -654,15 +654,12 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ResourceSpecGetter{
&vmextensions.ExtensionSpec{
&vmssextensions.VMSSExtensionSpec{
Name: "CAPZ.Linux.Bootstrapping",
ResourceGroup: "my-rg",
VMName: "machinepool-name",
Expand All @@ -671,7 +668,6 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
ProtectedSettings: map[string]string{
"commandToExecute": azure.LinuxBootstrapExtensionCommand,
},
Location: "westus",
},
},
},
Expand Down Expand Up @@ -702,9 +698,6 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
Expand Down Expand Up @@ -739,15 +732,12 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
},
want: []azure.ResourceSpecGetter{
&vmextensions.ExtensionSpec{
&vmssextensions.VMSSExtensionSpec{
Name: "CAPZ.Windows.Bootstrapping",
ResourceGroup: "my-rg",
// Note: machine pool names longer than 9 characters get truncated. See MachinePoolScope::Name() for more details.
Expand All @@ -757,7 +747,6 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
ProtectedSettings: map[string]string{
"commandToExecute": azure.WindowsBootstrapExtensionCommand,
},
Location: "westus",
},
},
},
Expand Down Expand Up @@ -788,9 +777,6 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
Expand Down Expand Up @@ -824,9 +810,6 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
Expand Down Expand Up @@ -860,9 +843,6 @@ func TestMachinePoolScope_VMSSExtensionSpecs(t *testing.T) {
AzureCluster: &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
ResourceGroup: "my-rg",
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
Location: "westus",
},
},
},
},
Expand Down
17 changes: 3 additions & 14 deletions azure/services/scalesets/scalesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,11 @@ func (s *Service) generateExtensions() ([]compute.VirtualMachineScaleSetExtensio
if err != nil {
return nil, err
}
vmextension, ok := parameters.(compute.VirtualMachineExtension)
vmssextension, ok := parameters.(compute.VirtualMachineScaleSetExtension)
if !ok {
return nil, errors.Errorf("%T is not a compute.VirtualMachineExtension", parameters)
}

name := extensionSpec.ResourceName()
extensions[i] = compute.VirtualMachineScaleSetExtension{
Name: &name,
VirtualMachineScaleSetExtensionProperties: &compute.VirtualMachineScaleSetExtensionProperties{
Publisher: vmextension.Publisher,
Type: &name,
TypeHandlerVersion: vmextension.TypeHandlerVersion,
Settings: nil,
ProtectedSettings: vmextension.ProtectedSettings,
},
return nil, errors.Errorf("%T is not a compute.VirtualMachineScaleSetExtension", parameters)
}
extensions[i] = vmssextension
}

return extensions, nil
Expand Down
5 changes: 2 additions & 3 deletions azure/services/scalesets/scalesets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/scalesets/mock_scalesets"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmssextensions"
gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
Expand Down Expand Up @@ -1220,7 +1220,7 @@ func setupVMSSExpectationsWithoutVMImage(s *mock_scalesets.MockScaleSetScopeMock
s.ClusterName().Return("my-cluster")
s.GetBootstrapData(gomockinternal.AContext()).Return("fake-bootstrap-data", nil)
s.VMSSExtensionSpecs().Return([]azure.ResourceSpecGetter{
&vmextensions.ExtensionSpec{
&vmssextensions.VMSSExtensionSpec{
Name: "someExtension",
ResourceGroup: "my-rg",
VMName: "my-vmss",
Expand All @@ -1229,7 +1229,6 @@ func setupVMSSExpectationsWithoutVMImage(s *mock_scalesets.MockScaleSetScopeMock
ProtectedSettings: map[string]string{
"commandToExecute": "echo hello",
},
Location: "test-location",
},
}).AnyTimes()
}
Expand Down
2 changes: 0 additions & 2 deletions azure/services/vmextensions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ type azureClient struct {
vmextensions compute.VirtualMachineExtensionsClient
}

// var _ client = (*azureClient)(nil)

// newClient creates a new VM client from subscription ID.
func newClient(auth azure.Authorizer) *azureClient {
c := newVirtualMachineExtensionsClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
Expand Down
12 changes: 6 additions & 6 deletions azure/services/vmextensions/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/pkg/errors"
)

// ExtensionSpec defines the specification for a VM or VMScaleSet extension.
type ExtensionSpec struct {
// VMExtensionSpec defines the specification for a VM or VMScaleSet extension.
type VMExtensionSpec struct {
Name string
ResourceGroup string
VMName string
Expand All @@ -34,22 +34,22 @@ type ExtensionSpec struct {
}

// ResourceName returns the name of the VM extension.
func (s *ExtensionSpec) ResourceName() string {
func (s *VMExtensionSpec) ResourceName() string {
return s.Name
}

// ResourceGroupName returns the name of the resource group.
func (s *ExtensionSpec) ResourceGroupName() string {
func (s *VMExtensionSpec) ResourceGroupName() string {
return s.ResourceGroup
}

// OwnerResourceName returns the name of the VM that owns this VM extension.
func (s *ExtensionSpec) OwnerResourceName() string {
func (s *VMExtensionSpec) OwnerResourceName() string {
return s.VMName
}

// Parameters returns the parameters for the VM extension.
func (s *ExtensionSpec) Parameters(existing interface{}) (interface{}, error) {
func (s *VMExtensionSpec) Parameters(existing interface{}) (interface{}, error) {
if existing != nil {
_, ok := existing.(compute.VirtualMachineExtension)
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions azure/services/vmextensions/vmextensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

var (
extensionSpec1 = ExtensionSpec{
extensionSpec1 = VMExtensionSpec{
Name: "my-extension-1",
ResourceGroup: "my-rg",
VMName: "my-vm",
Expand Down Expand Up @@ -72,7 +72,7 @@ var (
Name: to.StringPtr("my-extension-1"),
}

extensionSpec2 = ExtensionSpec{
extensionSpec2 = VMExtensionSpec{
Name: "my-extension-2",
ResourceGroup: "my-rg",
VMName: "my-vm",
Expand Down
8 changes: 4 additions & 4 deletions azure/services/vmssextensions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// Client wraps go-sdk.
type client interface {
Get(context.Context, string, string, string) (compute.VirtualMachineScaleSetExtension, error)
Get(context.Context, azure.ResourceSpecGetter) (result interface{}, err error)
}

// AzureClient contains the Azure go-sdk Client.
Expand All @@ -50,10 +50,10 @@ func newVirtualMachineScaleSetExtensionsClient(subscriptionID string, baseURI st
return vmssextensionsClient
}

// Get creates or updates the virtual machine scale set extension.
func (ac *azureClient) Get(ctx context.Context, resourceGroupName, vmssName, name string) (compute.VirtualMachineScaleSetExtension, error) {
// Get the virtual machine scale set extension.
func (ac *azureClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "vmssextensions.AzureClient.Get")
defer done()

return ac.vmssextensions.Get(ctx, resourceGroupName, vmssName, name, "")
return ac.vmssextensions.Get(ctx, spec.ResourceGroupName(), spec.OwnerResourceName(), spec.ResourceName(), "")
}
12 changes: 6 additions & 6 deletions azure/services/vmssextensions/mock_vmssextensions/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9891e80

Please sign in to comment.