From 53480d7f0a301e4e44e3799cfa9c8c2273c313e2 Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Mon, 21 Oct 2019 23:41:46 +0200 Subject: [PATCH 1/3] Add shadow option `KubeConfig` This prepares for upcoming commits which introduce the `--kubeconfig` flag. --- pkg/skaffold/config/options.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/skaffold/config/options.go b/pkg/skaffold/config/options.go index 91c27404bf2..89f8500be6f 100644 --- a/pkg/skaffold/config/options.go +++ b/pkg/skaffold/config/options.go @@ -55,6 +55,7 @@ type SkaffoldOptions struct { CacheFile string Trigger string KubeContext string + KubeConfig string WatchPollInterval int DefaultRepo string CustomLabels []string From 4fd247342f262c8284392a9153a9b4a80c331748 Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Sat, 19 Oct 2019 12:02:49 +0200 Subject: [PATCH 2/3] Pass `--kubeconfig` CLI setting to kubectl Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- pkg/skaffold/kubectl/cli.go | 5 ++++ pkg/skaffold/kubectl/cli_test.go | 47 ++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/pkg/skaffold/kubectl/cli.go b/pkg/skaffold/kubectl/cli.go index 5992a68e92c..af5d6ae69e3 100644 --- a/pkg/skaffold/kubectl/cli.go +++ b/pkg/skaffold/kubectl/cli.go @@ -29,6 +29,7 @@ import ( // CLI holds parameters to run kubectl. type CLI struct { KubeContext string + KubeConfig string Namespace string version ClientVersion @@ -38,6 +39,7 @@ type CLI struct { func NewFromRunContext(runCtx *runcontext.RunContext) *CLI { return &CLI{ KubeContext: runCtx.KubeContext, + KubeConfig: runCtx.Opts.KubeConfig, Namespace: runCtx.Opts.Namespace, } } @@ -86,6 +88,9 @@ func (c *CLI) args(command string, namespace string, arg ...string) []string { if namespace != "" { args = append(args, "--namespace", namespace) } + if c.KubeConfig != "" { + args = append(args, "--kubeconfig", c.KubeConfig) + } args = append(args, command) args = append(args, arg...) return args diff --git a/pkg/skaffold/kubectl/cli_test.go b/pkg/skaffold/kubectl/cli_test.go index ca7bf713375..f737ecda8d9 100644 --- a/pkg/skaffold/kubectl/cli_test.go +++ b/pkg/skaffold/kubectl/cli_test.go @@ -27,25 +27,36 @@ import ( ) func TestCLI(t *testing.T) { + const ( + kubeContext = "some-kubecontext" + output = "this is the expected output" + ) + tests := []struct { name string - kubecontext string + kubeconfig string namespace string - output string expectedCommand string }{ { - name: "with context and namespace", - kubecontext: "some-kubecontext", + name: "without namespace or kubeconfig", + expectedCommand: "kubectl --context some-kubecontext exec arg1 arg2", + }, + { + name: "only namespace, no kubeconfig", namespace: "some-namespace", - output: "this is the expected output", expectedCommand: "kubectl --context some-kubecontext --namespace some-namespace exec arg1 arg2", }, { - name: "only context, no namespace", - kubecontext: "some-kubecontext", - output: "this is the expected output", - expectedCommand: "kubectl --context some-kubecontext exec arg1 arg2", + name: "only kubeconfig, no namespace", + kubeconfig: "some-kubeconfig", + expectedCommand: "kubectl --context some-kubecontext --kubeconfig some-kubeconfig exec arg1 arg2", + }, + { + name: "with namespace and kubeconfig", + kubeconfig: "some-kubeconfig", + namespace: "some-namespace", + expectedCommand: "kubectl --context some-kubecontext --namespace some-namespace --kubeconfig some-kubeconfig exec arg1 arg2", }, } @@ -57,8 +68,11 @@ func TestCLI(t *testing.T) { )) cli := NewFromRunContext(&runcontext.RunContext{ - Opts: config.SkaffoldOptions{Namespace: test.namespace}, - KubeContext: test.kubecontext, + Opts: config.SkaffoldOptions{ + Namespace: test.namespace, + KubeConfig: test.kubeconfig, + }, + KubeContext: kubeContext, }) err := cli.Run(context.Background(), nil, nil, "exec", "arg1", "arg2") @@ -71,17 +85,20 @@ func TestCLI(t *testing.T) { testutil.Run(t, test.name, func(t *testutil.T) { t.Override(&util.DefaultExecCommand, testutil.CmdRunOut( test.expectedCommand, - test.output, + output, )) cli := NewFromRunContext(&runcontext.RunContext{ - Opts: config.SkaffoldOptions{Namespace: test.namespace}, - KubeContext: test.kubecontext, + Opts: config.SkaffoldOptions{ + Namespace: test.namespace, + KubeConfig: test.kubeconfig, + }, + KubeContext: kubeContext, }) out, err := cli.RunOut(context.Background(), "exec", "arg1", "arg2") t.CheckNoError(err) - t.CheckDeepEqual(string(out), test.output) + t.CheckDeepEqual(string(out), output) }) } } From a20242b0a36a2aa3c7f90c967e81fd6596a1907d Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Sun, 20 Oct 2019 23:37:06 +0200 Subject: [PATCH 3/3] Pass `--kubeconfig` setting to helm Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- pkg/skaffold/deploy/helm.go | 5 +++++ pkg/skaffold/deploy/helm_test.go | 11 ++++++++--- pkg/skaffold/deploy/kubectl_test.go | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/skaffold/deploy/helm.go b/pkg/skaffold/deploy/helm.go index b9c0a10e424..a9a01678c18 100644 --- a/pkg/skaffold/deploy/helm.go +++ b/pkg/skaffold/deploy/helm.go @@ -50,6 +50,7 @@ type HelmDeployer struct { *latest.HelmDeploy kubeContext string + kubeConfig string namespace string defaultRepo string forceDeploy bool @@ -61,6 +62,7 @@ func NewHelmDeployer(runCtx *runcontext.RunContext) *HelmDeployer { return &HelmDeployer{ HelmDeploy: runCtx.Cfg.Deploy.HelmDeploy, kubeContext: runCtx.KubeContext, + kubeConfig: runCtx.Opts.KubeConfig, namespace: runCtx.Opts.Namespace, defaultRepo: runCtx.DefaultRepo, forceDeploy: runCtx.Opts.Force, @@ -161,6 +163,9 @@ func (h *HelmDeployer) Cleanup(ctx context.Context, out io.Writer) error { func (h *HelmDeployer) helm(ctx context.Context, out io.Writer, useSecrets bool, arg ...string) error { args := append([]string{"--kube-context", h.kubeContext}, arg...) args = append(args, h.Flags.Global...) + if h.kubeConfig != "" { + args = append(args, "--kubeconfig", h.kubeConfig) + } if useSecrets { args = append([]string{"secrets"}, args...) diff --git a/pkg/skaffold/deploy/helm_test.go b/pkg/skaffold/deploy/helm_test.go index 1cca519affb..d318e3cfe9f 100644 --- a/pkg/skaffold/deploy/helm_test.go +++ b/pkg/skaffold/deploy/helm_test.go @@ -529,9 +529,13 @@ func (m *MockHelm) RunCmd(c *exec.Cmd) error { m.t.Errorf("Not enough args in command %v", c) } - if c.Args[1] != "--kube-context" || c.Args[2] != testKubeContext { + argString := strings.Join(c.Args, " ") + if !strings.Contains(argString, "--kube-context "+testKubeContext) { m.t.Errorf("Invalid Kubernetes context %v", c) } + if !strings.Contains(argString, "--kubeconfig "+testKubeConfig) { + m.t.Errorf("Invalid Kubernetes config %v", c) + } if c.Args[3] == "get" || c.Args[3] == "upgrade" { if releaseName := c.Args[4]; strings.Contains(releaseName, "{{") { @@ -863,8 +867,9 @@ func makeRunContext(deploy latest.HelmDeploy, force bool) *runcontext.RunContext Cfg: pipeline, KubeContext: testKubeContext, Opts: config.SkaffoldOptions{ - Namespace: testNamespace, - Force: force, + Namespace: testNamespace, + KubeConfig: testKubeConfig, + Force: force, }, } } diff --git a/pkg/skaffold/deploy/kubectl_test.go b/pkg/skaffold/deploy/kubectl_test.go index 5d3bf5f752b..2ba18e85cd4 100644 --- a/pkg/skaffold/deploy/kubectl_test.go +++ b/pkg/skaffold/deploy/kubectl_test.go @@ -36,6 +36,7 @@ import ( const ( testKubeContext = "kubecontext" + testKubeConfig = "kubeconfig" kubectlVersion = `{"clientVersion":{"major":"1","minor":"12"}}` )