forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdata.go
More file actions
201 lines (174 loc) · 5.55 KB
/
Copy pathuserdata.go
File metadata and controls
201 lines (174 loc) · 5.55 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package nodebootstrap
import (
"fmt"
"strings"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
kubeletapi "k8s.io/kubelet/config/v1beta1"
"sigs.k8s.io/yaml"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cloudconfig"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
)
//go:generate ${GOBIN}/go-bindata -pkg ${GOPACKAGE} -prefix assets -nometadata -o assets.go assets
//go:generate go run ./maxpods_generate.go
const (
configDir = "/etc/eksctl/"
kubeletDropInUnitDir = "/etc/systemd/system/kubelet.service.d/"
)
type configFile struct {
content string
isAsset bool
}
type configFiles = map[string]map[string]configFile
func getAsset(name string) (string, error) {
data, err := Asset(name)
if err != nil {
return "", errors.Wrapf(err, "decoding embedded file %q", name)
}
return string(data), nil
}
func addFilesAndScripts(config *cloudconfig.CloudConfig, files configFiles, scripts []string) error {
for dir, fileNames := range files {
for fileName, file := range fileNames {
f := cloudconfig.File{
Path: dir + fileName,
}
if file.isAsset {
data, err := getAsset(fileName)
if err != nil {
return err
}
f.Content = data
} else {
f.Content = file.content
}
config.AddFile(f)
}
}
for _, scriptName := range scripts {
data, err := getAsset(scriptName)
if err != nil {
return err
}
config.RunScript(scriptName, data)
}
return nil
}
func makeClientConfigData(spec *api.ClusterConfig, ng *api.NodeGroup) ([]byte, error) {
clientConfig, _, _ := kubeconfig.New(spec, "kubelet", configDir+"ca.crt")
authenticator := kubeconfig.AWSIAMAuthenticator
if ng.AMIFamily == api.NodeImageFamilyUbuntu1804 {
authenticator = kubeconfig.HeptioAuthenticatorAWS
}
kubeconfig.AppendAuthenticator(clientConfig, spec, authenticator, "", "")
clientConfigData, err := clientcmd.Write(*clientConfig)
if err != nil {
return nil, errors.Wrap(err, "serialising kubeconfig for nodegroup")
}
return clientConfigData, nil
}
func clusterDNS(spec *api.ClusterConfig, ng *api.NodeGroup) string {
if ng.ClusterDNS != "" {
return ng.ClusterDNS
}
// Default service network is 10.100.0.0, but it gets set 172.20.0.0 automatically when pod network
// is anywhere within 10.0.0.0/8
if spec.VPC.CIDR != nil && spec.VPC.CIDR.IP[0] == 10 {
return "172.20.0.10"
}
return "10.100.0.10"
}
func makeKubeletConfigYAML(spec *api.ClusterConfig, ng *api.NodeGroup) ([]byte, error) {
data, err := Asset("kubelet.yaml")
if err != nil {
return nil, err
}
// use a map here, as using struct will require us to add defaulting etc,
// and we only need to add a few top-level fields
obj := api.InlineDocument{}
if err := yaml.UnmarshalStrict(data, &obj); err != nil {
return nil, err
}
obj["clusterDNS"] = []string{
clusterDNS(spec, ng),
}
// Set default reservations if specs about instance is available
if info, ok := instanceTypeInfos[ng.InstanceType]; ok {
if _, ok := obj["kubeReserved"]; !ok {
obj["kubeReserved"] = api.InlineDocument{}
}
obj["kubeReserved"].(api.InlineDocument)["ephemeral-storage"] = info.DefaultStorageToReserve()
obj["kubeReserved"].(api.InlineDocument)["cpu"] = info.DefaultCPUToReserve()
obj["kubeReserved"].(api.InlineDocument)["memory"] = info.DefaultMemoryToReserve()
}
// Add extra configuration from configfile
if ng.KubeletExtraConfig != nil {
for k, v := range *ng.KubeletExtraConfig {
obj[k] = v
}
}
data, err = yaml.Marshal(obj)
if err != nil {
return nil, err
}
// validate if data can be decoded as KubeletConfiguration
if err := yaml.UnmarshalStrict(data, &kubeletapi.KubeletConfiguration{}); err != nil {
return nil, errors.Wrap(err, "validating generated KubeletConfiguration object")
}
return data, nil
}
func kvs(kv map[string]string) string {
var params []string
for k, v := range kv {
params = append(params, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(params, ",")
}
func toCLIArgs(values map[string]string) string {
var args []string
for k, v := range values {
args = append(args, fmt.Sprintf("--%s=%s", k, v))
}
return strings.Join(args, " ")
}
func makeCommonKubeletEnvParams(spec *api.ClusterConfig, ng *api.NodeGroup) []string {
variables := []string{
fmt.Sprintf("NODE_LABELS=%s", kvs(ng.Labels)),
fmt.Sprintf("NODE_TAINTS=%s", kvs(ng.Taints)),
}
if ng.MaxPodsPerNode != 0 {
variables = append(variables, fmt.Sprintf("MAX_PODS=%d", ng.MaxPodsPerNode))
}
return variables
}
func makeMetadata(spec *api.ClusterConfig) []string {
return []string{
fmt.Sprintf("AWS_DEFAULT_REGION=%s", spec.Metadata.Region),
fmt.Sprintf("AWS_EKS_CLUSTER_NAME=%s", spec.Metadata.Name),
fmt.Sprintf("AWS_EKS_ENDPOINT=%s", spec.Status.Endpoint),
fmt.Sprintf("AWS_EKS_ECR_ACCOUNT=%s", api.EKSResourceAccountID(spec.Metadata.Region)),
}
}
func makeMaxPodsMapping() string {
var text strings.Builder
for k, v := range maxPodsPerNodeType {
text.WriteString(fmt.Sprintf("%s %d\n", k, v))
}
return text.String()
}
// NewUserData creates new user data for a given node image family
func NewUserData(spec *api.ClusterConfig, ng *api.NodeGroup) (string, error) {
switch ng.AMIFamily {
case api.NodeImageFamilyAmazonLinux2:
return NewUserDataForAmazonLinux2(spec, ng)
case api.NodeImageFamilyUbuntu1804:
return NewUserDataForUbuntu1804(spec, ng)
case api.NodeImageFamilyBottlerocket:
return NewUserDataForBottlerocket(spec, ng)
case api.NodeImageFamilyWindowsServer2019FullContainer, api.NodeImageFamilyWindowsServer2019CoreContainer:
return newUserDataForWindows(spec, ng)
default:
return "", nil
}
}