sigs.k8s.io/cluster-api/bootstrap/kubeadm@v0.0.0-20191016155141-23a891785b60/cloudinit/cloudinit.go (about)

     1  /*
     2  Copyright 2019 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 cloudinit
    18  
    19  import (
    20  	"bytes"
    21  	"text/template"
    22  
    23  	"github.com/pkg/errors"
    24  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2"
    25  )
    26  
    27  const (
    28  	cloudConfigHeader = `## template: jinja
    29  #cloud-config
    30  `
    31  )
    32  
    33  // BaseUserData is shared across all the various types of files written to disk.
    34  type BaseUserData struct {
    35  	Header              string
    36  	PreKubeadmCommands  []string
    37  	PostKubeadmCommands []string
    38  	AdditionalFiles     []bootstrapv1.File
    39  	WriteFiles          []bootstrapv1.File
    40  	Users               []bootstrapv1.User
    41  	NTP                 *bootstrapv1.NTP
    42  }
    43  
    44  func generate(kind string, tpl string, data interface{}) ([]byte, error) {
    45  	tm := template.New(kind).Funcs(defaultTemplateFuncMap)
    46  	if _, err := tm.Parse(filesTemplate); err != nil {
    47  		return nil, errors.Wrap(err, "failed to parse files template")
    48  	}
    49  
    50  	if _, err := tm.Parse(commandsTemplate); err != nil {
    51  		return nil, errors.Wrap(err, "failed to parse commands template")
    52  	}
    53  
    54  	if _, err := tm.Parse(ntpTemplate); err != nil {
    55  		return nil, errors.Wrap(err, "failed to parse ntp template")
    56  	}
    57  
    58  	if _, err := tm.Parse(usersTemplate); err != nil {
    59  		return nil, errors.Wrap(err, "failed to parse users template")
    60  	}
    61  
    62  	t, err := tm.Parse(tpl)
    63  	if err != nil {
    64  		return nil, errors.Wrapf(err, "failed to parse %s template", kind)
    65  	}
    66  
    67  	var out bytes.Buffer
    68  	if err := t.Execute(&out, data); err != nil {
    69  		return nil, errors.Wrapf(err, "failed to generate %s template", kind)
    70  	}
    71  
    72  	return out.Bytes(), nil
    73  }