-
Notifications
You must be signed in to change notification settings - Fork 55
/
resource_spotinst_ocean_aks.go
284 lines (228 loc) · 8.4 KB
/
resource_spotinst_ocean_aks.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package spotinst
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/azure"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/client"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_auto_scaling"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_extensions"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_health"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_image"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_launch_specification"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_load_balancers"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_login"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_network"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_os_disk"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_strategy"
"github.com/spotinst/terraform-provider-spotinst/spotinst/ocean_aks_vm_sizes"
)
func resourceSpotinstOceanAKS() *schema.Resource {
setupClusterAKSResource()
return &schema.Resource{
Create: resourceSpotinstClusterAKSCreate,
Read: resourceSpotinstClusterAKSRead,
Update: resourceSpotinstClusterAKSUpdate,
Delete: resourceSpotinstClusterAKSDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: commons.OceanAKSResource.GetSchemaMap(),
}
}
func setupClusterAKSResource() {
fieldsMap := make(map[commons.FieldName]*commons.GenericField)
ocean_aks.Setup(fieldsMap)
ocean_aks_login.Setup(fieldsMap)
ocean_aks_launch_specification.Setup(fieldsMap)
ocean_aks_auto_scaling.Setup(fieldsMap)
ocean_aks_strategy.Setup(fieldsMap)
ocean_aks_health.Setup(fieldsMap)
ocean_aks_vm_sizes.Setup(fieldsMap)
ocean_aks_os_disk.Setup(fieldsMap)
ocean_aks_image.Setup(fieldsMap)
ocean_aks_extensions.Setup(fieldsMap)
ocean_aks_load_balancers.Setup(fieldsMap)
ocean_aks_network.Setup(fieldsMap)
commons.OceanAKSResource = commons.NewOceanAKSResource(fieldsMap)
}
// region Create
func resourceSpotinstClusterAKSCreate(resourceData *schema.ResourceData, meta interface{}) error {
log.Printf(string(commons.ResourceOnCreate), commons.OceanAKSResource.GetName())
importedCluster, err := importAKSCluster(resourceData, meta.(*Client))
if err != nil {
return err
}
cluster, err := commons.OceanAKSResource.OnCreate(importedCluster, resourceData, meta)
if err != nil {
return err
}
clusterID, err := createAKSCluster(cluster, meta.(*Client))
if err != nil {
return err
}
resourceData.SetId(spotinst.StringValue(clusterID))
log.Printf("ocean/aks: AKS cluster created successfully: %s", resourceData.Id())
return resourceSpotinstClusterAKSRead(resourceData, meta)
}
func createAKSCluster(cluster *azure.Cluster, spotinstClient *Client) (*string, error) {
if json, err := commons.ToJson(cluster); err != nil {
return nil, err
} else {
log.Printf("ocean/aks: cluster configuration: %s", json)
}
input := &azure.CreateClusterInput{
Cluster: cluster,
}
output, err := spotinstClient.ocean.CloudProviderAzure().CreateCluster(context.TODO(), input)
if err != nil {
return nil, fmt.Errorf("ocean/aks: failed to create cluster: %v", err)
}
return output.Cluster.ID, nil
}
// endregion
// region Read
func resourceSpotinstClusterAKSRead(resourceData *schema.ResourceData, meta interface{}) error {
clusterID := resourceData.Id()
log.Printf(string(commons.ResourceOnRead), commons.OceanAKSResource.GetName(), clusterID)
cluster, err := readAKSCluster(context.TODO(), clusterID, meta.(*Client))
if err != nil {
return err
}
// If nothing was found, return no state.
if cluster == nil {
resourceData.SetId("")
return nil
}
// Expose the controller cluster identifier.
if cluster.ControllerClusterID != nil {
_ = resourceData.Set(string(ocean_aks.ControllerClusterID),
spotinst.StringValue(cluster.ControllerClusterID))
}
if err := commons.OceanAKSResource.OnRead(cluster, resourceData, meta); err != nil {
return err
}
log.Printf("ocean/aks: cluster read successfully: %s", clusterID)
return nil
}
func readAKSCluster(ctx context.Context, clusterID string, spotinstClient *Client) (*azure.Cluster, error) {
input := &azure.ReadClusterInput{
ClusterID: spotinst.String(clusterID),
}
output, err := spotinstClient.ocean.CloudProviderAzure().ReadCluster(ctx, input)
if err != nil {
// If the cluster was not found, return nil so that we can show that it
// does not exist.
if errs, ok := err.(client.Errors); ok && len(errs) > 0 {
for _, err := range errs {
if err.Code == ErrCodeClusterNotFound {
return nil, nil
}
}
}
// Some other error, report it.
return nil, fmt.Errorf("ocean/aks: failed to read cluster: %v", err)
}
return output.Cluster, nil
}
// endregion
// region Update
func resourceSpotinstClusterAKSUpdate(resourceData *schema.ResourceData, meta interface{}) error {
clusterID := resourceData.Id()
log.Printf(string(commons.ResourceOnUpdate), commons.OceanAKSResource.GetName(), clusterID)
shouldUpdate, cluster, err := commons.OceanAKSResource.OnUpdate(resourceData, meta)
if err != nil {
return err
}
if shouldUpdate {
cluster.SetId(spotinst.String(clusterID))
if err := updateAKSCluster(cluster, meta.(*Client)); err != nil {
return err
}
}
log.Printf("ocean/aks: cluster updated successfully: %s", clusterID)
return resourceSpotinstClusterAKSRead(resourceData, meta)
}
func updateAKSCluster(cluster *azure.Cluster, spotinstClient *Client) error {
input := &azure.UpdateClusterInput{
Cluster: cluster,
}
if json, err := commons.ToJson(cluster); err != nil {
return err
} else {
log.Printf("ocean/aks: cluster update configuration: %s", json)
}
if _, err := spotinstClient.ocean.CloudProviderAzure().UpdateCluster(context.TODO(), input); err != nil {
return fmt.Errorf("ocean/aks: failed to update cluster: %v", err)
}
return nil
}
// endregion
// region Delete
func resourceSpotinstClusterAKSDelete(resourceData *schema.ResourceData, meta interface{}) error {
clusterID := resourceData.Id()
log.Printf(string(commons.ResourceOnDelete), commons.OceanAKSResource.GetName(), clusterID)
if err := deleteAKSCluster(clusterID, meta.(*Client)); err != nil {
return err
}
log.Printf("ocean/aks: cluster deleted successfully: %s", clusterID)
resourceData.SetId("")
return nil
}
func deleteAKSCluster(clusterID string, spotinstClient *Client) error {
input := &azure.DeleteClusterInput{
ClusterID: spotinst.String(clusterID),
}
if json, err := commons.ToJson(input); err != nil {
return err
} else {
log.Printf("ocean/aks: cluster delete configuration: %s", json)
}
if _, err := spotinstClient.ocean.CloudProviderAzure().DeleteCluster(context.TODO(), input); err != nil {
return fmt.Errorf("ocean/aks: failed to delete cluster: %v", err)
}
return nil
}
// endregion
// region Import
func importAKSCluster(resourceData *schema.ResourceData, spotinstClient *Client) (*azure.Cluster, error) {
var cluster *azure.Cluster
err := resource.Retry(time.Hour, func() *resource.RetryError {
input := &azure.ImportClusterInput{
ACDIdentifier: spotinst.String(resourceData.Get("acd_identifier").(string)),
Cluster: &azure.ImportCluster{
Name: spotinst.String(resourceData.Get("name").(string)),
AKS: &azure.AKS{
Name: spotinst.String(resourceData.Get("aks_name").(string)),
ResourceGroupName: spotinst.String(resourceData.Get("aks_resource_group_name").(string)),
}},
}
output, err := spotinstClient.ocean.CloudProviderAzure().ImportCluster(context.TODO(), input)
if err != nil {
// Check whether the request should be retried.
if errs, ok := err.(client.Errors); ok && len(errs) > 0 {
for _, e := range errs {
if strings.Contains(e.Code, "FAILED_TO_IMPORT_OCEAN_CLUSTER") {
return resource.RetryableError(e)
}
}
}
// Some other error, report it.
return resource.NonRetryableError(err)
}
cluster = output.Cluster
return nil
})
if err != nil {
return nil, fmt.Errorf("ocean/aks: failed to import cluster: %v", err)
}
return cluster, err
}
// endregion