sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/internal/ignition/ignition.go (about)

     1  /*
     2  Copyright 2021 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 ignition aggregates all Ignition flavors into a single package to be consumed
    18  // by the bootstrap provider by exposing an API similar to 'internal/cloudinit' package.
    19  package ignition
    20  
    21  import (
    22  	"fmt"
    23  
    24  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
    25  	"sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/cloudinit"
    26  	"sigs.k8s.io/cluster-api/bootstrap/kubeadm/internal/ignition/clc"
    27  )
    28  
    29  const (
    30  	joinSubcommand         = "join"
    31  	initSubcommand         = "init"
    32  	kubeadmCommandTemplate = "kubeadm %s --config /etc/kubeadm.yml %s"
    33  )
    34  
    35  // NodeInput defines the context to generate a node user data.
    36  type NodeInput struct {
    37  	*cloudinit.NodeInput
    38  
    39  	Ignition *bootstrapv1.IgnitionSpec
    40  }
    41  
    42  // ControlPlaneJoinInput defines context to generate controlplane instance user data for control plane node join.
    43  type ControlPlaneJoinInput struct {
    44  	*cloudinit.ControlPlaneJoinInput
    45  
    46  	Ignition *bootstrapv1.IgnitionSpec
    47  }
    48  
    49  // ControlPlaneInput defines the context to generate a controlplane instance user data.
    50  type ControlPlaneInput struct {
    51  	*cloudinit.ControlPlaneInput
    52  
    53  	Ignition *bootstrapv1.IgnitionSpec
    54  }
    55  
    56  // NewNode returns Ignition configuration for new worker node joining the cluster.
    57  func NewNode(input *NodeInput) ([]byte, string, error) {
    58  	if input == nil {
    59  		return nil, "", fmt.Errorf("input can't be nil")
    60  	}
    61  
    62  	if input.NodeInput == nil {
    63  		return nil, "", fmt.Errorf("node input can't be nil")
    64  	}
    65  
    66  	input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
    67  	input.KubeadmCommand = fmt.Sprintf(kubeadmCommandTemplate, joinSubcommand, input.KubeadmVerbosity)
    68  
    69  	return render(&input.BaseUserData, input.Ignition, input.JoinConfiguration)
    70  }
    71  
    72  // NewJoinControlPlane returns Ignition configuration for new controlplane node joining the cluster.
    73  func NewJoinControlPlane(input *ControlPlaneJoinInput) ([]byte, string, error) {
    74  	if input == nil {
    75  		return nil, "", fmt.Errorf("input can't be nil")
    76  	}
    77  
    78  	if input.ControlPlaneJoinInput == nil {
    79  		return nil, "", fmt.Errorf("controlplane join input can't be nil")
    80  	}
    81  
    82  	input.WriteFiles = input.Certificates.AsFiles()
    83  	input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
    84  	input.KubeadmCommand = fmt.Sprintf(kubeadmCommandTemplate, joinSubcommand, input.KubeadmVerbosity)
    85  
    86  	return render(&input.BaseUserData, input.Ignition, input.JoinConfiguration)
    87  }
    88  
    89  // NewInitControlPlane returns Ignition configuration for bootstrapping new cluster.
    90  func NewInitControlPlane(input *ControlPlaneInput) ([]byte, string, error) {
    91  	if input == nil {
    92  		return nil, "", fmt.Errorf("input can't be nil")
    93  	}
    94  
    95  	if input.ControlPlaneInput == nil {
    96  		return nil, "", fmt.Errorf("controlplane input can't be nil")
    97  	}
    98  
    99  	input.WriteFiles = input.Certificates.AsFiles()
   100  	input.WriteFiles = append(input.WriteFiles, input.AdditionalFiles...)
   101  	input.KubeadmCommand = fmt.Sprintf(kubeadmCommandTemplate, initSubcommand, input.KubeadmVerbosity)
   102  
   103  	kubeadmConfig := fmt.Sprintf("%s\n---\n%s", input.ClusterConfiguration, input.InitConfiguration)
   104  
   105  	return render(&input.BaseUserData, input.Ignition, kubeadmConfig)
   106  }
   107  
   108  func render(input *cloudinit.BaseUserData, ignitionConfig *bootstrapv1.IgnitionSpec, kubeadmConfig string) ([]byte, string, error) {
   109  	clcConfig := &bootstrapv1.ContainerLinuxConfig{}
   110  	if ignitionConfig != nil && ignitionConfig.ContainerLinuxConfig != nil {
   111  		clcConfig = ignitionConfig.ContainerLinuxConfig
   112  	}
   113  
   114  	return clc.Render(input, clcConfig, kubeadmConfig)
   115  }