sigs.k8s.io/cluster-api/bootstrap/kubeadm@v0.0.0-20191016155141-23a891785b60/cloudinit/controlplane_join.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  	"github.com/pkg/errors"
    21  	"sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/cluster"
    22  )
    23  
    24  const (
    25  	controlPlaneJoinCloudInit = `{{.Header}}
    26  {{template "files" .WriteFiles}}
    27  -   path: /tmp/kubeadm-controlplane-join-config.yaml
    28      owner: root:root
    29      permissions: '0640'
    30      content: |
    31  {{.JoinConfiguration | Indent 6}}
    32  runcmd:
    33  {{- template "commands" .PreKubeadmCommands }}
    34    - 'kubeadm join --config /tmp/kubeadm-controlplane-join-config.yaml'
    35  {{- template "commands" .PostKubeadmCommands }}
    36  {{- template "ntp" .NTP }}
    37  {{- template "users" .Users }}
    38  `
    39  )
    40  
    41  // ControlPlaneJoinInput defines context to generate controlplane instance user data for control plane node join.
    42  type ControlPlaneJoinInput struct {
    43  	BaseUserData
    44  	cluster.Certificates
    45  
    46  	BootstrapToken    string
    47  	JoinConfiguration string
    48  }
    49  
    50  // NewJoinControlPlane returns the user data string to be used on a new control plane instance.
    51  func NewJoinControlPlane(input *ControlPlaneJoinInput) ([]byte, error) {
    52  	input.Header = cloudConfigHeader
    53  	// TODO: Consider validating that the correct certificates exist. It is different for external/stacked etcd
    54  	input.WriteFiles = input.Certificates.AsFiles()
    55  	input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
    56  	userData, err := generate("JoinControlplane", controlPlaneJoinCloudInit, input)
    57  	if err != nil {
    58  		return nil, errors.Wrapf(err, "failed to generate user data for machine joining control plane")
    59  	}
    60  
    61  	return userData, err
    62  }