Skip to content

Commit

Permalink
Merge pull request #3108 from corneliusweig/w/kubeconfig-kubectl-helm
Browse files Browse the repository at this point in the history
Prepare kubectl and helm deployers for `--kubeconfig` flag
  • Loading branch information
balopat authored Nov 6, 2019
2 parents 99dad21 + a20242b commit a29ee9a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
5 changes: 5 additions & 0 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type HelmDeployer struct {
*latest.HelmDeploy

kubeContext string
kubeConfig string
namespace string
defaultRepo string
forceDeploy bool
Expand All @@ -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,
Expand Down Expand Up @@ -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...)
Expand Down
11 changes: 8 additions & 3 deletions pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "{{") {
Expand Down Expand Up @@ -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,
},
}
}
1 change: 1 addition & 0 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

const (
testKubeContext = "kubecontext"
testKubeConfig = "kubeconfig"
kubectlVersion = `{"clientVersion":{"major":"1","minor":"12"}}`
)

Expand Down
5 changes: 5 additions & 0 deletions pkg/skaffold/kubectl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// CLI holds parameters to run kubectl.
type CLI struct {
KubeContext string
KubeConfig string
Namespace string

version ClientVersion
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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
Expand Down
47 changes: 32 additions & 15 deletions pkg/skaffold/kubectl/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand All @@ -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")

Expand All @@ -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)
})
}
}

0 comments on commit a29ee9a

Please sign in to comment.