remove obsolete mutli-orchestrator support

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof
2022-02-22 13:46:35 +01:00
parent 1d48749c1c
commit 193ede9b12
44 changed files with 186 additions and 581 deletions

View File

@ -3,7 +3,6 @@ package context
import (
"bytes"
"fmt"
"github.com/sirupsen/logrus"
"text/tabwriter"
"github.com/docker/cli/cli"
@ -59,10 +58,12 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
"default-stack-orchestrator", "",
"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.SetAnnotation("kubernetes", "kubernetes", nil)
flags.SetAnnotation("kubernetes", "deprecated", nil)
flags.MarkDeprecated("kubernetes", "option will be ignored")
flags.StringVar(&opts.From, "from", "", "create context from a named context")
return cmd
}
@ -70,20 +71,17 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
// RunCreate creates a Docker context
func RunCreate(cli command.Cli, o *CreateOptions) error {
s := cli.ContextStore()
if err := checkContextNameForCreation(s, o.Name); err != nil {
return err
}
stackOrchestrator, err := command.NormalizeOrchestrator(o.DefaultStackOrchestrator)
err := checkContextNameForCreation(s, o.Name)
if err != nil {
return errors.Wrap(err, "unable to parse default-stack-orchestrator")
return err
}
switch {
case o.From == "" && o.Docker == nil && o.Kubernetes == nil:
err = createFromExistingContext(s, cli.CurrentContext(), stackOrchestrator, o)
err = createFromExistingContext(s, cli.CurrentContext(), o)
case o.From != "":
err = createFromExistingContext(s, o.From, stackOrchestrator, o)
err = createFromExistingContext(s, o.From, o)
default:
err = createNewContext(o, stackOrchestrator, cli, s)
err = createNewContext(o, cli, s)
}
if err == nil {
fmt.Fprintln(cli.Out(), o.Name)
@ -92,11 +90,11 @@ func RunCreate(cli command.Cli, o *CreateOptions) error {
return err
}
func createNewContext(o *CreateOptions, stackOrchestrator command.Orchestrator, cli command.Cli, s store.Writer) error {
func createNewContext(o *CreateOptions, cli command.Cli, s store.Writer) error {
if o.Docker == nil {
return errors.New("docker endpoint configuration is required")
}
contextMetadata := newContextMetadata(stackOrchestrator, o)
contextMetadata := newContextMetadata(o)
contextTLSData := store.ContextTLSData{
Endpoints: make(map[string]store.EndpointTLSData),
}
@ -108,10 +106,7 @@ func createNewContext(o *CreateOptions, stackOrchestrator command.Orchestrator,
if dockerTLS != nil {
contextTLSData.Endpoints[docker.DockerEndpoint] = *dockerTLS
}
if len(o.Kubernetes) != 0 {
logrus.Warn("kubernetes orchestrator is deprecated")
}
if err := validateEndpointsAndOrchestrator(contextMetadata); err != nil {
if err := validateEndpoints(contextMetadata); err != nil {
return err
}
if err := s.CreateOrUpdate(contextMetadata); err != nil {
@ -136,26 +131,24 @@ func checkContextNameForCreation(s store.Reader, name string) error {
return nil
}
func createFromExistingContext(s store.ReaderWriter, fromContextName string, stackOrchestrator command.Orchestrator, o *CreateOptions) 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")
}
reader := store.Export(fromContextName, &descriptionAndOrchestratorStoreDecorator{
Reader: s,
description: o.Description,
orchestrator: stackOrchestrator,
reader := store.Export(fromContextName, &descriptionDecorator{
Reader: s,
description: o.Description,
})
defer reader.Close()
return store.Import(o.Name, s, reader)
}
type descriptionAndOrchestratorStoreDecorator struct {
type descriptionDecorator struct {
store.Reader
description string
orchestrator command.Orchestrator
description string
}
func (d *descriptionAndOrchestratorStoreDecorator) GetMetadata(name string) (store.Metadata, error) {
func (d *descriptionDecorator) GetMetadata(name string) (store.Metadata, error) {
c, err := d.Reader.GetMetadata(name)
if err != nil {
return c, err
@ -167,19 +160,15 @@ func (d *descriptionAndOrchestratorStoreDecorator) GetMetadata(name string) (sto
if d.description != "" {
typedContext.Description = d.description
}
if d.orchestrator != command.Orchestrator("") {
typedContext.StackOrchestrator = d.orchestrator
}
c.Metadata = typedContext
return c, nil
}
func newContextMetadata(stackOrchestrator command.Orchestrator, o *CreateOptions) store.Metadata {
func newContextMetadata(o *CreateOptions) store.Metadata {
return store.Metadata{
Endpoints: make(map[string]interface{}),
Metadata: command.DockerContext{
Description: o.Description,
StackOrchestrator: stackOrchestrator,
Description: o.Description,
},
Name: o.Name,
}

View File

@ -12,7 +12,6 @@ import (
"github.com/docker/cli/cli/context/store"
"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
"gotest.tools/v3/env"
)
func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func()) {
@ -33,8 +32,7 @@ func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func
},
},
Metadata: command.DockerContext{
Description: "",
StackOrchestrator: command.OrchestratorSwarm,
Description: "",
},
Name: command.DefaultContextName,
},
@ -59,7 +57,7 @@ func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
}
}
func TestCreateInvalids(t *testing.T) {
func TestCreate(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
@ -102,7 +100,7 @@ func TestCreateInvalids(t *testing.T) {
Name: "invalid-orchestrator",
DefaultStackOrchestrator: "invalid",
},
expecterErr: `specified orchestrator "invalid" is invalid, please use either kubernetes, swarm or all`,
expecterErr: "",
},
{
options: CreateOptions{
@ -158,37 +156,25 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
func TestCreateFromContext(t *testing.T) {
cases := []struct {
name string
description string
orchestrator string
expectedDescription string
docker map[string]string
kubernetes map[string]string
expectedOrchestrator command.Orchestrator
name string
description string
expectedDescription string
docker map[string]string
kubernetes map[string]string
}{
{
name: "no-override",
expectedDescription: "original description",
expectedOrchestrator: command.OrchestratorSwarm,
name: "no-override",
expectedDescription: "original description",
},
{
name: "override-description",
description: "new description",
expectedDescription: "new description",
expectedOrchestrator: command.OrchestratorSwarm,
},
{
name: "override-orchestrator",
orchestrator: "kubernetes",
expectedDescription: "original description",
expectedOrchestrator: command.OrchestratorKubernetes,
name: "override-description",
description: "new description",
expectedDescription: "new description",
},
}
cli, cleanup := makeFakeCli(t)
defer cleanup()
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
defer revert()
cli.ResetOutputBuffers()
assert.NilError(t, RunCreate(cli, &CreateOptions{
Name: "original",
@ -196,10 +182,6 @@ func TestCreateFromContext(t *testing.T) {
Docker: map[string]string{
keyHost: "tcp://42.42.42.42:2375",
},
Kubernetes: map[string]string{
keyFrom: "default",
},
DefaultStackOrchestrator: "swarm",
}))
assertContextCreateLogging(t, cli, "original")
@ -210,10 +192,6 @@ func TestCreateFromContext(t *testing.T) {
Docker: map[string]string{
keyHost: "tcp://24.24.24.24:2375",
},
Kubernetes: map[string]string{
keyFrom: "default",
},
DefaultStackOrchestrator: "swarm",
}))
assertContextCreateLogging(t, cli, "dummy")
@ -224,12 +202,10 @@ func TestCreateFromContext(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
cli.ResetOutputBuffers()
err := RunCreate(cli, &CreateOptions{
From: "original",
Name: c.name,
Description: c.description,
DefaultStackOrchestrator: c.orchestrator,
Docker: c.docker,
Kubernetes: c.kubernetes,
From: "original",
Name: c.name,
Description: c.description,
Docker: c.docker,
})
assert.NilError(t, err)
assertContextCreateLogging(t, cli, c.name)
@ -240,7 +216,6 @@ func TestCreateFromContext(t *testing.T) {
dockerEndpoint, err := docker.EndpointFromContext(newContext)
assert.NilError(t, err)
assert.Equal(t, newContextTyped.Description, c.expectedDescription)
assert.Equal(t, newContextTyped.StackOrchestrator, c.expectedOrchestrator)
assert.Equal(t, dockerEndpoint.Host, "tcp://42.42.42.42:2375")
})
}
@ -248,29 +223,24 @@ func TestCreateFromContext(t *testing.T) {
func TestCreateFromCurrent(t *testing.T) {
cases := []struct {
name string
description string
orchestrator string
expectedDescription string
expectedOrchestrator command.Orchestrator
name string
description string
orchestrator string
expectedDescription string
}{
{
name: "no-override",
expectedDescription: "original description",
expectedOrchestrator: command.OrchestratorSwarm,
name: "no-override",
expectedDescription: "original description",
},
{
name: "override-description",
description: "new description",
expectedDescription: "new description",
expectedOrchestrator: command.OrchestratorSwarm,
name: "override-description",
description: "new description",
expectedDescription: "new description",
},
}
cli, cleanup := makeFakeCli(t)
defer cleanup()
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
defer revert()
cli.ResetOutputBuffers()
assert.NilError(t, RunCreate(cli, &CreateOptions{
Name: "original",
@ -278,10 +248,6 @@ func TestCreateFromCurrent(t *testing.T) {
Docker: map[string]string{
keyHost: "tcp://42.42.42.42:2375",
},
Kubernetes: map[string]string{
keyFrom: "default",
},
DefaultStackOrchestrator: "swarm",
}))
assertContextCreateLogging(t, cli, "original")
@ -292,9 +258,8 @@ func TestCreateFromCurrent(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
cli.ResetOutputBuffers()
err := RunCreate(cli, &CreateOptions{
Name: c.name,
Description: c.description,
DefaultStackOrchestrator: c.orchestrator,
Name: c.name,
Description: c.description,
})
assert.NilError(t, err)
assertContextCreateLogging(t, cli, c.name)
@ -305,7 +270,6 @@ func TestCreateFromCurrent(t *testing.T) {
dockerEndpoint, err := docker.EndpointFromContext(newContext)
assert.NilError(t, err)
assert.Equal(t, newContextTyped.Description, c.expectedDescription)
assert.Equal(t, newContextTyped.StackOrchestrator, c.expectedOrchestrator)
assert.Equal(t, dockerEndpoint.Host, "tcp://42.42.42.42:2375")
})
}

View File

@ -59,11 +59,10 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
meta.Description = "Current DOCKER_HOST based configuration"
}
desc := formatter.ClientContext{
Name: rawMeta.Name,
Current: rawMeta.Name == curContext,
Description: meta.Description,
StackOrchestrator: string(meta.StackOrchestrator),
DockerEndpoint: dockerEndpoint.Host,
Name: rawMeta.Name,
Current: rawMeta.Name == curContext,
Description: meta.Description,
DockerEndpoint: dockerEndpoint.Host,
}
contexts = append(contexts, &desc)
}

View File

@ -2,8 +2,7 @@
{
"Name": "current",
"Metadata": {
"Description": "description of current",
"StackOrchestrator": "all"
"Description": "description of current"
},
"Endpoints": {
"docker": {

View File

@ -1,5 +1,5 @@
NAME DESCRIPTION DOCKER ENDPOINT ORCHESTRATOR
current * description of current https://someswarmserver.example.com all
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
other description of other https://someswarmserver.example.com all
unset description of unset https://someswarmserver.example.com
NAME DESCRIPTION DOCKER ENDPOINT
current * description of current https://someswarmserver.example.com
default Current DOCKER_HOST based configuration unix:///var/run/docker.sock
other description of other https://someswarmserver.example.com
unset description of unset https://someswarmserver.example.com

View File

@ -3,7 +3,6 @@ package context
import (
"bytes"
"fmt"
"github.com/sirupsen/logrus"
"text/tabwriter"
"github.com/docker/cli/cli"
@ -58,10 +57,12 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
"default-stack-orchestrator", "",
"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.SetAnnotation("kubernetes", "kubernetes", nil)
flags.SetAnnotation("kubernetes", "deprecated", nil)
flags.MarkDeprecated("kubernetes", "option will be ignored")
return cmd
}
@ -79,13 +80,6 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
if err != nil {
return err
}
if o.DefaultStackOrchestrator != "" {
stackOrchestrator, err := command.NormalizeOrchestrator(o.DefaultStackOrchestrator)
if err != nil {
return errors.Wrap(err, "unable to parse default-stack-orchestrator")
}
dockerContext.StackOrchestrator = stackOrchestrator
}
if o.Description != "" {
dockerContext.Description = o.Description
}
@ -102,10 +96,7 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
c.Endpoints[docker.DockerEndpoint] = dockerEP
tlsDataToReset[docker.DockerEndpoint] = dockerTLS
}
if len(o.Kubernetes) != 0 {
logrus.Warn("kubernetes orchestrator is deprecated")
}
if err := validateEndpointsAndOrchestrator(c); err != nil {
if err := validateEndpoints(c); err != nil {
return err
}
if err := s.CreateOrUpdate(c); err != nil {
@ -122,7 +113,7 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
return nil
}
func validateEndpointsAndOrchestrator(c store.Metadata) error {
func validateEndpoints(c store.Metadata) error {
_, err := command.GetDockerContext(c)
return err
}

View File

@ -28,7 +28,6 @@ func TestUpdateDescriptionOnly(t *testing.T) {
assert.NilError(t, err)
dc, err := command.GetDockerContext(c)
assert.NilError(t, err)
assert.Equal(t, dc.StackOrchestrator, command.OrchestratorSwarm)
assert.Equal(t, dc.Description, "description")
assert.Equal(t, "test\n", cli.OutBuffer().String())
@ -49,7 +48,6 @@ func TestUpdateDockerOnly(t *testing.T) {
assert.NilError(t, err)
dc, err := command.GetDockerContext(c)
assert.NilError(t, err)
assert.Equal(t, dc.StackOrchestrator, command.OrchestratorSwarm)
assert.Equal(t, dc.Description, "description of test")
assert.Check(t, cmp.Contains(c.Endpoints, docker.DockerEndpoint))
assert.Equal(t, c.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host, "tcp://some-host")