@@ -45,6 +45,8 @@ import (
45
45
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
46
46
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
47
47
"k8s.io/kubernetes/cmd/kubeadm/app/discovery"
48
+ "k8s.io/kubernetes/cmd/kubeadm/app/discovery/token"
49
+ "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
48
50
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
49
51
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
50
52
)
@@ -464,7 +466,7 @@ func newJoinData(cmd *cobra.Command, args []string, opt *joinOptions, out io.Wri
464
466
// if dry running, creates a temporary folder to save kubeadm generated files
465
467
dryRunDir := ""
466
468
if opt .dryRun || cfg .DryRun {
467
- if dryRunDir , err = kubeadmconstants .CreateTempDirForKubeadm ( "" , "kubeadm-join-dryrun" ); err != nil {
469
+ if dryRunDir , err = kubeadmconstants .GetDryRunDir ( kubeadmconstants . EnvVarJoinDryRunDir , "kubeadm-join-dryrun" , klog . Warningf ); err != nil {
468
470
return nil , errors .Wrap (err , "couldn't create a temporary directory on dryrun" )
469
471
}
470
472
}
@@ -535,8 +537,19 @@ func (j *joinData) TLSBootstrapCfg() (*clientcmdapi.Config, error) {
535
537
if j .tlsBootstrapCfg != nil {
536
538
return j .tlsBootstrapCfg , nil
537
539
}
540
+
541
+ var (
542
+ client clientset.Interface
543
+ err error
544
+ )
545
+ if j .dryRun {
546
+ client , err = j .Client ()
547
+ if err != nil {
548
+ return nil , errors .Wrap (err , "could not create a client for TLS bootstrap" )
549
+ }
550
+ }
538
551
klog .V (1 ).Infoln ("[preflight] Discovering cluster-info" )
539
- tlsBootstrapCfg , err := discovery .For (j .cfg )
552
+ tlsBootstrapCfg , err := discovery .For (client , j .cfg )
540
553
j .tlsBootstrapCfg = tlsBootstrapCfg
541
554
return tlsBootstrapCfg , err
542
555
}
@@ -550,19 +563,58 @@ func (j *joinData) InitCfg() (*kubeadmapi.InitConfiguration, error) {
550
563
return nil , err
551
564
}
552
565
klog .V (1 ).Infoln ("[preflight] Fetching init configuration" )
553
- initCfg , err := fetchInitConfigurationFromJoinConfiguration (j .cfg , j .tlsBootstrapCfg )
566
+ var client clientset.Interface
567
+ if j .dryRun {
568
+ var err error
569
+ client , err = j .Client ()
570
+ if err != nil {
571
+ return nil , errors .Wrap (err , "could not get dry-run client for fetching InitConfiguration" )
572
+ }
573
+ }
574
+ initCfg , err := fetchInitConfigurationFromJoinConfiguration (j .cfg , client , j .tlsBootstrapCfg )
554
575
j .initCfg = initCfg
555
576
return initCfg , err
556
577
}
557
578
558
579
// Client returns the Client for accessing the cluster with the identity defined in admin.conf.
559
580
func (j * joinData ) Client () (clientset.Interface , error ) {
560
- if j .client != nil {
581
+ pathAdmin := filepath .Join (j .KubeConfigDir (), kubeadmconstants .AdminKubeConfigFileName )
582
+
583
+ if j .dryRun {
584
+ dryRun := apiclient .NewDryRun ()
585
+ // For the dynamic dry-run client use this kubeconfig only if it exists.
586
+ // That would happen presumably after TLS bootstrap.
587
+ if _ , err := os .Stat (pathAdmin ); err == nil {
588
+ if err := dryRun .WithKubeConfigFile (pathAdmin ); err != nil {
589
+ return nil , err
590
+ }
591
+ } else if j .tlsBootstrapCfg != nil {
592
+ if err := dryRun .WithKubeConfig (j .tlsBootstrapCfg ); err != nil {
593
+ return nil , err
594
+ }
595
+ } else if j .cfg .Discovery .BootstrapToken != nil {
596
+ insecureConfig := token .BuildInsecureBootstrapKubeConfig (j .cfg .Discovery .BootstrapToken .APIServerEndpoint )
597
+ resetConfig , err := clientcmd .NewDefaultClientConfig (* insecureConfig , & clientcmd.ConfigOverrides {}).ClientConfig ()
598
+ if err != nil {
599
+ return nil , errors .Wrap (err , "failed to create API client configuration from kubeconfig" )
600
+ }
601
+ if err := dryRun .WithRestConfig (resetConfig ); err != nil {
602
+ return nil , err
603
+ }
604
+ }
605
+
606
+ dryRun .WithDefaultMarshalFunction ().
607
+ WithWriter (os .Stdout ).
608
+ AppendReactor (dryRun .GetClusterInfoReactor ()).
609
+ AppendReactor (dryRun .GetKubeadmConfigReactor ()).
610
+ AppendReactor (dryRun .GetKubeProxyConfigReactor ()).
611
+ AppendReactor (dryRun .GetKubeletConfigReactor ())
612
+
613
+ j .client = dryRun .FakeClient ()
561
614
return j .client , nil
562
615
}
563
- path := filepath .Join (j .KubeConfigDir (), kubeadmconstants .AdminKubeConfigFileName )
564
616
565
- client , err := kubeconfigutil .ClientSetFromFile (path )
617
+ client , err := kubeconfigutil .ClientSetFromFile (pathAdmin )
566
618
if err != nil {
567
619
return nil , errors .Wrap (err , "[preflight] couldn't create Kubernetes client" )
568
620
}
@@ -593,10 +645,18 @@ func (j *joinData) PatchesDir() string {
593
645
}
594
646
595
647
// fetchInitConfigurationFromJoinConfiguration retrieves the init configuration from a join configuration, performing the discovery
596
- func fetchInitConfigurationFromJoinConfiguration (cfg * kubeadmapi.JoinConfiguration , tlsBootstrapCfg * clientcmdapi.Config ) (* kubeadmapi.InitConfiguration , error ) {
597
- // Retrieves the kubeadm configuration
648
+ func fetchInitConfigurationFromJoinConfiguration (cfg * kubeadmapi.JoinConfiguration , client clientset.Interface , tlsBootstrapCfg * clientcmdapi.Config ) (* kubeadmapi.InitConfiguration , error ) {
649
+ var err error
650
+
598
651
klog .V (1 ).Infoln ("[preflight] Retrieving KubeConfig objects" )
599
- initConfiguration , err := fetchInitConfiguration (tlsBootstrapCfg )
652
+ if client == nil {
653
+ // creates a client to access the cluster using the bootstrap token identity
654
+ client , err = kubeconfigutil .ToClientSet (tlsBootstrapCfg )
655
+ if err != nil {
656
+ return nil , errors .Wrap (err , "unable to access the cluster" )
657
+ }
658
+ }
659
+ initConfiguration , err := fetchInitConfiguration (client )
600
660
if err != nil {
601
661
return nil , err
602
662
}
@@ -618,15 +678,8 @@ func fetchInitConfigurationFromJoinConfiguration(cfg *kubeadmapi.JoinConfigurati
618
678
}
619
679
620
680
// fetchInitConfiguration reads the cluster configuration from the kubeadm-admin configMap
621
- func fetchInitConfiguration (tlsBootstrapCfg * clientcmdapi.Config ) (* kubeadmapi.InitConfiguration , error ) {
622
- // creates a client to access the cluster using the bootstrap token identity
623
- tlsClient , err := kubeconfigutil .ToClientSet (tlsBootstrapCfg )
624
- if err != nil {
625
- return nil , errors .Wrap (err , "unable to access the cluster" )
626
- }
627
-
628
- // Fetches the init configuration
629
- initConfiguration , err := configutil .FetchInitConfigurationFromCluster (tlsClient , nil , "preflight" , true , false )
681
+ func fetchInitConfiguration (client clientset.Interface ) (* kubeadmapi.InitConfiguration , error ) {
682
+ initConfiguration , err := configutil .FetchInitConfigurationFromCluster (client , nil , "preflight" , true , false )
630
683
if err != nil {
631
684
return nil , errors .Wrap (err , "unable to fetch the kubeadm-config ConfigMap" )
632
685
}
0 commit comments