Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions cli/command/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ type CreateOptions struct {
Description string
Docker map[string]string
From string

// Deprecated
DefaultStackOrchestrator string
// Deprecated
Kubernetes map[string]string
}

func longCreateDescription() string {
Expand Down Expand Up @@ -53,14 +48,15 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
}
flags := cmd.Flags()
flags.StringVar(&opts.Description, "description", "", "Description of the context")
flags.StringVar(
&opts.DefaultStackOrchestrator,
flags.String(
"default-stack-orchestrator", "",
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)",
)
flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil)
flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil)
flags.MarkDeprecated("default-stack-orchestrator", "option will be ignored")
flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint")
flags.StringToStringVar(&opts.Kubernetes, "kubernetes", nil, "set the kubernetes endpoint")
flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
flags.SetAnnotation("kubernetes", "kubernetes", nil)
flags.SetAnnotation("kubernetes", "deprecated", nil)
flags.MarkDeprecated("kubernetes", "option will be ignored")
Expand All @@ -76,7 +72,7 @@ func RunCreate(cli command.Cli, o *CreateOptions) error {
return err
}
switch {
case o.From == "" && o.Docker == nil && o.Kubernetes == nil:
case o.From == "" && o.Docker == nil:
err = createFromExistingContext(s, cli.CurrentContext(), o)
case o.From != "":
err = createFromExistingContext(s, o.From, o)
Expand Down Expand Up @@ -132,8 +128,8 @@ func checkContextNameForCreation(s store.Reader, name string) error {
}

func createFromExistingContext(s store.ReaderWriter, fromContextName string, o *CreateOptions) error {
if len(o.Docker) != 0 || len(o.Kubernetes) != 0 {
return errors.New("cannot use --docker or --kubernetes flags when --from is set")
if len(o.Docker) != 0 {
return errors.New("cannot use --docker flag when --from is set")
}
reader := store.Export(fromContextName, &descriptionDecorator{
Reader: s,
Expand Down
29 changes: 0 additions & 29 deletions cli/command/context/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,6 @@ func TestCreate(t *testing.T) {
},
expecterErr: `unable to parse docker host`,
},
{
options: CreateOptions{
Name: "invalid-orchestrator",
DefaultStackOrchestrator: "invalid",
},
expecterErr: "",
},
{
options: CreateOptions{
Name: "orchestrator-all-no-endpoint",
DefaultStackOrchestrator: "all",
Docker: map[string]string{},
},
expecterErr: "",
},
}
for _, tc := range tests {
tc := tc
Expand All @@ -129,19 +114,6 @@ func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", n), cli.ErrBuffer().String())
}

func TestCreateOrchestratorSwarm(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()

err := RunCreate(cli, &CreateOptions{
Name: "test",
DefaultStackOrchestrator: "swarm",
Docker: map[string]string{},
})
assert.NilError(t, err)
assertContextCreateLogging(t, cli, "test")
}

func TestCreateOrchestratorEmpty(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
Expand All @@ -160,7 +132,6 @@ func TestCreateFromContext(t *testing.T) {
description string
expectedDescription string
docker map[string]string
kubernetes map[string]string
}{
{
name: "no-override",
Expand Down
15 changes: 2 additions & 13 deletions cli/command/context/export-import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"testing"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams"
"gotest.tools/v3/assert"
)
Expand All @@ -20,7 +19,7 @@ func TestExportImportWithFile(t *testing.T) {
contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli)
createTestContext(t, cli, "test")
cli.ErrBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{
ContextName: "test",
Expand All @@ -46,7 +45,7 @@ func TestExportImportWithFile(t *testing.T) {
func TestExportImportPipe(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContext(t, cli)
createTestContext(t, cli, "test")
cli.ErrBuffer().Reset()
cli.OutBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{
Expand Down Expand Up @@ -83,13 +82,3 @@ func TestExportExistingFile(t *testing.T) {
err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
assert.Assert(t, os.IsExist(err))
}

func createTestContext(t *testing.T, cli command.Cli) {
t.Helper()

err := RunCreate(cli, &CreateOptions{
Name: "test",
Docker: map[string]string{},
})
assert.NilError(t, err)
}
2 changes: 1 addition & 1 deletion cli/command/context/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestInspect(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContext(t, cli, "current")
cli.OutBuffer().Reset()
assert.NilError(t, runInspect(cli, inspectOptions{
refs: []string{"current"},
Expand Down
24 changes: 10 additions & 14 deletions cli/command/context/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@ import (

"github.com/docker/cli/cli/command"
"gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/golden"
)

func createTestContextWithKubeAndSwarm(t *testing.T, cli command.Cli, name string, orchestrator string) {
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
defer revert()
func createTestContext(t *testing.T, cli command.Cli, name string) {
t.Helper()

err := RunCreate(cli, &CreateOptions{
Name: name,
DefaultStackOrchestrator: orchestrator,
Description: "description of " + name,
Kubernetes: map[string]string{keyFrom: "default"},
Docker: map[string]string{keyHost: "https://someswarmserver.example.com"},
Name: name,
Description: "description of " + name,
Docker: map[string]string{keyHost: "https://someswarmserver.example.com"},
})
assert.NilError(t, err)
}

func TestList(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContextWithKubeAndSwarm(t, cli, "unset", "unset")
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
createTestContext(t, cli, "unset")
cli.SetCurrentContext("current")
cli.OutBuffer().Reset()
assert.NilError(t, runList(cli, &listOptions{}))
Expand All @@ -38,8 +34,8 @@ func TestList(t *testing.T) {
func TestListQuiet(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current")
cli.OutBuffer().Reset()
assert.NilError(t, runList(cli, &listOptions{quiet: true}))
Expand Down
18 changes: 9 additions & 9 deletions cli/command/context/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
func TestRemove(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"}))
_, err := cli.ContextStore().GetMetadata("current")
assert.NilError(t, err)
Expand All @@ -27,17 +27,17 @@ func TestRemove(t *testing.T) {
func TestRemoveNotAContext(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"})
assert.ErrorContains(t, err, `context "not-a-context" does not exist`)
}

func TestRemoveCurrent(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"current"})
assert.ErrorContains(t, err, "current: context is in use, set -f flag to force remove")
Expand All @@ -54,8 +54,8 @@ func TestRemoveCurrentForce(t *testing.T) {

cli, cleanup := makeFakeCli(t, withCliConfig(testCfg))
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContext(t, cli, "current")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current")
assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"}))
reloadedConfig, err := config.Load(configDir)
Expand All @@ -66,7 +66,7 @@ func TestRemoveCurrentForce(t *testing.T) {
func TestRemoveDefault(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "other", "all")
createTestContext(t, cli, "other")
cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"default"})
assert.ErrorContains(t, err, `default: context "default" cannot be removed`)
Expand Down
19 changes: 0 additions & 19 deletions cli/command/context/testdata/test-kubeconfig

This file was deleted.

13 changes: 4 additions & 9 deletions cli/command/context/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ type UpdateOptions struct {
Name string
Description string
Docker map[string]string

// Deprecated
DefaultStackOrchestrator string
// Deprecated
Kubernetes map[string]string
}

func longUpdateDescription() string {
Expand Down Expand Up @@ -52,14 +47,14 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
}
flags := cmd.Flags()
flags.StringVar(&opts.Description, "description", "", "Description of the context")
flags.StringVar(
&opts.DefaultStackOrchestrator,
flags.String(
"default-stack-orchestrator", "",
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)",
)
flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil)
flags.MarkDeprecated("default-stack-orchestrator", "option will be ignored")
flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint")
flags.StringToStringVar(&opts.Kubernetes, "kubernetes", nil, "set the kubernetes endpoint")
flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
flags.SetAnnotation("kubernetes", "kubernetes", nil)
flags.SetAnnotation("kubernetes", "deprecated", nil)
flags.MarkDeprecated("kubernetes", "option will be ignored")
Expand Down
7 changes: 3 additions & 4 deletions cli/command/context/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ func TestUpdateDescriptionOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
err := RunCreate(cli, &CreateOptions{
Name: "test",
DefaultStackOrchestrator: "swarm",
Docker: map[string]string{},
Name: "test",
Docker: map[string]string{},
})
assert.NilError(t, err)
cli.OutBuffer().Reset()
Expand All @@ -37,7 +36,7 @@ func TestUpdateDescriptionOnly(t *testing.T) {
func TestUpdateDockerOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "test", "swarm")
createTestContext(t, cli, "test")
assert.NilError(t, RunUpdate(cli, &UpdateOptions{
Name: "test",
Docker: map[string]string{
Expand Down
4 changes: 1 addition & 3 deletions cli/context/store/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
//
// The context store itself has absolutely no knowledge about what a docker endpoint should contain in term of metadata or TLS config.
// Client code is responsible for generating and parsing endpoint metadata and TLS files.
// The multi-endpoints approach of this package allows to combine many different endpoints in the same "context" (e.g., the Docker CLI
// is able for a single context to define both a docker endpoint and a Kubernetes endpoint for the same cluster, and also specify which
// orchestrator to use by default when deploying a compose stack on this cluster).
// The multi-endpoints approach of this package allows to combine many different endpoints in the same "context".
//
// Context IDs are actually SHA256 hashes of the context name, and are there only to avoid dealing with special characters in context names.
package store
10 changes: 2 additions & 8 deletions e2e/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (

func TestContextList(t *testing.T) {
cmd := icmd.Command("docker", "context", "ls")
cmd.Env = append(cmd.Env,
"DOCKER_CONFIG=./testdata/test-dockerconfig",
"KUBECONFIG=./testdata/test-kubeconfig",
)
cmd.Env = append(cmd.Env, "DOCKER_CONFIG=./testdata/test-dockerconfig")
result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
Err: icmd.None,
ExitCode: 0,
Expand All @@ -35,10 +32,7 @@ func TestContextImportNoTLS(t *testing.T) {
icmd.RunCmd(cmd).Assert(t, icmd.Success)

cmd = icmd.Command("docker", "context", "ls")
cmd.Env = append(cmd.Env,
"DOCKER_CONFIG="+d,
"KUBECONFIG=./testdata/test-kubeconfig", // Allows reuse of context-ls.golden
)
cmd.Env = append(cmd.Env, "DOCKER_CONFIG="+d)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "context-ls.golden")
}
Expand Down
20 changes: 0 additions & 20 deletions e2e/context/testdata/test-kubeconfig

This file was deleted.

Loading