Skip to content

Commit bfc352c

Browse files
committed
move to a pluggable architecture
golang modules are not easy to work with as the modules need to match the dependencies, creating a tightly coupling. Since the feature set is not big and we just want to get a minimal decoupling to be able to develop the new features in the Network Policy Working Group withoutbreaking the existing consumers and user of the project, we just create multiple binaries with the features required. Unstable features will live under the plugins/ folder and moved once they are stable and GA to the main binary.
1 parent edc4a89 commit bfc352c

20 files changed

+512
-216
lines changed

.github/workflows/e2e.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ env:
1414
GO_VERSION: "1.24"
1515
K8S_VERSION: "v1.33.1"
1616
KIND_VERSION: "v0.29.0"
17-
IMAGE_NAME: registry.k8s.io/networking/kube-network-policies
1817
KIND_CLUSTER_NAME: kind
1918

2019
permissions: write-all
@@ -35,7 +34,7 @@ jobs:
3534

3635
- name: Build
3736
run: |
38-
docker build -t registry.k8s.io/networking/kube-network-policies:test -f Dockerfile .
37+
REGISTRY="registry.k8s.io/networking" IMAGE_NAME="kube-network-policies" TAG="test" make image-build-standard
3938
mkdir _output
4039
docker save registry.k8s.io/networking/kube-network-policies:test > _output/kube-network-policies-image.tar
4140

.github/workflows/npa.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ env:
1515
GO_VERSION: "1.24"
1616
K8S_VERSION: "v1.33.1"
1717
KIND_VERSION: "v0.29.0"
18-
IMAGE_NAME: registry.k8s.io/networking/kube-network-policies
1918
KIND_CLUSTER_NAME: kind
2019

2120
permissions: write-all
@@ -36,9 +35,9 @@ jobs:
3635

3736
- name: Build
3837
run: |
39-
docker build -t registry.k8s.io/networking/kube-network-policies:test -f Dockerfile .
38+
REGISTRY="registry.k8s.io/networking" IMAGE_NAME="kube-network-policies" TAG="test" make image-build-npa-v1alpha1
4039
mkdir _output
41-
docker save registry.k8s.io/networking/kube-network-policies:test > _output/kube-network-policies-image.tar
40+
docker save registry.k8s.io/networking/kube-network-policies:test-npa-v1alpha1 > _output/kube-network-policies-image.tar
4241
4342
- uses: actions/upload-artifact@v4
4443
with:
@@ -115,8 +114,8 @@ jobs:
115114
/usr/local/bin/kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/network-policy-api/main/config/crd/experimental/policy.networking.k8s.io_baselineadminnetworkpolicies.yaml
116115
# preload kube-network-policies image
117116
docker load --input kube-network-policies-image.tar
118-
/usr/local/bin/kind load docker-image registry.k8s.io/networking/kube-network-policies:test --name ${{ env.KIND_CLUSTER_NAME}}
119-
sed -i s#registry.k8s.io/networking/kube-network-policies.*#registry.k8s.io/networking/kube-network-policies:test# install-anp.yaml
117+
/usr/local/bin/kind load docker-image registry.k8s.io/networking/kube-network-policies:test-npa-v1alpha1 --name ${{ env.KIND_CLUSTER_NAME}}
118+
sed -i s#registry.k8s.io/networking/kube-network-policies.*#registry.k8s.io/networking/kube-network-policies:test-npa-v1alpha1# install-anp.yaml
120119
/usr/local/bin/kubectl apply -f ./install-anp.yaml
121120
122121
- name: Get Cluster status

Dockerfile

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
FROM --platform=$BUILDPLATFORM golang:1.24 AS builder
1+
# Use an ARG to select which build target to compile and use
2+
ARG TARGET_BUILD=standard
3+
ARG BINARY_NAME=kube-network-policies-${TARGET_BUILD}
24

5+
FROM --platform=$BUILDPLATFORM golang:1.24 AS builder
36
WORKDIR /src
4-
5-
COPY go.mod go.sum .
7+
COPY go.mod go.sum ./
68
RUN go mod download
7-
89
COPY . .
910

10-
ARG TARGETOS TARGETARCH
11-
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
12-
go build -o /go/bin/netpol ./cmd
11+
# Build the specific binary based on the build argument
12+
ARG TARGET_BUILD
13+
RUN make build-${TARGET_BUILD}
1314

14-
# STEP 2: Build small image
1515
FROM gcr.io/distroless/static-debian12
16-
COPY --from=builder --chown=root:root /go/bin/netpol /bin/netpol
1716

18-
CMD ["/bin/netpol"]
17+
# Copy the correct, compiled binary and give it a generic name inside the container
18+
ARG BINARY_NAME
19+
COPY --from=builder /src/bin/${BINARY_NAME} /bin/netpol
20+
21+
# The entrypoint is always the same, regardless of the build
22+
CMD ["/bin/netpol"]

Makefile

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,84 @@
11
REPO_ROOT:=${CURDIR}
22
OUT_DIR=$(REPO_ROOT)/bin
3-
BINARY_NAME?=kube-network-policies
43

5-
# go1.9+ can autodetect GOROOT, but if some other tool sets it ...
6-
GOROOT:=
7-
# enable modules
4+
# Go build settings
85
GO111MODULE=on
9-
# disable CGO by default for static binaries
106
CGO_ENABLED=0
11-
export GOROOT GO111MODULE CGO_ENABLED
7+
export GO111MODULE CGO_ENABLED
128

9+
# Docker image settings
10+
IMAGE_NAME?=kube-network-policies
11+
REGISTRY?=gcr.io/k8s-staging-networking
12+
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)")
13+
PLATFORMS?=linux/amd64,linux/arm64
14+
15+
.PHONY: all build build-standard build-npa-v1alpha1
16+
all: build
17+
build: build-standard build-npa-v1alpha1
18+
19+
build-standard:
20+
@echo "Building standard binary..."
21+
go build -o ./bin/kube-network-policies-standard ./cmd/standard
1322

14-
build:
15-
go build -v -o "$(OUT_DIR)/$(BINARY_NAME)" $(KIND_CLOUD_BUILD_FLAGS) cmd/main.go
23+
build-npa-v1alpha1:
24+
@echo "Building npa-v1alpha1 binary..."
25+
go build -o ./bin/kube-network-policies-npa-v1alpha1 ./cmd/npa-v1alpha1
1626

1727
clean:
1828
rm -rf "$(OUT_DIR)/"
1929

2030
test:
2131
CGO_ENABLED=1 go test -v -race -count 1 ./...
2232

23-
# code linters
2433
lint:
2534
hack/lint.sh
2635

2736
update:
2837
go mod tidy
2938

30-
# Generate Go code from the proto definition
3139
proto:
3240
hack/generate-proto.sh
3341

34-
# get image name from directory we're building
35-
IMAGE_NAME=kube-network-policies
36-
# docker image registry, default to upstream
37-
REGISTRY?=gcr.io/k8s-staging-networking
38-
# tag based on date-sha
39-
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)")
40-
# the full image tag
41-
KNP_IMAGE?=$(REGISTRY)/$(IMAGE_NAME):$(TAG)
42-
PLATFORMS?=linux/amd64,linux/arm64
43-
4442
.PHONY: ensure-buildx
4543
ensure-buildx:
4644
./hack/init-buildx.sh
47-
48-
image-build:
45+
46+
# Individual image build targets (load into local docker)
47+
image-build-standard: build-standard
4948
docker buildx build . \
50-
--tag="${KNP_IMAGE}" \
49+
--build-arg TARGET_BUILD=standard \
50+
--tag="${REGISTRY}/$(IMAGE_NAME):$(TAG)" \
5151
--load
5252

53-
image-push:
53+
image-build-npa-v1alpha1: build-npa-v1alpha1
5454
docker buildx build . \
55+
--build-arg TARGET_BUILD=npa-v1alpha1 \
56+
--tag="${REGISTRY}/$(IMAGE_NAME):$(TAG)-npa-v1alpha1" \
57+
--load
58+
59+
# Individual image push targets (multi-platform)
60+
image-push-standard: build-standard
61+
docker buildx build . \
62+
--build-arg TARGET_BUILD=standard \
63+
--platform="${PLATFORMS}" \
64+
--tag="${REGISTRY}/$(IMAGE_NAME):$(TAG)" \
65+
--push
66+
67+
image-push-npa-v1alpha1: build-npa-v1alpha1
68+
docker buildx build . \
69+
--build-arg TARGET_BUILD=npa-v1alpha1 \
5570
--platform="${PLATFORMS}" \
56-
--tag="${KNP_IMAGE}" \
71+
--tag="${REGISTRY}/$(IMAGE_NAME):$(TAG)-npa-v1alpha1" \
5772
--push
5873

59-
.PHONY: release # Build a multi-arch docker image
60-
release: ensure-buildx image-push
74+
# --- Aggregate Targets ---
75+
.PHONY: images-build images-push release
76+
77+
# Build all image variants and load them into the local Docker daemon
78+
images-build: ensure-buildx image-build-standard image-build-npa-v1alpha1
79+
80+
# Build and push all multi-platform image variants to the registry
81+
images-push: ensure-buildx image-push-standard image-push-npa-v1alpha1
82+
83+
# The main release target, which pushes all images
84+
release: images-push

0 commit comments

Comments
 (0)