@@ -221,9 +221,10 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er
221
221
if ! bytes .Equal (caCurrent , caExpected ) {
222
222
return errors .Errorf ("a kubeconfig file %q exists already but has got the wrong CA cert" , kubeConfigFilePath )
223
223
}
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
225
225
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 )
227
228
}
228
229
229
230
return nil
@@ -331,33 +332,49 @@ func writeKubeConfigFromSpec(out io.Writer, spec *kubeConfigSpec, clustername st
331
332
332
333
// ValidateKubeconfigsForExternalCA check if the kubeconfig file exist and has the expected CA and server URL using kubeadmapi.InitConfiguration.
333
334
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
-
341
335
// Creates a kubeconfig file with the target CA and server URL
342
336
// to be used as a input for validating user provided kubeconfig files
343
337
caCert , err := pkiutil .TryLoadCertFromDisk (cfg .CertificatesDir , kubeadmconstants .CACertAndKeyBaseName )
344
338
if err != nil {
345
339
return errors .Wrapf (err , "the CA file couldn't be loaded" )
346
340
}
347
341
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
348
361
controlPlaneEndpoint , err := kubeadmutil .GetControlPlaneEndpoint (cfg .ControlPlaneEndpoint , & cfg .LocalAPIEndpoint )
349
362
if err != nil {
350
363
return err
351
364
}
352
365
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
+ }
354
371
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 {
358
374
return errors .Wrapf (err , "the %s file does not exists or it is not valid" , kubeConfigFileName )
359
375
}
360
376
}
377
+
361
378
return nil
362
379
}
363
380
0 commit comments