Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 8750d05

Browse files
authored
Merge pull request #1222 from gtardif/kube_ctx_description
Kube ctx description
2 parents b6b2f29 + 794f638 commit 8750d05

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<!-- If this is a bug fix, make sure your description includes "fixes #xxxx", or "closes #xxxx" -->
55

66
<!-- optional tests
7-
You can add a / mention to run tests executed by default only on main branch :
7+
You can add a / mention to run tests executed by default only on main branch :
8+
* `test-kube` to run Kube E2E tests
89
* `test-aci` to run ACI E2E tests
910
* `test-ecs` to run ECS E2E tests
1011
* `test-windows` to run tests & E2E tests on windows

kube/context.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package kube
2020

2121
import (
22+
"fmt"
23+
2224
"github.com/AlecAivazis/survey/v2/terminal"
2325
"github.com/docker/compose-cli/api/context/store"
2426
"github.com/docker/compose-cli/api/errdefs"
@@ -41,7 +43,7 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
4143
// we use the current kubectl context from a $KUBECONFIG path
4244
return store.KubeContext{
4345
FromEnvironment: cp.FromEnvironment,
44-
}, cp.Description, nil
46+
}, cp.getDescription(), nil
4547
}
4648
user := prompt.User{}
4749
selectContext := func() error {
@@ -67,7 +69,7 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
6769
ContextName: cp.KubeContextName,
6870
KubeconfigPath: cp.KubeConfigPath,
6971
FromEnvironment: cp.FromEnvironment,
70-
}, cp.Description, nil
72+
}, cp.getDescription(), nil
7173
}
7274
err := selectContext()
7375
if err != nil {
@@ -105,5 +107,19 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
105107
ContextName: cp.KubeContextName,
106108
KubeconfigPath: cp.KubeConfigPath,
107109
FromEnvironment: cp.FromEnvironment,
108-
}, cp.Description, nil
110+
}, cp.getDescription(), nil
111+
}
112+
113+
func (cp ContextParams) getDescription() string {
114+
if cp.Description != "" {
115+
return cp.Description
116+
}
117+
if cp.FromEnvironment {
118+
return "From environment variables"
119+
}
120+
configFile := "default kube config"
121+
if cp.KubeConfigPath != "" {
122+
configFile = cp.KubeConfigPath
123+
}
124+
return fmt.Sprintf("%s (in %s)", cp.KubeContextName, configFile)
109125
}

kube/context_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// +build kube
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kube
20+
21+
import (
22+
"testing"
23+
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func TestContextDescriptionIfEnvVar(t *testing.T) {
28+
cp := ContextParams{
29+
FromEnvironment: true,
30+
}
31+
description := cp.getDescription()
32+
assert.Equal(t, description, "From environment variables")
33+
}
34+
35+
func TestContextDescriptionIfProvided(t *testing.T) {
36+
cp := ContextParams{
37+
Description: "custom description",
38+
FromEnvironment: true,
39+
}
40+
description := cp.getDescription()
41+
assert.Equal(t, description, "custom description")
42+
}
43+
44+
func TestContextDescriptionIfConfigFile(t *testing.T) {
45+
cp := ContextParams{
46+
KubeContextName: "my-context",
47+
KubeConfigPath: "~/.kube/config",
48+
}
49+
description := cp.getDescription()
50+
assert.Equal(t, description, "my-context (in ~/.kube/config)")
51+
}
52+
func TestContextDescriptionIfDefaultConfigFile(t *testing.T) {
53+
cp := ContextParams{
54+
KubeContextName: "my-context",
55+
}
56+
description := cp.getDescription()
57+
assert.Equal(t, description, "my-context (in default kube config)")
58+
}

kube/e2e/compose_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func TestComposeUp(t *testing.T) {
6565
res := c.RunDockerCmd("context", "create", "kubernetes", "--kubeconfig", kubeconfig, "--kubecontext", kubeContextName, dockerContextName)
6666
res.Assert(t, icmd.Expected{Out: fmt.Sprintf("Successfully created kube context %q", dockerContextName)})
6767
c.RunDockerCmd("context", "use", dockerContextName)
68+
res = c.RunDockerCmd("context", "ls")
69+
res.Assert(t, icmd.Expected{Out: fmt.Sprintf("%s * kube %s (in %s)", dockerContextName, kubeContextName, kubeconfig)})
6870
})
6971

7072
t.Run("up", func(t *testing.T) {

0 commit comments

Comments
 (0)