Skip to content

Commit

Permalink
feat: base64 encoded KUBECONFIG_CONTENT (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-svensson authored Nov 12, 2019
1 parent 6dcdf55 commit 52d14e9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
28 changes: 22 additions & 6 deletions pkg/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubectl
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"github.com/buildtool/build-tools/pkg/config"
"github.com/liamg/tml"
Expand Down Expand Up @@ -38,11 +39,11 @@ var newKubectlCmd = cmd.NewKubectlCommand
func New(environment *config.Environment, out, eout io.Writer) Kubectl {
name, _ := ioutil.TempDir(os.TempDir(), "build-tools")

arg := argsFromEnvironment(environment, name, out)
arg := argsFromEnvironment(environment, name, out, eout)
return &kubectl{args: arg, tempDir: name, out: out, eout: eout}
}

func argsFromEnvironment(e *config.Environment, tempDir string, out io.Writer) map[string]string {
func argsFromEnvironment(e *config.Environment, tempDir string, out, eout io.Writer) map[string]string {
kubeConfigArg := "kubeconfig"
args := make(map[string]string)
if len(e.Context) > 0 {
Expand All @@ -51,11 +52,19 @@ func argsFromEnvironment(e *config.Environment, tempDir string, out io.Writer) m
if len(e.Namespace) > 0 {
args["namespace"] = e.Namespace
}
if content, exist := os.LookupEnv(envKubeConfigContent); exist {
if content, exist := os.LookupEnv(envKubeConfigContentBase64); exist {
// Not a file, create file from Base64 encoded content
_, _ = fmt.Fprintf(out, "Parsing config from env: %s\n", envKubeConfigContentBase64)
bytes, err := base64.StdEncoding.DecodeString(content)
if err != nil {
_, _ = fmt.Fprintln(eout, tml.Sprintf("Failed to decode content %+v", err))
return nil
}
args[kubeConfigArg] = writeKubeConfigFile(tempDir, kubeConfigArg, bytes)
} else if content, exist := os.LookupEnv(envKubeConfigContent); exist {
// Not a file, create file from content
kubeconfigFile := filepath.Join(tempDir, kubeConfigArg)
_ = ioutil.WriteFile(kubeconfigFile, []byte(content), 0777)
args[kubeConfigArg] = kubeconfigFile
_, _ = fmt.Fprintf(out, "Parsing config from env: %s\n", envKubeConfigContent)
args[kubeConfigArg] = writeKubeConfigFile(tempDir, kubeConfigArg, []byte(content))
} else if len(e.Kubeconfig) > 0 {
args[kubeConfigArg] = e.Kubeconfig
}
Expand All @@ -66,6 +75,12 @@ func argsFromEnvironment(e *config.Environment, tempDir string, out io.Writer) m
return args
}

func writeKubeConfigFile(tempDir string, kubeConfigArg string, content []byte) string {
kubeconfigFile := filepath.Join(tempDir, kubeConfigArg)
_ = ioutil.WriteFile(kubeconfigFile, content, 0777)
return kubeconfigFile
}

func (k kubectl) defaultArgs() (args []string) {
var keys []string
for key := range k.args {
Expand Down Expand Up @@ -167,3 +182,4 @@ func (k kubectl) extractEvents(output string) string {
var _ Kubectl = &kubectl{}

const envKubeConfigContent = "KUBECONFIG_CONTENT"
const envKubeConfigContentBase64 = "KUBECONFIG_CONTENT_BASE64"
30 changes: 30 additions & 0 deletions pkg/kubectl/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubectl

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"github.com/buildtool/build-tools/pkg"
Expand Down Expand Up @@ -212,6 +213,35 @@ func TestKubectl_KubeconfigSet(t *testing.T) {
k.Cleanup()
}

func TestKubectl_KubeconfigBase64Set(t *testing.T) {
out := &bytes.Buffer{}
eout := &bytes.Buffer{}
yaml := `contexts:
- context:
cluster: k8s.prod
user: [email protected]
`
defer pkg.SetEnv(envKubeConfigContentBase64, base64.StdEncoding.EncodeToString([]byte(yaml)))()
k := New(&config.Environment{}, out, eout)

kubeConfigFile := filepath.Join(k.(*kubectl).tempDir, "kubeconfig")
fileContent, err := ioutil.ReadFile(kubeConfigFile)
assert.NoError(t, err)
assert.Equal(t, "contexts:\n- context:\n cluster: k8s.prod\n user: [email protected]\n", string(fileContent))
assert.Equal(t, kubeConfigFile, k.(*kubectl).args["kubeconfig"])
k.Cleanup()
}

func TestKubectl_KubeconfigInvalidBase64Set(t *testing.T) {
out := &bytes.Buffer{}
eout := &bytes.Buffer{}
defer pkg.SetEnv(envKubeConfigContentBase64, "äö")()
k := New(&config.Environment{}, out, eout)
assert.Equal(t, "\x1b[0mFailed to decode content illegal base64 data at input byte 0\x1b[0m\n", eout.String())

k.Cleanup()
}

func TestKubectl_KubeconfigExistingFile(t *testing.T) {

out := &bytes.Buffer{}
Expand Down

0 comments on commit 52d14e9

Please sign in to comment.