Skip to content

Commit

Permalink
Allow creating the registry on user-defined networks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
serverwentdown committed Nov 20, 2021
1 parent 380840a commit b8a2233
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
9 changes: 6 additions & 3 deletions cmd/registry/registryCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion pkg/client/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/types/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 7 additions & 6 deletions pkg/types/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit b8a2233

Please sign in to comment.