From a18502615ea7900d3848f89d4d3599a37d8e3026 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 9 Apr 2020 01:11:30 +0300 Subject: [PATCH] kubeadm-init: allow overriding the dry-run temp directory Allow overriding the dry-run temporary directory with an env. variable (KUBEADM_INIT_DRYRUN_DIR). Use the same variable in test/cmd/init_test.go. This allows running integration tests as non-root. --- cmd/kubeadm/app/cmd/init.go | 5 ++++- cmd/kubeadm/test/cmd/init_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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...)