diff --git a/cmd/kubeadm/app/cmd/init.go b/cmd/kubeadm/app/cmd/init.go index 57e87d6603e..43aab68780a 100644 --- a/cmd/kubeadm/app/cmd/init.go +++ b/cmd/kubeadm/app/cmd/init.go @@ -357,7 +357,10 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io // if dry running creates a temporary folder for saving kubeadm generated files dryRunDir := "" if options.dryRun { - if dryRunDir, err = kubeadmconstants.CreateTempDirForKubeadm("", "kubeadm-init-dryrun"); err != nil { + // the KUBEADM_INIT_DRYRUN_DIR environment variable allows overriding the dry-run temporary + // directory from the command line. This makes it possible to run "kubeadm init" integration + // tests without root. + if dryRunDir, err = kubeadmconstants.CreateTempDirForKubeadm(os.Getenv("KUBEADM_INIT_DRYRUN_DIR"), "kubeadm-init-dryrun"); err != nil { return nil, errors.Wrap(err, "couldn't create a temporary directory") } } diff --git a/cmd/kubeadm/test/cmd/init_test.go b/cmd/kubeadm/test/cmd/init_test.go index 888e92df5a6..6a323923d9f 100644 --- a/cmd/kubeadm/test/cmd/init_test.go +++ b/cmd/kubeadm/test/cmd/init_test.go @@ -17,6 +17,8 @@ limitations under the License. package kubeadm import ( + "fmt" + "os" "os/exec" "strings" "testing" @@ -30,6 +32,10 @@ import ( ) func runKubeadmInit(args ...string) (string, string, int, error) { + const dryRunDir = "KUBEADM_INIT_DRYRUN_DIR" + if err := os.Setenv(dryRunDir, os.TempDir()); err != nil { + panic(fmt.Sprintf("could not set the %s environment variable", dryRunDir)) + } kubeadmPath := getKubeadmPath() kubeadmArgs := []string{"init", "--dry-run", "--ignore-preflight-errors=all"} kubeadmArgs = append(kubeadmArgs, args...)