Skip to content

Commit

Permalink
delete data disks when deleting azure machine
Browse files Browse the repository at this point in the history
  • Loading branch information
nader-ziada committed Sep 23, 2020
1 parent 7612824 commit 81df829
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cloud/scope/machine.go
Expand Up @@ -118,7 +118,7 @@ func (m *MachineScope) TagsSpecs() []azure.TagsSpec {
}
}

// PublicIPSpec returns the public IP specs.
// PublicIPSpecs returns the public IP specs.
func (m *MachineScope) PublicIPSpecs() []azure.PublicIPSpec {
var spec []azure.PublicIPSpec
if m.AzureMachine.Spec.AllocatePublicIP == true {
Expand Down Expand Up @@ -183,6 +183,7 @@ func (m *MachineScope) NICSpecs() []azure.NICSpec {
return specs
}

// NICNames returns the NIC names
func (m *MachineScope) NICNames() []string {
nicNames := make([]string, len(m.NICSpecs()))
for i, nic := range m.NICSpecs() {
Expand All @@ -196,8 +197,12 @@ func (m *MachineScope) DiskSpecs() []azure.DiskSpec {
spec := azure.DiskSpec{
Name: azure.GenerateOSDiskName(m.Name()),
}
disks := []azure.DiskSpec{spec}

return []azure.DiskSpec{spec}
for _, dd := range m.AzureMachine.Spec.DataDisks {
disks = append(disks, azure.DiskSpec{Name: azure.GenerateDataDiskName(m.Name(), dd.NameSuffix)})
}
return disks
}

// BastionSpecs returns the bastion specs.
Expand Down
140 changes: 140 additions & 0 deletions cloud/services/disks/disks_test.go
Expand Up @@ -23,8 +23,14 @@ import (

"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
"sigs.k8s.io/cluster-api-provider-azure/cloud/scope"
"sigs.k8s.io/cluster-api-provider-azure/cloud/services/disks/mock_disks"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/golang/mock/gomock"

Expand Down Expand Up @@ -120,3 +126,137 @@ func TestDeleteDisk(t *testing.T) {
})
}
}

func TestDiskSpecs(t *testing.T) {
testcases := []struct {
name string
azureMachine *infrav1.AzureMachine
expectedDisks []azure.DiskSpec
}{
{
name: "only os disk",
azureMachine: &infrav1.AzureMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "my-azure-machine",
},
Spec: infrav1.AzureMachineSpec{
OSDisk: infrav1.OSDisk{
DiskSizeGB: 30,
OSType: "Linux",
},
},
},
expectedDisks: []azure.DiskSpec{
{
Name: "my-azure-machine_OSDisk",
},
},
}, {
name: "os and data disks",
azureMachine: &infrav1.AzureMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "my-azure-machine",
},
Spec: infrav1.AzureMachineSpec{
OSDisk: infrav1.OSDisk{
DiskSizeGB: 30,
OSType: "Linux",
},
DataDisks: []infrav1.DataDisk{{
NameSuffix: "etcddisk",
}},
},
},
expectedDisks: []azure.DiskSpec{
{
Name: "my-azure-machine_OSDisk",
},
{
Name: "my-azure-machine_etcddisk",
},
},
}, {
name: "os and multiple data disks",
azureMachine: &infrav1.AzureMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "my-azure-machine",
},
Spec: infrav1.AzureMachineSpec{
OSDisk: infrav1.OSDisk{
DiskSizeGB: 30,
OSType: "Linux",
},
DataDisks: []infrav1.DataDisk{
{
NameSuffix: "etcddisk",
},
{
NameSuffix: "otherdisk",
}},
},
},
expectedDisks: []azure.DiskSpec{
{
Name: "my-azure-machine_OSDisk",
},
{
Name: "my-azure-machine_etcddisk",
},
{
Name: "my-azure-machine_otherdisk",
},
},
}}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(infrav1.AddToScheme(scheme)).ToNot(HaveOccurred())
g.Expect(clusterv1.AddToScheme(scheme)).ToNot(HaveOccurred())

t.Parallel()
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
},
}
azureCluster := &infrav1.AzureCluster{
Spec: infrav1.AzureClusterSpec{
SubscriptionID: "1234",
},
}
machine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "my-machine",
},
}
initObjects := []runtime.Object{
cluster,
machine,
azureCluster,
tc.azureMachine,
}
client := fake.NewFakeClientWithScheme(scheme, initObjects...)
clusterScope, err := scope.NewClusterScope(scope.ClusterScopeParams{
AzureClients: scope.AzureClients{
Authorizer: autorest.NullAuthorizer{},
},
Client: client,
Cluster: cluster,
AzureCluster: azureCluster,
})
g.Expect(err).NotTo(HaveOccurred())
machineScope, err := scope.NewMachineScope(scope.MachineScopeParams{
Client: client,
ClusterDescriber: clusterScope,
Machine: machine,
AzureMachine: tc.azureMachine,
})
g.Expect(err).NotTo(HaveOccurred())

output := machineScope.DiskSpecs()
g.Expect(output).To(Equal(tc.expectedDisks))
})
}
}

0 comments on commit 81df829

Please sign in to comment.