Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into fix-port-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mneverov authored Jan 22, 2024
2 parents 166310e + c7476d4 commit 51c9201
Show file tree
Hide file tree
Showing 60 changed files with 909 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/checkout@v3

- name: setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version-file: go.work

Expand Down
10 changes: 2 additions & 8 deletions .github/workflows/e2e_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ jobs:
sudo apt-get update && sudo apt-get install -y clang llvm libelf-dev libpcap-dev gcc-multilib build-essential
fi
- name: initialize ipvs module via ipvsadm
run: |
if [[ ${{ env.BACKEND }} == "ipvs" ]]; then
sudo apt-get update && sudo apt-get install ipvsadm && sudo ipvsadm -L
fi
- name: run e2e tests
run: ./hack/test_e2e.sh -i ${{ env.IP_FAMILY }} -b ${{ env.BACKEND }} -c -n 1 -S

Expand All @@ -80,7 +74,7 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: kind-junit-${{ env.JOB_NAME }}-${{ github.run_id }}
path: './hack/temp/e2e/artifacts/reports/*.xml'
path: './temp/e2e/artifacts/reports/*.xml'

- name: Upload logs
if: always()
Expand All @@ -93,4 +87,4 @@ jobs:
uses: mikepenz/action-junit-report@v3
if: always()
with:
report_paths: './hack/temp/e2e/artifacts/reports/*.xml'
report_paths: './temp/e2e/artifacts/reports/*.xml'
3 changes: 1 addition & 2 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ jobs:
- name: checkout
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version-file: go.work
stable: 'false'

- name: Install dependencies
run: |
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/vet-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ jobs:
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version-file: go.work
stable: 'false'

- name: Install dependencies
run: |
Expand Down
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from alpine:3.17 as gomods
from alpine:3.18 as gomods

copy . /src/
run cd /src/ && find -type f \! \( -name go.work -o -name go.mod -o -name go.sum \) -exec rm {} +

from golang:1.20.3-alpine3.17 as build
from golang:1.21.0-alpine3.18 as build

# install dependencies
run apk add --update --no-cache \
Expand All @@ -14,6 +14,10 @@ run apk add --update --no-cache \
arg GOPROXY
arg GONOSUMDB

arg RELEASE
arg GIT_REPO_URL
arg GIT_COMMIT_HASH

# cache dependencies, they don't change as much as the code
copy --from=gomods /src/ /src/

Expand All @@ -24,10 +28,10 @@ run go mod download

add . ./
#run for f in $(find -name go.mod); do d=$(dirname $f); echo "testing in $d"; ( cd $d && go test ./... ); done
run go install -trimpath -buildvcs=false ./cmd/...
run go install -trimpath -buildvcs=false -ldflags "-X main.RELEASE=$RELEASE -X main.REPO=$GIT_REPO_URL -X main.COMMIT=$GIT_COMMIT_HASH" ./cmd/...

# the real image
from alpine:3.17
from alpine:3.18
entrypoint ["/bin/kpng"]
run apk add --update iptables ip6tables iproute2 ipvsadm nftables ipset conntrack-tools
copy --from=build /go/bin/ /bin/
4 changes: 2 additions & 2 deletions Dockerfile.static
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from alpine:3.16 as gomods
from alpine:3.18 as gomods

copy . /src/
run cd /src/ && find -type f \! \( -name go.work -o -name go.mod -o -name go.sum \) -exec rm {} +

from golang:1.19.2-alpine3.16 as build
from golang:1.21.0-alpine3.18 as build

# install dependencies
run apk add --update --no-cache \
Expand Down
41 changes: 32 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@

all: help

# kpng build info
VERSION=$(shell git describe --tags --always --long)
LDFLAGS="-X main.version=$(VERSION)"
ARCH="amd64"
BUILD_DIR="kpng-bin"
export PLATFORM=""
# Host info
HOST_ARCH = $(shell which go >/dev/null 2>&1 && go env GOARCH)
export ARCH ?= $(HOST_ARCH)
ifeq ($(ARCH),)
$(error mandatory variable ARCH is empty, either set it when calling the command or make sure 'go env GOARCH' works)
endif
BUILD_DIR=kpng-bin
PLATFORM=""

# Build info
RELEASE ?= UNKNOWN
GIT_REPO_URL ?= $(shell git config --get remote.origin.url)
GIT_COMMIT_HASH ?= git-$(shell git rev-parse --short HEAD)
LDFLAGS="-X main.RELEASE=$(RELEASE) -X main.REPO=$(GIT_REPO_URL) -X main.COMMIT=$(GIT_COMMIT_HASH)"

# Image info
CONTAINER_ENGINE ?= docker
CONTAINER_FILE ?= $(shell echo "${PWD}/Dockerfile")
KPNG_IMAGE_TAG_NAME ?= kpng:test

# Auto Generate help from: https://gist.github.com/prwhite/8168133
# COLORS
Expand Down Expand Up @@ -123,17 +136,27 @@ windows:
@$(MAKE) -e build

build:
@mkdir -p $(BUILD_DIR)/$(PLATFORM)/$(VERSION)
@mkdir -p $(BUILD_DIR)/$(ARCH)
@cd cmd && \
env GOOS=$(PLATFORM) \
GOARCH=$(ARCH) \
go build \
-trimpath \
-o ../$(BUILD_DIR)/$(PLATFORM)/$(VERSION) \
-o ../$(BUILD_DIR)/$(ARCH) \
-v \
-ldflags=$(LDFLAGS) \
./...
@echo "\tkpng $(YELLOW)$(PLATFORM)$(RESET) binaries available in: $(GREEN)$(BUILD_DIR)/$(PLATFORM)/$(VERSION)$(RESET)\n"
@echo "\tkpng $(YELLOW)$(ARCH)$(RESET) binaries available in: $(GREEN)$(BUILD_DIR)/$(ARCH)$(RESET)\n"

image:
$(CONTAINER_ENGINE) build \
$(QUIET_MODE) \
-t $(KPNG_IMAGE_TAG_NAME) \
-f $(CONTAINER_FILE) \
--build-arg RELEASE=$(RELEASE) \
--build-arg GIT_REPO_URL=$(GIT_REPO_URL) \
--build-arg GIT_COMMIT_HASH=$(GIT_COMMIT_HASH) \
.

## Create Kind cluster for Tilt. It requires backend(b) and ipfamily (i) as args. eg: make tilt-setup i=ipv4 b=nft m=split-process-per-node
tilt-setup:
Expand Down
10 changes: 6 additions & 4 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ module sigs.k8s.io/kpng/api
go 1.20

require (
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.3
google.golang.org/grpc v1.50.0
google.golang.org/protobuf v1.28.1
k8s.io/klog/v2 v2.80.1
)

require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20221010155953-15ba04fc1c0e // indirect
)
11 changes: 6 additions & 5 deletions api/go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20221010155953-15ba04fc1c0e h1:halCgTFuLWDRD61piiNSxPsARANGD3Xl16hPrLgLiIg=
google.golang.org/genproto v0.0.0-20221010155953-15ba04fc1c0e/go.mod h1:3526vdqwhZAwq4wsRUaVG555sVgsNmIjRtO7t/JH29U=
Expand All @@ -16,3 +16,4 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
42 changes: 33 additions & 9 deletions api/localv1/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package localv1

import (
"fmt"
"k8s.io/klog/v2"
"net"
)

Expand All @@ -29,30 +31,52 @@ func (ep *Endpoint) AddAddress(s string) (ip net.IP) {
return ep.IPs.Add(s)
}

func (ep *Endpoint) PortMapping(port *PortMapping) (target int32) {
target = port.TargetPort
if port.TargetPortName != "" {
func (ep *Endpoint) PortMapping(port *PortMapping) (int32, error) {
nameToFind := ""
if port.Name != "" {
nameToFind = port.Name
} else if port.TargetPortName != "" {
nameToFind = port.TargetPortName
}

if nameToFind != "" {
for _, override := range ep.PortOverrides {
if override.Name == port.Name {
target = override.Port
break
if override.Name == nameToFind {
return override.Port, nil
}
}
return 0, fmt.Errorf("not found %s in port overrides", nameToFind)
}
return

if port.TargetPort > 0 {
return port.TargetPort, nil
}

return 0, fmt.Errorf("port mapping is undefined")
}

func (ep *Endpoint) PortMappings(ports []*PortMapping) (mapping map[int32]int32) {
mapping = make(map[int32]int32, len(ports))
for _, port := range ports {
mapping[port.Port] = ep.PortMapping(port)
p, err := ep.PortMapping(port)
if err != nil {
klog.V(1).InfoS("failed to map port", "err", err)
continue
}
mapping[port.Port] = p
}
return
}

func (ep *Endpoint) PortNameMappings(ports []*PortMapping) (mapping map[string]int32) {
mapping = make(map[string]int32, len(ports))
for _, port := range ports {
mapping[port.Name] = ep.PortMapping(port)
p, err := ep.PortMapping(port)
if err != nil {
klog.V(1).InfoS("failed to map port", "err", err)
continue
}
mapping[port.Name] = p
}
return
}
29 changes: 24 additions & 5 deletions api/localv1/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@ import "fmt"

func ExampleEndpointPortMapping() {
ports := []*PortMapping{
// name doesn't match -> ignore the rest
{Name: "http", TargetPortName: "t-http", TargetPort: 8080},
// name matches -> ignore the rest
{Name: "http2", TargetPortName: "t-http2", TargetPort: 800},
{Name: "metrics", TargetPortName: "t-metrics"},
// name matches -> ignore the rest
{Name: "metrics", TargetPortName: "http2"},
// name matches -> ignore the rest
{Name: "metrics", TargetPort: 80},
// name matches
{Name: "metrics"},
// targetPortName matches, no name -> ignore TargetPort
{TargetPortName: "metrics", TargetPort: 8080},
// targetPortName doesn't match, no name -> ignore targetPort
{TargetPortName: "t-metrics", TargetPort: 8080},
// nothing to match -> err expected
{},
}

ep := &Endpoint{
Expand All @@ -33,11 +46,17 @@ func ExampleEndpointPortMapping() {
}

for _, port := range ports {
fmt.Println(port.Name, ep.PortMapping(port))
p, err := ep.PortMapping(port)
fmt.Println(port.Name, p, err)
}

// Output:
// http 8080
// http2 888
// metrics 1011
// http 0 not found http in port overrides
// http2 888 <nil>
// metrics 1011 <nil>
// metrics 1011 <nil>
// metrics 1011 <nil>
// 1011 <nil>
// 0 not found t-metrics in port overrides
// 0 port mapping is undefined
}
10 changes: 5 additions & 5 deletions backends/ebpf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ require (
)

require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -24,9 +24,9 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/spf13/cobra v1.4.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20221010155953-15ba04fc1c0e // indirect
google.golang.org/grpc v1.50.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
12 changes: 5 additions & 7 deletions backends/ebpf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzP
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -73,18 +71,18 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
Loading

0 comments on commit 51c9201

Please sign in to comment.