@@ -19,11 +19,9 @@ package cmd
1919import (
2020 "fmt"
2121 "io"
22- "net"
2322 "os"
2423 "path/filepath"
2524 "slices"
26- "strconv"
2725
2826 "github.com/spf13/cobra"
2927 flag "github.com/spf13/pflag"
@@ -93,6 +91,7 @@ type initData struct {
9391 skipTokenPrint bool
9492 dryRun bool
9593 kubeconfig * clientcmdapi.Config
94+ kubeconfigOriginal * clientcmdapi.Config
9695 kubeconfigDir string
9796 kubeconfigPath string
9897 ignorePreflightErrors sets.Set [string ]
@@ -463,6 +462,9 @@ func (d *initData) CertificateDir() string {
463462}
464463
465464// KubeConfig returns a kubeconfig after loading it from KubeConfigPath().
465+ // If the default kubeconfig path is used (admin.conf), instead of constructing
466+ // a kubeconfig that points to the control plane endpoint, make it point to the localAPIEndpoint.
467+ // This would allow 'kubeadm init' to only talk to the local kube-apiserver instance.
466468func (d * initData ) KubeConfig () (* clientcmdapi.Config , error ) {
467469 if d .kubeconfig != nil {
468470 return d .kubeconfig , nil
@@ -473,10 +475,26 @@ func (d *initData) KubeConfig() (*clientcmdapi.Config, error) {
473475 if err != nil {
474476 return nil , err
475477 }
478+ d .kubeconfigOriginal = d .kubeconfig .DeepCopy ()
479+
480+ if d .kubeconfigPath == kubeadmconstants .GetAdminKubeConfigPath () {
481+ kubeconfigutil .PointKubeConfigToLocalAPIEndpoint (d .kubeconfig , & d .Cfg ().LocalAPIEndpoint )
482+ }
476483
477484 return d .kubeconfig , nil
478485}
479486
487+ // KubeConfigOriginal returns the original kubeconfig loaded from file, without any modifications.
488+ func (d * initData ) KubeConfigOriginal () (* clientcmdapi.Config , error ) {
489+ if d .kubeconfigOriginal == nil {
490+ if _ , err := d .KubeConfig (); err != nil {
491+ return nil , err
492+ }
493+ }
494+
495+ return d .kubeconfigOriginal , nil
496+ }
497+
480498// KubeConfigDir returns the Kubernetes configuration directory or the temporary directory if DryRun is true.
481499func (d * initData ) KubeConfigDir () string {
482500 if d .dryRun {
@@ -521,8 +539,12 @@ func (d *initData) OutputWriter() io.Writer {
521539
522540// getDryRunClient creates a fake client that answers some GET calls in order to be able to do the full init flow in dry-run mode.
523541func getDryRunClient (d * initData ) (clientset.Interface , error ) {
542+ kubeconfig , err := d .KubeConfig ()
543+ if err != nil {
544+ return nil , err
545+ }
524546 dryRun := apiclient .NewDryRun ()
525- if err := dryRun .WithKubeConfigFile ( d . KubeConfigPath () ); err != nil {
547+ if err := dryRun .WithKubeConfig ( kubeconfig ); err != nil {
526548 return nil , err
527549 }
528550 dryRun .WithDefaultMarshalFunction ().
@@ -550,7 +572,11 @@ func (d *initData) Client() (clientset.Interface, error) {
550572 // and if the bootstrapping was not already done
551573 if ! d .adminKubeConfigBootstrapped && isDefaultKubeConfigPath {
552574 // Call EnsureAdminClusterRoleBinding() to obtain a working client from admin.conf.
553- d .client , err = kubeconfigphase .EnsureAdminClusterRoleBinding (kubeadmconstants .KubernetesDir , nil )
575+ d .client , err = kubeconfigphase .EnsureAdminClusterRoleBinding (
576+ kubeadmconstants .KubernetesDir ,
577+ & d .Cfg ().LocalAPIEndpoint ,
578+ nil ,
579+ )
554580 if err != nil {
555581 return nil , errors .Wrapf (err , "could not bootstrap the admin user in file %s" , kubeadmconstants .AdminKubeConfigFileName )
556582 }
@@ -571,30 +597,6 @@ func (d *initData) Client() (clientset.Interface, error) {
571597 return d .client , nil
572598}
573599
574- // WaitControlPlaneClient returns a basic client used for the purpose of waiting
575- // for control plane components to report 'ok' on their respective health check endpoints.
576- // It uses the admin.conf as the base, but modifies it to point at the local API server instead
577- // of the control plane endpoint.
578- func (d * initData ) WaitControlPlaneClient () (clientset.Interface , error ) {
579- config , err := clientcmd .LoadFromFile (d .KubeConfigPath ())
580- if err != nil {
581- return nil , err
582- }
583- for _ , v := range config .Clusters {
584- v .Server = fmt .Sprintf ("https://%s" ,
585- net .JoinHostPort (
586- d .Cfg ().LocalAPIEndpoint .AdvertiseAddress ,
587- strconv .Itoa (int (d .Cfg ().LocalAPIEndpoint .BindPort )),
588- ),
589- )
590- }
591- client , err := kubeconfigutil .ToClientSet (config )
592- if err != nil {
593- return nil , err
594- }
595- return client , nil
596- }
597-
598600// Tokens returns an array of token strings.
599601func (d * initData ) Tokens () []string {
600602 tokens := []string {}
0 commit comments