Skip to content

Commit 51197e4

Browse files
committed
kubeadm: Refactor InitConfiguration init APIs
Currently ConfigFileAndDefaultsToInternalConfig and FetchConfigFromFileOrCluster are used to default and load InitConfiguration from file or cluster. These two APIs do a couple of completely separate things depending on how they were invoked. In the case of ConfigFileAndDefaultsToInternalConfig, an InitConfiguration could be either defaulted with external override parameters, or loaded from file. With FetchConfigFromFileOrCluster an InitConfiguration is either loaded from file or from the config map in the cluster. The two share both some functionality, but not enough code. They are also quite difficult to use and sometimes even error prone. To solve the issues, the following steps were taken: - Introduce DefaultedInitConfiguration which returns defaulted version agnostic InitConfiguration. The function takes InitConfiguration for overriding the defaults. - Introduce LoadInitConfigurationFromFile, which loads, converts, validates and defaults an InitConfiguration from file. - Introduce FetchInitConfigurationFromCluster that fetches InitConfiguration from the config map. - Reduce, when possible, the usage of ConfigFileAndDefaultsToInternalConfig by replacing it with DefaultedInitConfiguration or LoadInitConfigurationFromFile invocations. - Replace all usages of FetchConfigFromFileOrCluster with calls to LoadInitConfigurationFromFile or FetchInitConfigurationFromCluster. - Delete FetchConfigFromFileOrCluster as it's no longer used. - Rename ConfigFileAndDefaultsToInternalConfig to LoadOrDefaultInitConfiguration in order to better describe what the function is actually doing. Signed-off-by: Rostislav M. Georgiev <[email protected]>
1 parent 196047f commit 51197e4

24 files changed

+178
-193
lines changed

cmd/kubeadm/app/cmd/alpha/certs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func addFlags(cmd *cobra.Command, cfg *renewConfig) {
135135

136136
func generateRenewalFunction(cert *certsphase.KubeadmCert, caCert *certsphase.KubeadmCert, cfg *renewConfig) func() {
137137
return func() {
138-
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfg.cfgPath, &cfg.cfg)
138+
internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfg.cfgPath, &cfg.cfg)
139139
kubeadmutil.CheckErr(err)
140140

141141
if cfg.useCSR {

cmd/kubeadm/app/cmd/alpha/kubeconfig.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func newCmdUserKubeConfig(out io.Writer) *cobra.Command {
7979
kubeadmutil.CheckErr(errors.New("missing required argument --client-name"))
8080
}
8181

82-
// 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
83-
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig("", cfg)
82+
// This call returns the ready-to-use configuration based on the default cfg populated by flags
83+
internalcfg, err := configutil.DefaultedInitConfiguration(cfg)
8484
kubeadmutil.CheckErr(err)
8585

8686
// if the kubeconfig file for an additional user has to use a token, use it

cmd/kubeadm/app/cmd/alpha/selfhosting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ func getSelfhostingSubCommand(in io.Reader) *cobra.Command {
124124
kubeadmutil.CheckErr(err)
125125

126126
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
127-
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
127+
// of the version from the internet when executing LoadOrDefaultInitConfiguration
128128
phases.SetKubernetesVersion(&cfg.ClusterConfiguration)
129129

130130
// 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
131-
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
131+
internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, cfg)
132132
kubeadmutil.CheckErr(err)
133133

134134
// Converts the Static Pod-hosted control plane into a self-hosted one

cmd/kubeadm/app/cmd/config.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func getSupportedComponentConfigAPIObjects() []string {
178178
}
179179

180180
func getDefaultedInitConfig() (*kubeadmapi.InitConfiguration, error) {
181-
return configutil.ConfigFileAndDefaultsToInternalConfig("", &kubeadmapiv1beta1.InitConfiguration{
181+
return configutil.DefaultedInitConfiguration(&kubeadmapiv1beta1.InitConfiguration{
182182
// TODO: Probably move to getDefaultedClusterConfig?
183183
LocalAPIEndpoint: kubeadmapiv1beta1.APIEndpoint{AdvertiseAddress: "1.2.3.4"},
184184
ClusterConfiguration: kubeadmapiv1beta1.ClusterConfiguration{
@@ -320,12 +320,13 @@ func NewCmdConfigUploadFromFile(out io.Writer, kubeConfigFile *string) *cobra.Co
320320
client, err := kubeconfigutil.ClientSetFromFile(*kubeConfigFile)
321321
kubeadmutil.CheckErr(err)
322322

323-
// The default configuration is empty; everything should come from the file on disk
324-
klog.V(1).Infoln("[config] creating empty default configuration")
325-
defaultcfg := &kubeadmapiv1beta1.InitConfiguration{}
326-
// Upload the configuration using the file; don't care about the defaultcfg really
323+
// Default both statically and dynamically, convert to internal API type, and validate everything
324+
internalcfg, err := configutil.LoadInitConfigurationFromFile(cfgPath)
325+
kubeadmutil.CheckErr(err)
326+
327+
// Upload the configuration using the file
327328
klog.V(1).Infof("[config] uploading configuration")
328-
err = uploadConfiguration(client, cfgPath, defaultcfg)
329+
err = uploadconfig.UploadConfiguration(internalcfg, client)
329330
kubeadmutil.CheckErr(err)
330331
},
331332
}
@@ -360,10 +361,18 @@ func NewCmdConfigUploadFromFlags(out io.Writer, kubeConfigFile *string) *cobra.C
360361
client, err := kubeconfigutil.ClientSetFromFile(*kubeConfigFile)
361362
kubeadmutil.CheckErr(err)
362363

364+
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
365+
// of the version from the internet when executing DefaultedInitConfiguration
366+
phaseutil.SetKubernetesVersion(&cfg.ClusterConfiguration)
367+
363368
// Default both statically and dynamically, convert to internal API type, and validate everything
364-
// The cfgPath argument is unset here as we shouldn't load a config file from disk, just go with cfg
369+
klog.V(1).Infoln("[config] converting to internal API type")
370+
internalcfg, err := configutil.DefaultedInitConfiguration(cfg)
371+
kubeadmutil.CheckErr(err)
372+
373+
// Finally, upload the configuration
365374
klog.V(1).Infof("[config] uploading configuration")
366-
err = uploadConfiguration(client, "", cfg)
375+
err = uploadconfig.UploadConfiguration(internalcfg, client)
367376
kubeadmutil.CheckErr(err)
368377
},
369378
}
@@ -384,24 +393,6 @@ func RunConfigView(out io.Writer, client clientset.Interface) error {
384393
return nil
385394
}
386395

387-
// uploadConfiguration handles the uploading of the configuration internally
388-
func uploadConfiguration(client clientset.Interface, cfgPath string, defaultcfg *kubeadmapiv1beta1.InitConfiguration) error {
389-
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
390-
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
391-
phaseutil.SetKubernetesVersion(&defaultcfg.ClusterConfiguration)
392-
393-
// Default both statically and dynamically, convert to internal API type, and validate everything
394-
// First argument is unset here as we shouldn't load a config file from disk
395-
klog.V(1).Infoln("[config] converting to internal API type")
396-
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, defaultcfg)
397-
if err != nil {
398-
return err
399-
}
400-
401-
// Then just call the uploadconfig phase to do the rest of the work
402-
return uploadconfig.UploadConfiguration(internalcfg, client)
403-
}
404-
405396
// NewCmdConfigImages returns the "kubeadm config images" command
406397
func NewCmdConfigImages(out io.Writer) *cobra.Command {
407398
cmd := &cobra.Command{
@@ -427,7 +418,7 @@ func NewCmdConfigImagesPull() *cobra.Command {
427418
Run: func(_ *cobra.Command, _ []string) {
428419
externalcfg.ClusterConfiguration.FeatureGates, err = features.NewFeatureGate(&features.InitFeatureGates, featureGatesString)
429420
kubeadmutil.CheckErr(err)
430-
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, externalcfg)
421+
internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, externalcfg)
431422
kubeadmutil.CheckErr(err)
432423
containerRuntime, err := utilruntime.NewContainerRuntime(utilsexec.New(), internalcfg.GetCRISocket())
433424
kubeadmutil.CheckErr(err)
@@ -496,7 +487,7 @@ func NewCmdConfigImagesList(out io.Writer, mockK8sVersion *string) *cobra.Comman
496487

497488
// NewImagesList returns the underlying struct for the "kubeadm config images list" command
498489
func NewImagesList(cfgPath string, cfg *kubeadmapiv1beta1.InitConfiguration) (*ImagesList, error) {
499-
initcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
490+
initcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, cfg)
500491
if err != nil {
501492
return nil, errors.Wrap(err, "could not convert cfg to an internal cfg")
502493
}

cmd/kubeadm/app/cmd/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func TestMigrate(t *testing.T) {
275275
t.Fatalf("failed to set new-config flag")
276276
}
277277
command.Run(nil, nil)
278-
if _, err := configutil.ConfigFileAndDefaultsToInternalConfig(newConfigPath, &kubeadmapiv1beta1.InitConfiguration{}); err != nil {
278+
if _, err := configutil.LoadInitConfigurationFromFile(newConfigPath); err != nil {
279279
t.Fatalf("Could not read output back into internal type: %v", err)
280280
}
281281
}

cmd/kubeadm/app/cmd/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io
302302

303303
// Either use the config file if specified, or convert public kubeadm API to the internal InitConfiguration
304304
// and validates InitConfiguration
305-
cfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(options.cfgPath, options.externalcfg)
305+
cfg, err := configutil.LoadOrDefaultInitConfiguration(options.cfgPath, options.externalcfg)
306306
if err != nil {
307307
return nil, err
308308
}

cmd/kubeadm/app/cmd/join.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func fetchInitConfiguration(tlsBootstrapCfg *clientcmdapi.Config) (*kubeadmapi.I
530530
}
531531

532532
// Fetches the init configuration
533-
initConfiguration, err := configutil.FetchConfigFromFileOrCluster(tlsClient, os.Stdout, "join", "", true)
533+
initConfiguration, err := configutil.FetchInitConfigurationFromCluster(tlsClient, os.Stdout, "join", true)
534534
if err != nil {
535535
return nil, errors.Wrap(err, "unable to fetch the kubeadm-config ConfigMap")
536536
}

cmd/kubeadm/app/cmd/reset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func getEtcdDataDir(manifestPath string, client clientset.Interface) (string, er
208208
var dataDir string
209209

210210
if client != nil {
211-
cfg, err := configutil.FetchConfigFromFileOrCluster(client, os.Stdout, "reset", "", false)
211+
cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false)
212212
if err == nil && cfg.Etcd.Local != nil {
213213
return cfg.Etcd.Local.DataDir, nil
214214
}
@@ -299,7 +299,7 @@ func resetConfigDir(configPathDir, pkiPathDir string) {
299299

300300
func resetDetectCRISocket(client clientset.Interface) (string, error) {
301301
// first try to connect to the cluster for the CRI socket
302-
cfg, err := configutil.FetchConfigFromFileOrCluster(client, os.Stdout, "reset", "", false)
302+
cfg, err := configutil.FetchInitConfigurationFromCluster(client, os.Stdout, "reset", false)
303303
if err == nil {
304304
return cfg.NodeRegistration.CRISocket, nil
305305
}

cmd/kubeadm/app/cmd/token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,12 @@ func NewCmdTokenGenerate(out io.Writer) *cobra.Command {
211211
// RunCreateToken generates a new bootstrap token and stores it as a secret on the server.
212212
func RunCreateToken(out io.Writer, client clientset.Interface, cfgPath string, cfg *kubeadmapiv1beta1.InitConfiguration, printJoinCommand bool, kubeConfigFile string) error {
213213
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
214-
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
214+
// of the version from the internet when executing LoadOrDefaultInitConfiguration
215215
phaseutil.SetKubernetesVersion(&cfg.ClusterConfiguration)
216216

217217
// 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
218218
klog.V(1).Infoln("[token] loading configurations")
219-
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
219+
internalcfg, err := configutil.LoadOrDefaultInitConfiguration(cfgPath, cfg)
220220
if err != nil {
221221
return err
222222
}

cmd/kubeadm/app/cmd/token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestRunCreateToken(t *testing.T) {
176176
cfg := &kubeadmapiv1beta1.InitConfiguration{
177177
ClusterConfiguration: kubeadmapiv1beta1.ClusterConfiguration{
178178
// KubernetesVersion is not used, but we set this explicitly to avoid
179-
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
179+
// the lookup of the version from the internet when executing LoadOrDefaultInitConfiguration
180180
KubernetesVersion: constants.MinimumControlPlaneVersion.String(),
181181
},
182182
BootstrapTokens: []kubeadmapiv1beta1.BootstrapToken{

0 commit comments

Comments
 (0)