Skip to content

Commit

Permalink
[registry] support create pull-through registry cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ethinx committed May 31, 2022
1 parent 1009992 commit 1160b23
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
44 changes: 40 additions & 4 deletions cmd/registry/registryCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ import (
type regCreatePreProcessedFlags struct {
Port string
Clusters []string
Volumes []string
}

type regCreateFlags struct {
Image string
Network string
NoHelp bool
Image string
Network string
ProxyRemoteURL string
ProxyUsername string
ProxyPassword string
NoHelp bool
}

var helptext string = `# You can now use the registry like this (example):
Expand Down Expand Up @@ -103,8 +107,12 @@ func NewCmdRegistryCreate() *cobra.Command {
cmd.Flags().StringVarP(&flags.Image, "image", "i", fmt.Sprintf("%s:%s", k3d.DefaultRegistryImageRepo, k3d.DefaultRegistryImageTag), "Specify image used for the registry")

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().StringArrayVarP(&ppFlags.Volumes, "volume", "v", nil, "Mount volumes into the registry node (Format: `[SOURCE:]DEST`")

cmd.Flags().StringVar(&flags.Network, "default-network", k3d.DefaultRuntimeNetwork, "Specify the network connected to the registry")
cmd.Flags().StringVar(&flags.ProxyRemoteURL, "proxy-remote-url", "", "Specify the url of the proxied remote registry")
cmd.Flags().StringVar(&flags.ProxyUsername, "proxy-username", "", "Specify the username of the proxied remote registry")
cmd.Flags().StringVar(&flags.ProxyPassword, "proxy-password", "", "Specify the password of the proxied remote registry")

cmd.Flags().BoolVar(&flags.NoHelp, "no-help", false, "Disable the help text (How-To use the registry)")

Expand Down Expand Up @@ -138,5 +146,33 @@ 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, Network: flags.Network}, clusters
// -- proxy
var options k3d.RegistryOptions

if flags.ProxyRemoteURL != "" {
proxy := k3d.RegistryProxy{
RemoteURL: flags.ProxyRemoteURL,
Username: flags.ProxyUsername,
Password: flags.ProxyPassword,
}
options.Proxy = proxy
l.Log().Traceln("Proxy info:", proxy)
}

// --volume
var volumes []string
if len(ppFlags.Volumes) > 0 {
volumes = []string{}

for _, volumeFlag := range ppFlags.Volumes {
volume, _, err := cliutil.SplitFiltersFromFlag(volumeFlag)
if err != nil {
l.Log().Fatalln(err)
}
volumes = append(volumes, volume)
}

}

return &k3d.Registry{Host: registryName, Image: flags.Image, ExposureOpts: *exposePort, Network: flags.Network, Options: options, Volumes: volumes}, clusters
}
17 changes: 17 additions & 0 deletions pkg/client/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ func RegistryCreate(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Regi
Role: k3d.RegistryRole,
Networks: []string{reg.Network},
Restart: true,
Env: []string{},
}

if reg.Options.Proxy.RemoteURL != "" {
registryNode.Env = append(registryNode.Env, fmt.Sprintf("REGISTRY_PROXY_REMOTEURL=%s", reg.Options.Proxy.RemoteURL))

if reg.Options.Proxy.Username != "" {
registryNode.Env = append(registryNode.Env, fmt.Sprintf("REGISTRY_PROXY_USERNAME=%s", reg.Options.Proxy.Username))
}

if reg.Options.Proxy.Password != "" {
registryNode.Env = append(registryNode.Env, fmt.Sprintf("REGISTRY_PROXY_PASSWORD=%s", reg.Options.Proxy.Password))
}
}

if len(reg.Volumes) > 0 {
registryNode.Volumes = reg.Volumes
}

// error out if that registry exists already
Expand Down
33 changes: 19 additions & 14 deletions pkg/types/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ const (
DefaultLocalRegistryHostingConfigmapTempPath = "/tmp/localRegistryHostingCM.yaml"
)

type RegistryOptions struct {
ConfigFile string `yaml:"configFile,omitempty" json:"configFile,omitempty"`
Proxy RegistryProxy `yaml:"proxy,omitempty" json:"proxy,omitempty"`
}

type RegistryProxy struct {
RemoteURL string `yaml:"remoteURL" json:"remoteURL"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
Password string `yaml:"password,omitempty" json:"password,omitempty"`
}

// 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"`
Network string `yaml:"Network,omitempty" json:"Network,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"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
Password string `yaml:"password,omitempty" json:"password,omitempty"`
} `yaml:"proxy,omitempty" json:"proxy,omitempty"`
} `yaml:"options,omitempty" json:"options,omitempty"`
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"`
Network string `yaml:"Network,omitempty" json:"Network,omitempty"`
Volumes []string `yaml:"Volumes,omitempty" json:"Volumes,omitempty"`
ExposureOpts ExposureOpts `yaml:"expose" json:"expose"`
Options RegistryOptions `yaml:"options,omitempty" json:"options,omitempty"`
}

// RegistryExternal describes a minimal spec for an "external" registry
Expand Down

0 comments on commit 1160b23

Please sign in to comment.