Skip to content

Commit

Permalink
Add preBootstrapCommands to userdata when using customAMI (#4065)
Browse files Browse the repository at this point in the history
  • Loading branch information
kishoregv committed Aug 24, 2021
1 parent 0551122 commit 8bcd365
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
26 changes: 21 additions & 5 deletions pkg/nodebootstrap/managed_al2.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ func NewManagedAL2Bootstrapper(ng *api.ManagedNodeGroup) *ManagedAL2 {
func (m *ManagedAL2) UserData() (string, error) {
ng := m.ng

// We don't use MIME format when launching managed nodegroups with a custom AMI
if strings.HasPrefix(ng.AMI, "ami-") {
return makeCustomAMIUserData(ng.NodeGroupBase)
return makeCustomAMIUserData(ng.NodeGroupBase, m.UserDataMimeBoundary)
}

var (
Expand Down Expand Up @@ -71,12 +70,29 @@ func (m *ManagedAL2) UserData() (string, error) {
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}

func makeCustomAMIUserData(ng *api.NodeGroupBase) (string, error) {
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 {
return base64.StdEncoding.EncodeToString([]byte(*ng.OverrideBootstrapCommand)), nil
scripts = append(scripts, *ng.OverrideBootstrapCommand)
}

return "", nil
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 {
Expand Down
26 changes: 24 additions & 2 deletions pkg/nodebootstrap/managed_al2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ var _ = DescribeTable("Managed AL2", func(e managedEntry) {
Name: "custom-ami",
InstanceType: "m5.xlarge",
AMI: "ami-custom",
PreBootstrapCommands: []string{
"date",
"echo hello",
},
OverrideBootstrapCommand: aws.String(`#!/bin/bash
set -ex
B64_CLUSTER_CA=dGVzdAo=
Expand All @@ -60,12 +64,30 @@ API_SERVER_URL=https://test.com
},
},

// remove gke
expectedUserData: `#!/bin/bash
expectedUserData: `MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=//
--//
Content-Type: text/x-shellscript
Content-Type: charset="us-ascii"
date
--//
Content-Type: text/x-shellscript
Content-Type: charset="us-ascii"
echo hello
--//
Content-Type: text/x-shellscript
Content-Type: charset="us-ascii"
#!/bin/bash
set -ex
B64_CLUSTER_CA=dGVzdAo=
API_SERVER_URL=https://test.com
/etc/eks/bootstrap.sh launch-template --b64-cluster-ca $B64_CLUSTER_CA --apiserver-endpoint $API_SERVER_URL
--//--
`,
}),

Expand Down

0 comments on commit 8bcd365

Please sign in to comment.