From c578a7aa1e2ec9ea499bab5571bb79163be3e35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sun, 10 May 2020 16:51:38 +0200 Subject: [PATCH] Add a PointToHost function for podman as well And add an active variable, similiar to docker --- cmd/minikube/cmd/podman-env.go | 21 +++++++++++---------- cmd/minikube/cmd/podman-env_test.go | 3 ++- cmd/minikube/cmd/root.go | 4 ++++ pkg/drivers/kic/oci/oci.go | 18 ++++++++++++++++++ pkg/minikube/constants/constants.go | 5 +++++ site/content/en/docs/commands/podman-env.md | 4 ++-- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/cmd/minikube/cmd/podman-env.go b/cmd/minikube/cmd/podman-env.go index 62356a49ee26..8bff0ce56fd7 100644 --- a/cmd/minikube/cmd/podman-env.go +++ b/cmd/minikube/cmd/podman-env.go @@ -38,12 +38,13 @@ import ( "k8s.io/minikube/pkg/minikube/shell" ) -var podmanEnvTmpl = fmt.Sprintf("{{ .Prefix }}%s{{ .Delimiter }}{{ .VarlinkBridge }}{{ .Suffix }}{{ .UsageHint }}", constants.PodmanVarlinkBridgeEnv) +var podmanEnvTmpl = fmt.Sprintf("{{ .Prefix }}%s{{ .Delimiter }}{{ .VarlinkBridge }}{{ .Suffix }}{{ .Prefix }}%s{{ .Delimiter }}{{ .MinikubePodmanProfile }}{{ .Suffix }}{{ .UsageHint }}", constants.PodmanVarlinkBridgeEnv, constants.MinikubeActivePodmanEnv) // PodmanShellConfig represents the shell config for Podman type PodmanShellConfig struct { shell.Config - VarlinkBridge string + VarlinkBridge string + MinikubePodmanProfile string } var ( @@ -59,6 +60,7 @@ func podmanShellCfgSet(ec PodmanEnvConfig, envMap map[string]string) *PodmanShel Config: *shell.CfgSet(ec.EnvConfig, usgPlz, usgCmd), } s.VarlinkBridge = envMap[constants.PodmanVarlinkBridgeEnv] + s.MinikubePodmanProfile = envMap[constants.MinikubeActivePodmanEnv] return s } @@ -103,7 +105,7 @@ func createExternalSSHClient(d drivers.Driver) (*ssh.ExternalClient, error) { // podmanEnvCmd represents the podman-env command var podmanEnvCmd = &cobra.Command{ Use: "podman-env", - Short: "Configure environment to use minikube's Podman daemon", + Short: "Configure environment to use minikube's Podman service", Long: `Sets up podman env variables; similar to '$(podman-machine env)'.`, Run: func(cmd *cobra.Command, args []string) { cname := ClusterFlagValue() @@ -163,10 +165,7 @@ type PodmanEnvConfig struct { // podmanSetScript writes out a shell-compatible 'podman-env' script func podmanSetScript(ec PodmanEnvConfig, w io.Writer) error { - envVars, err := podmanEnvVars(ec) - if err != nil { - return err - } + envVars := podmanEnvVars(ec) return shell.SetScript(ec.EnvConfig, w, podmanEnvTmpl, podmanShellCfgSet(ec, envVars)) } @@ -174,6 +173,7 @@ func podmanSetScript(ec PodmanEnvConfig, w io.Writer) error { func podmanUnsetScript(ec PodmanEnvConfig, w io.Writer) error { vars := []string{ constants.PodmanVarlinkBridgeEnv, + constants.MinikubeActivePodmanEnv, } return shell.UnsetScript(ec.EnvConfig, w, vars) } @@ -187,11 +187,12 @@ func podmanBridge(client *ssh.ExternalClient) string { } // podmanEnvVars gets the necessary podman env variables to allow the use of minikube's podman service -func podmanEnvVars(ec PodmanEnvConfig) (map[string]string, error) { // nolint result 1 (error) is always nil +func podmanEnvVars(ec PodmanEnvConfig) map[string]string { env := map[string]string{ - constants.PodmanVarlinkBridgeEnv: podmanBridge(ec.client), + constants.PodmanVarlinkBridgeEnv: podmanBridge(ec.client), + constants.MinikubeActivePodmanEnv: ec.profile, } - return env, nil + return env } func init() { diff --git a/cmd/minikube/cmd/podman-env_test.go b/cmd/minikube/cmd/podman-env_test.go index 2e090541fb77..088867193a6e 100644 --- a/cmd/minikube/cmd/podman-env_test.go +++ b/cmd/minikube/cmd/podman-env_test.go @@ -44,11 +44,12 @@ func TestGeneratePodmanScripts(t *testing.T) { PodmanEnvConfig{profile: "bash", driver: "kvm2", client: newFakeClient()}, nil, `export PODMAN_VARLINK_BRIDGE="/usr/bin/ssh root@host -- sudo varlink -A \'podman varlink \\\$VARLINK_ADDRESS\' bridge" +export MINIKUBE_ACTIVE_PODMAN="bash" # To point your shell to minikube's podman service, run: # eval $(minikube -p bash podman-env) `, - `unset PODMAN_VARLINK_BRIDGE + `unset PODMAN_VARLINK_BRIDGE MINIKUBE_ACTIVE_PODMAN `, }, } diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 23eb988cc69c..af4bcc6dded2 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -105,6 +105,10 @@ func Execute() { glog.Errorf("oci env: %v", err) } + if err := oci.PointToHostPodman(); err != nil { + glog.Errorf("oci env: %v", err) + } + if err := RootCmd.Execute(); err != nil { // Cobra already outputs the error, typically because the user provided an unknown command. os.Exit(exit.BadUsage) diff --git a/pkg/drivers/kic/oci/oci.go b/pkg/drivers/kic/oci/oci.go index 047fe928fb5a..c784e37ef38d 100644 --- a/pkg/drivers/kic/oci/oci.go +++ b/pkg/drivers/kic/oci/oci.go @@ -465,6 +465,24 @@ func PointToHostDockerDaemon() error { return nil } +// PointToHostPodman will unset env variables that point to podman inside minikube +func PointToHostPodman() error { + p := os.Getenv(constants.MinikubeActivePodmanEnv) + if p != "" { + glog.Infof("shell is pointing to podman inside minikube. will unset to use host") + } + + for i := range constants.PodmanRemoteEnvs { + e := constants.PodmanRemoteEnvs[i] + err := os.Setenv(e, "") + if err != nil { + return errors.Wrapf(err, "resetting %s env", e) + } + + } + return nil +} + // ContainerRunning returns running state of a container func ContainerRunning(ociBin string, name string, warnSlow ...bool) (bool, error) { rr, err := runCmd(exec.Command(ociBin, "inspect", name, "--format={{.State.Running}}"), warnSlow...) diff --git a/pkg/minikube/constants/constants.go b/pkg/minikube/constants/constants.go index aafa28ed353d..3e4e26467e78 100644 --- a/pkg/minikube/constants/constants.go +++ b/pkg/minikube/constants/constants.go @@ -65,6 +65,9 @@ const ( MinikubeActiveDockerdEnv = "MINIKUBE_ACTIVE_DOCKERD" // PodmanVarlinkBridgeEnv is used for podman settings PodmanVarlinkBridgeEnv = "PODMAN_VARLINK_BRIDGE" + // MinikubeActivePodmanEnv holds the podman service that the user's shell is pointing at + // value would be profile or empty if pointing to the user's host. + MinikubeActivePodmanEnv = "MINIKUBE_ACTIVE_PODMAN" // MinikubeForceSystemdEnv is used to force systemd as cgroup manager for the container runtime MinikubeForceSystemdEnv = "MINIKUBE_FORCE_SYSTEMD" ) @@ -82,6 +85,8 @@ var ( // DockerDaemonEnvs is list of docker-daemon related environment variables. DockerDaemonEnvs = [3]string{DockerHostEnv, DockerTLSVerifyEnv, DockerCertPathEnv} + // PodmanRemoteEnvs is list of podman-remote related environment variables. + PodmanRemoteEnvs = [1]string{PodmanVarlinkBridgeEnv} // DefaultMinipath is the default minikube path (under the home directory) DefaultMinipath = filepath.Join(homedir.HomeDir(), ".minikube") diff --git a/site/content/en/docs/commands/podman-env.md b/site/content/en/docs/commands/podman-env.md index ff9db7fb0252..00c0e7d9544c 100644 --- a/site/content/en/docs/commands/podman-env.md +++ b/site/content/en/docs/commands/podman-env.md @@ -1,14 +1,14 @@ --- title: "podman-env" description: > - Configure environment to use minikube's Podman daemon + Configure environment to use minikube's Podman service --- ## minikube podman-env -Configure environment to use minikube's Podman daemon +Configure environment to use minikube's Podman service ### Synopsis