Skip to content

Commit

Permalink
feat(cli): override kubectl path with env-var (#221)
Browse files Browse the repository at this point in the history
Up to now, Tanka was using hard-coded string as a `kubectl` path, making
it hard to do any other tool without playing tricks with $PATH. This
commit allows to use TANKA_KUBECTL_PATH env variable to override the
path to `kubectl` used in Tanka's code.
  • Loading branch information
mplzik committed Feb 29, 2020
1 parent cd84c7c commit 6546515
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
11 changes: 11 additions & 0 deletions docs/docs/env-vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: "Environment variables"
route: "/env-vars"
---

# Environment Variables

### TANKA_KUBECTL_PATH

**Description**: Path to the `kubectl` tool executable
**Default**: `$PATH/kubectl`
14 changes: 10 additions & 4 deletions docs/doczrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ export default {
// "Using libraries",
// "Creating and structure",
"Installing and publishing",
"Overriding",
"Overriding",
],
},

// additional features
"Output filtering",
"Exporting as YAML",
"Command-line completion",
"Directory structure",
"Diff strategies",
"Output filtering",


// reference
"Directory structure",
"Environment variables",

"Frequently asked questions",
"Known issues",
],
Expand Down
5 changes: 2 additions & 3 deletions pkg/kubernetes/client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -62,7 +61,7 @@ func writeNamespacePatch(context objx.Map, namespace string) (string, error) {

// Kubeconfig returns the merged $KUBECONFIG of the host
func Kubeconfig() (map[string]interface{}, error) {
cmd := exec.Command("kubectl", "config", "view", "-o", "json")
cmd := kubectlCmd("config", "view", "-o", "json")
cfgJSON := bytes.Buffer{}
cmd.Stdout = &cfgJSON
cmd.Stderr = os.Stderr
Expand All @@ -77,7 +76,7 @@ func Kubeconfig() (map[string]interface{}, error) {
}

func Contexts() ([]string, error) {
cmd := exec.Command("kubectl", "config", "get-contexts", "-o=name")
cmd := kubectlCmd("config", "get-contexts", "-o=name")
buf := bytes.Buffer{}
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
Expand Down
6 changes: 1 addition & 5 deletions pkg/kubernetes/client/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"fmt"
"os"
"os/exec"
)

// Delete removes the specified object from the cluster
Expand All @@ -23,14 +22,11 @@ func (k Kubectl) DeleteByLabels(namespace string, labels map[string]interface{},

func (k Kubectl) delete(namespace string, sel []string, opts DeleteOpts) error {
argv := append([]string{"-n", namespace}, sel...)
k.ctl("delete", argv...)

if opts.Force {
argv = append(argv, "--force")
}

cmd := exec.Command("kubectl", argv...)

cmd := k.ctl("delete", argv...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
Expand Down
12 changes: 11 additions & 1 deletion pkg/kubernetes/client/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (
"strings"
)

// kubectlCmd returns command a object that will launch kubectl at an appropriate path.
func kubectlCmd(args ...string) *exec.Cmd {
binary := "kubectl"
if env := os.Getenv("TANKA_KUBECTL_PATH"); env != "" {
binary = env
}

return exec.Command(binary, args...)
}

// ctl returns an `exec.Cmd` for `kubectl`. It also forces the correct context
// and injects our patched $KUBECONFIG for the default namespace.
func (k Kubectl) ctl(action string, args ...string) *exec.Cmd {
Expand All @@ -19,7 +29,7 @@ func (k Kubectl) ctl(action string, args ...string) *exec.Cmd {
argv = append(argv, args...)

// prepare the cmd
cmd := exec.Command("kubectl", argv...)
cmd := kubectlCmd(argv...)
cmd.Env = patchKubeconfig(k.nsPatch, os.Environ())

return cmd
Expand Down

0 comments on commit 6546515

Please sign in to comment.