Skip to content

Commit c569517

Browse files
authored
enhancement: enforcing the internal registry port matching the external one (#1550)
* fix: GOFLAGS changed so that the build process uses the vendor directory; * fix: a redundant makefile line removed, prevents local cache build-up; * feat(GH-1406): enforcing the internal registry port matching the external one. * feat(GH-1406): added a unit test.
1 parent 45ea751 commit c569517

File tree

8 files changed

+64
-8
lines changed

8 files changed

+64
-8
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ K3D_HELPER_VERSION ?=
6666
# Go options
6767
GO ?= go
6868
GOENVPATH := $(shell go env GOPATH)
69-
PKG := $(shell go work vendor)
7069
TAGS :=
7170
TESTS := ./...
7271
TESTFLAGS :=
7372
LDFLAGS := -w -s -X github.com/k3d-io/k3d/v5/version.Version=${GIT_TAG} -X github.com/k3d-io/k3d/v5/version.K3sVersion=${K3S_TAG}
7473
GCFLAGS :=
75-
GOFLAGS := -mod=readonly
74+
GOFLAGS := -mod=vendor
7675
BINDIR := $(CURDIR)/bin
7776
BINARIES := k3d
7877

cmd/cluster/clusterCreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) {
575575
}
576576
cfg.Registries.Create.Name = fvSplit[0]
577577
if len(fvSplit) > 1 {
578-
exposeAPI, err = cliutil.ParsePortExposureSpec(fvSplit[1], "1234") // internal port is unused after all
578+
exposeAPI, err = cliutil.ParseRegistryPortExposureSpec(fvSplit[1])
579579
if err != nil {
580580
return cfg, fmt.Errorf("failed to registry port spec: %w", err)
581581
}

cmd/registry/registryCreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func parseCreateRegistryCmd(cmd *cobra.Command, args []string, flags *regCreateF
134134
}
135135

136136
// --port
137-
exposePort, err := cliutil.ParsePortExposureSpec(ppFlags.Port, k3d.DefaultRegistryPort)
137+
exposePort, err := cliutil.ParseRegistryPortExposureSpec(ppFlags.Port)
138138
if err != nil {
139139
l.Log().Errorln("Failed to parse registry port")
140140
l.Log().Fatalln(err)

cmd/util/ports.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ import (
3636

3737
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))$`)
3838

39-
// ParsePortExposureSpec parses/validates a string to create an exposePort struct from it
4039
func ParsePortExposureSpec(exposedPortSpec, internalPort string) (*k3d.ExposureOpts, error) {
40+
return parsePortExposureSpec(exposedPortSpec, internalPort, false)
41+
}
42+
43+
func ParseRegistryPortExposureSpec(exposedPortSpec string) (*k3d.ExposureOpts, error) {
44+
return parsePortExposureSpec(exposedPortSpec, k3d.DefaultRegistryPort, true)
45+
}
46+
47+
// ParsePortExposureSpec parses/validates a string to create an exposePort struct from it
48+
func parsePortExposureSpec(exposedPortSpec, internalPort string, enforcePortMatch bool) (*k3d.ExposureOpts, error) {
4149
match := apiPortRegexp.FindStringSubmatch(exposedPortSpec)
4250

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

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

107+
if enforcePortMatch {
108+
internalPort = submatches["port"]
109+
}
110+
99111
realPortString += fmt.Sprintf("%s:%s/tcp", submatches["port"], internalPort)
100112

101113
portMapping, err := nat.ParsePortSpec(realPortString)

cmd/util/ports_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package util
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"gotest.tools/assert"
8+
)
9+
10+
func Test_ParsePortExposureSpec_PortMatchEnforcement(t *testing.T) {
11+
12+
r, err := ParsePortExposureSpec("9999", "1111")
13+
if nil != err {
14+
t.Fail()
15+
} else {
16+
assert.Equal(t, string(r.Port), "1111/tcp")
17+
assert.Equal(t, string(r.Binding.HostPort), "9999")
18+
}
19+
20+
r, err = ParseRegistryPortExposureSpec("9999")
21+
if nil != err {
22+
t.Fail()
23+
} else {
24+
assert.Equal(t, string(r.Port), "9999/tcp")
25+
assert.Equal(t, string(r.Binding.HostPort), "9999")
26+
}
27+
28+
r, err = ParsePortExposureSpec("random", "1")
29+
if nil != err {
30+
t.Fail()
31+
} else {
32+
assert.Assert(t, strings.Split(string(r.Port), "/")[0] != string(r.Binding.HostPort))
33+
}
34+
35+
r, err = ParseRegistryPortExposureSpec("random")
36+
if nil != err {
37+
t.Fail()
38+
} else {
39+
assert.Equal(t, strings.Split(string(r.Port), "/")[0], string(r.Binding.HostPort))
40+
}
41+
}

pkg/client/registry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func RegistryCreate(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Regi
7676
Env: []string{},
7777
}
7878

79+
if reg.ExposureOpts.Binding.HostPort != "" {
80+
registryNode.Env = append(registryNode.Env, fmt.Sprintf("REGISTRY_HTTP_ADDR=:%s", reg.ExposureOpts.Binding.HostPort))
81+
}
82+
7983
if reg.Options.Proxy.RemoteURL != "" {
8084
registryNode.Env = append(registryNode.Env, fmt.Sprintf("REGISTRY_PROXY_REMOTEURL=%s", reg.Options.Proxy.RemoteURL))
8185

pkg/config/transform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
320320
epSpecHost = simpleConfig.Registries.Create.Host
321321
}
322322

323-
regPort, err := cliutil.ParsePortExposureSpec(fmt.Sprintf("%s:%s", epSpecHost, epSpecPort), k3d.DefaultRegistryPort)
323+
regPort, err := cliutil.ParseRegistryPortExposureSpec(fmt.Sprintf("%s:%s", epSpecHost, epSpecPort))
324324
if err != nil {
325325
return nil, fmt.Errorf("failed to get port for registry: %w", err)
326326
}

tests/test_registry.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ kubectl get configmap -n kube-public local-registry-hosting -o go-template='{{in
5252

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

0 commit comments

Comments
 (0)