Skip to content

Commit

Permalink
Merge pull request #8062 from afbjorklund/podman-point
Browse files Browse the repository at this point in the history
Add a PointToHost function for podman as well
  • Loading branch information
medyagh committed May 11, 2020
2 parents acd2ae1 + 16af75e commit 9c60548
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
21 changes: 11 additions & 10 deletions cmd/minikube/cmd/podman-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -163,17 +165,15 @@ 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))
}

// podmanUnsetScript writes out a shell-compatible 'podman-env unset' script
func podmanUnsetScript(ec PodmanEnvConfig, w io.Writer) error {
vars := []string{
constants.PodmanVarlinkBridgeEnv,
constants.MinikubeActivePodmanEnv,
}
return shell.UnsetScript(ec.EnvConfig, w, vars)
}
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/minikube/cmd/podman-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
},
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pkg/drivers/kic/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down
21 changes: 14 additions & 7 deletions pkg/minikube/machine/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,24 @@ func maybeWarnAboutEvalEnv(drver string, name string) {
if !driver.IsKIC(drver) {
return
}
p := os.Getenv(constants.MinikubeActiveDockerdEnv)
if p == "" {
return
}
out.T(out.Notice, "Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:", out.V{"driver_name": drver})
// TODO: refactor docker-env package to generate only eval command per shell. https://github.com/kubernetes/minikube/issues/6887
out.WarningT(`Please re-eval your docker-env, To ensure your environment variables have updated ports:
if os.Getenv(constants.MinikubeActiveDockerdEnv) != "" {
out.T(out.Notice, "Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:", out.V{"driver_name": drver})
// TODO: refactor docker-env package to generate only eval command per shell. https://github.com/kubernetes/minikube/issues/6887
out.WarningT(`Please re-eval your docker-env, To ensure your environment variables have updated ports:
'minikube -p {{.profile_name}} docker-env'
`, out.V{"profile_name": name})
}
if os.Getenv(constants.MinikubeActivePodmanEnv) != "" {
out.T(out.Notice, "Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:", out.V{"driver_name": drver})
// TODO: refactor podman-env package to generate only eval command per shell. https://github.com/kubernetes/minikube/issues/6887
out.WarningT(`Please re-eval your podman-env, To ensure your environment variables have updated ports:
'minikube -p {{.profile_name}} podman-env'
`, out.V{"profile_name": name})
}

}

Expand Down
4 changes: 2 additions & 2 deletions site/content/en/docs/commands/podman-env.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 9c60548

Please sign in to comment.