kubeadm: fix the bug that kubeadm always do CRI detection when --config is passed even if it is not required by subcommand
This commit is contained in:
		@@ -136,6 +136,7 @@ func (o *genCSRConfig) load() (err error) {
 | 
			
		||||
		o.kubeadmConfigPath,
 | 
			
		||||
		cmdutil.DefaultInitConfiguration(),
 | 
			
		||||
		&kubeadmapiv1.ClusterConfiguration{},
 | 
			
		||||
		true, /* skipCRIDetect */
 | 
			
		||||
	)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -353,7 +354,7 @@ func getInternalCfg(cfgPath string, kubeconfigPath string, cfg kubeadmapiv1.Clus
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Read config from --config if provided. Otherwise, use the default configuration
 | 
			
		||||
	return configutil.LoadOrDefaultInitConfiguration(cfgPath, cmdutil.DefaultInitConfiguration(), &cfg)
 | 
			
		||||
	return configutil.LoadOrDefaultInitConfiguration(cfgPath, cmdutil.DefaultInitConfiguration(), &cfg, true /* skipCRIDetect */)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// newCmdCertsExpiration creates a new `cert check-expiration` command.
 | 
			
		||||
 
 | 
			
		||||
@@ -223,7 +223,7 @@ func getDefaultNodeConfigBytes() ([]byte, error) {
 | 
			
		||||
		NodeRegistration: kubeadmapiv1old.NodeRegistrationOptions{
 | 
			
		||||
			CRISocket: constants.DefaultCRISocket, // avoid CRI detection
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	}, true /* skipCRIDetect */)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -234,7 +234,7 @@ func getDefaultNodeConfigBytes() ([]byte, error) {
 | 
			
		||||
func getDefaultResetConfigBytes() ([]byte, error) {
 | 
			
		||||
	internalcfg, err := configutil.DefaultedResetConfiguration(&kubeadmapiv1.ResetConfiguration{
 | 
			
		||||
		CRISocket: constants.DefaultCRISocket, // avoid CRI detection
 | 
			
		||||
	})
 | 
			
		||||
	}, true /* skipCRIDetect */)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -367,7 +367,7 @@ func newCmdConfigImagesPull() *cobra.Command {
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, externalInitCfg, externalClusterCfg)
 | 
			
		||||
			internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, externalInitCfg, externalClusterCfg, false)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
@@ -442,7 +442,7 @@ func newCmdConfigImagesList(out io.Writer, mockK8sVersion *string) *cobra.Comman
 | 
			
		||||
 | 
			
		||||
// NewImagesList returns the underlying struct for the "kubeadm config images list" command
 | 
			
		||||
func NewImagesList(cfgPath string, cfg *kubeadmapiv1old.ClusterConfiguration) (*ImagesList, error) {
 | 
			
		||||
	initcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, cmdutil.DefaultInitConfiguration(), cfg)
 | 
			
		||||
	initcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, cmdutil.DefaultInitConfiguration(), cfg, true /* skipCRIDetect */)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, errors.Wrap(err, "could not convert cfg to an internal cfg")
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -63,6 +63,7 @@ type initOptions struct {
 | 
			
		||||
	uploadCerts             bool
 | 
			
		||||
	skipCertificateKeyPrint bool
 | 
			
		||||
	patchesDir              string
 | 
			
		||||
	skipCRIDetect           bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// compile-time assert that the local data object satisfies the phases data interface.
 | 
			
		||||
@@ -150,9 +151,9 @@ func newCmdInit(out io.Writer, initOptions *initOptions) *cobra.Command {
 | 
			
		||||
	// both when running the entire workflow or single phases
 | 
			
		||||
	initRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
 | 
			
		||||
		if cmd.Flags().Lookup(options.NodeCRISocket) == nil {
 | 
			
		||||
			// avoid CRI detection
 | 
			
		||||
			// skip CRI detection
 | 
			
		||||
			// assume that the command execution does not depend on CRISocket when --cri-socket flag is not set
 | 
			
		||||
			initOptions.externalInitCfg.NodeRegistration.CRISocket = kubeadmconstants.UnknownCRISocket
 | 
			
		||||
			initOptions.skipCRIDetect = true
 | 
			
		||||
		}
 | 
			
		||||
		data, err := newInitData(cmd, args, initOptions, out)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@@ -301,7 +302,7 @@ func newInitData(cmd *cobra.Command, args []string, initOptions *initOptions, ou
 | 
			
		||||
 | 
			
		||||
	// Either use the config file if specified, or convert public kubeadm API to the internal InitConfiguration
 | 
			
		||||
	// and validates InitConfiguration
 | 
			
		||||
	cfg, err := configutil.LoadOrDefaultInitConfiguration(initOptions.cfgPath, initOptions.externalInitCfg, initOptions.externalClusterCfg)
 | 
			
		||||
	cfg, err := configutil.LoadOrDefaultInitConfiguration(initOptions.cfgPath, initOptions.externalInitCfg, initOptions.externalClusterCfg, initOptions.skipCRIDetect)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -135,6 +135,7 @@ type joinOptions struct {
 | 
			
		||||
	externalcfg           *kubeadmapiv1.JoinConfiguration
 | 
			
		||||
	patchesDir            string
 | 
			
		||||
	dryRun                bool
 | 
			
		||||
	skipCRIDetect         bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// compile-time assert that the local data object satisfies the phases data interface.
 | 
			
		||||
@@ -221,9 +222,9 @@ func newCmdJoin(out io.Writer, joinOptions *joinOptions) *cobra.Command {
 | 
			
		||||
	// both when running the entire workflow or single phases
 | 
			
		||||
	joinRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
 | 
			
		||||
		if cmd.Flags().Lookup(options.NodeCRISocket) == nil {
 | 
			
		||||
			// avoid CRI detection
 | 
			
		||||
			// skip CRI detection
 | 
			
		||||
			// assume that the command execution does not depend on CRISocket when --cri-socket flag is not set
 | 
			
		||||
			joinOptions.externalcfg.NodeRegistration.CRISocket = kubeadmconstants.UnknownCRISocket
 | 
			
		||||
			joinOptions.skipCRIDetect = true
 | 
			
		||||
		}
 | 
			
		||||
		data, err := newJoinData(cmd, args, joinOptions, out, kubeadmconstants.GetAdminKubeConfigPath())
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@@ -426,7 +427,7 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
 | 
			
		||||
		opt.externalcfg.Discovery.BootstrapToken = nil //NB. this could be removed when we get better control on args (e.g. phases without discovery should have NoArgs )
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cfg, err := configutil.LoadOrDefaultJoinConfiguration(opt.cfgPath, opt.externalcfg)
 | 
			
		||||
	cfg, err := configutil.LoadOrDefaultJoinConfiguration(opt.cfgPath, opt.externalcfg, opt.skipCRIDetect)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -82,7 +82,7 @@ func newCmdUserKubeConfig(out io.Writer) *cobra.Command {
 | 
			
		||||
		Example: userKubeconfigExample,
 | 
			
		||||
		RunE: func(cmd *cobra.Command, args []string) error {
 | 
			
		||||
			// This call returns the ready-to-use configuration based on the defaults populated by flags
 | 
			
		||||
			internalCfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, initCfg, clusterCfg)
 | 
			
		||||
			internalCfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, initCfg, clusterCfg, true /* skipCRIDetect */)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -67,6 +67,7 @@ type resetOptions struct {
 | 
			
		||||
	cfgPath               string
 | 
			
		||||
	ignorePreflightErrors []string
 | 
			
		||||
	externalcfg           *v1beta4.ResetConfiguration
 | 
			
		||||
	skipCRIDetect         bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// resetData defines all the runtime information used when running the kubeadm reset workflow;
 | 
			
		||||
@@ -107,7 +108,7 @@ func newResetData(cmd *cobra.Command, opts *resetOptions, in io.Reader, out io.W
 | 
			
		||||
	var initCfg *kubeadmapi.InitConfiguration
 | 
			
		||||
 | 
			
		||||
	// Either use the config file if specified, or convert public kubeadm API to the internal ResetConfiguration and validates cfg.
 | 
			
		||||
	resetCfg, err := configutil.LoadOrDefaultResetConfiguration(opts.cfgPath, opts.externalcfg, allowExperimental)
 | 
			
		||||
	resetCfg, err := configutil.LoadOrDefaultResetConfiguration(opts.cfgPath, opts.externalcfg, allowExperimental, opts.skipCRIDetect)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -229,9 +230,9 @@ func newCmdReset(in io.Reader, out io.Writer, resetOptions *resetOptions) *cobra
 | 
			
		||||
	// both when running the entire workflow or single phases
 | 
			
		||||
	resetRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
 | 
			
		||||
		if cmd.Flags().Lookup(options.NodeCRISocket) == nil {
 | 
			
		||||
			// avoid CRI detection
 | 
			
		||||
			// skip CRI detection
 | 
			
		||||
			// assume that the command execution does not depend on CRISocket when --cri-socket flag is not set
 | 
			
		||||
			resetOptions.externalcfg.CRISocket = kubeadmconstants.UnknownCRISocket
 | 
			
		||||
			resetOptions.skipCRIDetect = true
 | 
			
		||||
		}
 | 
			
		||||
		data, err := newResetData(cmd, resetOptions, in, out, true)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -242,7 +242,7 @@ func RunCreateToken(out io.Writer, client clientset.Interface, cfgPath string, i
 | 
			
		||||
	// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
 | 
			
		||||
	klog.V(1).Infoln("[token] loading configurations")
 | 
			
		||||
 | 
			
		||||
	internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, initCfg, clusterCfg)
 | 
			
		||||
	internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, initCfg, clusterCfg, true /* skipCRIDetect */)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -92,7 +92,7 @@ func loadConfig(cfgPath string, client clientset.Interface, skipComponentConfigs
 | 
			
		||||
	// The resulting configs overwrite the existing cluster ones at the end of a successful upgrade apply operation.
 | 
			
		||||
	if isKubeadmConfigPresent(docmap) {
 | 
			
		||||
		klog.Warning("WARNING: Usage of the --config flag with kubeadm config types for reconfiguring the cluster during upgrade is not recommended!")
 | 
			
		||||
		cfg, err := configutil.BytesToInitConfiguration(configBytes)
 | 
			
		||||
		cfg, err := configutil.BytesToInitConfiguration(configBytes, false)
 | 
			
		||||
		return cfg, true, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -111,7 +111,7 @@ func runDiff(flags *diffFlags, args []string) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	var cfg *kubeadmapi.InitConfiguration
 | 
			
		||||
	if flags.cfgPath != "" {
 | 
			
		||||
		cfg, err = configutil.LoadInitConfigurationFromFile(flags.cfgPath)
 | 
			
		||||
		cfg, err = configutil.LoadInitConfigurationFromFile(flags.cfgPath, false)
 | 
			
		||||
	} else {
 | 
			
		||||
		var client *client.Clientset
 | 
			
		||||
		client, err = kubeconfigutil.ClientSetFromFile(flags.kubeConfigPath)
 | 
			
		||||
 
 | 
			
		||||
@@ -600,7 +600,7 @@ func getConfig(version, certsDir, etcdDataDir string) (*kubeadmapi.InitConfigura
 | 
			
		||||
	configBytes := []byte(fmt.Sprintf(testConfiguration, certsDir, etcdDataDir, version))
 | 
			
		||||
 | 
			
		||||
	// Unmarshal the config
 | 
			
		||||
	return configutil.BytesToInitConfiguration(configBytes)
 | 
			
		||||
	return configutil.BytesToInitConfiguration(configBytes, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getTempDir(t *testing.T, name string) (string, func()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1082,7 +1082,7 @@ func TestJoinIPCheck(t *testing.T) {
 | 
			
		||||
				UnsafeSkipCAVerification: true,
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	})
 | 
			
		||||
	}, true)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("unexpected failure when defaulting JoinConfiguration: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -59,7 +59,7 @@ func FetchInitConfigurationFromCluster(client clientset.Interface, printer outpu
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Apply dynamic defaults
 | 
			
		||||
	if err := SetInitDynamicDefaults(cfg); err != nil {
 | 
			
		||||
	if err := SetInitDynamicDefaults(cfg, false); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -265,7 +265,7 @@ func MigrateOldConfig(oldConfig []byte, allowExperimental bool) ([]byte, error)
 | 
			
		||||
	}
 | 
			
		||||
	// Migrate InitConfiguration and ClusterConfiguration if there are any in the config
 | 
			
		||||
	if kubeadmutil.GroupVersionKindsHasInitConfiguration(gvks...) || kubeadmutil.GroupVersionKindsHasClusterConfiguration(gvks...) {
 | 
			
		||||
		o, err := documentMapToInitConfiguration(gvkmap, true, allowExperimental, true)
 | 
			
		||||
		o, err := documentMapToInitConfiguration(gvkmap, true, allowExperimental, true, false)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return []byte{}, err
 | 
			
		||||
		}
 | 
			
		||||
@@ -278,7 +278,7 @@ func MigrateOldConfig(oldConfig []byte, allowExperimental bool) ([]byte, error)
 | 
			
		||||
 | 
			
		||||
	// Migrate JoinConfiguration if there is any
 | 
			
		||||
	if kubeadmutil.GroupVersionKindsHasJoinConfiguration(gvks...) {
 | 
			
		||||
		o, err := documentMapToJoinConfiguration(gvkmap, true, allowExperimental, true)
 | 
			
		||||
		o, err := documentMapToJoinConfiguration(gvkmap, true, allowExperimental, true, false)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return []byte{}, err
 | 
			
		||||
		}
 | 
			
		||||
@@ -291,7 +291,7 @@ func MigrateOldConfig(oldConfig []byte, allowExperimental bool) ([]byte, error)
 | 
			
		||||
 | 
			
		||||
	// Migrate ResetConfiguration if there is any
 | 
			
		||||
	if kubeadmutil.GroupVersionKindsHasResetConfiguration(gvks...) {
 | 
			
		||||
		o, err := documentMapToResetConfiguration(gvkmap, true, allowExperimental, true)
 | 
			
		||||
		o, err := documentMapToResetConfiguration(gvkmap, true, allowExperimental, true, false)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return []byte{}, err
 | 
			
		||||
		}
 | 
			
		||||
@@ -324,21 +324,21 @@ func ValidateConfig(config []byte, allowExperimental bool) error {
 | 
			
		||||
 | 
			
		||||
	// Validate InitConfiguration and ClusterConfiguration if there are any in the config
 | 
			
		||||
	if kubeadmutil.GroupVersionKindsHasInitConfiguration(gvks...) || kubeadmutil.GroupVersionKindsHasClusterConfiguration(gvks...) {
 | 
			
		||||
		if _, err := documentMapToInitConfiguration(gvkmap, true, allowExperimental, true); err != nil {
 | 
			
		||||
		if _, err := documentMapToInitConfiguration(gvkmap, true, allowExperimental, true, false); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Validate JoinConfiguration if there is any
 | 
			
		||||
	if kubeadmutil.GroupVersionKindsHasJoinConfiguration(gvks...) {
 | 
			
		||||
		if _, err := documentMapToJoinConfiguration(gvkmap, true, allowExperimental, true); err != nil {
 | 
			
		||||
		if _, err := documentMapToJoinConfiguration(gvkmap, true, allowExperimental, true, false); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Validate ResetConfiguration if there is any
 | 
			
		||||
	if kubeadmutil.GroupVersionKindsHasResetConfiguration(gvks...) {
 | 
			
		||||
		if _, err := documentMapToResetConfiguration(gvkmap, true, allowExperimental, true); err != nil {
 | 
			
		||||
		if _, err := documentMapToResetConfiguration(gvkmap, true, allowExperimental, true, false); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -57,11 +57,11 @@ var (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// SetInitDynamicDefaults checks and sets configuration values for the InitConfiguration object
 | 
			
		||||
func SetInitDynamicDefaults(cfg *kubeadmapi.InitConfiguration) error {
 | 
			
		||||
func SetInitDynamicDefaults(cfg *kubeadmapi.InitConfiguration, skipCRIDetect bool) error {
 | 
			
		||||
	if err := SetBootstrapTokensDynamicDefaults(&cfg.BootstrapTokens); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if err := SetNodeRegistrationDynamicDefaults(&cfg.NodeRegistration, true); err != nil {
 | 
			
		||||
	if err := SetNodeRegistrationDynamicDefaults(&cfg.NodeRegistration, true, skipCRIDetect); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if err := SetAPIEndpointDynamicDefaults(&cfg.LocalAPIEndpoint); err != nil {
 | 
			
		||||
@@ -97,7 +97,7 @@ func SetBootstrapTokensDynamicDefaults(cfg *[]bootstraptokenv1.BootstrapToken) e
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SetNodeRegistrationDynamicDefaults checks and sets configuration values for the NodeRegistration object
 | 
			
		||||
func SetNodeRegistrationDynamicDefaults(cfg *kubeadmapi.NodeRegistrationOptions, controlPlaneTaint bool) error {
 | 
			
		||||
func SetNodeRegistrationDynamicDefaults(cfg *kubeadmapi.NodeRegistrationOptions, controlPlaneTaint, skipCRIDetect bool) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	cfg.Name, err = nodeutil.GetHostname(cfg.Name)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -110,6 +110,11 @@ func SetNodeRegistrationDynamicDefaults(cfg *kubeadmapi.NodeRegistrationOptions,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if cfg.CRISocket == "" {
 | 
			
		||||
		if skipCRIDetect {
 | 
			
		||||
			klog.V(4).Infof("skip CRI socket detection, fill with placeholder %s", kubeadmconstants.UnknownCRISocket)
 | 
			
		||||
			cfg.CRISocket = kubeadmconstants.UnknownCRISocket // set a value to pass the ValidateSocketPath
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		cfg.CRISocket, err = kubeadmruntime.DetectCRISocket()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
@@ -224,7 +229,7 @@ func DefaultedStaticInitConfiguration() (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DefaultedInitConfiguration takes a versioned init config (often populated by flags), defaults it and converts it into internal InitConfiguration
 | 
			
		||||
func DefaultedInitConfiguration(versionedInitCfg *kubeadmapiv1.InitConfiguration, versionedClusterCfg *kubeadmapiv1.ClusterConfiguration) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
func DefaultedInitConfiguration(versionedInitCfg *kubeadmapiv1.InitConfiguration, versionedClusterCfg *kubeadmapiv1.ClusterConfiguration, skipCRIDetect bool) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
	internalcfg := &kubeadmapi.InitConfiguration{}
 | 
			
		||||
 | 
			
		||||
	// Takes passed flags into account; the defaulting is executed once again enforcing assignment of
 | 
			
		||||
@@ -240,7 +245,7 @@ func DefaultedInitConfiguration(versionedInitCfg *kubeadmapiv1.InitConfiguration
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Applies dynamic defaults to settings not provided with flags
 | 
			
		||||
	if err := SetInitDynamicDefaults(internalcfg); err != nil {
 | 
			
		||||
	if err := SetInitDynamicDefaults(internalcfg, skipCRIDetect); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	// Validates cfg (flags/configs + defaults + dynamic defaults)
 | 
			
		||||
@@ -251,7 +256,7 @@ func DefaultedInitConfiguration(versionedInitCfg *kubeadmapiv1.InitConfiguration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LoadInitConfigurationFromFile loads a supported versioned InitConfiguration from a file, converts it into internal config, defaults it and verifies it.
 | 
			
		||||
func LoadInitConfigurationFromFile(cfgPath string) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
func LoadInitConfigurationFromFile(cfgPath string, skipCRIDetect bool) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
	klog.V(1).Infof("loading configuration from %q", cfgPath)
 | 
			
		||||
 | 
			
		||||
	b, err := os.ReadFile(cfgPath)
 | 
			
		||||
@@ -259,7 +264,7 @@ func LoadInitConfigurationFromFile(cfgPath string) (*kubeadmapi.InitConfiguratio
 | 
			
		||||
		return nil, errors.Wrapf(err, "unable to read config from %q ", cfgPath)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return BytesToInitConfiguration(b)
 | 
			
		||||
	return BytesToInitConfiguration(b, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LoadOrDefaultInitConfiguration takes a path to a config file and a versioned configuration that can serve as the default config
 | 
			
		||||
@@ -267,31 +272,31 @@ func LoadInitConfigurationFromFile(cfgPath string) (*kubeadmapi.InitConfiguratio
 | 
			
		||||
// The external, versioned configuration is defaulted and converted to the internal type.
 | 
			
		||||
// Right thereafter, the configuration is defaulted again with dynamic values (like IP addresses of a machine, etc)
 | 
			
		||||
// Lastly, the internal config is validated and returned.
 | 
			
		||||
func LoadOrDefaultInitConfiguration(cfgPath string, versionedInitCfg *kubeadmapiv1.InitConfiguration, versionedClusterCfg *kubeadmapiv1.ClusterConfiguration) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
func LoadOrDefaultInitConfiguration(cfgPath string, versionedInitCfg *kubeadmapiv1.InitConfiguration, versionedClusterCfg *kubeadmapiv1.ClusterConfiguration, skipCRIDetect bool) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
	if cfgPath != "" {
 | 
			
		||||
		// Loads configuration from config file, if provided
 | 
			
		||||
		// Nb. --config overrides command line flags
 | 
			
		||||
		return LoadInitConfigurationFromFile(cfgPath)
 | 
			
		||||
		return LoadInitConfigurationFromFile(cfgPath, skipCRIDetect)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return DefaultedInitConfiguration(versionedInitCfg, versionedClusterCfg)
 | 
			
		||||
	return DefaultedInitConfiguration(versionedInitCfg, versionedClusterCfg, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BytesToInitConfiguration converts a byte slice to an internal, defaulted and validated InitConfiguration object.
 | 
			
		||||
// The map may contain many different YAML documents. These YAML documents are parsed one-by-one
 | 
			
		||||
// and well-known ComponentConfig GroupVersionKinds are stored inside of the internal InitConfiguration struct.
 | 
			
		||||
// The resulting InitConfiguration is then dynamically defaulted and validated prior to return.
 | 
			
		||||
func BytesToInitConfiguration(b []byte) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
func BytesToInitConfiguration(b []byte, skipCRIDetect bool) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
	gvkmap, err := kubeadmutil.SplitYAMLDocuments(b)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return documentMapToInitConfiguration(gvkmap, false, false, false)
 | 
			
		||||
	return documentMapToInitConfiguration(gvkmap, false, false, false, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// documentMapToInitConfiguration converts a map of GVKs and YAML documents to defaulted and validated configuration object.
 | 
			
		||||
func documentMapToInitConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental, strictErrors bool) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
func documentMapToInitConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental, strictErrors, skipCRIDetect bool) (*kubeadmapi.InitConfiguration, error) {
 | 
			
		||||
	var initcfg *kubeadmapi.InitConfiguration
 | 
			
		||||
	var clustercfg *kubeadmapi.ClusterConfiguration
 | 
			
		||||
 | 
			
		||||
@@ -370,7 +375,7 @@ func documentMapToInitConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Applies dynamic defaults to settings not provided with flags
 | 
			
		||||
	if err := SetInitDynamicDefaults(initcfg); err != nil {
 | 
			
		||||
	if err := SetInitDynamicDefaults(initcfg, skipCRIDetect); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,7 @@ kubernetesVersion: %s`, kubeadmapiv1.SchemeGroupVersion.String(), certDir, const
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			obj, err := LoadInitConfigurationFromFile(cfgPath)
 | 
			
		||||
			obj, err := LoadInitConfigurationFromFile(cfgPath, true)
 | 
			
		||||
			if rt.expectErr {
 | 
			
		||||
				if err == nil {
 | 
			
		||||
					t.Error("Unexpected success")
 | 
			
		||||
@@ -185,7 +185,7 @@ func TestDefaultTaintsMarshaling(t *testing.T) {
 | 
			
		||||
				t.Fatalf("unexpected error while marshalling to YAML: %v", err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			cfg, err := BytesToInitConfiguration(b)
 | 
			
		||||
			cfg, err := BytesToInitConfiguration(b, true)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				t.Fatalf("unexpected error of BytesToInitConfiguration: %v\nconfig: %s", err, string(b))
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -34,12 +34,12 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// SetJoinDynamicDefaults checks and sets configuration values for the JoinConfiguration object
 | 
			
		||||
func SetJoinDynamicDefaults(cfg *kubeadmapi.JoinConfiguration) error {
 | 
			
		||||
func SetJoinDynamicDefaults(cfg *kubeadmapi.JoinConfiguration, skipCRIDetect bool) error {
 | 
			
		||||
	addControlPlaneTaint := false
 | 
			
		||||
	if cfg.ControlPlane != nil {
 | 
			
		||||
		addControlPlaneTaint = true
 | 
			
		||||
	}
 | 
			
		||||
	if err := SetNodeRegistrationDynamicDefaults(&cfg.NodeRegistration, addControlPlaneTaint); err != nil {
 | 
			
		||||
	if err := SetNodeRegistrationDynamicDefaults(&cfg.NodeRegistration, addControlPlaneTaint, skipCRIDetect); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -61,18 +61,18 @@ func SetJoinControlPlaneDefaults(cfg *kubeadmapi.JoinControlPlane) error {
 | 
			
		||||
// Then the external, versioned configuration is defaulted and converted to the internal type.
 | 
			
		||||
// Right thereafter, the configuration is defaulted again with dynamic values (like IP addresses of a machine, etc)
 | 
			
		||||
// Lastly, the internal config is validated and returned.
 | 
			
		||||
func LoadOrDefaultJoinConfiguration(cfgPath string, defaultversionedcfg *kubeadmapiv1.JoinConfiguration) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
func LoadOrDefaultJoinConfiguration(cfgPath string, defaultversionedcfg *kubeadmapiv1.JoinConfiguration, skipCRIDetect bool) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
	if cfgPath != "" {
 | 
			
		||||
		// Loads configuration from config file, if provided
 | 
			
		||||
		// Nb. --config overrides command line flags, TODO: fix this
 | 
			
		||||
		return LoadJoinConfigurationFromFile(cfgPath)
 | 
			
		||||
		return LoadJoinConfigurationFromFile(cfgPath, skipCRIDetect)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return DefaultedJoinConfiguration(defaultversionedcfg)
 | 
			
		||||
	return DefaultedJoinConfiguration(defaultversionedcfg, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LoadJoinConfigurationFromFile loads versioned JoinConfiguration from file, converts it to internal, defaults and validates it
 | 
			
		||||
func LoadJoinConfigurationFromFile(cfgPath string) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
func LoadJoinConfigurationFromFile(cfgPath string, skipCRIDetect bool) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
	klog.V(1).Infof("loading configuration from %q", cfgPath)
 | 
			
		||||
 | 
			
		||||
	b, err := os.ReadFile(cfgPath)
 | 
			
		||||
@@ -85,12 +85,12 @@ func LoadJoinConfigurationFromFile(cfgPath string) (*kubeadmapi.JoinConfiguratio
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return documentMapToJoinConfiguration(gvkmap, false, false, false)
 | 
			
		||||
	return documentMapToJoinConfiguration(gvkmap, false, false, false, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// documentMapToJoinConfiguration takes a map between GVKs and YAML documents (as returned by SplitYAMLDocuments),
 | 
			
		||||
// finds a JoinConfiguration, decodes it, dynamically defaults it and then validates it prior to return.
 | 
			
		||||
func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental, strictErrors bool) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental, strictErrors, skipCRIDetect bool) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
	joinBytes := []byte{}
 | 
			
		||||
	for gvk, bytes := range gvkmap {
 | 
			
		||||
		// not interested in anything other than JoinConfiguration
 | 
			
		||||
@@ -125,7 +125,7 @@ func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Applies dynamic defaults to settings not provided with flags
 | 
			
		||||
	if err := SetJoinDynamicDefaults(internalcfg); err != nil {
 | 
			
		||||
	if err := SetJoinDynamicDefaults(internalcfg, skipCRIDetect); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	// Validates cfg (flags/configs + defaults)
 | 
			
		||||
@@ -137,7 +137,7 @@ func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DefaultedJoinConfiguration takes a versioned JoinConfiguration (usually filled in by command line parameters), defaults it, converts it to internal and validates it
 | 
			
		||||
func DefaultedJoinConfiguration(defaultversionedcfg *kubeadmapiv1.JoinConfiguration) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
func DefaultedJoinConfiguration(defaultversionedcfg *kubeadmapiv1.JoinConfiguration, skipCRIDetect bool) (*kubeadmapi.JoinConfiguration, error) {
 | 
			
		||||
	internalcfg := &kubeadmapi.JoinConfiguration{}
 | 
			
		||||
 | 
			
		||||
	// Takes passed flags into account; the defaulting is executed once again enforcing assignment of
 | 
			
		||||
@@ -148,7 +148,7 @@ func DefaultedJoinConfiguration(defaultversionedcfg *kubeadmapiv1.JoinConfigurat
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Applies dynamic defaults to settings not provided with flags
 | 
			
		||||
	if err := SetJoinDynamicDefaults(internalcfg); err != nil {
 | 
			
		||||
	if err := SetJoinDynamicDefaults(internalcfg, skipCRIDetect); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	// Validates cfg (flags/configs + defaults)
 | 
			
		||||
 
 | 
			
		||||
@@ -78,7 +78,7 @@ func TestLoadJoinConfigurationFromFile(t *testing.T) {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			obj, err := LoadJoinConfigurationFromFile(cfgPath)
 | 
			
		||||
			obj, err := LoadJoinConfigurationFromFile(cfgPath, true)
 | 
			
		||||
			if rt.expectErr {
 | 
			
		||||
				if err == nil {
 | 
			
		||||
					t.Error("Unexpected success")
 | 
			
		||||
 
 | 
			
		||||
@@ -36,9 +36,14 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// SetResetDynamicDefaults checks and sets configuration values for the ResetConfiguration object
 | 
			
		||||
func SetResetDynamicDefaults(cfg *kubeadmapi.ResetConfiguration) error {
 | 
			
		||||
func SetResetDynamicDefaults(cfg *kubeadmapi.ResetConfiguration, skipCRIDetect bool) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	if cfg.CRISocket == "" {
 | 
			
		||||
		if skipCRIDetect {
 | 
			
		||||
			klog.V(4).Infof("skip CRI socket detection, fill with placeholder %s", constants.UnknownCRISocket)
 | 
			
		||||
			cfg.CRISocket = constants.UnknownCRISocket // set a value to pass the ValidateSocketPath
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
		cfg.CRISocket, err = kubeadmruntime.DetectCRISocket()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
@@ -60,17 +65,17 @@ func SetResetDynamicDefaults(cfg *kubeadmapi.ResetConfiguration) error {
 | 
			
		||||
// Then the external, versioned configuration is defaulted and converted to the internal type.
 | 
			
		||||
// Right thereafter, the configuration is defaulted again with dynamic values
 | 
			
		||||
// Lastly, the internal config is validated and returned.
 | 
			
		||||
func LoadOrDefaultResetConfiguration(cfgPath string, defaultversionedcfg *kubeadmapiv1.ResetConfiguration, allowExperimental bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
func LoadOrDefaultResetConfiguration(cfgPath string, defaultversionedcfg *kubeadmapiv1.ResetConfiguration, allowExperimental, skipCRIDetect bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
	if cfgPath != "" {
 | 
			
		||||
		// Loads configuration from config file, if provided
 | 
			
		||||
		return LoadResetConfigurationFromFile(cfgPath, allowExperimental)
 | 
			
		||||
		return LoadResetConfigurationFromFile(cfgPath, allowExperimental, skipCRIDetect)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return DefaultedResetConfiguration(defaultversionedcfg)
 | 
			
		||||
	return DefaultedResetConfiguration(defaultversionedcfg, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LoadResetConfigurationFromFile loads versioned ResetConfiguration from file, converts it to internal, defaults and validates it
 | 
			
		||||
func LoadResetConfigurationFromFile(cfgPath string, allowExperimental bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
func LoadResetConfigurationFromFile(cfgPath string, allowExperimental, skipCRIDetect bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
	klog.V(1).Infof("loading configuration from %q", cfgPath)
 | 
			
		||||
 | 
			
		||||
	b, err := os.ReadFile(cfgPath)
 | 
			
		||||
@@ -83,12 +88,12 @@ func LoadResetConfigurationFromFile(cfgPath string, allowExperimental bool) (*ku
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return documentMapToResetConfiguration(gvkmap, false, allowExperimental, false)
 | 
			
		||||
	return documentMapToResetConfiguration(gvkmap, false, allowExperimental, false, skipCRIDetect)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// documentMapToResetConfiguration takes a map between GVKs and YAML documents (as returned by SplitYAMLDocuments),
 | 
			
		||||
// finds a ResetConfiguration, decodes it, dynamically defaults it and then validates it prior to return.
 | 
			
		||||
func documentMapToResetConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental bool, strictErrors bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
func documentMapToResetConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecated, allowExperimental bool, strictErrors bool, skipCRIDetect bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
	resetBytes := []byte{}
 | 
			
		||||
	for gvk, bytes := range gvkmap {
 | 
			
		||||
		// not interested in anything other than ResetConfiguration
 | 
			
		||||
@@ -123,7 +128,7 @@ func documentMapToResetConfiguration(gvkmap kubeadmapi.DocumentMap, allowDepreca
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Applies dynamic defaults to settings not provided with flags
 | 
			
		||||
	if err := SetResetDynamicDefaults(internalcfg); err != nil {
 | 
			
		||||
	if err := SetResetDynamicDefaults(internalcfg, skipCRIDetect); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	// Validates cfg
 | 
			
		||||
@@ -135,7 +140,7 @@ func documentMapToResetConfiguration(gvkmap kubeadmapi.DocumentMap, allowDepreca
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DefaultedResetConfiguration takes a versioned ResetConfiguration (usually filled in by command line parameters), defaults it, converts it to internal and validates it
 | 
			
		||||
func DefaultedResetConfiguration(defaultversionedcfg *kubeadmapiv1.ResetConfiguration) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
func DefaultedResetConfiguration(defaultversionedcfg *kubeadmapiv1.ResetConfiguration, skipCRIDetect bool) (*kubeadmapi.ResetConfiguration, error) {
 | 
			
		||||
	internalcfg := &kubeadmapi.ResetConfiguration{}
 | 
			
		||||
 | 
			
		||||
	// Takes passed flags into account; the defaulting is executed once again enforcing assignment of
 | 
			
		||||
@@ -146,7 +151,7 @@ func DefaultedResetConfiguration(defaultversionedcfg *kubeadmapiv1.ResetConfigur
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Applies dynamic defaults to settings not provided with flags
 | 
			
		||||
	if err := SetResetDynamicDefaults(internalcfg); err != nil {
 | 
			
		||||
	if err := SetResetDynamicDefaults(internalcfg, skipCRIDetect); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	// Validates cfg
 | 
			
		||||
 
 | 
			
		||||
@@ -75,7 +75,7 @@ func TestLoadResetConfigurationFromFile(t *testing.T) {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			obj, err := LoadResetConfigurationFromFile(cfgPath, true)
 | 
			
		||||
			obj, err := LoadResetConfigurationFromFile(cfgPath, true, true)
 | 
			
		||||
			if rt.expectErr {
 | 
			
		||||
				if err == nil {
 | 
			
		||||
					t.Error("Unexpected success")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user