-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
delete.go
103 lines (90 loc) · 2.84 KB
/
delete.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
package addon
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/kris-nova/logger"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
)
func (a *Manager) DeleteWithPreserve(ctx context.Context, addon *api.Addon) error {
logger.Info("deleting addon %q and preserving its resources", addon.Name)
_, err := a.deleteAddon(ctx, addon, true)
return err
}
func (a *Manager) Delete(ctx context.Context, addon *api.Addon) error {
logger.Debug("addon: %v", addon)
logger.Info("deleting addon: %s", addon.Name)
addonExists, err := a.deleteAddon(ctx, addon, false)
if err != nil {
return err
}
if addonExists {
logger.Info("deleted addon: %s", addon.Name)
}
deleteAddonIAMTasks, err := NewRemover(a.stackManager).DeleteAddonIAMTasksFiltered(ctx, addon.Name, false)
if err != nil {
return err
}
if deleteAddonIAMTasks.Len() > 0 {
logger.Info("deleting associated IAM stack(s)")
if err := runAllTasks(deleteAddonIAMTasks); err != nil {
return err
}
} else if addonExists {
logger.Info("no associated IAM stacks found")
} else {
return errors.New("could not find addon or associated IAM stack(s) to delete")
}
return nil
}
func (a *Manager) deleteAddon(ctx context.Context, addon *api.Addon, preserve bool) (addonExists bool, err error) {
_, err = a.eksAPI.DeleteAddon(ctx, &eks.DeleteAddonInput{
AddonName: &addon.Name,
ClusterName: &a.clusterConfig.Metadata.Name,
Preserve: preserve,
})
if err != nil {
var notFoundErr *ekstypes.ResourceNotFoundException
if errors.As(err, ¬FoundErr) {
logger.Info("addon %q does not exist", addon.Name)
return false, nil
}
return true, fmt.Errorf("failed to delete addon %q: %v", addon.Name, err)
}
return true, nil
}
type Remover struct {
stackManager StackManager
}
func NewRemover(stackManager StackManager) *Remover {
return &Remover{
stackManager: stackManager,
}
}
func (ar *Remover) DeleteAddonIAMTasks(ctx context.Context, wait bool) (*tasks.TaskTree, error) {
return ar.DeleteAddonIAMTasksFiltered(ctx, "", wait)
}
func (ar *Remover) DeleteAddonIAMTasksFiltered(ctx context.Context, addonName string, wait bool) (*tasks.TaskTree, error) {
stacks, err := ar.stackManager.GetIAMAddonsStacks(ctx)
if err != nil {
return nil, fmt.Errorf("failed to fetch addons stacks: %v", err)
}
taskTree := &tasks.TaskTree{Parallel: true}
for _, s := range stacks {
if addonName != "" && manager.GetIAMAddonName(s) != addonName {
continue
}
taskTree.Append(&deleteAddonIAMTask{
ctx: ctx,
info: fmt.Sprintf("delete addon IAM %q", *s.StackName),
stack: s,
stackManager: ar.stackManager,
wait: wait,
})
}
return taskTree, nil
}