From e2c0874dbb29a3b8fe17ff2c1823073009b4a811 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sat, 20 Nov 2021 16:09:25 +0800 Subject: [PATCH 1/4] fix: Remove call to ContainerExecStart when execing This call seems to be unnecessary and removing it allows usage with Podman. See https://github.com/docker/cli/blob/b0343d9104a1794c3252fa45b985710d6b6afc05/cli/command/container/exec.go#L111 --- pkg/runtimes/docker/node.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pkg/runtimes/docker/node.go b/pkg/runtimes/docker/node.go index 820ddc581..eb4fcd291 100644 --- a/pkg/runtimes/docker/node.go +++ b/pkg/runtimes/docker/node.go @@ -395,16 +395,13 @@ func executeInNode(ctx context.Context, node *k3d.Node, cmd []string, stdin io.R } execConnection, err := docker.ContainerExecAttach(ctx, exec.ID, types.ExecStartCheck{ - Tty: true, + // Don't use tty true when piping stdin. + Tty: !attachStdin, }) if err != nil { return nil, fmt.Errorf("docker failed to attach to exec process in node '%s': %w", node.Name, err) } - if err := docker.ContainerExecStart(ctx, exec.ID, types.ExecStartCheck{Tty: true}); err != nil { - return nil, fmt.Errorf("docker failed to start exec process in node '%s': %w", node.Name, err) - } - // If we need to write to stdin pipe, start a new goroutine that writes the stream to stdin if stdin != nil { go func() { From 873865744ea13e85da9fb690c6925643560c907e Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sat, 20 Nov 2021 16:09:44 +0800 Subject: [PATCH 2/4] feat: Allow creating the registry on user-defined networks This might be useful when a user wants to use a different network to run the registry, especially for Podman users. This patch avoids adding such a flag to clusterCreate to avoid polluting the arguments for that command. A better long-term solution would be to create a new network for the registry. --- cmd/registry/registryCreate.go | 9 ++++++--- pkg/client/cluster.go | 2 +- pkg/client/registry.go | 6 +++++- pkg/types/defaults.go | 3 +++ pkg/types/registry.go | 13 +++++++------ 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cmd/registry/registryCreate.go b/cmd/registry/registryCreate.go index 3eaa42852..81604c5d8 100644 --- a/cmd/registry/registryCreate.go +++ b/cmd/registry/registryCreate.go @@ -41,8 +41,9 @@ type regCreatePreProcessedFlags struct { } type regCreateFlags struct { - Image string - NoHelp bool + Image string + DefaultNetwork string + NoHelp bool } var helptext string = `# You can now use the registry like this (example): @@ -103,6 +104,8 @@ func NewCmdRegistryCreate() *cobra.Command { cmd.Flags().StringVarP(&ppFlags.Port, "port", "p", "random", "Select which port the registry should be listening on on your machine (localhost) (Format: `[HOST:]HOSTPORT`)\n - Example: `k3d registry create --port 0.0.0.0:5111`") + cmd.Flags().StringVar(&flags.DefaultNetwork, "default-network", k3d.DefaultNetwork, "Specify the default network connected to the registry (bridge)") + cmd.Flags().BoolVar(&flags.NoHelp, "no-help", false, "Disable the help text (How-To use the registry)") // done @@ -135,5 +138,5 @@ func parseCreateRegistryCmd(cmd *cobra.Command, args []string, flags *regCreateF registryName = fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, args[0]) } - return &k3d.Registry{Host: registryName, Image: flags.Image, ExposureOpts: *exposePort}, clusters + return &k3d.Registry{Host: registryName, Image: flags.Image, ExposureOpts: *exposePort, DefaultNetwork: flags.DefaultNetwork}, clusters } diff --git a/pkg/client/cluster.go b/pkg/client/cluster.go index b06d73aa5..5063c7c32 100644 --- a/pkg/client/cluster.go +++ b/pkg/client/cluster.go @@ -583,7 +583,7 @@ func ClusterDelete(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus if net == cluster.Network.Name { continue } - if net == "bridge" || net == "host" { + if net == k3d.DefaultNetwork || net == "host" { continue } l.Log().Tracef("net: %s", net) diff --git a/pkg/client/registry.go b/pkg/client/registry.go index 7432c4c4a..316a34ab4 100644 --- a/pkg/client/registry.go +++ b/pkg/client/registry.go @@ -62,11 +62,15 @@ func RegistryCreate(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Regi // l.Log().Fatalln(err) // } + if len(reg.DefaultNetwork) == 0 { + reg.DefaultNetwork = k3d.DefaultNetwork + } + registryNode := &k3d.Node{ Name: reg.Host, Image: reg.Image, Role: k3d.RegistryRole, - Networks: []string{"bridge"}, // Default network: TODO: change to const from types + Networks: []string{reg.DefaultNetwork}, Restart: true, } diff --git a/pkg/types/defaults.go b/pkg/types/defaults.go index 4acd19ab1..2c9220cd8 100644 --- a/pkg/types/defaults.go +++ b/pkg/types/defaults.go @@ -94,3 +94,6 @@ func GetDefaultObjectName(name string) string { // container is in a crash loop. // This makes sense e.g. when a new server is waiting to join an existing cluster and has to wait for other learners to finish. const DefaultNodeWaitForLogMessageCrashLoopBackOffLimit = 10 + +// DefaultNetwork defines the default Docker network +const DefaultNetwork = "bridge" diff --git a/pkg/types/registry.go b/pkg/types/registry.go index 07102a6ab..a847a8668 100644 --- a/pkg/types/registry.go +++ b/pkg/types/registry.go @@ -34,12 +34,13 @@ const ( // Registry describes a k3d-managed registry type Registry struct { - ClusterRef string // filled automatically -> if created with a cluster - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` // default: http - Host string `yaml:"host" json:"host"` - Image string `yaml:"image,omitempty" json:"image,omitempty"` - ExposureOpts ExposureOpts `yaml:"expose" json:"expose"` - Options struct { + ClusterRef string // filled automatically -> if created with a cluster + Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` // default: http + Host string `yaml:"host" json:"host"` + Image string `yaml:"image,omitempty" json:"image,omitempty"` + DefaultNetwork string `yaml:"defaultNetwork,omitempty" json:"defaultNetwork,omitempty"` + ExposureOpts ExposureOpts `yaml:"expose" json:"expose"` + Options struct { ConfigFile string `yaml:"configFile,omitempty" json:"configFile,omitempty"` Proxy struct { RemoteURL string `yaml:"remoteURL" json:"remoteURL"` From d41cbcc37d9a6e85ba7be04c095c003c67246756 Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Sat, 4 Dec 2021 13:28:03 +0100 Subject: [PATCH 3/4] Simplify/Clarify Names - network is not ambiguous for registry, so it can just be network instead of DefaultNetwork - DefaultNetwork as a type however could be ambiguous in that it could be confused for k3d's default network (i.e. `k3d cluster create` without a name would create the "default" `k3d-k3s-default` network) --- cmd/registry/registryCreate.go | 6 +++--- pkg/client/cluster.go | 2 +- pkg/client/registry.go | 6 +++--- pkg/types/defaults.go | 4 ++-- pkg/types/registry.go | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/registry/registryCreate.go b/cmd/registry/registryCreate.go index 81604c5d8..7cee0a937 100644 --- a/cmd/registry/registryCreate.go +++ b/cmd/registry/registryCreate.go @@ -42,7 +42,7 @@ type regCreatePreProcessedFlags struct { type regCreateFlags struct { Image string - DefaultNetwork string + Network string NoHelp bool } @@ -104,7 +104,7 @@ func NewCmdRegistryCreate() *cobra.Command { cmd.Flags().StringVarP(&ppFlags.Port, "port", "p", "random", "Select which port the registry should be listening on on your machine (localhost) (Format: `[HOST:]HOSTPORT`)\n - Example: `k3d registry create --port 0.0.0.0:5111`") - cmd.Flags().StringVar(&flags.DefaultNetwork, "default-network", k3d.DefaultNetwork, "Specify the default network connected to the registry (bridge)") + cmd.Flags().StringVar(&flags.Network, "default-network", k3d.Network, "Specify the network connected to the registry") cmd.Flags().BoolVar(&flags.NoHelp, "no-help", false, "Disable the help text (How-To use the registry)") @@ -138,5 +138,5 @@ func parseCreateRegistryCmd(cmd *cobra.Command, args []string, flags *regCreateF registryName = fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, args[0]) } - return &k3d.Registry{Host: registryName, Image: flags.Image, ExposureOpts: *exposePort, DefaultNetwork: flags.DefaultNetwork}, clusters + return &k3d.Registry{Host: registryName, Image: flags.Image, ExposureOpts: *exposePort, Network: flags.Network}, clusters } diff --git a/pkg/client/cluster.go b/pkg/client/cluster.go index 2ca1d8c6d..47b32a35d 100644 --- a/pkg/client/cluster.go +++ b/pkg/client/cluster.go @@ -583,7 +583,7 @@ func ClusterDelete(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus if net == cluster.Network.Name { continue } - if net == k3d.DefaultNetwork || net == "host" { + if net == k3d.DefaultRuntimeNetwork || net == "host" { continue } l.Log().Tracef("net: %s", net) diff --git a/pkg/client/registry.go b/pkg/client/registry.go index 316a34ab4..6b52aa08d 100644 --- a/pkg/client/registry.go +++ b/pkg/client/registry.go @@ -62,15 +62,15 @@ func RegistryCreate(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Regi // l.Log().Fatalln(err) // } - if len(reg.DefaultNetwork) == 0 { - reg.DefaultNetwork = k3d.DefaultNetwork + if len(reg.Network) == 0 { + reg.Network = k3d.DefaultRuntimeNetwork } registryNode := &k3d.Node{ Name: reg.Host, Image: reg.Image, Role: k3d.RegistryRole, - Networks: []string{reg.DefaultNetwork}, + Networks: []string{reg.Network}, Restart: true, } diff --git a/pkg/types/defaults.go b/pkg/types/defaults.go index 2c9220cd8..018a79ca4 100644 --- a/pkg/types/defaults.go +++ b/pkg/types/defaults.go @@ -95,5 +95,5 @@ func GetDefaultObjectName(name string) string { // This makes sense e.g. when a new server is waiting to join an existing cluster and has to wait for other learners to finish. const DefaultNodeWaitForLogMessageCrashLoopBackOffLimit = 10 -// DefaultNetwork defines the default Docker network -const DefaultNetwork = "bridge" +// DefaultNetwork defines the default (Docker) runtime network +const DefaultRuntimeNetwork = "bridge" diff --git a/pkg/types/registry.go b/pkg/types/registry.go index a847a8668..aae83f9b3 100644 --- a/pkg/types/registry.go +++ b/pkg/types/registry.go @@ -38,7 +38,7 @@ type Registry struct { Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` // default: http Host string `yaml:"host" json:"host"` Image string `yaml:"image,omitempty" json:"image,omitempty"` - DefaultNetwork string `yaml:"defaultNetwork,omitempty" json:"defaultNetwork,omitempty"` + Network string `yaml:"Network,omitempty" json:"Network,omitempty"` ExposureOpts ExposureOpts `yaml:"expose" json:"expose"` Options struct { ConfigFile string `yaml:"configFile,omitempty" json:"configFile,omitempty"` From d3b38232db55c6d013adcbc6e8f0a6922f95e92f Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Sat, 4 Dec 2021 13:45:37 +0100 Subject: [PATCH 4/4] Update cmd/registry/registryCreate.go --- cmd/registry/registryCreate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/registry/registryCreate.go b/cmd/registry/registryCreate.go index 7cee0a937..0309a6665 100644 --- a/cmd/registry/registryCreate.go +++ b/cmd/registry/registryCreate.go @@ -104,7 +104,7 @@ func NewCmdRegistryCreate() *cobra.Command { cmd.Flags().StringVarP(&ppFlags.Port, "port", "p", "random", "Select which port the registry should be listening on on your machine (localhost) (Format: `[HOST:]HOSTPORT`)\n - Example: `k3d registry create --port 0.0.0.0:5111`") - cmd.Flags().StringVar(&flags.Network, "default-network", k3d.Network, "Specify the network connected to the registry") + cmd.Flags().StringVar(&flags.Network, "default-network", k3d.DefaultRuntimeNetwork, "Specify the network connected to the registry") cmd.Flags().BoolVar(&flags.NoHelp, "no-help", false, "Disable the help text (How-To use the registry)")