Skip to content

Commit 325dd78

Browse files
richardcasecnmcavoy
authored andcommitted
wip
Signed-off-by: Richard Case <[email protected]>
1 parent 1424bef commit 325dd78

File tree

7 files changed

+164
-10
lines changed

7 files changed

+164
-10
lines changed

bootstrap/eks/api/v1beta1/eksconfig_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ type EKSConfigSpec struct {
5151
// the ip family will be set to ipv6.
5252
// +optional
5353
ServiceIPV6Cidr *string `json:"serviceIPV6Cidr,omitempty"`
54+
// PreBootstrapCommands specifies extra commands to run before bootstrapping nodes to the cluster
55+
// +optional
56+
PreBootstrapCommands []string `json:"preBootstrapCommands,omitempty"`
57+
// PostBootstrapCommands specifies extra commands to run after bootstrapping nodes to the cluster
58+
// +optional
59+
PostBootstrapCommands []string `json:"postBootstrapCommands,omitempty"`
60+
// BootstrapCommandOverride allows you to override the bootstrap command to use for EKS nodes.
61+
// +optional
62+
BootstrapCommandOverride *string `json:"boostrapCommandOverride,omitempty"`
5463
}
5564

5665
// PauseContainer contains details of pause container.

bootstrap/eks/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootstrap/eks/controllers/eksconfig_controller.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,16 @@ func (r *EKSConfigReconciler) joinWorker(ctx context.Context, cluster *clusterv1
189189

190190
nodeInput := &userdata.NodeInput{
191191
// AWSManagedControlPlane webhooks default and validate EKSClusterName
192-
ClusterName: controlPlane.Spec.EKSClusterName,
193-
KubeletExtraArgs: config.Spec.KubeletExtraArgs,
194-
ContainerRuntime: config.Spec.ContainerRuntime,
195-
DNSClusterIP: config.Spec.DNSClusterIP,
196-
DockerConfigJSON: config.Spec.DockerConfigJSON,
197-
APIRetryAttempts: config.Spec.APIRetryAttempts,
198-
UseMaxPods: config.Spec.UseMaxPods,
192+
ClusterName: controlPlane.Spec.EKSClusterName,
193+
KubeletExtraArgs: config.Spec.KubeletExtraArgs,
194+
ContainerRuntime: config.Spec.ContainerRuntime,
195+
DNSClusterIP: config.Spec.DNSClusterIP,
196+
DockerConfigJSON: config.Spec.DockerConfigJSON,
197+
APIRetryAttempts: config.Spec.APIRetryAttempts,
198+
UseMaxPods: config.Spec.UseMaxPods,
199+
PreBootstrapCommands: config.Spec.PreBootstrapCommands,
200+
PostBootstrapCommands: config.Spec.PostBootstrapCommands,
201+
BootstrapCommandOverride: config.Spec.BootstrapCommandOverride,
199202
}
200203
if config.Spec.PauseContainer != nil {
201204
nodeInput.PauseContainerAccount = &config.Spec.PauseContainer.AccountNumber

bootstrap/eks/internal/userdata/node.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ import (
2525
)
2626

2727
const (
28+
defaultBootstrapCommand = "/etc/eks/bootstrap.sh"
29+
2830
nodeUserData = `#!/bin/bash
29-
/etc/eks/bootstrap.sh {{.ClusterName}} {{- template "args" . }}
31+
set -o errexit; set -o pipefail; set -o nounset;
32+
{{- template "commands" .PreBootstrapCommands }}
33+
{{ .BootstrapCommand }} {{.ClusterName}} {{- template "args" . }}
34+
{{- template "commands" .PostBootstrapCommands }}
35+
`
36+
commandsTemplate = `{{- define "commands" -}}
37+
{{ range . }}
38+
{{.}}
39+
{{- end -}}
40+
{{- end -}}
3041
`
3142
)
3243

@@ -43,8 +54,11 @@ type NodeInput struct {
4354
UseMaxPods *bool
4455
// NOTE: currently the IPFamily/ServiceIPV6Cidr isn't exposed to the user.
4556
// TODO (richardcase): remove the above comment when IPV6 / dual stack is implemented.
46-
IPFamily *string
47-
ServiceIPV6Cidr *string
57+
IPFamily *string
58+
ServiceIPV6Cidr *string
59+
PreBootstrapCommands []string
60+
PostBootstrapCommands []string
61+
BootstrapCommandOverride *string
4862
}
4963

5064
func (ni *NodeInput) DockerConfigJSONEscaped() string {
@@ -55,6 +69,14 @@ func (ni *NodeInput) DockerConfigJSONEscaped() string {
5569
return shellescape.Quote(*ni.DockerConfigJSON)
5670
}
5771

72+
func (ni *NodeInput) BootstrapCommand() string {
73+
if ni.BootstrapCommandOverride != nil && *ni.BootstrapCommandOverride != "" {
74+
return *ni.BootstrapCommandOverride
75+
}
76+
77+
return defaultBootstrapCommand
78+
}
79+
5880
// NewNode returns the user data string to be used on a node instance.
5981
func NewNode(input *NodeInput) ([]byte, error) {
6082
tm := template.New("Node")
@@ -67,6 +89,10 @@ func NewNode(input *NodeInput) ([]byte, error) {
6789
return nil, fmt.Errorf("failed to parse kubeletExtraArgs template: %w", err)
6890
}
6991

92+
if _, err := tm.Parse(commandsTemplate); err != nil {
93+
return nil, fmt.Errorf("failed to parse commandsTemplate template: %w", err)
94+
}
95+
7096
t, err := tm.Parse(nodeUserData)
7197
if err != nil {
7298
return nil, fmt.Errorf("failed to parse Node template: %w", err)

bootstrap/eks/internal/userdata/node_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestNewNode(t *testing.T) {
4646
},
4747
},
4848
expectedBytes: []byte(`#!/bin/bash
49+
set -o errexit; set -o pipefail; set -o nounset;
4950
/etc/eks/bootstrap.sh test-cluster
5051
`),
5152
expectErr: false,
@@ -62,6 +63,7 @@ func TestNewNode(t *testing.T) {
6263
},
6364
},
6465
expectedBytes: []byte(`#!/bin/bash
66+
set -o errexit; set -o pipefail; set -o nounset;
6567
/etc/eks/bootstrap.sh test-cluster --kubelet-extra-args '--node-labels=node-role.undistro.io/infra=true --register-with-taints=dedicated=infra:NoSchedule'
6668
`),
6769
},
@@ -74,6 +76,7 @@ func TestNewNode(t *testing.T) {
7476
},
7577
},
7678
expectedBytes: []byte(`#!/bin/bash
79+
set -o errexit; set -o pipefail; set -o nounset;
7780
/etc/eks/bootstrap.sh test-cluster --container-runtime containerd
7881
`),
7982
},
@@ -90,6 +93,7 @@ func TestNewNode(t *testing.T) {
9093
},
9194
},
9295
expectedBytes: []byte(`#!/bin/bash
96+
set -o errexit; set -o pipefail; set -o nounset;
9397
/etc/eks/bootstrap.sh test-cluster --kubelet-extra-args '--node-labels=node-role.undistro.io/infra=true --register-with-taints=dedicated=infra:NoSchedule' --container-runtime containerd
9498
`),
9599
},
@@ -103,6 +107,7 @@ func TestNewNode(t *testing.T) {
103107
},
104108
},
105109
expectedBytes: []byte(`#!/bin/bash
110+
set -o errexit; set -o pipefail; set -o nounset;
106111
/etc/eks/bootstrap.sh test-cluster --ip-family ipv6 --service-ipv6-cidr fe80:0000:0000:0000:0204:61ff:fe9d:f156/24
107112
`),
108113
},
@@ -115,6 +120,7 @@ func TestNewNode(t *testing.T) {
115120
},
116121
},
117122
expectedBytes: []byte(`#!/bin/bash
123+
set -o errexit; set -o pipefail; set -o nounset;
118124
/etc/eks/bootstrap.sh test-cluster --use-max-pods false
119125
`),
120126
},
@@ -127,6 +133,7 @@ func TestNewNode(t *testing.T) {
127133
},
128134
},
129135
expectedBytes: []byte(`#!/bin/bash
136+
set -o errexit; set -o pipefail; set -o nounset;
130137
/etc/eks/bootstrap.sh test-cluster --aws-api-retry-attempts 5
131138
`),
132139
},
@@ -140,6 +147,7 @@ func TestNewNode(t *testing.T) {
140147
},
141148
},
142149
expectedBytes: []byte(`#!/bin/bash
150+
set -o errexit; set -o pipefail; set -o nounset;
143151
/etc/eks/bootstrap.sh test-cluster --pause-container-account 12345678 --pause-container-version v1
144152
`),
145153
},
@@ -152,6 +160,7 @@ func TestNewNode(t *testing.T) {
152160
},
153161
},
154162
expectedBytes: []byte(`#!/bin/bash
163+
set -o errexit; set -o pipefail; set -o nounset;
155164
/etc/eks/bootstrap.sh test-cluster --dns-cluster-ip 192.168.0.1
156165
`),
157166
},
@@ -164,7 +173,67 @@ func TestNewNode(t *testing.T) {
164173
},
165174
},
166175
expectedBytes: []byte(`#!/bin/bash
176+
set -o errexit; set -o pipefail; set -o nounset;
167177
/etc/eks/bootstrap.sh test-cluster --docker-config-json '{"debug":true}'
178+
`),
179+
},
180+
{
181+
name: "with pre-bootstrap command",
182+
args: args{
183+
input: &NodeInput{
184+
ClusterName: "test-cluster",
185+
PreBootstrapCommands: []string{"date", "echo \"testing\""},
186+
},
187+
},
188+
expectedBytes: []byte(`#!/bin/bash
189+
set -o errexit; set -o pipefail; set -o nounset;
190+
date
191+
echo "testing"
192+
/etc/eks/bootstrap.sh test-cluster
193+
`),
194+
},
195+
{
196+
name: "with post-bootstrap command",
197+
args: args{
198+
input: &NodeInput{
199+
ClusterName: "test-cluster",
200+
PostBootstrapCommands: []string{"date", "echo \"testing\""},
201+
},
202+
},
203+
expectedBytes: []byte(`#!/bin/bash
204+
set -o errexit; set -o pipefail; set -o nounset;
205+
/etc/eks/bootstrap.sh test-cluster
206+
date
207+
echo "testing"
208+
`),
209+
},
210+
{
211+
name: "with pre & post-bootstrap command",
212+
args: args{
213+
input: &NodeInput{
214+
ClusterName: "test-cluster",
215+
PreBootstrapCommands: []string{"echo \"testing pre\""},
216+
PostBootstrapCommands: []string{"echo \"testing post\""},
217+
},
218+
},
219+
expectedBytes: []byte(`#!/bin/bash
220+
set -o errexit; set -o pipefail; set -o nounset;
221+
echo "testing pre"
222+
/etc/eks/bootstrap.sh test-cluster
223+
echo "testing post"
224+
`),
225+
},
226+
{
227+
name: "with bootstrap override command",
228+
args: args{
229+
input: &NodeInput{
230+
ClusterName: "test-cluster",
231+
BootstrapCommandOverride: pointer.String("/custom/mybootstrap.sh"),
232+
},
233+
},
234+
expectedBytes: []byte(`#!/bin/bash
235+
set -o errexit; set -o pipefail; set -o nounset;
236+
/custom/mybootstrap.sh test-cluster
168237
`),
169238
},
170239
}

config/crd/bases/bootstrap.cluster.x-k8s.io_eksconfigs.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ spec:
205205
description: APIRetryAttempts is the number of retry attempts for
206206
AWS API call.
207207
type: integer
208+
boostrapCommandOverride:
209+
description: BootstrapCommandOverride allows you to override the bootstrap
210+
command to use for EKS nodes.
211+
type: string
208212
containerRuntime:
209213
description: ContainerRuntime specify the container runtime to use
210214
when bootstrapping EKS.
@@ -243,6 +247,18 @@ spec:
243247
description: ServiceIPV6Cidr is the ipv6 cidr range of the cluster.
244248
If this is specified then the ip family will be set to ipv6.
245249
type: string
250+
postBootstrapCommands:
251+
description: PostBootstrapCommands specifies extra commands to run
252+
after bootstrapping nodes to the cluster
253+
items:
254+
type: string
255+
type: array
256+
preBootstrapCommands:
257+
description: PreBootstrapCommands specifies extra commands to run
258+
before bootstrapping nodes to the cluster
259+
items:
260+
type: string
261+
type: array
246262
useMaxPods:
247263
description: UseMaxPods sets --max-pods for the kubelet when true.
248264
type: boolean

config/crd/bases/bootstrap.cluster.x-k8s.io_eksconfigtemplates.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ spec:
138138
description: APIRetryAttempts is the number of retry attempts
139139
for AWS API call.
140140
type: integer
141+
boostrapCommandOverride:
142+
description: BootstrapCommandOverride allows you to override
143+
the bootstrap command to use for EKS nodes.
144+
type: string
141145
containerRuntime:
142146
description: ContainerRuntime specify the container runtime
143147
to use when bootstrapping EKS.
@@ -179,6 +183,18 @@ spec:
179183
cluster. If this is specified then the ip family will be
180184
set to ipv6.
181185
type: string
186+
postBootstrapCommands:
187+
description: PostBootstrapCommands specifies extra commands
188+
to run after bootstrapping nodes to the cluster
189+
items:
190+
type: string
191+
type: array
192+
preBootstrapCommands:
193+
description: PreBootstrapCommands specifies extra commands
194+
to run before bootstrapping nodes to the cluster
195+
items:
196+
type: string
197+
type: array
182198
useMaxPods:
183199
description: UseMaxPods sets --max-pods for the kubelet when
184200
true.

0 commit comments

Comments
 (0)