Skip to content

Commit f682c4f

Browse files
authored
Merge pull request kubernetes#94816 from neolit123/1.20-relax-kubeconfig-server-validation
kubeadm: relax the validation of kubeconfig server URLs
2 parents 0c3286e + edaef35 commit f682c4f

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,10 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er
221221
if !bytes.Equal(caCurrent, caExpected) {
222222
return errors.Errorf("a kubeconfig file %q exists already but has got the wrong CA cert", kubeConfigFilePath)
223223
}
224-
// If the current API Server location on disk doesn't match the expected API server, error out because we have a file, but it's stale
224+
// If the current API Server location on disk doesn't match the expected API server, show a warning
225225
if currentConfig.Clusters[currentCluster].Server != config.Clusters[expectedCluster].Server {
226-
return errors.Errorf("a kubeconfig file %q exists already but has got the wrong API Server URL", kubeConfigFilePath)
226+
klog.Warningf("a kubeconfig file %q exists already but has an unexpected API Server URL: expected: %s, got: %s",
227+
kubeConfigFilePath, config.Clusters[expectedCluster].Server, currentConfig.Clusters[currentCluster].Server)
227228
}
228229

229230
return nil
@@ -331,33 +332,49 @@ func writeKubeConfigFromSpec(out io.Writer, spec *kubeConfigSpec, clustername st
331332

332333
// ValidateKubeconfigsForExternalCA check if the kubeconfig file exist and has the expected CA and server URL using kubeadmapi.InitConfiguration.
333334
func ValidateKubeconfigsForExternalCA(outDir string, cfg *kubeadmapi.InitConfiguration) error {
334-
kubeConfigFileNames := []string{
335-
kubeadmconstants.AdminKubeConfigFileName,
336-
kubeadmconstants.KubeletKubeConfigFileName,
337-
kubeadmconstants.ControllerManagerKubeConfigFileName,
338-
kubeadmconstants.SchedulerKubeConfigFileName,
339-
}
340-
341335
// Creates a kubeconfig file with the target CA and server URL
342336
// to be used as a input for validating user provided kubeconfig files
343337
caCert, err := pkiutil.TryLoadCertFromDisk(cfg.CertificatesDir, kubeadmconstants.CACertAndKeyBaseName)
344338
if err != nil {
345339
return errors.Wrapf(err, "the CA file couldn't be loaded")
346340
}
347341

342+
// validate user provided kubeconfig files for the scheduler and controller-manager
343+
localAPIEndpoint, err := kubeadmutil.GetLocalAPIEndpoint(&cfg.LocalAPIEndpoint)
344+
if err != nil {
345+
return err
346+
}
347+
348+
validationConfigLocal := kubeconfigutil.CreateBasic(localAPIEndpoint, "dummy", "dummy", pkiutil.EncodeCertPEM(caCert))
349+
kubeConfigFileNamesLocal := []string{
350+
kubeadmconstants.ControllerManagerKubeConfigFileName,
351+
kubeadmconstants.SchedulerKubeConfigFileName,
352+
}
353+
354+
for _, kubeConfigFileName := range kubeConfigFileNamesLocal {
355+
if err = validateKubeConfig(outDir, kubeConfigFileName, validationConfigLocal); err != nil {
356+
return errors.Wrapf(err, "the %s file does not exists or it is not valid", kubeConfigFileName)
357+
}
358+
}
359+
360+
// validate user provided kubeconfig files for the kubelet and admin
348361
controlPlaneEndpoint, err := kubeadmutil.GetControlPlaneEndpoint(cfg.ControlPlaneEndpoint, &cfg.LocalAPIEndpoint)
349362
if err != nil {
350363
return err
351364
}
352365

353-
validationConfig := kubeconfigutil.CreateBasic(controlPlaneEndpoint, "dummy", "dummy", pkiutil.EncodeCertPEM(caCert))
366+
validationConfigCPE := kubeconfigutil.CreateBasic(controlPlaneEndpoint, "dummy", "dummy", pkiutil.EncodeCertPEM(caCert))
367+
kubeConfigFileNamesCPE := []string{
368+
kubeadmconstants.AdminKubeConfigFileName,
369+
kubeadmconstants.KubeletKubeConfigFileName,
370+
}
354371

355-
// validate user provided kubeconfig files
356-
for _, kubeConfigFileName := range kubeConfigFileNames {
357-
if err = validateKubeConfig(outDir, kubeConfigFileName, validationConfig); err != nil {
372+
for _, kubeConfigFileName := range kubeConfigFileNamesCPE {
373+
if err = validateKubeConfig(outDir, kubeConfigFileName, validationConfigCPE); err != nil {
358374
return errors.Wrapf(err, "the %s file does not exists or it is not valid", kubeConfigFileName)
359375
}
360376
}
377+
361378
return nil
362379
}
363380

cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,10 @@ func TestCreateKubeConfigFileIfNotExists(t *testing.T) {
261261
kubeConfig: configWithAnotherClusterCa,
262262
expectedError: true,
263263
},
264-
{ // if KubeConfig is not equal to the existingKubeConfig - refers to the another cluster (a cluster with another address) -, raise error
264+
{ // if KubeConfig is not equal to the existingKubeConfig - tollerate custom server addresses
265265
name: "KubeConfig referst to the cluster with another address",
266266
existingKubeConfig: config,
267267
kubeConfig: configWithAnotherClusterAddress,
268-
expectedError: true,
269268
},
270269
}
271270

@@ -505,10 +504,9 @@ func TestValidateKubeConfig(t *testing.T) {
505504
kubeConfig: config,
506505
expectedError: true,
507506
},
508-
"kubeconfig exist and has invalid server url": {
507+
"kubeconfig exist and has a different server url": {
509508
existingKubeConfig: configWithAnotherServerURL,
510509
kubeConfig: config,
511-
expectedError: true,
512510
},
513511
"kubeconfig exist and is valid": {
514512
existingKubeConfig: config,
@@ -608,15 +606,14 @@ func TestValidateKubeconfigsForExternalCA(t *testing.T) {
608606
initConfig: initConfig,
609607
expectedError: true,
610608
},
611-
"some files have invalid Server Url": {
609+
"some files have a different Server URL": {
612610
filesToWrite: map[string]*clientcmdapi.Config{
613611
kubeadmconstants.AdminKubeConfigFileName: config,
614612
kubeadmconstants.KubeletKubeConfigFileName: config,
615613
kubeadmconstants.ControllerManagerKubeConfigFileName: config,
616614
kubeadmconstants.SchedulerKubeConfigFileName: configWithAnotherServerURL,
617615
},
618-
initConfig: initConfig,
619-
expectedError: true,
616+
initConfig: initConfig,
620617
},
621618
"all files are valid": {
622619
filesToWrite: map[string]*clientcmdapi.Config{

0 commit comments

Comments
 (0)