/
eks_metadata.go
413 lines (349 loc) · 11.6 KB
/
eks_metadata.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//
package model
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
const (
ProvisionerEKS = "eks"
NodeGroupWorker = "worker"
)
// EKSMetadata is metadata for EKS cluster and node groups.
type EKSMetadata struct {
Name string
Version string
AMI string
VPC string
Networking string
ClusterRoleARN string
NodeRoleARN string
MaxPodsPerNode int64
NodeGroups map[string]NodeGroupMetadata
ChangeRequest *EKSMetadataRequestedState `json:"ChangeRequest,omitempty"`
Warnings []string `json:"Warnings,omitempty"`
}
// NodeGroupMetadata is the metadata of an instance group.
type NodeGroupMetadata struct {
Name string
Type string `json:"Type,omitempty"`
InstanceType string `json:"InstanceType,omitempty"`
MinCount int64 `json:"MinCount,omitempty"`
MaxCount int64 `json:"MaxCount,omitempty"`
WithPublicSubnet bool `json:"WithPublicSubnet,omitempty"`
WithSecurityGroup bool `json:"WithSecurityGroup,omitempty"`
}
// EKSMetadataRequestedState is the requested state for eks metadata.
type EKSMetadataRequestedState struct {
Version string `json:"Version,omitempty"`
AMI string `json:"AMI,omitempty"`
MaxPodsPerNode int64 `json:"MaxPodsPerNode,omitempty"`
Networking string `json:"Networking,omitempty"`
VPC string `json:"VPC,omitempty"`
ClusterRoleARN string `json:"ClusterRoleARN,omitempty"`
NodeRoleARN string `json:"NodeRoleARN,omitempty"`
NodeGroups map[string]NodeGroupMetadata `json:"NodeGroups,omitempty"`
}
// CopyMissingFieldsFrom copy empty fields from the given NodeGroupMetadata to the current metadata.
func (ng *NodeGroupMetadata) CopyMissingFieldsFrom(other NodeGroupMetadata) {
if len(ng.Type) == 0 {
ng.Type = other.Type
}
if ng.InstanceType == "" {
ng.InstanceType = other.InstanceType
}
if ng.MinCount == 0 {
ng.MinCount = other.MinCount
}
if ng.MaxCount == 0 {
ng.MaxCount = other.MaxCount
}
if !ng.WithPublicSubnet {
ng.WithPublicSubnet = other.WithPublicSubnet
}
if !ng.WithSecurityGroup {
ng.WithSecurityGroup = other.WithSecurityGroup
}
}
func (em *EKSMetadata) ApplyClusterCreateRequest(createRequest *CreateClusterRequest) bool {
em.ChangeRequest = &EKSMetadataRequestedState{
Version: createRequest.Version,
AMI: createRequest.AMI,
MaxPodsPerNode: createRequest.MaxPodsPerNode,
VPC: createRequest.VPC,
ClusterRoleARN: createRequest.ClusterRoleARN,
NodeRoleARN: createRequest.NodeRoleARN,
NodeGroups: map[string]NodeGroupMetadata{},
}
nodeGroups := createRequest.AdditionalNodeGroups
if nodeGroups == nil {
nodeGroups = map[string]NodeGroupMetadata{}
}
nodeGroups[NodeGroupWorker] = NodeGroupMetadata{
InstanceType: createRequest.NodeInstanceType,
MinCount: createRequest.NodeMinCount,
MaxCount: createRequest.NodeMaxCount,
}
for _, ng := range createRequest.NodeGroupWithPublicSubnet {
nodeGroup := nodeGroups[ng]
nodeGroup.WithPublicSubnet = true
nodeGroups[ng] = nodeGroup
}
for _, ng := range createRequest.NodeGroupWithSecurityGroup {
nodeGroup := nodeGroups[ng]
nodeGroup.WithSecurityGroup = true
nodeGroups[ng] = nodeGroup
}
for name, ng := range nodeGroups {
em.ChangeRequest.NodeGroups[name] = NodeGroupMetadata{
Name: fmt.Sprintf("%s-%s", name, NewNodeGroupSuffix()),
Type: name,
InstanceType: ng.InstanceType,
MinCount: ng.MinCount,
MaxCount: ng.MaxCount,
WithPublicSubnet: ng.WithPublicSubnet,
WithSecurityGroup: ng.WithSecurityGroup,
}
}
em.NodeGroups = map[string]NodeGroupMetadata{}
return true
}
// ApplyUpgradePatch applies the patch to the given cluster's metadata.
func (em *EKSMetadata) ApplyUpgradePatch(patchRequest *PatchUpgradeClusterRequest) bool {
changes := &EKSMetadataRequestedState{}
var applied bool
if patchRequest.Version != nil && *patchRequest.Version != em.Version {
applied = true
changes.Version = *patchRequest.Version
}
if patchRequest.AMI != nil && *patchRequest.AMI != em.AMI {
applied = true
changes.AMI = *patchRequest.AMI
}
if patchRequest.MaxPodsPerNode != nil && *patchRequest.MaxPodsPerNode != em.MaxPodsPerNode {
applied = true
changes.MaxPodsPerNode = *patchRequest.MaxPodsPerNode
}
if applied {
changes.NodeGroups = map[string]NodeGroupMetadata{}
for ng := range em.NodeGroups {
changes.NodeGroups[ng] = NodeGroupMetadata{
Name: fmt.Sprintf("%s-%s", ng, NewNodeGroupSuffix()),
}
}
em.ChangeRequest = changes
}
return applied
}
func (em *EKSMetadata) ValidateClusterSizePatch(patchRequest *PatchClusterSizeRequest) error {
nodeGroups := patchRequest.NodeGroups
if len(em.NodeGroups) == 0 {
return errors.New("no nodegroups available to resize")
}
if len(nodeGroups) == 0 {
if len(em.NodeGroups) > 1 {
return errors.New("must specify nodegroups to resize")
}
for ng := range em.NodeGroups {
nodeGroups = append(nodeGroups, ng)
}
}
for _, ngToResize := range nodeGroups {
if _, f := em.NodeGroups[ngToResize]; !f {
return errors.Errorf("nodegroup %s not found to resize", ngToResize)
}
}
if patchRequest.NodeMinCount != nil && patchRequest.NodeMaxCount != nil {
if *patchRequest.NodeMinCount > *patchRequest.NodeMaxCount {
return errors.New("min node count cannot be greater than max node count")
}
return nil
}
if patchRequest.NodeMinCount != nil {
for _, ngToResize := range nodeGroups {
ng := em.NodeGroups[ngToResize]
nodeMaxCount := ng.MaxCount
if *patchRequest.NodeMinCount > nodeMaxCount {
return errors.New("resize patch would set min node count higher than max node count")
}
}
}
if patchRequest.NodeMaxCount != nil {
for _, ngToResize := range nodeGroups {
ng := em.NodeGroups[ngToResize]
nodeMinCount := ng.MinCount
if *patchRequest.NodeMaxCount < nodeMinCount {
return errors.New("resize patch would set max node count lower than min node count")
}
}
}
if patchRequest.MasterInstanceType != nil {
return errors.New("master instance type cannot be changed on an EKS cluster")
}
return nil
}
func (em *EKSMetadata) ApplyClusterSizePatch(patchRequest *PatchClusterSizeRequest) bool {
changes := &EKSMetadataRequestedState{
NodeGroups: map[string]NodeGroupMetadata{},
}
var applied bool
nodeGroupsMeta := patchRequest.NodeGroups
if len(nodeGroupsMeta) == 0 {
for ngPrefix := range em.NodeGroups {
nodeGroupsMeta = append(nodeGroupsMeta, ngPrefix)
}
}
for _, ng := range nodeGroupsMeta {
ngChangeRequest := NodeGroupMetadata{
Name: fmt.Sprintf("%s-%s", ng, NewNodeGroupSuffix()),
}
if patchRequest.NodeInstanceType != nil {
applied = true
ngChangeRequest.InstanceType = *patchRequest.NodeInstanceType
}
if patchRequest.NodeMinCount != nil {
applied = true
ngChangeRequest.MinCount = *patchRequest.NodeMinCount
}
if patchRequest.NodeMaxCount != nil {
applied = true
ngChangeRequest.MaxCount = *patchRequest.NodeMaxCount
}
changes.NodeGroups[ng] = ngChangeRequest
}
if applied {
em.ChangeRequest = changes
}
return applied
}
// ValidateChangeRequest ensures that the ChangeRequest has at least one
// actionable value.
func (em *EKSMetadata) ValidateChangeRequest() error {
changeRequest := em.ChangeRequest
if changeRequest == nil {
return errors.New("the EKS Metadata ChangeRequest is nil")
}
changeAllowed := false
if len(changeRequest.Version) != 0 || len(changeRequest.AMI) != 0 || changeRequest.MaxPodsPerNode != 0 {
changeAllowed = true
}
if changeAllowed {
return nil
}
for _, ng := range changeRequest.NodeGroups {
if len(ng.InstanceType) != 0 || ng.MinCount != 0 || ng.MaxCount != 0 {
changeAllowed = true
break
}
}
if !changeAllowed {
return errors.New("the EKS Metadata ChangeRequest has no change values set")
}
return nil
}
// ApplyChangeRequest applies change request values to the KopsMetadata that are
// not reflected by calling refreshKopsMetadata().
func (em *EKSMetadata) ApplyChangeRequest() {
}
// ValidateNodegroupsCreateRequest ensures that the nodegroups to create do not
// already exist.
func (em *EKSMetadata) ValidateNodegroupsCreateRequest(nodegroups map[string]NodeGroupMetadata) error {
if len(nodegroups) == 0 {
return errors.New("must specify at least one nodegroup to create")
}
for ng := range nodegroups {
if _, f := em.NodeGroups[ng]; f {
return errors.Errorf("nodegroup %s already exists", ng)
}
}
return nil
}
// ValidateNodegroupDeleteRequest ensures that the nodegroup to delete exists.
func (em *EKSMetadata) ValidateNodegroupDeleteRequest(nodegroup string) error {
if _, f := em.NodeGroups[nodegroup]; !f {
return errors.Errorf("nodegroup %s not found to delete", nodegroup)
}
return nil
}
// ApplyNodegroupsCreateRequest applies the nodegroups to create to the
// KopsMetadata.
func (em *EKSMetadata) ApplyNodegroupsCreateRequest(request *CreateNodegroupsRequest) {
em.ChangeRequest = &EKSMetadataRequestedState{
NodeGroups: map[string]NodeGroupMetadata{},
}
nodeGroups := request.Nodegroups
for _, ng := range request.NodeGroupWithPublicSubnet {
nodeGroup := nodeGroups[ng]
nodeGroup.WithPublicSubnet = true
nodeGroups[ng] = nodeGroup
}
for _, ng := range request.NodeGroupWithSecurityGroup {
nodeGroup := nodeGroups[ng]
nodeGroup.WithSecurityGroup = true
nodeGroups[ng] = nodeGroup
}
for name, ng := range nodeGroups {
em.ChangeRequest.NodeGroups[name] = NodeGroupMetadata{
Name: fmt.Sprintf("%s-%s", name, NewNodeGroupSuffix()),
Type: name,
InstanceType: ng.InstanceType,
MinCount: ng.MinCount,
MaxCount: ng.MaxCount,
WithPublicSubnet: ng.WithPublicSubnet,
WithSecurityGroup: ng.WithSecurityGroup,
}
}
}
// ApplyNodegroupDeleteRequest applies the nodegroup to delete to the
// KopsMetadata.
func (em *EKSMetadata) ApplyNodegroupDeleteRequest(nodegroup string) {
if em.NodeGroups == nil {
return
}
em.ChangeRequest = &EKSMetadataRequestedState{
NodeGroups: map[string]NodeGroupMetadata{
nodegroup: {
Name: em.NodeGroups[nodegroup].Name,
},
},
}
}
func (em *EKSMetadata) GetCommonMetadata() ProvisionerMetadata {
workerNodeGroup := em.NodeGroups[NodeGroupWorker]
return ProvisionerMetadata{
Name: em.Name,
Version: em.Version,
AMI: em.AMI,
NodeInstanceType: workerNodeGroup.InstanceType,
NodeMinCount: workerNodeGroup.MinCount,
NodeMaxCount: workerNodeGroup.MaxCount,
MaxPodsPerNode: em.MaxPodsPerNode,
VPC: em.VPC,
Networking: em.Networking,
}
}
// ClearChangeRequest clears the kops metadata change request.
func (em *EKSMetadata) ClearChangeRequest() {
em.ChangeRequest = nil
}
// ClearWarnings clears the kops metadata warnings.
func (em *EKSMetadata) ClearWarnings() {
em.Warnings = []string{}
}
// NewEKSMetadata creates an instance of EKSMetadata given the raw provisioner metadata.
func NewEKSMetadata(metadataBytes []byte) (*EKSMetadata, error) {
// Check if length of metadata is 0 as opposed to if the value is nil. This
// is done to avoid an issue encountered where the metadata value provided
// had a length of 0, but had non-zero capacity.
if len(metadataBytes) == 0 || string(metadataBytes) == "null" {
return nil, nil
}
eksMetadata := EKSMetadata{}
err := json.Unmarshal(metadataBytes, &eksMetadata)
if err != nil {
return nil, err
}
return &eksMetadata, nil
}