Skip to content

Commit 61e8106

Browse files
committed
code refactor
1 parent 4cbed3d commit 61e8106

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

bootstrap/eks/controllers/eksconfig_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (r *EKSConfigReconciler) joinWorker(ctx context.Context, cluster *clusterv1
329329
Namespace: cluster.Namespace,
330330
Name: cluster.Name,
331331
}
332-
ca, err := extractCAFromSecret(ctx, r.Client, obj)
332+
ca, err := r.extractCAFromSecret(ctx, obj)
333333
if err != nil {
334334
log.Error(err, "Failed to extract CA from kubeconfig secret")
335335
conditions.MarkFalse(config, eksbootstrapv1.DataSecretAvailableCondition,
@@ -568,8 +568,8 @@ func (r *EKSConfigReconciler) updateBootstrapSecret(ctx context.Context, secret
568568
return false, nil
569569
}
570570

571-
func extractCAFromSecret(ctx context.Context, c client.Client, obj client.ObjectKey) (string, error) {
572-
data, err := kubeconfigutil.FromSecret(ctx, c, obj)
571+
func (r *EKSConfigReconciler) extractCAFromSecret(ctx context.Context, obj client.ObjectKey) (string, error) {
572+
data, err := kubeconfigutil.FromSecret(ctx, r.Client, obj)
573573
if err != nil {
574574
return "", errors.Wrapf(err, "failed to get kubeconfig secret %s", obj.Name)
575575
}

bootstrap/eks/internal/userdata/node.go

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ runcmd:
5252
{{- template "mounts" .Mounts}}
5353
`
5454

55-
// Shell script part template for AL2023.
55+
// Common MIME header and boundary template
56+
mimeHeaderTemplate = `MIME-Version: 1.0
57+
Content-Type: multipart/mixed; boundary="{{.Boundary}}"
58+
59+
`
60+
61+
// Shell script part template for AL2023
5662
shellScriptPartTemplate = `--{{.Boundary}}
5763
Content-Type: text/x-shellscript; charset="us-ascii"
5864
@@ -93,8 +99,111 @@ spec:
9399
- "--node-labels={{if and .KubeletExtraArgs (index .KubeletExtraArgs "node-labels")}}{{index .KubeletExtraArgs "node-labels"}}{{else}}eks.amazonaws.com/nodegroup-image={{if .AMIImageID}}{{.AMIImageID}}{{end}},eks.amazonaws.com/capacityType={{if .CapacityType}}{{.CapacityType}}{{else}}ON_DEMAND{{end}},eks.amazonaws.com/nodegroup={{.NodeGroupName}}{{end}}"
94100
95101
--{{.Boundary}}--`
102+
103+
// AL2023-specific templates.
104+
al2023KubeletExtraArgsTemplate = `{{- define "al2023KubeletExtraArgs" -}}
105+
{{- if . }}
106+
- "--node-labels={{range $k, $v := .}}{{$k}}={{$v}}{{end}}"
107+
{{- end -}}
108+
{{- end -}}`
109+
110+
al2023ContainerRuntimeTemplate = `{{- define "al2023ContainerRuntime" -}}
111+
{{- if . -}}
112+
containerRuntime: {{.}}
113+
{{- end -}}
114+
{{- end -}}`
115+
116+
al2023DockerConfigTemplate = `{{- define "al2023DockerConfig" -}}
117+
{{- if and . (ne . "''") -}}
118+
dockerConfig: {{.}}
119+
{{- end -}}
120+
{{- end -}}`
121+
122+
al2023APIRetryAttemptsTemplate = `{{- define "al2023APIRetryAttempts" -}}
123+
{{- if . -}}
124+
apiRetryAttempts: {{.}}
125+
{{- end -}}
126+
{{- end -}}`
127+
128+
al2023PauseContainerTemplate = `{{- define "al2023PauseContainer" -}}
129+
{{- if and .AccountNumber .Version -}}
130+
pauseContainer:
131+
accountNumber: {{.AccountNumber}}
132+
version: {{.Version}}
133+
{{- end -}}
134+
{{- end -}}`
135+
136+
al2023FilesTemplate = `{{- define "al2023Files" -}}
137+
{{- if . -}}
138+
files:{{ range . }}
139+
- path: {{.Path}}
140+
content: |
141+
{{.Content | Indent 8}}{{ if ne .Owner "" }}
142+
owner: {{.Owner}}{{ end }}{{ if ne .Permissions "" }}
143+
permissions: '{{.Permissions}}'{{ end }}{{ end }}
144+
{{- end -}}
145+
{{- end -}}`
146+
147+
al2023DiskSetupTemplate = `{{- define "al2023DiskSetup" -}}
148+
{{- if . -}}
149+
diskSetup:{{ if .Partitions }}
150+
partitions:{{ range .Partitions }}
151+
- device: {{.Device}}
152+
layout: {{.Layout}}{{ if .Overwrite }}
153+
overwrite: {{.Overwrite}}{{ end }}{{ if .TableType }}
154+
tableType: {{.TableType}}{{ end }}{{ end }}{{ end }}{{ if .Filesystems }}
155+
filesystems:{{ range .Filesystems }}
156+
- device: {{.Device}}
157+
filesystem: {{.Filesystem}}
158+
label: {{.Label}}{{ if .Partition }}
159+
partition: {{.Partition}}{{ end }}{{ if .Overwrite }}
160+
overwrite: {{.Overwrite}}{{ end }}{{ if .ExtraOpts }}
161+
extraOpts:{{ range .ExtraOpts }}
162+
- {{.}}{{ end }}{{ end }}{{ end }}{{ end }}
163+
{{- end -}}
164+
{{- end -}}`
165+
166+
al2023MountsTemplate = `{{- define "al2023Mounts" -}}
167+
{{- if . -}}
168+
mounts:{{ range . }}
169+
-{{ range . }}
170+
- {{.}}{{ end }}{{ end }}
171+
{{- end -}}
172+
{{- end -}}`
173+
174+
al2023UsersTemplate = `{{- define "al2023Users" -}}
175+
{{- if . -}}
176+
users:{{ range . }}
177+
- name: {{.Name}}{{ if .Gecos }}
178+
gecos: {{.Gecos}}{{ end }}{{ if .Groups }}
179+
groups: {{.Groups}}{{ end }}{{ if .HomeDir }}
180+
homeDir: {{.HomeDir}}{{ end }}{{ if .Inactive }}
181+
inactive: {{.Inactive}}{{ end }}{{ if .Shell }}
182+
shell: {{.Shell}}{{ end }}{{ if .Passwd }}
183+
passwd: {{.Passwd}}{{ end }}{{ if .PrimaryGroup }}
184+
primaryGroup: {{.PrimaryGroup}}{{ end }}{{ if .LockPassword }}
185+
lockPassword: {{.LockPassword}}{{ end }}{{ if .Sudo }}
186+
sudo: {{.Sudo}}{{ end }}{{ if .SSHAuthorizedKeys }}
187+
sshAuthorizedKeys:{{ range .SSHAuthorizedKeys }}
188+
- {{.}}{{ end }}{{ end }}{{ end }}
189+
{{- end -}}
190+
{{- end -}}`
191+
192+
al2023NTPTemplate = `{{- define "al2023NTP" -}}
193+
{{- if . -}}
194+
ntp:{{ if .Enabled }}
195+
enabled: true{{ end }}{{ if .Servers }}
196+
servers:{{ range .Servers }}
197+
- {{.}}{{ end }}{{ end }}
198+
{{- end -}}
199+
{{- end -}}`
96200
)
97201

202+
// NodeUserData is responsible for generating userdata for EKS nodes.
203+
type NodeUserData struct {
204+
input *NodeInput
205+
}
206+
98207
// NodeInput contains all the information required to generate user data for a node.
99208
type NodeInput struct {
100209
ClusterName string
@@ -231,7 +340,7 @@ func generateAL2023UserData(input *NodeInput) ([]byte, error) {
231340
var buf bytes.Buffer
232341

233342
// Write MIME header
234-
if _, err := buf.WriteString(fmt.Sprintf("MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=%q\n\n", input.Boundary)); err != nil {
343+
if _, err := buf.WriteString(fmt.Sprintf("MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"%s\"\n\n", input.Boundary)); err != nil {
235344
return nil, fmt.Errorf("failed to write MIME header: %v", err)
236345
}
237346

0 commit comments

Comments
 (0)