Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing the makefile; enforcing the registry port match; #1550

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ K3D_HELPER_VERSION ?=
# Go options
GO ?= go
GOENVPATH := $(shell go env GOPATH)
PKG := $(shell go work vendor)
TAGS :=
TESTS := ./...
TESTFLAGS :=
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}
GCFLAGS :=
GOFLAGS := -mod=readonly
GOFLAGS := -mod=vendor
BINDIR := $(CURDIR)/bin
BINARIES := k3d

Expand Down
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
41 changes: 41 additions & 0 deletions cmd/util/ports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package util

import (
"strings"
"testing"

"gotest.tools/assert"
)

func Test_ParsePortExposureSpec_PortMatchEnforcement(t *testing.T) {

r, err := ParsePortExposureSpec("9999", "1111")
if nil != err {
t.Fail()
} else {
assert.Equal(t, string(r.Port), "1111/tcp")
assert.Equal(t, string(r.Binding.HostPort), "9999")
}

r, err = ParseRegistryPortExposureSpec("9999")
if nil != err {
t.Fail()
} else {
assert.Equal(t, string(r.Port), "9999/tcp")
assert.Equal(t, string(r.Binding.HostPort), "9999")
}

r, err = ParsePortExposureSpec("random", "1")
if nil != err {
t.Fail()
} else {
assert.Assert(t, strings.Split(string(r.Port), "/")[0] != string(r.Binding.HostPort))
}

r, err = ParseRegistryPortExposureSpec("random")
if nil != err {
t.Fail()
} else {
assert.Equal(t, strings.Split(string(r.Port), "/")[0], string(r.Binding.HostPort))
}
}
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