-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
managed_al2.go
142 lines (115 loc) · 3.42 KB
/
managed_al2.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
package nodebootstrap
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"mime/multipart"
"strings"
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/nodebootstrap/assets"
)
// ManagedAL2 is a bootstrapper for managed Amazon Linux 2 nodegroups
type ManagedAL2 struct {
ng *api.ManagedNodeGroup
// UserDataMimeBoundary sets the MIME boundary for user data
UserDataMimeBoundary string
}
// NewManagedAL2Bootstrapper creates a new ManagedAL2 bootstrapper
func NewManagedAL2Bootstrapper(ng *api.ManagedNodeGroup) *ManagedAL2 {
return &ManagedAL2{
ng: ng,
}
}
// UserData returns user data for AL2 managed nodegroups
func (m *ManagedAL2) UserData() (string, error) {
ng := m.ng
if strings.HasPrefix(ng.AMI, "ami-") {
return makeCustomAMIUserData(ng.NodeGroupBase, m.UserDataMimeBoundary)
}
var (
buf bytes.Buffer
scripts []string
cloudboot []string
)
if len(ng.PreBootstrapCommands) > 0 {
scripts = append(scripts, ng.PreBootstrapCommands...)
}
if ng.OverrideBootstrapCommand != nil {
scripts = append(scripts, *ng.OverrideBootstrapCommand)
} else if ng.MaxPodsPerNode != 0 {
scripts = append(scripts, makeMaxPodsScript(ng.MaxPodsPerNode))
}
if api.IsEnabled(ng.EFAEnabled) {
cloudboot = append(cloudboot, assets.EfaManagedBoothook)
}
if len(scripts) == 0 && len(cloudboot) == 0 {
return "", nil
}
if err := createMimeMessage(&buf, scripts, cloudboot, m.UserDataMimeBoundary); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
func makeCustomAMIUserData(ng *api.NodeGroupBase, mimeBoundary string) (string, error) {
var (
buf bytes.Buffer
scripts []string
)
if len(ng.PreBootstrapCommands) > 0 {
scripts = append(scripts, ng.PreBootstrapCommands...)
}
if ng.OverrideBootstrapCommand != nil {
scripts = append(scripts, *ng.OverrideBootstrapCommand)
}
if len(scripts) == 0 {
return "", nil
}
if err := createMimeMessage(&buf, scripts, nil, mimeBoundary); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
func makeMaxPodsScript(maxPods int) string {
script := `#!/bin/sh
set -ex
sed -i -E "s/^USE_MAX_PODS=\"\\$\{USE_MAX_PODS:-true}\"/USE_MAX_PODS=false/" /etc/eks/bootstrap.sh
KUBELET_CONFIG=/etc/kubernetes/kubelet/kubelet-config.json
`
script += fmt.Sprintf(`echo "$(jq ".maxPods=%v" $KUBELET_CONFIG)" > $KUBELET_CONFIG`, maxPods)
return script
}
func createMimeMessage(writer io.Writer, scripts, cloudboots []string, mimeBoundary string) error {
mw := multipart.NewWriter(writer)
if mimeBoundary != "" {
if err := mw.SetBoundary(mimeBoundary); err != nil {
return errors.Wrap(err, "unexpected error setting MIME boundary")
}
}
fmt.Fprint(writer, "MIME-Version: 1.0\r\n")
fmt.Fprintf(writer, "Content-Type: multipart/mixed; boundary=%s\r\n\r\n", mw.Boundary())
for _, script := range scripts {
part, err := mw.CreatePart(map[string][]string{
"Content-Type": {"text/x-shellscript", `charset="us-ascii"`},
})
if err != nil {
return err
}
if _, err = part.Write([]byte(script)); err != nil {
return err
}
}
for _, cloudboot := range cloudboots {
part, err := mw.CreatePart(map[string][]string{
"Content-Type": {"text/cloud-boothook", `charset="us-ascii"`},
})
if err != nil {
return err
}
if _, err = part.Write([]byte(cloudboot)); err != nil {
return err
}
}
return mw.Close()
}