Skip to content

Commit 2a23099

Browse files
authored
controller manifest for kustomize (#13)
* controller manifest for kustomize modify resource names to amazon-network-policy-controller-k8s * pass go runner image as arg
1 parent e9caada commit 2a23099

19 files changed

+198
-225
lines changed

Dockerfile

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
# Build the manager binary
2-
FROM golang:1.19 as builder
3-
ARG TARGETOS
4-
ARG TARGETARCH
1+
ARG BASE_IMAGE
2+
ARG BUILD_IMAGE
3+
ARG GO_RUNNER_IMAGE
4+
ARG ARCH=amd64
5+
# Build the controller binary
6+
FROM $BUILD_IMAGE as builder
57

68
WORKDIR /workspace
9+
ENV GOPROXY direct
10+
711
# Copy the Go Modules manifests
812
COPY go.mod go.mod
913
COPY go.sum go.sum
@@ -12,22 +16,27 @@ COPY go.sum go.sum
1216
RUN go mod download
1317

1418
# Copy the go source
19+
COPY .git/ .git/
1520
COPY cmd/main.go cmd/main.go
1621
COPY api/ api/
22+
COPY pkg/ pkg/
1723
COPY internal/controller/ internal/controller/
1824

25+
# Version package for passing the ldflags
26+
# TODO: change this to network controller's version
27+
ENV VERSION_PKG=https://github.com/aws/amazon-network-policy-controller-k8s/pkg/version
1928
# Build
20-
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21-
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22-
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23-
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
29+
RUN GIT_VERSION=$(git describe --tags --always) && \
30+
GIT_COMMIT=$(git rev-parse HEAD) && \
31+
BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) && \
32+
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on go build \
33+
-ldflags="-X ${VERSION_PKG}.GitVersion=${GIT_VERSION} -X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} -X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" -a -o controller main.go
34+
35+
FROM $BASE_IMAGE
2536

26-
# Use distroless as minimal base image to package the manager binary
27-
# Refer to https://github.com/GoogleContainerTools/distroless for more details
28-
FROM gcr.io/distroless/static:nonroot
2937
WORKDIR /
30-
COPY --from=builder /workspace/manager .
38+
COPY --from=$GO_RUNNER_IMAGE /go-runner /usr/local/bin/go-runner
39+
COPY --from=builder /workspace/controller .
3140
USER 65532:65532
3241

33-
ENTRYPOINT ["/manager"]
42+
ENTRYPOINT ["/controller"]

Makefile

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@ help: ## Display this help.
5757

5858
.PHONY: manifests
5959
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
60-
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
60+
$(CONTROLLER_GEN) rbac:roleName=controller-k8s crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
6161

6262
.PHONY: generate
6363
generate: controller-gen mockgen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
6464
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
6565
MOCKGEN=$(MOCKGEN) ./scripts/gen_mocks.sh
6666

67-
6867
.PHONY: fmt
6968
fmt: ## Run go fmt against code.
7069
go fmt ./...
@@ -80,8 +79,8 @@ test: manifests generate fmt vet envtest ## Run tests.
8079
##@ Build
8180

8281
.PHONY: build
83-
build: manifests generate fmt vet ## Build manager binary.
84-
go build -o bin/manager cmd/main.go
82+
build: manifests generate fmt vet ## Build controller binary.
83+
go build -o bin/controller cmd/main.go
8584

8685
.PHONY: run
8786
run: manifests generate fmt vet ## Run a controller from your host.
@@ -98,16 +97,6 @@ image-push: ko
9897
BUILD_DATE=$(shell date +%Y-%m-%dT%H:%M:%S%z) \
9998
$(KO) build --tags $(word 2,$(subst :, ,${IMG})) --platform=${PLATFORM} --bare --sbom ${IMG_SBOM} ./cmd
10099

101-
.PHONY: docker-buildx
102-
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
103-
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
104-
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
105-
- docker buildx create --name project-v3-builder
106-
docker buildx use project-v3-builder
107-
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
108-
- docker buildx rm project-v3-builder
109-
rm Dockerfile.cross
110-
111100
##@ Deployment
112101

113102
ifndef ignore-not-found
@@ -124,7 +113,7 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified
124113

125114
.PHONY: deploy
126115
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
127-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
116+
cd config/controller && $(KUSTOMIZE) edit set image controller=${IMG}
128117
$(KUSTOMIZE) build config/default | kubectl apply -f -
129118

130119
.PHONY: undeploy
@@ -179,3 +168,11 @@ $(KO): $(LOCALBIN)
179168
mockgen: $(MOCKGEN)
180169
$(MOCKGEN): $(LOCALBIN)
181170
test -s $(MOCKGEN) || GOBIN=$(LOCALBIN) go install github.com/golang/mock/[email protected]
171+
172+
GOARCH=amd64
173+
BUILD_IMAGE=public.ecr.aws/docker/library/golang:1.20.5
174+
BASE_IMAGE=public.ecr.aws/eks-distro-build-tooling/eks-distro-minimal-base-nonroot:latest.2
175+
GO_RUNNER_IMAGE=public.ecr.aws/eks-distro/kubernetes/go-runner:v0.15.0-eks-1-27-3
176+
.PHONY: docker-buildx
177+
docker-buildx: test
178+
docker buildx build --platform=$(PLATFORMS) -t $(IMG)-$(GOARCH) --build-arg BASE_IMAGE=$(BASE_IMAGE) --build-arg BUILD_IMAGE=$(BUILD_IMAGE) --build-arg $(GOARCH) --load .

config/controller/controller.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-k8s
5+
labels:
6+
app.kubernetes.io/component: controller
7+
spec:
8+
selector:
9+
matchLabels:
10+
app.kubernetes.io/component: controller
11+
replicas: 1
12+
template:
13+
metadata:
14+
annotations:
15+
kubectl.kubernetes.io/default-container: controller
16+
labels:
17+
app.kubernetes.io/component: controller
18+
spec:
19+
containers:
20+
- image: controller:latest
21+
name: controller
22+
securityContext:
23+
allowPrivilegeEscalation: false
24+
capabilities:
25+
drop:
26+
- "ALL"
27+
livenessProbe:
28+
httpGet:
29+
path: /healthz
30+
port: 8081
31+
initialDelaySeconds: 15
32+
periodSeconds: 20
33+
readinessProbe:
34+
httpGet:
35+
path: /readyz
36+
port: 8081
37+
initialDelaySeconds: 5
38+
periodSeconds: 10
39+
serviceAccountName: controller-k8s
40+
terminationGracePeriodSeconds: 10
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resources:
2+
- controller.yaml
3+
apiVersion: kustomize.config.k8s.io/v1beta1
4+
kind: Kustomization
5+
images:
6+
- name: controller
7+
newName: public.ecr.aws/eks/amazon-network-policy-controller-k8s
8+
newTag: v0.5.0

config/crd/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ patchesStrategicMerge:
1111
#- patches/webhook_in_policyendpoints.yaml
1212
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1313

14-
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
14+
# [CERTMANAGER] To enable cert-controller, uncomment all the sections with [CERTMANAGER] prefix.
1515
# patches here are for enabling the CA injection for each CRD
1616
#- patches/cainjection_in_policyendpoints.yaml
1717
#+kubebuilder:scaffold:crdkustomizecainjectionpatch

config/default/kustomization.yaml

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,121 @@
11
# Adds namespace to all resources.
2-
namespace: amazon-network-policy-controller-k8s-system
2+
namespace: kube-system
33

44
# Value of this field is prepended to the
55
# names of all resources, e.g. a deployment named
66
# "wordpress" becomes "alices-wordpress".
77
# Note that it should also match with the prefix (text before '-') of the namespace
88
# field above.
9-
namePrefix: amazon-network-policy-controller-k8s-
9+
namePrefix: amazon-network-policy-
1010

1111
# Labels to add to all resources and selectors.
12-
#labels:
13-
#- includeSelectors: true
14-
# pairs:
15-
# someName: someValue
12+
# Labels to add to all resources and selectors.
13+
commonLabels:
14+
app.kubernetes.io/name: amazon-network-policy-controller-k8s
1615

1716
resources:
1817
- ../crd
1918
- ../rbac
20-
- ../manager
19+
- ../controller
2120
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2221
# crd/kustomization.yaml
2322
#- ../webhook
24-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
23+
# [CERTMANAGER] To enable cert-controller, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
2524
#- ../certmanager
2625
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2726
#- ../prometheus
2827

2928
patchesStrategicMerge:
3029
# Protect the /metrics endpoint by putting it behind auth.
31-
# If you want your controller-manager to expose the /metrics
30+
# If you want your controller-controller to expose the /metrics
3231
# endpoint w/o any authn/z, please comment the following line.
33-
- manager_auth_proxy_patch.yaml
32+
# - manager_auth_proxy_patch.yaml
3433

3534

3635

3736
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3837
# crd/kustomization.yaml
3938
#- manager_webhook_patch.yaml
4039

41-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
40+
# [CERTMANAGER] To enable cert-controller, uncomment all sections with 'CERTMANAGER'.
4241
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
4342
# 'CERTMANAGER' needs to be enabled to use ca injection
4443
#- webhookcainjection_patch.yaml
4544

46-
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
47-
# Uncomment the following replacements to add the cert-manager CA injection annotations
45+
# [CERTMANAGER] To enable cert-controller, uncomment all sections with 'CERTMANAGER' prefix.
46+
# Uncomment the following replacements to add the cert-controller CA injection annotations
4847
#replacements:
49-
# - source: # Add cert-manager annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs
48+
# - source: # Add cert-controller annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs
5049
# kind: Certificate
51-
# group: cert-manager.io
50+
# group: cert-controller.io
5251
# version: v1
5352
# name: serving-cert # this name should match the one in certificate.yaml
5453
# fieldPath: .metadata.namespace # namespace of the certificate CR
5554
# targets:
5655
# - select:
5756
# kind: ValidatingWebhookConfiguration
5857
# fieldPaths:
59-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
58+
# - .metadata.annotations.[cert-controller.io/inject-ca-from]
6059
# options:
6160
# delimiter: '/'
6261
# index: 0
6362
# create: true
6463
# - select:
6564
# kind: MutatingWebhookConfiguration
6665
# fieldPaths:
67-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
66+
# - .metadata.annotations.[cert-controller.io/inject-ca-from]
6867
# options:
6968
# delimiter: '/'
7069
# index: 0
7170
# create: true
7271
# - select:
7372
# kind: CustomResourceDefinition
7473
# fieldPaths:
75-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
74+
# - .metadata.annotations.[cert-controller.io/inject-ca-from]
7675
# options:
7776
# delimiter: '/'
7877
# index: 0
7978
# create: true
8079
# - source:
8180
# kind: Certificate
82-
# group: cert-manager.io
81+
# group: cert-controller.io
8382
# version: v1
8483
# name: serving-cert # this name should match the one in certificate.yaml
8584
# fieldPath: .metadata.name
8685
# targets:
8786
# - select:
8887
# kind: ValidatingWebhookConfiguration
8988
# fieldPaths:
90-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
89+
# - .metadata.annotations.[cert-controller.io/inject-ca-from]
9190
# options:
9291
# delimiter: '/'
9392
# index: 1
9493
# create: true
9594
# - select:
9695
# kind: MutatingWebhookConfiguration
9796
# fieldPaths:
98-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
97+
# - .metadata.annotations.[cert-controller.io/inject-ca-from]
9998
# options:
10099
# delimiter: '/'
101100
# index: 1
102101
# create: true
103102
# - select:
104103
# kind: CustomResourceDefinition
105104
# fieldPaths:
106-
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
105+
# - .metadata.annotations.[cert-controller.io/inject-ca-from]
107106
# options:
108107
# delimiter: '/'
109108
# index: 1
110109
# create: true
111-
# - source: # Add cert-manager annotation to the webhook Service
110+
# - source: # Add cert-controller annotation to the webhook Service
112111
# kind: Service
113112
# version: v1
114113
# name: webhook-service
115114
# fieldPath: .metadata.name # namespace of the service
116115
# targets:
117116
# - select:
118117
# kind: Certificate
119-
# group: cert-manager.io
118+
# group: cert-controller.io
120119
# version: v1
121120
# fieldPaths:
122121
# - .spec.dnsNames.0
@@ -133,7 +132,7 @@ patchesStrategicMerge:
133132
# targets:
134133
# - select:
135134
# kind: Certificate
136-
# group: cert-manager.io
135+
# group: cert-controller.io
137136
# version: v1
138137
# fieldPaths:
139138
# - .spec.dnsNames.0

config/default/manager_auth_proxy_patch.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This patch inject a sidecar container which is a HTTP proxy for the
2-
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
2+
# controller controller, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
33
apiVersion: apps/v1
44
kind: Deployment
55
metadata:
6-
name: controller-manager
6+
name: controller-controller
77
namespace: system
88
spec:
99
template:
@@ -48,7 +48,7 @@ spec:
4848
requests:
4949
cpu: 5m
5050
memory: 64Mi
51-
- name: manager
51+
- name: controller
5252
args:
5353
- "--health-probe-bind-address=:8081"
5454
- "--metrics-bind-address=127.0.0.1:8080"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
apiVersion: apps/v1
22
kind: Deployment
33
metadata:
4-
name: controller-manager
4+
name: controller-controller
55
namespace: system
66
spec:
77
template:
88
spec:
99
containers:
10-
- name: manager
10+
- name: controller

config/manager/kustomization.yaml

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)