opts: move swarm-specific options to a separate package
This prevents users of the CLI that don't implement swarm-related features from depending on the swarm API types. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
4c882e0f6c
commit
ad21055bac
@ -4,12 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/cli/opts/swarmopts"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
is "gotest.tools/v3/assert/cmp"
|
||||||
|
|
||||||
cliopts "github.com/docker/cli/opts"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// fakeConfigAPIClientList is used to let us pass a closure as a
|
// fakeConfigAPIClientList is used to let us pass a closure as a
|
||||||
@ -43,8 +42,8 @@ func (fakeConfigAPIClientList) ConfigUpdate(_ context.Context, _ string, _ swarm
|
|||||||
func TestSetConfigsWithCredSpecAndConfigs(t *testing.T) {
|
func TestSetConfigsWithCredSpecAndConfigs(t *testing.T) {
|
||||||
// we can't directly access the internal fields of the ConfigOpt struct, so
|
// we can't directly access the internal fields of the ConfigOpt struct, so
|
||||||
// we need to let it do the parsing
|
// we need to let it do the parsing
|
||||||
configOpt := &cliopts.ConfigOpt{}
|
configOpt := &swarmopts.ConfigOpt{}
|
||||||
configOpt.Set("bar")
|
assert.Check(t, configOpt.Set("bar"))
|
||||||
opts := &serviceOptions{
|
opts := &serviceOptions{
|
||||||
credentialSpec: credentialSpecOpt{
|
credentialSpec: credentialSpecOpt{
|
||||||
value: &swarm.CredentialSpec{
|
value: &swarm.CredentialSpec{
|
||||||
@ -187,8 +186,8 @@ func TestSetConfigsOnlyCredSpec(t *testing.T) {
|
|||||||
// TestSetConfigsOnlyConfigs verifies setConfigs when only configs (and not a
|
// TestSetConfigsOnlyConfigs verifies setConfigs when only configs (and not a
|
||||||
// CredentialSpec) is needed.
|
// CredentialSpec) is needed.
|
||||||
func TestSetConfigsOnlyConfigs(t *testing.T) {
|
func TestSetConfigsOnlyConfigs(t *testing.T) {
|
||||||
configOpt := &cliopts.ConfigOpt{}
|
configOpt := &swarmopts.ConfigOpt{}
|
||||||
configOpt.Set("bar")
|
assert.Check(t, configOpt.Set("bar"))
|
||||||
opts := &serviceOptions{
|
opts := &serviceOptions{
|
||||||
configs: *configOpt,
|
configs: *configOpt,
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/opts"
|
"github.com/docker/cli/opts"
|
||||||
|
"github.com/docker/cli/opts/swarmopts"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
@ -395,7 +396,7 @@ func convertNetworks(networks opts.NetworkOpt) []swarm.NetworkAttachmentConfig {
|
|||||||
|
|
||||||
type endpointOptions struct {
|
type endpointOptions struct {
|
||||||
mode string
|
mode string
|
||||||
publishPorts opts.PortOpt
|
publishPorts swarmopts.PortOpt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *endpointOptions) ToEndpointSpec() *swarm.EndpointSpec {
|
func (e *endpointOptions) ToEndpointSpec() *swarm.EndpointSpec {
|
||||||
@ -553,8 +554,8 @@ type serviceOptions struct {
|
|||||||
logDriver logDriverOptions
|
logDriver logDriverOptions
|
||||||
|
|
||||||
healthcheck healthCheckOptions
|
healthcheck healthCheckOptions
|
||||||
secrets opts.SecretOpt
|
secrets swarmopts.SecretOpt
|
||||||
configs opts.ConfigOpt
|
configs swarmopts.ConfigOpt
|
||||||
|
|
||||||
isolation string
|
isolation string
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/command/completion"
|
"github.com/docker/cli/cli/command/completion"
|
||||||
"github.com/docker/cli/opts"
|
"github.com/docker/cli/opts"
|
||||||
|
"github.com/docker/cli/opts/swarmopts"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
mounttypes "github.com/docker/docker/api/types/mount"
|
mounttypes "github.com/docker/docker/api/types/mount"
|
||||||
@ -55,7 +56,7 @@ func newUpdateCommand(dockerCLI command.Cli) *cobra.Command {
|
|||||||
flags.Var(newListOptsVar(), flagContainerLabelRemove, "Remove a container label by its key")
|
flags.Var(newListOptsVar(), flagContainerLabelRemove, "Remove a container label by its key")
|
||||||
flags.Var(newListOptsVar(), flagMountRemove, "Remove a mount by its target path")
|
flags.Var(newListOptsVar(), flagMountRemove, "Remove a mount by its target path")
|
||||||
// flags.Var(newListOptsVar().WithValidator(validatePublishRemove), flagPublishRemove, "Remove a published port by its target port")
|
// flags.Var(newListOptsVar().WithValidator(validatePublishRemove), flagPublishRemove, "Remove a published port by its target port")
|
||||||
flags.Var(&opts.PortOpt{}, flagPublishRemove, "Remove a published port by its target port")
|
flags.Var(&swarmopts.PortOpt{}, flagPublishRemove, "Remove a published port by its target port")
|
||||||
flags.Var(newListOptsVar(), flagConstraintRemove, "Remove a constraint")
|
flags.Var(newListOptsVar(), flagConstraintRemove, "Remove a constraint")
|
||||||
flags.Var(newListOptsVar(), flagDNSRemove, "Remove a custom DNS server")
|
flags.Var(newListOptsVar(), flagDNSRemove, "Remove a custom DNS server")
|
||||||
flags.SetAnnotation(flagDNSRemove, "version", []string{"1.25"})
|
flags.SetAnnotation(flagDNSRemove, "version", []string{"1.25"})
|
||||||
@ -804,7 +805,7 @@ func getUpdatedSecrets(ctx context.Context, apiClient client.SecretAPIClient, fl
|
|||||||
}
|
}
|
||||||
|
|
||||||
if flags.Changed(flagSecretAdd) {
|
if flags.Changed(flagSecretAdd) {
|
||||||
values := flags.Lookup(flagSecretAdd).Value.(*opts.SecretOpt).Value()
|
values := flags.Lookup(flagSecretAdd).Value.(*swarmopts.SecretOpt).Value()
|
||||||
|
|
||||||
addSecrets, err := ParseSecrets(ctx, apiClient, values)
|
addSecrets, err := ParseSecrets(ctx, apiClient, values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -852,7 +853,7 @@ func getUpdatedConfigs(ctx context.Context, apiClient client.ConfigAPIClient, fl
|
|||||||
resolveConfigs := []*swarm.ConfigReference{}
|
resolveConfigs := []*swarm.ConfigReference{}
|
||||||
|
|
||||||
if flags.Changed(flagConfigAdd) {
|
if flags.Changed(flagConfigAdd) {
|
||||||
resolveConfigs = append(resolveConfigs, flags.Lookup(flagConfigAdd).Value.(*opts.ConfigOpt).Value()...)
|
resolveConfigs = append(resolveConfigs, flags.Lookup(flagConfigAdd).Value.(*swarmopts.ConfigOpt).Value()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if credSpecConfigNameis non-empty at this point, it means its a new
|
// if credSpecConfigNameis non-empty at this point, it means its a new
|
||||||
@ -1091,7 +1092,7 @@ func updatePorts(flags *pflag.FlagSet, portConfig *[]swarm.PortConfig) error {
|
|||||||
newPorts := []swarm.PortConfig{}
|
newPorts := []swarm.PortConfig{}
|
||||||
|
|
||||||
// Clean current ports
|
// Clean current ports
|
||||||
toRemove := flags.Lookup(flagPublishRemove).Value.(*opts.PortOpt).Value()
|
toRemove := flags.Lookup(flagPublishRemove).Value.(*swarmopts.PortOpt).Value()
|
||||||
portLoop:
|
portLoop:
|
||||||
for _, port := range portSet {
|
for _, port := range portSet {
|
||||||
for _, pConfig := range toRemove {
|
for _, pConfig := range toRemove {
|
||||||
@ -1107,7 +1108,7 @@ portLoop:
|
|||||||
|
|
||||||
// Check to see if there are any conflict in flags.
|
// Check to see if there are any conflict in flags.
|
||||||
if flags.Changed(flagPublishAdd) {
|
if flags.Changed(flagPublishAdd) {
|
||||||
ports := flags.Lookup(flagPublishAdd).Value.(*opts.PortOpt).Value()
|
ports := flags.Lookup(flagPublishAdd).Value.(*swarmopts.PortOpt).Value()
|
||||||
|
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
if _, ok := portSet[portConfigToString(&port)]; ok {
|
if _, ok := portSet[portConfigToString(&port)]; ok {
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/docker/cli/cli/compose/template"
|
"github.com/docker/cli/cli/compose/template"
|
||||||
"github.com/docker/cli/cli/compose/types"
|
"github.com/docker/cli/cli/compose/types"
|
||||||
"github.com/docker/cli/opts"
|
"github.com/docker/cli/opts"
|
||||||
|
"github.com/docker/cli/opts/swarmopts"
|
||||||
"github.com/docker/docker/api/types/versions"
|
"github.com/docker/docker/api/types/versions"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
units "github.com/docker/go-units"
|
units "github.com/docker/go-units"
|
||||||
@ -925,7 +926,7 @@ func toServicePortConfigs(value string) ([]any, error) {
|
|||||||
|
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
// Reuse ConvertPortToPortConfig so that it is consistent
|
// Reuse ConvertPortToPortConfig so that it is consistent
|
||||||
portConfig, err := opts.ConvertPortToPortConfig(nat.Port(key), portBindings)
|
portConfig, err := swarmopts.ConvertPortToPortConfig(nat.Port(key), portBindings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
18
opts/opts_deprecated.go
Normal file
18
opts/opts_deprecated.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package opts
|
||||||
|
|
||||||
|
import "github.com/docker/cli/opts/swarmopts"
|
||||||
|
|
||||||
|
// PortOpt represents a port config in swarm mode.
|
||||||
|
//
|
||||||
|
// Deprecated: use [swarmopts.PortOpt]
|
||||||
|
type PortOpt = swarmopts.PortOpt
|
||||||
|
|
||||||
|
// ConfigOpt is a Value type for parsing configs.
|
||||||
|
//
|
||||||
|
// Deprecated: use [swarmopts.ConfigOpt]
|
||||||
|
type ConfigOpt = swarmopts.ConfigOpt
|
||||||
|
|
||||||
|
// SecretOpt is a Value type for parsing secrets
|
||||||
|
//
|
||||||
|
// Deprecated: use [swarmopts.SecretOpt]
|
||||||
|
type SecretOpt = swarmopts.SecretOpt
|
@ -1,4 +1,4 @@
|
|||||||
package opts
|
package swarmopts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
@ -1,4 +1,4 @@
|
|||||||
package opts
|
package swarmopts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
@ -1,4 +1,4 @@
|
|||||||
package opts
|
package swarmopts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
@ -1,4 +1,4 @@
|
|||||||
package opts
|
package swarmopts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -1,4 +1,4 @@
|
|||||||
package opts
|
package swarmopts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
@ -1,4 +1,4 @@
|
|||||||
package opts
|
package swarmopts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
Loading…
x
Reference in New Issue
Block a user