mirror of https://github.com/kubernetes/kops.git
Merge pull request #12991 from justinsb/avoid_double_encoding_scripts
Avoid double-encoding templates
This commit is contained in:
commit
648858a78a
|
@ -416,17 +416,24 @@ func (b *BootstrapScript) Run(c *fi.Context) error {
|
|||
},
|
||||
}
|
||||
|
||||
awsNodeUpTemplate, err := resources.AWSNodeUpTemplate(b.ig)
|
||||
nodeupScriptResource, err := NewTemplateResource("nodeup", resources.NodeUpTemplate, functions, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templateResource, err := NewTemplateResource("nodeup", awsNodeUpTemplate, functions, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.resource.Resource = fi.FunctionToResource(func() ([]byte, error) {
|
||||
nodeupScript, err := fi.ResourceAsString(nodeupScriptResource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.resource.Resource = templateResource
|
||||
awsUserData, err := resources.AWSMultipartMIME(nodeupScript, b.ig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []byte(awsUserData), nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -165,10 +165,10 @@ download-release
|
|||
echo "== nodeup node config done =="
|
||||
`
|
||||
|
||||
// AWSNodeUpTemplate returns a MIME Multi Part Archive containing the nodeup (bootstrap) script
|
||||
// AWSMultipartMIME returns a MIME Multi Part Archive containing the nodeup (bootstrap) script
|
||||
// and any additional User Data passed to using AdditionalUserData in the IG Spec
|
||||
func AWSNodeUpTemplate(ig *kops.InstanceGroup) (string, error) {
|
||||
userDataTemplate := NodeUpTemplate
|
||||
func AWSMultipartMIME(bootScript string, ig *kops.InstanceGroup) (string, error) {
|
||||
userData := bootScript
|
||||
|
||||
if len(ig.Spec.AdditionalUserData) > 0 {
|
||||
/* Create a buffer to hold the user-data*/
|
||||
|
@ -188,7 +188,7 @@ func AWSNodeUpTemplate(ig *kops.InstanceGroup) (string, error) {
|
|||
|
||||
var err error
|
||||
if !ig.IsBastion() {
|
||||
err := writeUserDataPart(mimeWriter, "nodeup.sh", "text/x-shellscript", []byte(userDataTemplate))
|
||||
err := writeUserDataPart(mimeWriter, "nodeup.sh", "text/x-shellscript", []byte(bootScript))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -206,10 +206,10 @@ func AWSNodeUpTemplate(ig *kops.InstanceGroup) (string, error) {
|
|||
writer.Flush()
|
||||
mimeWriter.Close()
|
||||
|
||||
userDataTemplate = buffer.String()
|
||||
userData = buffer.String()
|
||||
}
|
||||
|
||||
return userDataTemplate, nil
|
||||
return userData, nil
|
||||
}
|
||||
|
||||
func writeUserDataPart(mimeWriter *multipart.Writer, fileName string, contentType string, content []byte) error {
|
||||
|
|
|
@ -225,3 +225,28 @@ func (r *TaskDependentResource) GetDependencies(tasks map[string]Task) []Task {
|
|||
func (r *TaskDependentResource) IsReady() bool {
|
||||
return r.Resource != nil
|
||||
}
|
||||
|
||||
// FunctionToResource converts a function to a Resource. The result of executing the function is cached.
|
||||
func FunctionToResource(fn func() ([]byte, error)) Resource {
|
||||
return &functionResource{
|
||||
fn: fn,
|
||||
}
|
||||
}
|
||||
|
||||
type functionResource struct {
|
||||
data []byte
|
||||
fn func() ([]byte, error)
|
||||
}
|
||||
|
||||
func (r *functionResource) Open() (io.Reader, error) {
|
||||
b := r.data
|
||||
if b == nil {
|
||||
data, err := r.fn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.data = data
|
||||
b = data
|
||||
}
|
||||
return bytes.NewReader(b), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue