Skip to content

Commit 0549530

Browse files
authored
Pass kubeconfig to kpt live (#5906)
1 parent be29d76 commit 0549530

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

pkg/skaffold/deploy/kpt/kpt.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
k8syaml "sigs.k8s.io/yaml"
3535

3636
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
37+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
3738
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kustomize"
38-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/types"
3939
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
4040
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
4141
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest"
@@ -66,15 +66,25 @@ type Deployer struct {
6666
insecureRegistries map[string]bool
6767
labels map[string]string
6868
globalConfig string
69+
kubeContext string
70+
kubeConfig string
71+
namespace string
72+
}
73+
74+
type Config interface {
75+
kubectl.Config
6976
}
7077

7178
// NewDeployer generates a new Deployer object contains the kptDeploy schema.
72-
func NewDeployer(cfg types.Config, labels map[string]string, d *latestV1.KptDeploy) *Deployer {
79+
func NewDeployer(cfg Config, labels map[string]string, d *latestV1.KptDeploy) *Deployer {
7380
return &Deployer{
7481
KptDeploy: d,
7582
insecureRegistries: cfg.GetInsecureRegistries(),
7683
labels: labels,
7784
globalConfig: cfg.GlobalConfig(),
85+
kubeContext: cfg.GetKubeContext(),
86+
kubeConfig: cfg.GetKubeConfig(),
87+
namespace: cfg.GetKubeNamespace(),
7888
}
7989
}
8090

@@ -215,7 +225,7 @@ func (k *Deployer) Cleanup(ctx context.Context, out io.Writer) error {
215225
return fmt.Errorf("getting applyDir: %w", err)
216226
}
217227

218-
cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(applyDir, []string{"live", "destroy"}, nil, nil)...)
228+
cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(applyDir, []string{"live", "destroy"}, k.getGlobalFlags(), nil)...)
219229
cmd.Stdout = out
220230
cmd.Stderr = out
221231
if err := util.RunCmd(cmd); err != nil {
@@ -606,6 +616,7 @@ func (k *Deployer) getKptLiveApplyArgs() []string {
606616
flags = append(flags, "--reconcile-timeout", k.Live.Options.ReconcileTimeout)
607617
}
608618

619+
flags = append(flags, k.getGlobalFlags()...)
609620
return flags
610621
}
611622

@@ -617,8 +628,23 @@ func (k *Deployer) getKptLiveInitArgs() []string {
617628
flags = append(flags, "--inventory-id", k.Live.Apply.InventoryID)
618629
}
619630

631+
flags = append(flags, k.getGlobalFlags()...)
632+
return flags
633+
}
634+
func (k *Deployer) getGlobalFlags() []string {
635+
var flags []string
636+
637+
if k.kubeContext != "" {
638+
flags = append(flags, "--context", k.kubeContext)
639+
}
640+
if k.kubeConfig != "" {
641+
flags = append(flags, "--kubeconfig", k.kubeConfig)
642+
}
620643
if len(k.Live.Apply.InventoryNamespace) > 0 {
621644
flags = append(flags, "--namespace", k.Live.Apply.InventoryNamespace)
645+
} else if k.namespace != "" {
646+
// Note: UI duplication.
647+
flags = append(flags, "--namespace", k.namespace)
622648
}
623649

624650
return flags

pkg/skaffold/deploy/kpt/kpt_test.go

+37-13
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestKpt_Deploy(t *testing.T) {
120120
AndRunOut(fmt.Sprintf("kpt fn sink %v", tmpKustomizeDir), ``).
121121
AndRunOut(fmt.Sprintf("kustomize build %v", tmpKustomizeDir), ``).
122122
AndRunOut("kpt fn run --dry-run", testPod).
123-
AndRun("kpt live apply valid_path"),
123+
AndRun("kpt live apply valid_path --context kubecontext --namespace testNamespace"),
124124
expected: []string{"default"},
125125
},
126126
{
@@ -131,8 +131,8 @@ func TestKpt_Deploy(t *testing.T) {
131131
commands: testutil.
132132
CmdRunOut("kpt fn source .", ``).
133133
AndRunOut("kpt fn run --dry-run", testPod).
134-
AndRunOut("kpt live init .kpt-hydrated", ``).
135-
AndRunErr("kpt live apply .kpt-hydrated", errors.New("BUG")),
134+
AndRunOut("kpt live init .kpt-hydrated --context kubecontext --namespace testNamespace", ``).
135+
AndRunErr("kpt live apply .kpt-hydrated --context kubecontext --namespace testNamespace", errors.New("BUG")),
136136
shouldErr: true,
137137
},
138138
{
@@ -152,7 +152,7 @@ func TestKpt_Deploy(t *testing.T) {
152152
commands: testutil.
153153
CmdRunOut("kpt fn source .", ``).
154154
AndRunOut("kpt fn run --dry-run", testPod).
155-
AndRun("kpt live apply valid_path --poll-period 5s --reconcile-timeout 2m"),
155+
AndRun("kpt live apply valid_path --poll-period 5s --reconcile-timeout 2m --context kubecontext --namespace testNamespace"),
156156
},
157157
{
158158
description: "user specifies invalid reconcile timeout and poll period",
@@ -171,7 +171,7 @@ func TestKpt_Deploy(t *testing.T) {
171171
commands: testutil.
172172
CmdRunOut("kpt fn source .", ``).
173173
AndRunOut("kpt fn run --dry-run", testPod).
174-
AndRun("kpt live apply valid_path --poll-period foo --reconcile-timeout bar"),
174+
AndRun("kpt live apply valid_path --poll-period foo --reconcile-timeout bar --context kubecontext --namespace testNamespace"),
175175
},
176176
{
177177
description: "user specifies prune propagation policy and prune timeout",
@@ -190,7 +190,7 @@ func TestKpt_Deploy(t *testing.T) {
190190
commands: testutil.
191191
CmdRunOut("kpt fn source .", ``).
192192
AndRunOut("kpt fn run --dry-run", testPod).
193-
AndRun("kpt live apply valid_path --prune-propagation-policy Orphan --prune-timeout 2m"),
193+
AndRun("kpt live apply valid_path --prune-propagation-policy Orphan --prune-timeout 2m --context kubecontext --namespace testNamespace"),
194194
},
195195
{
196196
description: "user specifies invalid prune propagation policy and prune timeout",
@@ -209,7 +209,7 @@ func TestKpt_Deploy(t *testing.T) {
209209
commands: testutil.
210210
CmdRunOut("kpt fn source .", ``).
211211
AndRunOut("kpt fn run --dry-run", testPod).
212-
AndRun("kpt live apply valid_path --prune-propagation-policy foo --prune-timeout bar"),
212+
AndRun("kpt live apply valid_path --prune-propagation-policy foo --prune-timeout bar --context kubecontext --namespace testNamespace"),
213213
},
214214
}
215215
for _, test := range tests {
@@ -385,19 +385,19 @@ func TestKpt_Cleanup(t *testing.T) {
385385
{
386386
description: "valid user specified applyDir w/o template resource",
387387
applyDir: "valid_path",
388-
commands: testutil.CmdRunErr("kpt live destroy valid_path", errors.New("BUG")),
388+
commands: testutil.CmdRunErr("kpt live destroy valid_path --context kubecontext --namespace testNamespace", errors.New("BUG")),
389389
shouldErr: true,
390390
},
391391
{
392392
description: "valid user specified applyDir w/ template resource (emulated)",
393393
applyDir: "valid_path",
394-
commands: testutil.CmdRun("kpt live destroy valid_path"),
394+
commands: testutil.CmdRun("kpt live destroy valid_path --context kubecontext --namespace testNamespace"),
395395
},
396396
{
397397
description: "unspecified applyDir",
398398
commands: testutil.
399-
CmdRunOut("kpt live init .kpt-hydrated", "").
400-
AndRun("kpt live destroy .kpt-hydrated"),
399+
CmdRunOut("kpt live init .kpt-hydrated --context kubecontext --namespace testNamespace", "").
400+
AndRun("kpt live destroy .kpt-hydrated --context kubecontext --namespace testNamespace"),
401401
},
402402
}
403403
for _, test := range tests {
@@ -808,7 +808,7 @@ func TestKpt_GetApplyDir(t *testing.T) {
808808
{
809809
description: "unspecified applyDir",
810810
expected: ".kpt-hydrated",
811-
commands: testutil.CmdRunOut("kpt live init .kpt-hydrated", ""),
811+
commands: testutil.CmdRunOut("kpt live init .kpt-hydrated --context kubecontext --namespace testNamespace", ""),
812812
},
813813
{
814814
description: "unspecified applyDir with specified inventory-id and namespace",
@@ -819,7 +819,7 @@ func TestKpt_GetApplyDir(t *testing.T) {
819819
},
820820
},
821821
expected: ".kpt-hydrated",
822-
commands: testutil.CmdRunOut("kpt live init .kpt-hydrated --inventory-id 1a23bcde-4f56-7891-a2bc-de34fabcde5f6 --namespace foo", ""),
822+
commands: testutil.CmdRunOut("kpt live init .kpt-hydrated --inventory-id 1a23bcde-4f56-7891-a2bc-de34fabcde5f6 --context kubecontext --namespace foo", ""),
823823
},
824824
{
825825
description: "existing template resource in .kpt-hydrated",
@@ -1126,11 +1126,35 @@ func TestVersionCheck(t *testing.T) {
11261126
}
11271127
}
11281128

1129+
func TestNonEmptyKubeconfig(t *testing.T) {
1130+
commands := testutil.CmdRunOut("kpt fn source .", ``).
1131+
AndRunOut("kpt fn run --dry-run", testPod).
1132+
AndRun("kpt live apply valid_path --context kubecontext --kubeconfig testConfigPath --namespace testNamespace")
1133+
1134+
testutil.Run(t, "", func(t *testutil.T) {
1135+
t.Override(&util.DefaultExecCommand, commands)
1136+
k := NewDeployer(&kptConfig{config: "testConfigPath"}, nil, &latestV1.KptDeploy{
1137+
Dir: ".",
1138+
Live: latestV1.KptLive{
1139+
Apply: latestV1.KptApplyInventory{
1140+
Dir: "valid_path",
1141+
},
1142+
},
1143+
})
1144+
os.Mkdir(k.Live.Apply.Dir, 0755)
1145+
defer os.RemoveAll(k.Live.Apply.Dir)
1146+
_, err := k.Deploy(context.Background(), ioutil.Discard, []graph.Artifact{})
1147+
t.CheckNoError(err)
1148+
})
1149+
}
1150+
11291151
type kptConfig struct {
11301152
runcontext.RunContext // Embedded to provide the default values.
11311153
workingDir string
1154+
config string
11321155
}
11331156

11341157
func (c *kptConfig) WorkingDir() string { return c.workingDir }
11351158
func (c *kptConfig) GetKubeContext() string { return kubectl.TestKubeContext }
11361159
func (c *kptConfig) GetKubeNamespace() string { return kubectl.TestNamespace }
1160+
func (c *kptConfig) GetKubeConfig() string { return c.config }

0 commit comments

Comments
 (0)