sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/internal/builder/builders.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 builder
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  
    24  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
    25  )
    26  
    27  // KubeadmConfigBuilder contains the information needed to produce a KubeadmConfig.
    28  type KubeadmConfigBuilder struct {
    29  	name          string
    30  	namespace     string
    31  	joinConfig    *bootstrapv1.JoinConfiguration
    32  	initConfig    *bootstrapv1.InitConfiguration
    33  	clusterConfig *bootstrapv1.ClusterConfiguration
    34  }
    35  
    36  // KubeadmConfig returns a KubeadmConfigBuilder with the supplied name and namespace.
    37  func KubeadmConfig(namespace, name string) *KubeadmConfigBuilder {
    38  	return &KubeadmConfigBuilder{
    39  		name:      name,
    40  		namespace: namespace,
    41  	}
    42  }
    43  
    44  // WithJoinConfig adds the passed JoinConfig to the KubeadmConfigBuilder.
    45  func (k *KubeadmConfigBuilder) WithJoinConfig(joinConf *bootstrapv1.JoinConfiguration) *KubeadmConfigBuilder {
    46  	k.joinConfig = joinConf
    47  	return k
    48  }
    49  
    50  // WithClusterConfig adds the passed ClusterConfig to the KubeadmConfigBuilder.
    51  func (k *KubeadmConfigBuilder) WithClusterConfig(clusterConf *bootstrapv1.ClusterConfiguration) *KubeadmConfigBuilder {
    52  	k.clusterConfig = clusterConf
    53  	return k
    54  }
    55  
    56  // WithInitConfig adds the passed InitConfig to the KubeadmConfigBuilder.
    57  func (k *KubeadmConfigBuilder) WithInitConfig(initConf *bootstrapv1.InitConfiguration) *KubeadmConfigBuilder {
    58  	k.initConfig = initConf
    59  	return k
    60  }
    61  
    62  // Unstructured produces a KubeadmConfig as an unstructured Kubernetes object.
    63  func (k *KubeadmConfigBuilder) Unstructured() *unstructured.Unstructured {
    64  	config := k.Build()
    65  	rawMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(config)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  	return &unstructured.Unstructured{Object: rawMap}
    70  }
    71  
    72  // Build produces a KubeadmConfig from the variable in the KubeadmConfigBuilder.
    73  func (k *KubeadmConfigBuilder) Build() *bootstrapv1.KubeadmConfig {
    74  	config := &bootstrapv1.KubeadmConfig{
    75  		TypeMeta: metav1.TypeMeta{
    76  			Kind:       "KubeadmConfig",
    77  			APIVersion: bootstrapv1.GroupVersion.String(),
    78  		},
    79  		ObjectMeta: metav1.ObjectMeta{
    80  			Namespace: k.namespace,
    81  			Name:      k.name,
    82  		},
    83  	}
    84  	if k.initConfig != nil {
    85  		config.Spec.InitConfiguration = k.initConfig
    86  	}
    87  	if k.joinConfig != nil {
    88  		config.Spec.JoinConfiguration = k.joinConfig
    89  	}
    90  	if k.clusterConfig != nil {
    91  		config.Spec.ClusterConfiguration = k.clusterConfig
    92  	}
    93  	return config
    94  }