...

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

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

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

View as plain text