-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
service.go
147 lines (123 loc) · 3.74 KB
/
service.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
package managed
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/weaveworks/eksctl/pkg/awsapi"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
)
// A Service provides methods for managing managed nodegroups
type Service struct {
eksAPI awsapi.EKS
launchTemplateFetcher *builder.LaunchTemplateFetcher
clusterName string
stackCollection manager.StackManager
}
// HealthIssue represents a health issue with a managed nodegroup
type HealthIssue struct {
Message string
Code string
}
// TODO use goformation types
const (
labelsPath = "Resources.ManagedNodeGroup.Properties.Labels"
)
func NewService(eksAPI awsapi.EKS, ec2API awsapi.EC2,
stackCollection manager.StackManager, clusterName string) *Service {
return &Service{
eksAPI: eksAPI,
stackCollection: stackCollection,
launchTemplateFetcher: builder.NewLaunchTemplateFetcher(ec2API),
clusterName: clusterName,
}
}
// GetHealth fetches the health status for a nodegroup
func (m *Service) GetHealth(ctx context.Context, nodeGroupName string) ([]HealthIssue, error) {
input := &eks.DescribeNodegroupInput{
ClusterName: &m.clusterName,
NodegroupName: &nodeGroupName,
}
output, err := m.eksAPI.DescribeNodegroup(ctx, input)
if err != nil {
if IsNotFound(err) {
return nil, errors.Wrapf(err, "could not find a managed nodegroup with name %q", nodeGroupName)
}
return nil, err
}
health := output.Nodegroup.Health
if health == nil || len(health.Issues) == 0 {
// No health issues
return nil, nil
}
var healthIssues []HealthIssue
for _, issue := range health.Issues {
healthIssues = append(healthIssues, HealthIssue{
Message: *issue.Message,
Code: string(issue.Code),
})
}
return healthIssues, nil
}
// UpdateLabels adds or removes labels for a nodegroup
func (m *Service) UpdateLabels(ctx context.Context, nodeGroupName string, labelsToAdd map[string]string, labelsToRemove []string) error {
template, err := m.stackCollection.GetManagedNodeGroupTemplate(ctx, manager.GetNodegroupOption{
NodeGroupName: nodeGroupName,
})
if err != nil {
return err
}
newLabels, err := extractLabels(template)
if err != nil {
return err
}
for k, v := range labelsToAdd {
newLabels[k] = v
}
for _, k := range labelsToRemove {
delete(newLabels, k)
}
template, err = sjson.Set(template, labelsPath, newLabels)
if err != nil {
return err
}
return m.stackCollection.UpdateNodeGroupStack(ctx, nodeGroupName, template, true)
}
// GetLabels fetches the labels for a nodegroup
func (m *Service) GetLabels(ctx context.Context, nodeGroupName string) (map[string]string, error) {
template, err := m.stackCollection.GetManagedNodeGroupTemplate(ctx, manager.GetNodegroupOption{
NodeGroupName: nodeGroupName,
})
if err != nil {
return nil, err
}
return extractLabels(template)
}
func IsNotFound(err error) bool {
var notFoundErr *ekstypes.ResourceNotFoundException
return errors.As(err, ¬FoundErr)
}
// TODO switch to using goformation types
func extractLabels(template string) (map[string]string, error) {
labelsValue := gjson.Get(template, labelsPath)
if !labelsValue.Exists() {
return nil, errors.New("failed to find labels")
}
values, ok := labelsValue.Value().(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unexpected type for labels: %T", labelsValue.Value())
}
labels := make(map[string]string)
for k, v := range values {
value, ok := v.(string)
if !ok {
return nil, fmt.Errorf("unexpected type for label value: %T", value)
}
labels[k] = value
}
return labels, nil
}