-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathkubernetes_nodepool.go
420 lines (359 loc) · 12.5 KB
/
kubernetes_nodepool.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package containers
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2020-03-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func SchemaDefaultNodePool() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Required
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.KubernetesAgentPoolName,
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(containerservice.VirtualMachineScaleSets),
ValidateFunc: validation.StringInSlice([]string{
string(containerservice.AvailabilitySet),
string(containerservice.VirtualMachineScaleSets),
}, false),
},
"vm_size": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
// Optional
"availability_zones": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"enable_auto_scaling": {
Type: schema.TypeBool,
Optional: true,
},
"enable_node_public_ip": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"max_count": {
Type: schema.TypeInt,
Optional: true,
// NOTE: rather than setting `0` users should instead pass `null` here
ValidateFunc: validation.IntBetween(1, 100),
},
"max_pods": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"min_count": {
Type: schema.TypeInt,
Optional: true,
// NOTE: rather than setting `0` users should instead pass `null` here
ValidateFunc: validation.IntBetween(1, 100),
},
"node_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 100),
},
"node_labels": {
Type: schema.TypeMap,
ForceNew: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"node_taints": {
Type: schema.TypeList,
ForceNew: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"tags": tags.Schema(),
"os_disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(1),
},
"vnet_subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"orchestrator_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
}
}
func ConvertDefaultNodePoolToAgentPool(input *[]containerservice.ManagedClusterAgentPoolProfile) containerservice.AgentPool {
defaultCluster := (*input)[0]
return containerservice.AgentPool{
Name: defaultCluster.Name,
ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{
Count: defaultCluster.Count,
VMSize: defaultCluster.VMSize,
OsDiskSizeGB: defaultCluster.OsDiskSizeGB,
VnetSubnetID: defaultCluster.VnetSubnetID,
MaxPods: defaultCluster.MaxPods,
OsType: defaultCluster.OsType,
MaxCount: defaultCluster.MaxCount,
MinCount: defaultCluster.MinCount,
EnableAutoScaling: defaultCluster.EnableAutoScaling,
Type: defaultCluster.Type,
OrchestratorVersion: defaultCluster.OrchestratorVersion,
AvailabilityZones: defaultCluster.AvailabilityZones,
EnableNodePublicIP: defaultCluster.EnableNodePublicIP,
ScaleSetPriority: defaultCluster.ScaleSetPriority,
ScaleSetEvictionPolicy: defaultCluster.ScaleSetEvictionPolicy,
SpotMaxPrice: defaultCluster.SpotMaxPrice,
Mode: defaultCluster.Mode,
NodeLabels: defaultCluster.NodeLabels,
NodeTaints: defaultCluster.NodeTaints,
Tags: defaultCluster.Tags,
},
}
}
func ExpandDefaultNodePool(d *schema.ResourceData) (*[]containerservice.ManagedClusterAgentPoolProfile, error) {
input := d.Get("default_node_pool").([]interface{})
raw := input[0].(map[string]interface{})
enableAutoScaling := raw["enable_auto_scaling"].(bool)
nodeLabelsRaw := raw["node_labels"].(map[string]interface{})
nodeLabels := utils.ExpandMapStringPtrString(nodeLabelsRaw)
nodeTaintsRaw := raw["node_taints"].([]interface{})
nodeTaints := utils.ExpandStringSlice(nodeTaintsRaw)
t := raw["tags"].(map[string]interface{})
profile := containerservice.ManagedClusterAgentPoolProfile{
EnableAutoScaling: utils.Bool(enableAutoScaling),
EnableNodePublicIP: utils.Bool(raw["enable_node_public_ip"].(bool)),
Name: utils.String(raw["name"].(string)),
NodeLabels: nodeLabels,
NodeTaints: nodeTaints,
Tags: tags.Expand(t),
Type: containerservice.AgentPoolType(raw["type"].(string)),
VMSize: containerservice.VMSizeTypes(raw["vm_size"].(string)),
// at this time the default node pool has to be Linux or the AKS cluster fails to provision with:
// Pods not in Running status: coredns-7fc597cc45-v5z7x,coredns-autoscaler-7ccc76bfbd-djl7j,metrics-server-cbd95f966-5rl97,tunnelfront-7d9884977b-wpbvn
// Windows agents can be configured via the separate node pool resource
OsType: containerservice.Linux,
// without this set the API returns:
// Code="MustDefineAtLeastOneSystemPool" Message="Must define at least one system pool."
// since this is the "default" node pool we can assume this is a system node pool
Mode: containerservice.System,
// // TODO: support these in time
// ScaleSetEvictionPolicy: "",
// ScaleSetPriority: "",
}
availabilityZonesRaw := raw["availability_zones"].([]interface{})
availabilityZones := utils.ExpandStringSlice(availabilityZonesRaw)
// otherwise: Standard Load Balancer is required for availability zone.
if len(*availabilityZones) > 0 {
profile.AvailabilityZones = availabilityZones
}
if maxPods := int32(raw["max_pods"].(int)); maxPods > 0 {
profile.MaxPods = utils.Int32(maxPods)
}
if osDiskSizeGB := int32(raw["os_disk_size_gb"].(int)); osDiskSizeGB > 0 {
profile.OsDiskSizeGB = utils.Int32(osDiskSizeGB)
}
if vnetSubnetID := raw["vnet_subnet_id"].(string); vnetSubnetID != "" {
profile.VnetSubnetID = utils.String(vnetSubnetID)
}
if orchestratorVersion := raw["orchestrator_version"].(string); orchestratorVersion != "" {
profile.OrchestratorVersion = utils.String(orchestratorVersion)
}
count := raw["node_count"].(int)
maxCount := raw["max_count"].(int)
minCount := raw["min_count"].(int)
// Count must always be set (see #6094), RP behavior has changed
// since the API version upgrade in v2.1.0 making Count required
// for all create/update requests
profile.Count = utils.Int32(int32(count))
if enableAutoScaling {
// if Count has not been set use min count
if count == 0 {
count = minCount
profile.Count = utils.Int32(int32(count))
}
// Count must be set for the initial creation when using AutoScaling but cannot be updated
if d.HasChange("default_node_pool.0.node_count") && !d.IsNewResource() {
return nil, fmt.Errorf("cannot change `node_count` when `enable_auto_scaling` is set to `true`")
}
if maxCount > 0 {
profile.MaxCount = utils.Int32(int32(maxCount))
if maxCount < count {
return nil, fmt.Errorf("`node_count`(%d) must be equal to or less than `max_count`(%d) when `enable_auto_scaling` is set to `true`", count, maxCount)
}
} else {
return nil, fmt.Errorf("`max_count` must be configured when `enable_auto_scaling` is set to `true`")
}
if minCount > 0 {
profile.MinCount = utils.Int32(int32(minCount))
if minCount > count {
return nil, fmt.Errorf("`node_count`(%d) must be equal to or greater than `min_count`(%d) when `enable_auto_scaling` is set to `true`", count, minCount)
}
} else {
return nil, fmt.Errorf("`min_count` must be configured when `enable_auto_scaling` is set to `true`")
}
if minCount > maxCount {
return nil, fmt.Errorf("`max_count` must be >= `min_count`")
}
} else if minCount > 0 || maxCount > 0 {
return nil, fmt.Errorf("`max_count`(%d) and `min_count`(%d) must be set to `null` when `enable_auto_scaling` is set to `false`", maxCount, minCount)
}
return &[]containerservice.ManagedClusterAgentPoolProfile{
profile,
}, nil
}
func FlattenDefaultNodePool(input *[]containerservice.ManagedClusterAgentPoolProfile, d *schema.ResourceData) (*[]interface{}, error) {
if input == nil {
return &[]interface{}{}, nil
}
agentPool, err := findDefaultNodePool(input, d)
if err != nil {
return nil, err
}
var availabilityZones []string
if agentPool.AvailabilityZones != nil {
availabilityZones = *agentPool.AvailabilityZones
}
count := 0
if agentPool.Count != nil {
count = int(*agentPool.Count)
}
enableAutoScaling := false
if agentPool.EnableAutoScaling != nil {
enableAutoScaling = *agentPool.EnableAutoScaling
}
enableNodePublicIP := false
if agentPool.EnableNodePublicIP != nil {
enableNodePublicIP = *agentPool.EnableNodePublicIP
}
maxCount := 0
if agentPool.MaxCount != nil {
maxCount = int(*agentPool.MaxCount)
}
maxPods := 0
if agentPool.MaxPods != nil {
maxPods = int(*agentPool.MaxPods)
}
minCount := 0
if agentPool.MinCount != nil {
minCount = int(*agentPool.MinCount)
}
name := ""
if agentPool.Name != nil {
name = *agentPool.Name
}
var nodeLabels map[string]string
if agentPool.NodeLabels != nil {
nodeLabels = make(map[string]string)
for k, v := range agentPool.NodeLabels {
nodeLabels[k] = *v
}
}
var nodeTaints []string
if agentPool.NodeTaints != nil {
nodeTaints = *agentPool.NodeTaints
}
osDiskSizeGB := 0
if agentPool.OsDiskSizeGB != nil {
osDiskSizeGB = int(*agentPool.OsDiskSizeGB)
}
vnetSubnetId := ""
if agentPool.VnetSubnetID != nil {
vnetSubnetId = *agentPool.VnetSubnetID
}
orchestratorVersion := ""
if agentPool.OrchestratorVersion != nil {
orchestratorVersion = *agentPool.OrchestratorVersion
}
return &[]interface{}{
map[string]interface{}{
"availability_zones": availabilityZones,
"enable_auto_scaling": enableAutoScaling,
"enable_node_public_ip": enableNodePublicIP,
"max_count": maxCount,
"max_pods": maxPods,
"min_count": minCount,
"name": name,
"node_count": count,
"node_labels": nodeLabels,
"node_taints": nodeTaints,
"os_disk_size_gb": osDiskSizeGB,
"tags": tags.Flatten(agentPool.Tags),
"type": string(agentPool.Type),
"vm_size": string(agentPool.VMSize),
"orchestrator_version": orchestratorVersion,
"vnet_subnet_id": vnetSubnetId,
},
}, nil
}
func findDefaultNodePool(input *[]containerservice.ManagedClusterAgentPoolProfile, d *schema.ResourceData) (*containerservice.ManagedClusterAgentPoolProfile, error) {
// first try loading this from the Resource Data if possible (e.g. when Created)
defaultNodePoolName := d.Get("default_node_pool.0.name")
var agentPool *containerservice.ManagedClusterAgentPoolProfile
if defaultNodePoolName != "" {
// find it
for _, v := range *input {
if v.Name != nil && *v.Name == defaultNodePoolName {
agentPool = &v
break
}
}
} else {
// otherwise we need to fall back to the name of the first agent pool
for _, v := range *input {
if v.Name == nil {
continue
}
if v.Mode != containerservice.System {
continue
}
defaultNodePoolName = *v.Name
agentPool = &v
break
}
if defaultNodePoolName == nil {
return nil, fmt.Errorf("Unable to Determine Default Agent Pool")
}
}
if agentPool == nil {
return nil, fmt.Errorf("The Default Agent Pool %q was not found", defaultNodePoolName)
}
return agentPool, nil
}