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.
This commit is contained in:
Lubomir I. Ivanov 2020-04-09 01:11:30 +03:00
parent 04933f3a94
commit a18502615e
2 changed files with 10 additions and 1 deletions

View File

@ -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")
}
}

View File

@ -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...)