Merge pull request #1222 from gtardif/kube_ctx_description

Kube ctx description
This commit is contained in:
Guillaume Tardif 2021-02-03 10:40:38 +01:00 committed by GitHub
commit 8750d053a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 4 deletions

View File

@ -5,6 +5,7 @@
<!-- optional tests <!-- optional tests
You can add a / mention to run tests executed by default only on main branch : You can add a / mention to run tests executed by default only on main branch :
* `test-kube` to run Kube E2E tests
* `test-aci` to run ACI E2E tests * `test-aci` to run ACI E2E tests
* `test-ecs` to run ECS E2E tests * `test-ecs` to run ECS E2E tests
* `test-windows` to run tests & E2E tests on windows * `test-windows` to run tests & E2E tests on windows

View File

@ -19,6 +19,8 @@
package kube package kube
import ( import (
"fmt"
"github.com/AlecAivazis/survey/v2/terminal" "github.com/AlecAivazis/survey/v2/terminal"
"github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/api/errdefs" "github.com/docker/compose-cli/api/errdefs"
@ -41,7 +43,7 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
// we use the current kubectl context from a $KUBECONFIG path // we use the current kubectl context from a $KUBECONFIG path
return store.KubeContext{ return store.KubeContext{
FromEnvironment: cp.FromEnvironment, FromEnvironment: cp.FromEnvironment,
}, cp.Description, nil }, cp.getDescription(), nil
} }
user := prompt.User{} user := prompt.User{}
selectContext := func() error { selectContext := func() error {
@ -67,7 +69,7 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
ContextName: cp.KubeContextName, ContextName: cp.KubeContextName,
KubeconfigPath: cp.KubeConfigPath, KubeconfigPath: cp.KubeConfigPath,
FromEnvironment: cp.FromEnvironment, FromEnvironment: cp.FromEnvironment,
}, cp.Description, nil }, cp.getDescription(), nil
} }
err := selectContext() err := selectContext()
if err != nil { if err != nil {
@ -105,5 +107,19 @@ func (cp ContextParams) CreateContextData() (interface{}, string, error) {
ContextName: cp.KubeContextName, ContextName: cp.KubeContextName,
KubeconfigPath: cp.KubeConfigPath, KubeconfigPath: cp.KubeConfigPath,
FromEnvironment: cp.FromEnvironment, FromEnvironment: cp.FromEnvironment,
}, cp.Description, nil }, cp.getDescription(), nil
}
func (cp ContextParams) getDescription() string {
if cp.Description != "" {
return cp.Description
}
if cp.FromEnvironment {
return "From environment variables"
}
configFile := "default kube config"
if cp.KubeConfigPath != "" {
configFile = cp.KubeConfigPath
}
return fmt.Sprintf("%s (in %s)", cp.KubeContextName, configFile)
} }

58
kube/context_test.go Normal file
View File

@ -0,0 +1,58 @@
// +build kube
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kube
import (
"testing"
"gotest.tools/v3/assert"
)
func TestContextDescriptionIfEnvVar(t *testing.T) {
cp := ContextParams{
FromEnvironment: true,
}
description := cp.getDescription()
assert.Equal(t, description, "From environment variables")
}
func TestContextDescriptionIfProvided(t *testing.T) {
cp := ContextParams{
Description: "custom description",
FromEnvironment: true,
}
description := cp.getDescription()
assert.Equal(t, description, "custom description")
}
func TestContextDescriptionIfConfigFile(t *testing.T) {
cp := ContextParams{
KubeContextName: "my-context",
KubeConfigPath: "~/.kube/config",
}
description := cp.getDescription()
assert.Equal(t, description, "my-context (in ~/.kube/config)")
}
func TestContextDescriptionIfDefaultConfigFile(t *testing.T) {
cp := ContextParams{
KubeContextName: "my-context",
}
description := cp.getDescription()
assert.Equal(t, description, "my-context (in default kube config)")
}

View File

@ -65,6 +65,8 @@ func TestComposeUp(t *testing.T) {
res := c.RunDockerCmd("context", "create", "kubernetes", "--kubeconfig", kubeconfig, "--kubecontext", kubeContextName, dockerContextName) res := c.RunDockerCmd("context", "create", "kubernetes", "--kubeconfig", kubeconfig, "--kubecontext", kubeContextName, dockerContextName)
res.Assert(t, icmd.Expected{Out: fmt.Sprintf("Successfully created kube context %q", dockerContextName)}) res.Assert(t, icmd.Expected{Out: fmt.Sprintf("Successfully created kube context %q", dockerContextName)})
c.RunDockerCmd("context", "use", dockerContextName) c.RunDockerCmd("context", "use", dockerContextName)
res = c.RunDockerCmd("context", "ls")
res.Assert(t, icmd.Expected{Out: fmt.Sprintf("%s * kube %s (in %s)", dockerContextName, kubeContextName, kubeconfig)})
}) })
t.Run("up", func(t *testing.T) { t.Run("up", func(t *testing.T) {