Skip to content

Commit

Permalink
feat(GH-1406): enforcing the internal registry port matching the exte…
Browse files Browse the repository at this point in the history
…rnal one.
  • Loading branch information
yakom committed Feb 4, 2025
1 parent d6017c0 commit f29a9a2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) {
}
cfg.Registries.Create.Name = fvSplit[0]
if len(fvSplit) > 1 {
exposeAPI, err = cliutil.ParsePortExposureSpec(fvSplit[1], "1234") // internal port is unused after all
exposeAPI, err = cliutil.ParseRegistryPortExposureSpec(fvSplit[1])
if err != nil {
return cfg, fmt.Errorf("failed to registry port spec: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/registryCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func parseCreateRegistryCmd(cmd *cobra.Command, args []string, flags *regCreateF
}

// --port
exposePort, err := cliutil.ParsePortExposureSpec(ppFlags.Port, k3d.DefaultRegistryPort)
exposePort, err := cliutil.ParseRegistryPortExposureSpec(ppFlags.Port)
if err != nil {
l.Log().Errorln("Failed to parse registry port")
l.Log().Fatalln(err)
Expand Down
16 changes: 14 additions & 2 deletions cmd/util/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ import (

var apiPortRegexp = regexp.MustCompile(`^(?P<hostref>(?P<hostip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?P<hostname>\S+):)?(?P<port>(\d{1,5}|random))$`)

// ParsePortExposureSpec parses/validates a string to create an exposePort struct from it
func ParsePortExposureSpec(exposedPortSpec, internalPort string) (*k3d.ExposureOpts, error) {
return parsePortExposureSpec(exposedPortSpec, internalPort, false)
}

func ParseRegistryPortExposureSpec(exposedPortSpec string) (*k3d.ExposureOpts, error) {
return parsePortExposureSpec(exposedPortSpec, k3d.DefaultRegistryPort, true)
}

// ParsePortExposureSpec parses/validates a string to create an exposePort struct from it
func parsePortExposureSpec(exposedPortSpec, internalPort string, enforcePortMatch bool) (*k3d.ExposureOpts, error) {
match := apiPortRegexp.FindStringSubmatch(exposedPortSpec)

if len(match) == 0 {
Expand Down Expand Up @@ -83,7 +91,7 @@ func ParsePortExposureSpec(exposedPortSpec, internalPort string) (*k3d.ExposureO
}

// port: get a free one if there's none defined or set to random
if submatches["port"] == "" || submatches["port"] == "random" {
if submatches["port"] == "random" {
l.Log().Debugf("Port Exposure Mapping didn't specify hostPort, choosing one randomly...")
freePort, err := GetFreePort()
if err != nil || freePort == 0 {
Expand All @@ -96,6 +104,10 @@ func ParsePortExposureSpec(exposedPortSpec, internalPort string) (*k3d.ExposureO
}
}

if enforcePortMatch {
internalPort = submatches["port"]
}

realPortString += fmt.Sprintf("%s:%s/tcp", submatches["port"], internalPort)

portMapping, err := nat.ParsePortSpec(realPortString)
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func RegistryCreate(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Regi
Env: []string{},
}

if reg.ExposureOpts.Binding.HostPort != "" {
registryNode.Env = append(registryNode.Env, fmt.Sprintf("REGISTRY_HTTP_ADDR=:%s", reg.ExposureOpts.Binding.HostPort))
}

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
epSpecHost = simpleConfig.Registries.Create.Host
}

regPort, err := cliutil.ParsePortExposureSpec(fmt.Sprintf("%s:%s", epSpecHost, epSpecPort), k3d.DefaultRegistryPort)
regPort, err := cliutil.ParseRegistryPortExposureSpec(fmt.Sprintf("%s:%s", epSpecHost, epSpecPort))
if err != nil {
return nil, fmt.Errorf("failed to get port for registry: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ kubectl get configmap -n kube-public local-registry-hosting -o go-template='{{in

# 3. load an image into the registry
info "Pushing an image to the registry..."
registryPort=$(docker inspect $registryname | jq '.[0].NetworkSettings.Ports["5000/tcp"][0].HostPort' | sed -E 's/"//g')
registryPort=$(docker inspect $registryname | jq '.[0].NetworkSettings.Ports | with_entries(select(.value | . != null)) | to_entries[0].value[0].HostPort' | sed -E 's/"//g')
docker pull alpine:latest > /dev/null
docker tag alpine:latest "localhost:$registryPort/alpine:local" > /dev/null
docker push "localhost:$registryPort/alpine:local" || failed "Failed to push image to managed registry"
Expand Down

0 comments on commit f29a9a2

Please sign in to comment.