...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/config/joinconfiguration.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/config

     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 config
    18  
    19  import (
    20  	"os"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"k8s.io/klog/v2"
    26  
    27  	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
    28  	kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
    29  	kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
    30  	"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
    31  	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
    32  	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
    33  	"k8s.io/kubernetes/cmd/kubeadm/app/util/config/strict"
    34  )
    35  
    36  // SetJoinDynamicDefaults checks and sets configuration values for the JoinConfiguration object
    37  func SetJoinDynamicDefaults(cfg *kubeadmapi.JoinConfiguration, skipCRIDetect bool) error {
    38  	addControlPlaneTaint := false
    39  	if cfg.ControlPlane != nil {
    40  		addControlPlaneTaint = true
    41  	}
    42  	if err := SetNodeRegistrationDynamicDefaults(&cfg.NodeRegistration, addControlPlaneTaint, skipCRIDetect); err != nil {
    43  		return err
    44  	}
    45  
    46  	return SetJoinControlPlaneDefaults(cfg.ControlPlane)
    47  }
    48  
    49  // SetJoinControlPlaneDefaults checks and sets configuration values for the JoinControlPlane object
    50  func SetJoinControlPlaneDefaults(cfg *kubeadmapi.JoinControlPlane) error {
    51  	if cfg != nil {
    52  		if err := SetAPIEndpointDynamicDefaults(&cfg.LocalAPIEndpoint); err != nil {
    53  			return err
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  // LoadOrDefaultJoinConfiguration takes a path to a config file and a versioned configuration that can serve as the default config
    60  // If cfgPath is specified, defaultversionedcfg will always get overridden. Otherwise, the default config (often populated by flags) will be used.
    61  // Then the external, versioned configuration is defaulted and converted to the internal type.
    62  // Right thereafter, the configuration is defaulted again with dynamic values (like IP addresses of a machine, etc)
    63  // Lastly, the internal config is validated and returned.
    64  func LoadOrDefaultJoinConfiguration(cfgPath string, defaultversionedcfg *kubeadmapiv1.JoinConfiguration, opts LoadOrDefaultConfigurationOptions) (*kubeadmapi.JoinConfiguration, error) {
    65  	var (
    66  		config *kubeadmapi.JoinConfiguration
    67  		err    error
    68  	)
    69  	if cfgPath != "" {
    70  		// Loads configuration from config file, if provided
    71  		config, err = LoadJoinConfigurationFromFile(cfgPath, opts)
    72  	} else {
    73  		config, err = DefaultedJoinConfiguration(defaultversionedcfg, opts)
    74  	}
    75  	if err == nil {
    76  		prepareStaticVariables(config)
    77  	}
    78  	return config, err
    79  }
    80  
    81  // LoadJoinConfigurationFromFile loads versioned JoinConfiguration from file, converts it to internal, defaults and validates it
    82  func LoadJoinConfigurationFromFile(cfgPath string, opts LoadOrDefaultConfigurationOptions) (*kubeadmapi.JoinConfiguration, error) {
    83  	klog.V(1).Infof("loading configuration from %q", cfgPath)
    84  
    85  	b, err := os.ReadFile(cfgPath)
    86  	if err != nil {
    87  		return nil, errors.Wrapf(err, "unable to read config from %q ", cfgPath)
    88  	}
    89  
    90  	gvkmap, err := kubeadmutil.SplitYAMLDocuments(b)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	return documentMapToJoinConfiguration(gvkmap, false, false, false, opts.SkipCRIDetect)
    96  }
    97  
    98  // documentMapToJoinConfiguration takes a map between GVKs and YAML documents (as returned by SplitYAMLDocuments),
    99  // finds a JoinConfiguration, decodes it, dynamically defaults it and then validates it prior to return.
   100  func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental, strictErrors, skipCRIDetect bool) (*kubeadmapi.JoinConfiguration, error) {
   101  	joinBytes := []byte{}
   102  	for gvk, bytes := range gvkmap {
   103  		// not interested in anything other than JoinConfiguration
   104  		if gvk.Kind != constants.JoinConfigurationKind {
   105  			continue
   106  		}
   107  
   108  		// check if this version is supported and possibly not deprecated
   109  		if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
   110  			return nil, err
   111  		}
   112  
   113  		// verify the validity of the YAML
   114  		if err := strict.VerifyUnmarshalStrict([]*runtime.Scheme{kubeadmscheme.Scheme}, gvk, bytes); err != nil {
   115  			if !strictErrors {
   116  				klog.Warning(err.Error())
   117  			} else {
   118  				return nil, err
   119  			}
   120  		}
   121  
   122  		joinBytes = bytes
   123  	}
   124  
   125  	if len(joinBytes) == 0 {
   126  		return nil, errors.Errorf("no %s found in the supplied config", constants.JoinConfigurationKind)
   127  	}
   128  
   129  	internalcfg := &kubeadmapi.JoinConfiguration{}
   130  	if err := runtime.DecodeInto(kubeadmscheme.Codecs.UniversalDecoder(), joinBytes, internalcfg); err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	// Applies dynamic defaults to settings not provided with flags
   135  	if err := SetJoinDynamicDefaults(internalcfg, skipCRIDetect); err != nil {
   136  		return nil, err
   137  	}
   138  	// Validates cfg (flags/configs + defaults)
   139  	if err := validation.ValidateJoinConfiguration(internalcfg).ToAggregate(); err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	return internalcfg, nil
   144  }
   145  
   146  // DefaultedJoinConfiguration takes a versioned JoinConfiguration (usually filled in by command line parameters), defaults it, converts it to internal and validates it
   147  func DefaultedJoinConfiguration(defaultversionedcfg *kubeadmapiv1.JoinConfiguration, opts LoadOrDefaultConfigurationOptions) (*kubeadmapi.JoinConfiguration, error) {
   148  	internalcfg := &kubeadmapi.JoinConfiguration{}
   149  
   150  	// Takes passed flags into account; the defaulting is executed once again enforcing assignment of
   151  	// static default values to cfg only for values not provided with flags
   152  	kubeadmscheme.Scheme.Default(defaultversionedcfg)
   153  	if err := kubeadmscheme.Scheme.Convert(defaultversionedcfg, internalcfg, nil); err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	// Applies dynamic defaults to settings not provided with flags
   158  	if err := SetJoinDynamicDefaults(internalcfg, opts.SkipCRIDetect); err != nil {
   159  		return nil, err
   160  	}
   161  	// Validates cfg (flags/configs + defaults)
   162  	if err := validation.ValidateJoinConfiguration(internalcfg).ToAggregate(); err != nil {
   163  		return nil, err
   164  	}
   165  
   166  	return internalcfg, nil
   167  }
   168  

View as plain text