-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
addon.go
119 lines (105 loc) · 3.63 KB
/
addon.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
package addon
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/hashicorp/go-version"
"github.com/kris-nova/logger"
kubeclient "k8s.io/client-go/kubernetes"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/awsapi"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
iamoidc "github.com/weaveworks/eksctl/pkg/iam/oidc"
)
type Manager struct {
clusterConfig *api.ClusterConfig
eksAPI awsapi.EKS
withOIDC bool
oidcManager *iamoidc.OpenIDConnectManager
stackManager manager.StackManager
clientSet kubeclient.Interface
}
func New(clusterConfig *api.ClusterConfig, eksAPI awsapi.EKS, stackManager manager.StackManager, withOIDC bool, oidcManager *iamoidc.OpenIDConnectManager, clientSet kubeclient.Interface) (*Manager, error) {
return &Manager{
clusterConfig: clusterConfig,
eksAPI: eksAPI,
withOIDC: withOIDC,
oidcManager: oidcManager,
stackManager: stackManager,
clientSet: clientSet,
}, nil
}
func (a *Manager) waitForAddonToBeActive(ctx context.Context, addon *api.Addon, waitTimeout time.Duration) error {
// Don't wait for coredns, aws-ebs-csi-driver or aws-efs-csi-driver if there are no nodegroups.
// They will be in degraded state until nodegroups are added.
if (addon.Name == api.CoreDNSAddon ||
addon.Name == api.AWSEBSCSIDriverAddon ||
addon.Name == api.AWSEFSCSIDriverAddon) &&
!a.clusterConfig.HasNodes() {
return nil
}
activeWaiter := eks.NewAddonActiveWaiter(a.eksAPI)
input := &eks.DescribeAddonInput{
ClusterName: &a.clusterConfig.Metadata.Name,
AddonName: &addon.Name,
}
if err := activeWaiter.Wait(ctx, input, waitTimeout); err != nil {
getAddonStatus := func() string {
output, describeErr := a.eksAPI.DescribeAddon(ctx, input)
if describeErr != nil {
return describeErr.Error()
}
return string(output.Addon.Status)
}
switch {
case strings.Contains(err.Error(), "exceeded max wait time"):
return fmt.Errorf("timed out waiting for addon %q to become active, status: %q", addon.Name, getAddonStatus())
case strings.Contains(err.Error(), "waiter state transitioned to Failure"):
return fmt.Errorf("addon status transitioned to %q", getAddonStatus())
default:
return err
}
}
logger.Info("addon %q active", addon.Name)
return nil
}
func (a *Manager) getLatestMatchingVersion(ctx context.Context, addon *api.Addon) (string, error) {
addonInfos, err := a.describeVersions(ctx, addon)
if err != nil {
return "", err
}
if len(addonInfos.Addons) == 0 || len(addonInfos.Addons[0].AddonVersions) == 0 {
return "", fmt.Errorf("no versions available for %q", addon.Name)
}
addonVersion := addon.Version
var versions []*version.Version
for _, addonVersionInfo := range addonInfos.Addons[0].AddonVersions {
v, err := a.parseVersion(*addonVersionInfo.AddonVersion)
if err != nil {
return "", err
}
if addonVersion == "latest" || strings.Contains(*addonVersionInfo.AddonVersion, addonVersion) {
versions = append(versions, v)
}
}
if len(versions) == 0 {
return "", fmt.Errorf("no version(s) found matching %q for %q", addonVersion, addon.Name)
}
sort.SliceStable(versions, func(i, j int) bool {
return versions[j].LessThan(versions[i])
})
return versions[0].Original(), nil
}
func (a *Manager) makeAddonName(name string) string {
return fmt.Sprintf("eksctl-%s-addon-%s", a.clusterConfig.Metadata.Name, name)
}
func (a *Manager) parseVersion(v string) (*version.Version, error) {
version, err := version.NewVersion(v)
if err != nil {
return nil, fmt.Errorf("failed to parse version %q: %w", v, err)
}
return version, nil
}