Skip to content

Commit a185026

Browse files
committed
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.
1 parent 04933f3 commit a185026

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cmd/kubeadm/app/cmd/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,10 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io
357357
// if dry running creates a temporary folder for saving kubeadm generated files
358358
dryRunDir := ""
359359
if options.dryRun {
360-
if dryRunDir, err = kubeadmconstants.CreateTempDirForKubeadm("", "kubeadm-init-dryrun"); err != nil {
360+
// the KUBEADM_INIT_DRYRUN_DIR environment variable allows overriding the dry-run temporary
361+
// directory from the command line. This makes it possible to run "kubeadm init" integration
362+
// tests without root.
363+
if dryRunDir, err = kubeadmconstants.CreateTempDirForKubeadm(os.Getenv("KUBEADM_INIT_DRYRUN_DIR"), "kubeadm-init-dryrun"); err != nil {
361364
return nil, errors.Wrap(err, "couldn't create a temporary directory")
362365
}
363366
}

cmd/kubeadm/test/cmd/init_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package kubeadm
1818

1919
import (
20+
"fmt"
21+
"os"
2022
"os/exec"
2123
"strings"
2224
"testing"
@@ -30,6 +32,10 @@ import (
3032
)
3133

3234
func runKubeadmInit(args ...string) (string, string, int, error) {
35+
const dryRunDir = "KUBEADM_INIT_DRYRUN_DIR"
36+
if err := os.Setenv(dryRunDir, os.TempDir()); err != nil {
37+
panic(fmt.Sprintf("could not set the %s environment variable", dryRunDir))
38+
}
3339
kubeadmPath := getKubeadmPath()
3440
kubeadmArgs := []string{"init", "--dry-run", "--ignore-preflight-errors=all"}
3541
kubeadmArgs = append(kubeadmArgs, args...)

0 commit comments

Comments
 (0)