From 56b4eed9964f147ec9b4c7d7de7d26ae59fa5a42 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Wed, 5 Jul 2017 11:10:54 +0100 Subject: [PATCH 1/2] Inject stack name into userdata for nodepool workers Fixes #727 --- .../config/templates/cloud-config-worker | 18 +++++++++------- .../config/templates/stack-template.json | 2 +- filereader/texttemplate/texttemplate.go | 21 ++++++++++++++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/core/controlplane/config/templates/cloud-config-worker b/core/controlplane/config/templates/cloud-config-worker index 6e5975c7d..2e600bab7 100644 --- a/core/controlplane/config/templates/cloud-config-worker +++ b/core/controlplane/config/templates/cloud-config-worker @@ -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') @@ -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: diff --git a/core/nodepool/config/templates/stack-template.json b/core/nodepool/config/templates/stack-template.json index 2323526d3..6163c053b 100644 --- a/core/nodepool/config/templates/stack-template.json +++ b/core/nodepool/config/templates/stack-template.json @@ -101,7 +101,7 @@ {{end}} ], "SubnetId": {{$workerSubnet.Ref}}, - "UserData": {{ $.UserDataWorker.Parts.instance.Base64 true | checkSizeLessThan 16384 | quote }} + "UserData": {{ $.UserDataWorker.Parts.instance.Template }} } {{end}} {{end}} diff --git a/filereader/texttemplate/texttemplate.go b/filereader/texttemplate/texttemplate.go index 807a4d285..fbd9a735b 100644 --- a/filereader/texttemplate/texttemplate.go +++ b/filereader/texttemplate/texttemplate.go @@ -2,6 +2,7 @@ package texttemplate import ( "bytes" + "encoding/json" "fmt" "github.com/Masterminds/sprig" "io/ioutil" @@ -21,9 +22,27 @@ func Parse(filename string, funcs template.FuncMap) (*template.Template, error) } return content, nil }, + "toJSON": func(v interface{}) (string, error) { + data, err := json.Marshal(v) + return string(data), err + }, + "execTemplate": func(name string, ctx interface{}) (string, error) { + 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) { From 08e5eed66e0f8e3a5e4f51e8cab337120be025e1 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Wed, 5 Jul 2017 14:11:22 +0100 Subject: [PATCH 2/2] Forgot to make changes to non-SpotFleet workers to call new userdata differently --- core/nodepool/config/templates/stack-template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/nodepool/config/templates/stack-template.json b/core/nodepool/config/templates/stack-template.json index 6163c053b..5eadd7a29 100644 --- a/core/nodepool/config/templates/stack-template.json +++ b/core/nodepool/config/templates/stack-template.json @@ -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}}