-
Notifications
You must be signed in to change notification settings - Fork 17
/
alicloud.go
181 lines (162 loc) · 5.88 KB
/
alicloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package provider
import (
"context"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
api "github.com/gardener/machine-controller-manager-provider-alicloud/pkg/alicloud/apis"
"github.com/gardener/machine-controller-manager-provider-alicloud/pkg/spi"
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/gardener/machine-controller-manager/pkg/util/provider/driver"
providerDriver "github.com/gardener/machine-controller-manager-provider-alicloud/pkg/alicloud"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/json"
"log"
)
/**
Orphaned Resources
- VMs:
Describe instances with specified tag name:<cluster-name>
Report/Print out instances found
Describe volumes attached to the instance (using instance id)
Report/Print out volumes found
Delete attached volumes found
Terminate instances found
- Disks:
Describe volumes with tag status:available
Report/Print out volumes found
Delete identified volumes
**/
func newSession(machineClass *v1alpha1.MachineClass, secret *v1.Secret) spi.ECSClient {
var (
providerSpec *api.ProviderSpec
sPI spi.PluginSPIImpl
)
err := json.Unmarshal([]byte(machineClass.ProviderSpec.Raw), &providerSpec)
if err != nil {
providerSpec = nil
log.Printf("Error occured while performing unmarshal %s", err.Error())
}
sess, err := sPI.NewECSClient(secret, providerSpec.Region)
if err != nil {
log.Printf("Error occured while creating new session %s", err)
}
return sess
}
func getMachines(machineClass *v1alpha1.MachineClass, secretData map[string][]byte) ([]string, error) {
var machines []string
var sPI spi.PluginSPIImpl
driverProvider := providerDriver.NewAlicloudPlugin(&sPI)
machineList, err := driverProvider.ListMachines(context.TODO(), &driver.ListMachinesRequest{
MachineClass: machineClass,
Secret: &v1.Secret{Data: secretData},
})
if err != nil {
return nil, err
} else if len(machineList.MachineList) != 0 {
for _, machine := range machineList.MachineList {
machines = append(machines, machine)
}
}
return machines, nil
}
// getOrphanedInstances returns list of Orphan resources.
func getOrphanedInstances(tagName string, tagValue string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) ([]string, error) {
sess := newSession(machineClass, &v1.Secret{Data: secretData})
var instancesID []string
var tags = &[]ecs.DescribeInstancesTag{{Key: tagName, Value: tagValue}}
input := ecs.CreateDescribeInstancesRequest()
input.Status = "running"
input.Tag = tags
result, err := sess.DescribeInstances(input)
if err != nil {
return instancesID, err
}
for _, instance := range result.Instances.Instance {
instancesID = append(instancesID, instance.InstanceId)
}
return instancesID, nil
}
// getOrphanedDisks returns list of Orphan disks.
func getOrphanedDisks(tagName string, tagValue string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) ([]string, error) {
sess := newSession(machineClass, &v1.Secret{Data: secretData})
var volumeID []string
var tags = &[]ecs.DescribeDisksTag{{Key: tagName, Value: tagValue}}
input := ecs.CreateDescribeDisksRequest()
input.Status = "Available"
input.Tag = tags
result, err := sess.DescribeDisks(input)
if err != nil {
return volumeID, err
}
for _, disk := range result.Disks.Disk {
volumeID = append(volumeID, disk.DiskId)
}
return volumeID, nil
}
// getOrphanedNICs returns list of Orphan NICs
func getOrphanedNICs(tagName string, tagValue string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) ([]string, error) {
sess := newSession(machineClass, &v1.Secret{Data: secretData})
var nicIDs []string
var tags = &[]ecs.DescribeNetworkInterfacesTag{{Key: tagName, Value: tagValue}}
input := ecs.CreateDescribeNetworkInterfacesRequest()
input.Tag = tags
result, err := sess.DescribeNetworkInterfaces(input)
if err != nil {
return nicIDs, err
}
for _, nic := range result.NetworkInterfaceSets.NetworkInterfaceSet {
nicIDs = append(nicIDs, nic.NetworkInterfaceId)
}
return nicIDs, nil
}
func cleanOrphanResources(instanceIds []string, volumeIds []string, NICIds []string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) (delErrInstanceID []string, delErrVolumeIds []string, delErrNICs []string) {
for _, instanceID := range instanceIds {
if err := terminateInstance(instanceID, machineClass, secretData); err != nil {
fmt.Printf("error in deleting instance : %v", err)
delErrInstanceID = append(delErrInstanceID, instanceID)
}
}
for _, volumeID := range volumeIds {
if err := deleteVolume(volumeID, machineClass, secretData); err != nil {
fmt.Printf("error in deleting volume : %v", err)
delErrVolumeIds = append(delErrVolumeIds, volumeID)
}
}
for _, nicID := range NICIds {
if err := deleteNIC(nicID, machineClass, secretData); err != nil {
fmt.Printf("error in deleting volume : %v", err)
delErrNICs = append(delErrNICs, nicID)
}
}
return
}
func deleteNIC(nicID string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) error {
sess := newSession(machineClass, &v1.Secret{Data: secretData})
input := ecs.CreateDeleteNetworkInterfaceRequest()
input.NetworkInterfaceId = nicID
_, err := sess.DeleteNetworkInterface(input)
if err != nil {
return err
}
return nil
}
func deleteVolume(diskID string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) error {
sess := newSession(machineClass, &v1.Secret{Data: secretData})
input := ecs.CreateDeleteDiskRequest()
input.DiskId = diskID
_, err := sess.DeleteDisk(input)
if err != nil {
return err
}
return nil
}
func terminateInstance(instanceID string, machineClass *v1alpha1.MachineClass, secretData map[string][]byte) error {
sess := newSession(machineClass, &v1.Secret{Data: secretData})
input := ecs.CreateDeleteInstanceRequest()
input.InstanceId = instanceID
_, err := sess.DeleteInstance(input)
if err != nil {
return err
}
return nil
}