-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
nodegroup_filter.go
310 lines (258 loc) · 9.77 KB
/
nodegroup_filter.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
package filter
import (
"strings"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/eks/eksiface"
"github.com/kris-nova/logger"
"k8s.io/apimachinery/pkg/util/sets"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate . NodegroupFilter
// NodegroupFilter is an interface that holds filter configuration
type NodegroupFilter interface {
SetOnlyLocal(eksAPI eksiface.EKSAPI, lister StackLister, clusterConfig *api.ClusterConfig) error
Match(ngName string) bool
LogInfo(cfg *api.ClusterConfig)
}
// StackLister lists nodegroup stacks
type StackLister interface {
ListNodeGroupStacks() ([]manager.NodeGroupStack, error)
}
// NodeGroupFilter holds filter configuration
type NodeGroupFilter struct {
delegate *Filter
onlyLocal bool
onlyRemote bool
localNodegroups sets.String
remoteNodegroups sets.String
}
// NewNodeGroupFilter creates a new NodeGroupFilter struct
func NewNodeGroupFilter() *NodeGroupFilter {
return &NodeGroupFilter{
delegate: &Filter{
ExcludeAll: false,
includeNames: sets.NewString(),
excludeNames: sets.NewString(),
},
localNodegroups: sets.NewString(),
remoteNodegroups: sets.NewString(),
}
}
// AppendGlobs appends globs for inclusion and exclusion rules
func (f *NodeGroupFilter) AppendGlobs(includeGlobExprs, excludeGlobExprs, ngNames []string) error {
if err := f.AppendIncludeGlobs(ngNames, includeGlobExprs...); err != nil {
return err
}
return f.AppendExcludeGlobs(excludeGlobExprs...)
}
// AppendIncludeGlobs sets globs for inclusion rules
func (f *NodeGroupFilter) AppendIncludeGlobs(ngNames []string, globExprs ...string) error {
return f.delegate.doAppendIncludeGlobs(ngNames, "nodegroup", globExprs...)
}
// AppendExcludeGlobs sets globs for inclusion rules
func (f *NodeGroupFilter) AppendExcludeGlobs(globExprs ...string) error {
return f.delegate.AppendExcludeGlobs(globExprs...)
}
// AppendIncludeNames sets globs for inclusion rules
func (f *NodeGroupFilter) AppendIncludeNames(names ...string) {
f.delegate.AppendIncludeNames(names...)
}
// SetOnlyLocal uses StackLister to list existing nodegroup stacks and configures
// the filter to only include the nodegroups that don't exist in the cluster already.
// Note: they are present in the config file but not in the cluster. This is used by
// the create nodegroup command
func (f *NodeGroupFilter) SetOnlyLocal(eksAPI eksiface.EKSAPI, lister StackLister, clusterConfig *api.ClusterConfig) error {
f.onlyLocal = true
err := f.loadLocalAndRemoteNodegroups(eksAPI, lister, clusterConfig)
if err != nil {
return err
}
// Remote ones will be excluded
if f.remoteNodegroups.Len() > 0 {
logger.Info("%d existing %s(s) (%s) will be excluded", f.remoteNodegroups.Len(), "nodegroup", strings.Join(f.remoteNodegroups.List(), ","))
}
return nil
}
// SetOnlyRemote uses StackLister to list existing nodegroup stacks and configures
// the filter to exclude nodegroups already defined in the config file. It will include the
// nodegroups that exist in the cluster but not in the config
func (f *NodeGroupFilter) SetOnlyRemote(eksAPI eksiface.EKSAPI, lister StackLister, clusterConfig *api.ClusterConfig) error {
f.onlyRemote = true
err := f.loadLocalAndRemoteNodegroups(eksAPI, lister, clusterConfig)
if err != nil {
return err
}
// local ones will be excluded
if f.localNodegroups.Len() > 0 {
logger.Info("%d %s(s) present in the config file (%s) will be excluded", f.localNodegroups.Len(), "nodegroup", strings.Join(f.localNodegroups.List(), ","))
}
return nil
}
// SetExcludeAll sets the ExcludeAll flag in the filter so that no nodegroups are matched
func (f *NodeGroupFilter) SetExcludeAll(excludeAll bool) {
f.delegate.ExcludeAll = excludeAll
}
// GetExcludeAll returns whether all nodegroups will be excluded
func (f *NodeGroupFilter) GetExcludeAll() bool {
return f.delegate.ExcludeAll
}
func (f *NodeGroupFilter) loadLocalAndRemoteNodegroups(eksAPI eksiface.EKSAPI, lister StackLister, clusterConfig *api.ClusterConfig) error {
nodeGroupsWithStacks, nodeGroupsWithoutStacks, err := f.findAllNodeGroups(eksAPI, lister, clusterConfig)
if err != nil {
return err
}
for _, s := range nodeGroupsWithStacks {
f.remoteNodegroups.Insert(s.NodeGroupName)
}
for _, s := range nodeGroupsWithoutStacks {
f.remoteNodegroups.Insert(s)
}
f.localNodegroups.Insert(clusterConfig.GetAllNodeGroupNames()...)
// Get local nodegroups and discover if any specified don't have stacks
for _, localNodeGroup := range clusterConfig.GetAllNodeGroupNames() {
if !stackExists(nodeGroupsWithStacks, localNodeGroup) && !nodeExists(nodeGroupsWithoutStacks, localNodeGroup) {
logger.Debug("nodegroup %q present in the given config, but missing in the cluster", localNodeGroup)
}
}
for _, nodeGroupWithoutStack := range nodeGroupsWithoutStacks {
if !f.localNodegroups.Has(nodeGroupWithoutStack) {
ngBase := &api.NodeGroupBase{Name: nodeGroupWithoutStack}
logger.Debug("nodegroup %q present in the cluster, but missing from the given config", nodeGroupWithoutStack)
clusterConfig.ManagedNodeGroups = append(clusterConfig.ManagedNodeGroups, &api.ManagedNodeGroup{NodeGroupBase: ngBase})
}
}
// Log remote-only nodeg roups AND add them to the cluster config
for _, s := range nodeGroupsWithStacks {
remoteNodeGroupName := s.NodeGroupName
if !f.localNodegroups.Has(remoteNodeGroupName) {
ngBase := &api.NodeGroupBase{Name: s.NodeGroupName}
logger.Debug("nodegroup %q present in the cluster, but missing from the given config", s.NodeGroupName)
if s.Type == api.NodeGroupTypeManaged {
clusterConfig.ManagedNodeGroups = append(clusterConfig.ManagedNodeGroups, &api.ManagedNodeGroup{NodeGroupBase: ngBase})
} else {
clusterConfig.NodeGroups = append(clusterConfig.NodeGroups, &api.NodeGroup{NodeGroupBase: ngBase})
}
}
}
return nil
}
func (f *NodeGroupFilter) findAllNodeGroups(eksAPI eksiface.EKSAPI, lister StackLister, clusterConfig *api.ClusterConfig) ([]manager.NodeGroupStack, []string, error) {
// Get remote nodegroups stacks
nodeGroupsWithStacks, err := lister.ListNodeGroupStacks()
if err != nil {
return nil, nil, err
}
// Get all nodegroups
allNodeGroups, err := eksAPI.ListNodegroups(&eks.ListNodegroupsInput{
ClusterName: &clusterConfig.Metadata.Name,
})
if err != nil {
return nil, nil, err
}
nodeGroupsWithStacksSet := sets.NewString()
for _, s := range nodeGroupsWithStacks {
nodeGroupsWithStacksSet.Insert(s.NodeGroupName)
}
var nodeGroupsWithoutStacks []string
for _, node := range allNodeGroups.Nodegroups {
if !nodeGroupsWithStacksSet.Has(*node) {
nodeGroupsWithoutStacks = append(nodeGroupsWithoutStacks, *node)
}
}
return nodeGroupsWithStacks, nodeGroupsWithoutStacks, nil
}
func nodeExists(stacks []string, stackName string) bool {
for _, s := range stacks {
if s == stackName {
return true
}
}
return false
}
func stackExists(stacks []manager.NodeGroupStack, stackName string) bool {
for _, s := range stacks {
if s.NodeGroupName == stackName {
return true
}
}
return false
}
// LogInfo prints out a user-friendly message about how filter was applied
func (f *NodeGroupFilter) LogInfo(cfg *api.ClusterConfig) {
allNames := f.collectNames(cfg.NodeGroups)
for _, mng := range cfg.ManagedNodeGroups {
allNames.Insert(mng.NameString())
}
included, excluded := f.matchAll(allNames)
f.delegate.doLogInfo("nodegroup", included, excluded)
}
// matchAll all names against the filter and return two sets of names - included and excluded
func (f *NodeGroupFilter) matchAll(allNames sets.String) (sets.String, sets.String) {
matching, notMatching := f.delegate.doMatchAll(allNames.List())
if f.onlyLocal {
// From the ones that match, pick only the local ones
included := matching.Intersection(f.onlyLocalNodegroups())
excluded := allNames.Difference(included)
return included, excluded
}
if f.onlyRemote {
// From the ones that match, pick only the remote ones
included := matching.Intersection(f.onlyRemoteNodegroups())
excluded := allNames.Difference(included)
return included, excluded
}
return matching, notMatching
}
// Match decides whether the given nodegroup is considered included by this filter. It takes into account not only the
// inclusion and exclusion rules (globs) but also the modifiers onlyRemote and onlyLocal.
func (f *NodeGroupFilter) Match(ngName string) bool {
if f.onlyRemote {
if !f.onlyRemoteNodegroups().Has(ngName) {
return false
}
return f.delegate.Match(ngName)
}
if f.onlyLocal {
if !f.onlyLocalNodegroups().Has(ngName) {
return false
}
return f.delegate.Match(ngName)
}
return f.delegate.Match(ngName)
}
func (f *NodeGroupFilter) onlyLocalNodegroups() sets.String {
return f.localNodegroups.Difference(f.remoteNodegroups)
}
func (f *NodeGroupFilter) onlyRemoteNodegroups() sets.String {
return f.remoteNodegroups.Difference(f.localNodegroups)
}
// FilterMatching matches names against the filter and returns all included node groups
func (f *NodeGroupFilter) FilterMatching(nodeGroups []*api.NodeGroup) []*api.NodeGroup {
var match []*api.NodeGroup
for _, ng := range nodeGroups {
if f.Match(ng.NameString()) {
match = append(match, ng)
}
}
return match
}
// ForEach iterates over each nodegroup that is included by the filter and calls iterFn
func (f *NodeGroupFilter) ForEach(nodeGroups []*api.NodeGroup, iterFn func(i int, ng *api.NodeGroup) error) error {
for i, ng := range nodeGroups {
if f.Match(ng.NameString()) {
if err := iterFn(i, ng); err != nil {
return err
}
}
}
return nil
}
func (*NodeGroupFilter) collectNames(nodeGroups []*api.NodeGroup) sets.String {
names := sets.NewString()
for _, ng := range nodeGroups {
names.Insert(ng.NameString())
}
return names
}