Skip to content

Commit

Permalink
Add a PointToHost function for podman as well
Browse files Browse the repository at this point in the history
And add an active variable, similiar to docker
  • Loading branch information
afbjorklund committed May 10, 2020
1 parent 7e3da0f commit c578a7a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 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
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 c578a7a

Please sign in to comment.