diff --git a/cmd/kubeadm/app/cmd/init.go b/cmd/kubeadm/app/cmd/init.go index ad8e0c59057..0d0446d79fc 100644 --- a/cmd/kubeadm/app/cmd/init.go +++ b/cmd/kubeadm/app/cmd/init.go @@ -386,9 +386,6 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io // Validate that also the required kubeconfig files exists and are invalid, because // kubeadm can't regenerate them without the CA Key kubeconfigDir := options.kubeconfigDir - if options.dryRun { - kubeconfigDir = dryRunDir - } if err := kubeconfigphase.ValidateKubeconfigsForExternalCA(kubeconfigDir, cfg); err != nil { return nil, err } diff --git a/cmd/kubeadm/app/cmd/phases/init/certs.go b/cmd/kubeadm/app/cmd/phases/init/certs.go index 53473901845..e2000891fa2 100644 --- a/cmd/kubeadm/app/cmd/phases/init/certs.go +++ b/cmd/kubeadm/app/cmd/phases/init/certs.go @@ -18,6 +18,8 @@ package phases import ( "fmt" + "os" + "path/filepath" "strings" "github.com/pkg/errors" @@ -195,6 +197,20 @@ func runCerts(c workflow.RunData) error { } fmt.Printf("[certs] Using certificateDir folder %q\n", data.CertificateWriteDir()) + + // If using an external CA while dryrun, copy CA cert to dryrun dir for later use + if data.ExternalCA() && data.DryRun() { + externalCAFile := filepath.Join(data.Cfg().CertificatesDir, kubeadmconstants.CACertName) + fileInfo, _ := os.Stat(externalCAFile) + contents, err := os.ReadFile(externalCAFile) + if err != nil { + return err + } + err = os.WriteFile(filepath.Join(data.CertificateWriteDir(), kubeadmconstants.CACertName), contents, fileInfo.Mode()) + if err != nil { + return err + } + } return nil } diff --git a/cmd/kubeadm/app/cmd/phases/init/kubeconfig.go b/cmd/kubeadm/app/cmd/phases/init/kubeconfig.go index f2f6b7d3cb4..3d1f304a084 100644 --- a/cmd/kubeadm/app/cmd/phases/init/kubeconfig.go +++ b/cmd/kubeadm/app/cmd/phases/init/kubeconfig.go @@ -18,6 +18,8 @@ package phases import ( "fmt" + "os" + "path/filepath" "github.com/pkg/errors" "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" @@ -132,6 +134,19 @@ func runKubeConfigFile(kubeConfigFileName string) func(workflow.RunData) error { // if external CA mode, skip certificate authority generation if data.ExternalCA() { fmt.Printf("[kubeconfig] External CA mode: Using user provided %s\n", kubeConfigFileName) + // If using an external CA while dryrun, copy kubeconfig files to dryrun dir for later use + if data.DryRun() { + externalCAFile := filepath.Join(kubeadmconstants.KubernetesDir, kubeConfigFileName) + fileInfo, _ := os.Stat(externalCAFile) + contents, err := os.ReadFile(externalCAFile) + if err != nil { + return err + } + err = os.WriteFile(filepath.Join(data.KubeConfigDir(), kubeConfigFileName), contents, fileInfo.Mode()) + if err != nil { + return err + } + } return nil }