-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
update.go
132 lines (111 loc) · 3.62 KB
/
update.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
package addon
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/google/uuid"
"github.com/kris-nova/logger"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
)
func (a *Manager) Update(ctx context.Context, addon *api.Addon, waitTimeout time.Duration) error {
logger.Debug("addon: %v", addon)
updateAddonInput := &eks.UpdateAddonInput{
AddonName: &addon.Name,
ClusterName: &a.clusterConfig.Metadata.Name,
}
if addon.Force {
updateAddonInput.ResolveConflicts = ekstypes.ResolveConflictsOverwrite
logger.Debug("setting resolve conflicts to overwrite")
}
summary, err := a.Get(ctx, addon)
if err != nil {
return err
}
if addon.Version == "" {
// preserve existing version
// Might be redundant, does the API care?
logger.Info("no new version provided, preserving existing version: %s", summary.Version)
updateAddonInput.AddonVersion = &summary.Version
} else {
version, err := a.getLatestMatchingVersion(ctx, addon)
if err != nil {
return fmt.Errorf("failed to fetch addon version: %w", err)
}
if summary.Version != version {
logger.Info("new version provided %s", version)
}
updateAddonInput.AddonVersion = &version
}
//check if we have been provided a different set of policies/role
if addon.ServiceAccountRoleARN != "" {
updateAddonInput.ServiceAccountRoleArn = &addon.ServiceAccountRoleARN
} else if hasPoliciesSet(addon) {
serviceAccountRoleARN, err := a.updateWithNewPolicies(ctx, addon)
if err != nil {
return err
}
updateAddonInput.ServiceAccountRoleArn = &serviceAccountRoleARN
} else {
//preserve current role
if summary.IAMRole != "" {
updateAddonInput.ServiceAccountRoleArn = &summary.IAMRole
}
}
logger.Info("updating addon")
logger.Debug("%+v", updateAddonInput)
output, err := a.eksAPI.UpdateAddon(ctx, updateAddonInput)
if err != nil {
return fmt.Errorf("failed to update addon %q: %v", addon.Name, err)
}
if output != nil {
logger.Debug("%+v", output.Update)
}
if waitTimeout > 0 {
return a.waitForAddonToBeActive(ctx, addon, waitTimeout)
}
return nil
}
func (a *Manager) updateWithNewPolicies(ctx context.Context, addon *api.Addon) (string, error) {
stackName := a.makeAddonName(addon.Name)
stack, err := a.stackManager.DescribeStack(ctx, &manager.Stack{StackName: aws.String(stackName)})
if err != nil {
if manager.IsStackDoesNotExistError(err) {
return "", fmt.Errorf("failed to get stack: %w", err)
}
}
namespace, serviceAccount := a.getKnownServiceAccountLocation(addon)
if stack == nil {
return a.createRole(ctx, addon, namespace, serviceAccount)
}
createNewTemplate, err := a.createNewTemplate(addon, namespace, serviceAccount)
if err != nil {
return "", err
}
var templateBody manager.TemplateBody = createNewTemplate
err = a.stackManager.UpdateStack(ctx, manager.UpdateStackOptions{
Stack: stack,
ChangeSetName: fmt.Sprintf("updating-policy-%s", uuid.NewString()),
Description: "updating policies",
TemplateData: templateBody,
Wait: true,
})
if err != nil {
return "", err
}
stack, err = a.stackManager.DescribeStack(ctx, &manager.Stack{StackName: aws.String(stackName)})
if err != nil {
return "", err
}
return *stack.Outputs[0].OutputValue, nil
}
func (a *Manager) createNewTemplate(addon *api.Addon, namespace, serviceAccount string) ([]byte, error) {
resourceSet, err := a.createRoleResourceSet(addon, namespace, serviceAccount)
if err != nil {
return nil, err
}
return resourceSet.RenderJSON()
}