Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Inject stack name into userdata for nodepool workers #735

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions core/controlplane/config/templates/cloud-config-worker
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{ define "instance" -}}
{{ define "instance-script" -}}
{{- $S3URI := self.Parts.s3.Asset.S3URL -}}
#!/bin/bash -xe
. /etc/environment
export COREOS_PRIVATE_IPV4 COREOS_PRIVATE_IPV6 COREOS_PUBLIC_IPV4 COREOS_PUBLIC_IPV6
REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
Expand All @@ -22,14 +21,17 @@ run aws s3 --region $REGION cp {{ $S3URI }} /var/run/coreos/$USERDATA_FILE

INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

run /bin/sh -c 'echo {{.StackNameEnvVarName}}=$(aws ec2 describe-tags --region "'$REGION'" --filters \
"Name=resource-id,Values='$INSTANCE_ID'" \
"Name=key,Values=aws:cloudformation:stack-name" \
--output json \
| jq -r ".Tags[].Value") >> {{.StackNameEnvFileName}}'

exec /usr/bin/coreos-cloudinit --from-file /var/run/coreos/$USERDATA_FILE
{{ end }}

{{ define "instance" -}}
{ "Fn::Base64": { "Fn::Join" : ["", [
"#!/bin/bash -xe\n",
{"Fn::Join":["",[ "echo '{{.StackNameEnvVarName}}=", { "Ref": "AWS::StackName" }, "' >> {{.StackNameEnvFileName}}\n" ]]},
{{ (execTemplate "instance-script" .) | toJSON }}
]]}}
{{ end }}

{{ define "s3" -}}
#cloud-config
coreos:
Expand Down
4 changes: 2 additions & 2 deletions core/nodepool/config/templates/stack-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
{{end}}
],
"SubnetId": {{$workerSubnet.Ref}},
"UserData": {{ $.UserDataWorker.Parts.instance.Base64 true | checkSizeLessThan 16384 | quote }}
"UserData": {{ $.UserDataWorker.Parts.instance.Template }}
}
{{end}}
{{end}}
Expand Down Expand Up @@ -261,7 +261,7 @@
{{else}}
"PlacementTenancy": "{{.Tenancy}}",
{{end}}
"UserData": {{ .UserDataWorker.Parts.instance.Base64 true | checkSizeLessThan 16384 | quote }}
"UserData": {{ .UserDataWorker.Parts.instance.Template }}
},
"Type": "AWS::AutoScaling::LaunchConfiguration"
{{if not .IAMConfig.InstanceProfile.Arn}}
Expand Down
21 changes: 20 additions & 1 deletion filereader/texttemplate/texttemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package texttemplate

import (
"bytes"
"encoding/json"
"fmt"
"github.com/Masterminds/sprig"
"io/ioutil"
Expand All @@ -21,9 +22,27 @@ func Parse(filename string, funcs template.FuncMap) (*template.Template, error)
}
return content, nil
},
"toJSON": func(v interface{}) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toJson is already available through Masterminds/sprig according to https://github.com/Masterminds/sprig/blob/dba49a8d3a46ae8522f79e2c0241842d6ab613ca/defaults.go#L66
Could we use the one from sprig instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't check if it exists in a code, because it is undocumented on their godoc page: https://godoc.org/github.com/Masterminds/sprig

so I am not sure we can rely on it being available all the time

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redbaron I believe we can expect it to remain to be existing because it is used widely in many helm charts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good catch 👍 Let me also add that both sprig's and helm's implementations of toJson seems to swallow errors, which sounds problematic to me. I'm now convinced to have our own implementation!

data, err := json.Marshal(v)
return string(data), err
},
"execTemplate": func(name string, ctx interface{}) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the template func bundled in text/template instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a workaround for template not being a real function :) You can not pipe or capture its result

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good to know! Thanks 😄

panic("[bug] Stub 'execTemplate' was not replaced")
},
}

t, err := template.New(filename).Funcs(sprig.HermeticTxtFuncMap()).Funcs(funcs).Funcs(funcs2).Parse(string(raw))
if err == nil {
t = t.Funcs(template.FuncMap{
"execTemplate": func(name string, ctx interface{}) (string, error) {
b := bytes.Buffer{}
err := t.ExecuteTemplate(&b, name, ctx)
return b.String(), err
},
})
}
return t, err

return template.New(filename).Funcs(sprig.HermeticTxtFuncMap()).Funcs(funcs).Funcs(funcs2).Parse(string(raw))
}

func GetBytesBuffer(filename string, data interface{}) (*bytes.Buffer, error) {
Expand Down