sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/userdata/userdata.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package userdata
    18  
    19  import (
    20  	"bytes"
    21  	"text/template"
    22  
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  const (
    27  	defaultHeader = `#!/usr/bin/env bash
    28  
    29  # Copyright 2018 by the contributors
    30  #
    31  #    Licensed under the Apache License, Version 2.0 (the "License");
    32  #    you may not use this file except in compliance with the License.
    33  #    You may obtain a copy of the License at
    34  #
    35  #    	http://www.apache.org/licenses/LICENSE-2.0
    36  #
    37  #    Unless required by applicable law or agreed to in writing, software
    38  #    distributed under the License is distributed on an "AS IS" BASIS,
    39  #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    40  #    See the License for the specific language governing permissions and
    41  #    limitations under the License.
    42  
    43  set -o verbose
    44  set -o errexit
    45  set -o nounset
    46  set -o pipefail
    47  `
    48  )
    49  
    50  type baseUserData struct {
    51  	Header     string
    52  	WriteFiles []Files
    53  }
    54  
    55  func generate(kind, tpl string, data interface{}) (string, error) {
    56  	tm := template.New(kind).Funcs(defaultTemplateFuncMap)
    57  	if _, err := tm.Parse(filesTemplate); err != nil {
    58  		return "", errors.Wrap(err, "failed to parse files template")
    59  	}
    60  
    61  	t, err := tm.Parse(tpl)
    62  	if err != nil {
    63  		return "", errors.Wrapf(err, "failed to parse %s template", kind)
    64  	}
    65  
    66  	var out bytes.Buffer
    67  	if err := t.Execute(&out, data); err != nil {
    68  		return "", errors.Wrapf(err, "failed to generate %s template", kind)
    69  	}
    70  
    71  	return out.String(), nil
    72  }