Skip to content
This repository was archived by the owner on Jul 26, 2022. It is now read-only.

Commit 8d8a634

Browse files
authoredAug 28, 2019
Change context upon namespace creation (#9)
* Change context upon namespace creation
1 parent 79f7ac1 commit 8d8a634

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed
 

‎cmd/create.go

+6
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ func createOrUpdate(projectDir, namespace string, dryRun, create bool, rawValues
154154
logger.Error(err)
155155
return
156156
}
157+
158+
err = cluster.SetContextNamespace(cmd, namespace)
159+
if err != nil {
160+
logger.Warn("Unable to set namespace in context.")
161+
}
162+
157163
if dryRun {
158164
logger.Infof("Dry-run is complete, this is what will be deployed.")
159165
} else {

‎internal/cluster/kubectl.go

+6
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ func ListEnvironments(cmd commands.Command) []string {
5151
}
5252
return namespaces
5353
}
54+
55+
// SetContextNamespace triggers a command to set kubectl context to the new namespace
56+
func SetContextNamespace(cmd commands.Command, namespace string) error {
57+
_, err := cmd.Exec("update-context", "kubectl", "config", "set-context", "--current", "--namespace", namespace)
58+
return err
59+
}

‎internal/cluster/kubectl_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,36 @@ test Active 10d
143143
})
144144
}
145145
}
146+
147+
func TestSetContextNamespace(t *testing.T) {
148+
cases := []struct {
149+
name string
150+
cmd commands.Command
151+
namespace string
152+
wantErr bool
153+
}{
154+
{
155+
name: "any-name",
156+
cmd: commands.CmdMock{CmdExecuted: "kubectl config set-context --current --namespace any-name", ReturnError: false},
157+
namespace: "any-name",
158+
wantErr: false,
159+
},
160+
{
161+
name: "no-name",
162+
cmd: commands.CmdMock{CmdExecuted: "kubectl config set-context --current --namespace", ReturnError: true},
163+
namespace: "",
164+
wantErr: true,
165+
},
166+
}
167+
for _, tt := range cases {
168+
t.Run(tt.name, func(t *testing.T) {
169+
err := SetContextNamespace(tt.cmd, tt.namespace)
170+
if err != nil && !tt.wantErr {
171+
t.Errorf("%s test failed. \nGot: %v \nExpected: %v", tt.name, err, tt.wantErr)
172+
}
173+
if err == nil && tt.wantErr {
174+
t.Errorf("%s test failed.\nExpected: %v but got no error", tt.name, tt.wantErr)
175+
}
176+
})
177+
}
178+
}

0 commit comments

Comments
 (0)
This repository has been archived.