-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
al2023.go
173 lines (150 loc) · 4.98 KB
/
al2023.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
package nodebootstrap
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"sigs.k8s.io/yaml"
nodeadmapi "github.com/awslabs/amazon-eks-ami/nodeadm/api"
nodeadm "github.com/awslabs/amazon-eks-ami/nodeadm/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/nodebootstrap/assets"
"github.com/weaveworks/eksctl/pkg/nodebootstrap/utils"
)
type AL2023 struct {
cfg *api.ClusterConfig
nodePool api.NodePool
clusterDNS string
scripts []string
cloudboot []string
nodeConfigs []*nodeadm.NodeConfig
UserDataMimeBoundary string
}
func NewManagedAL2023Bootstrapper(cfg *api.ClusterConfig, mng *api.ManagedNodeGroup, clusterDNS string) *AL2023 {
al2023 := newAL2023Bootstrapper(cfg, mng, clusterDNS)
if api.IsEnabled(mng.EFAEnabled) {
al2023.cloudboot = append(al2023.cloudboot, assets.EfaManagedAL2023Boothook)
}
return al2023
}
func NewAL2023Bootstrapper(cfg *api.ClusterConfig, ng *api.NodeGroup, clusterDNS string) *AL2023 {
al2023 := newAL2023Bootstrapper(cfg, ng, clusterDNS)
if api.IsEnabled(ng.EFAEnabled) {
al2023.scripts = append(al2023.scripts, assets.EfaAl2023Sh)
}
return al2023
}
func newAL2023Bootstrapper(cfg *api.ClusterConfig, np api.NodePool, clusterDNS string) *AL2023 {
return &AL2023{
cfg: cfg,
nodePool: np,
clusterDNS: clusterDNS,
scripts: []string{assets.AL2023XTablesLock},
}
}
func (m *AL2023) UserData() (string, error) {
ng := m.nodePool.BaseNodeGroup()
minimalNodeConfig, err := m.createMinimalNodeConfig()
if err != nil {
return "", fmt.Errorf("generating minimal node config: %w", err)
}
if minimalNodeConfig != nil {
m.nodeConfigs = append(m.nodeConfigs, minimalNodeConfig)
}
if ng.MaxPodsPerNode > 0 {
nodeConfig := &nodeadm.NodeConfig{
TypeMeta: metav1.TypeMeta{
Kind: nodeadmapi.KindNodeConfig,
APIVersion: nodeadm.GroupVersion.String(),
},
}
kubeletConfig, err := ToKubeletConfig(api.InlineDocument{"maxPods": ng.MaxPodsPerNode})
if err != nil {
return "", err
}
nodeConfig.Spec.Kubelet.Config = kubeletConfig
m.nodeConfigs = append(m.nodeConfigs, nodeConfig)
}
for _, command := range m.nodePool.BaseNodeGroup().PreBootstrapCommands {
m.scripts = append(m.scripts, "#!/bin/bash\n"+command)
}
if ng.OverrideBootstrapCommand != nil {
nodeConfig, err := stringToNodeConfig(*ng.OverrideBootstrapCommand)
if err != nil {
return "", err
}
m.nodeConfigs = append(m.nodeConfigs, nodeConfig)
}
if len(m.scripts) == 0 && len(m.cloudboot) == 0 && len(m.nodeConfigs) == 0 {
return "", nil
}
var buf bytes.Buffer
if err := createMimeMessage(&buf, m.scripts, m.cloudboot, m.nodeConfigs, m.UserDataMimeBoundary); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
func (m *AL2023) createMinimalNodeConfig() (*nodeadm.NodeConfig, error) {
kubeletConfig := api.InlineDocument{}
switch nodeGroup := m.nodePool.(type) {
case *api.ManagedNodeGroup:
if !api.IsAMI(nodeGroup.AMI) {
return nil, nil
}
case *api.NodeGroup:
if nodeGroup.KubeletExtraConfig != nil {
kubeletConfig = *nodeGroup.KubeletExtraConfig.DeepCopy()
}
}
kubeletConfig["clusterDNS"] = []string{m.clusterDNS}
ng := m.nodePool.BaseNodeGroup()
nodeKubeletConfig, err := ToKubeletConfig(kubeletConfig)
if err != nil {
return nil, err
}
kubeletOptions := nodeadm.KubeletOptions{
Flags: []string{"--node-labels=" + formatLabels(ng.Labels)},
Config: nodeKubeletConfig,
}
if taints := m.nodePool.NGTaints(); len(taints) > 0 {
kubeletOptions.Flags = append(kubeletOptions.Flags, "--register-with-taints="+utils.FormatTaints(taints))
}
clusterStatus := m.cfg.Status
return &nodeadm.NodeConfig{
TypeMeta: metav1.TypeMeta{
Kind: nodeadmapi.KindNodeConfig,
APIVersion: nodeadm.GroupVersion.String(),
},
Spec: nodeadm.NodeConfigSpec{
Cluster: nodeadm.ClusterDetails{
Name: m.cfg.Metadata.Name,
APIServerEndpoint: clusterStatus.Endpoint,
CertificateAuthority: clusterStatus.CertificateAuthorityData,
CIDR: clusterStatus.KubernetesNetworkConfig.ServiceIPv4CIDR,
},
Kubelet: kubeletOptions,
},
}, nil
}
// ToKubeletConfig generates a kubelet config that can be used with nodeadm.NodeConfig.
func ToKubeletConfig(kubeletExtraConfig api.InlineDocument) (map[string]runtime.RawExtension, error) {
kubeletConfig := map[string]runtime.RawExtension{}
for k, v := range kubeletExtraConfig {
raw, err := json.Marshal(v)
if err != nil {
return nil, err
}
kubeletConfig[k] = runtime.RawExtension{Raw: raw}
}
return kubeletConfig, nil
}
func stringToNodeConfig(overrideBootstrapCommand string) (*nodeadm.NodeConfig, error) {
var config nodeadm.NodeConfig
err := yaml.Unmarshal([]byte(overrideBootstrapCommand), &config)
if err != nil {
return nil, fmt.Errorf("unmarshalling \"overrideBootstrapCommand\" into \"nodeadm.NodeConfig\": %w", err)
}
return &config, nil
}