Skip to content

Commit d20940b

Browse files
committed
Upgrade project to kubebuilder v3
Add full Terraform testing example (not E2E yet though) to validate Autoneg setup easier.
1 parent 9410ea5 commit d20940b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1737
-499
lines changed

.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore all files which are not go type
3+
!**/*.go
4+
!**/*.mod
5+
!**/*.sum

.github/workflows/go.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ jobs:
3535
3636
- name: Test
3737
run: |
38-
sudo mkdir -p /usr/local/kubebuilder
39-
curl -Ls https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.2/kubebuilder_2.3.2_linux_amd64.tar.gz | sudo tar -xvz --strip-components=1 -C /usr/local/kubebuilder -f -
38+
curl -Ls https://github.com/kubernetes-sigs/kubebuilder/releases/download/v3.8.0/kubebuilder_linux_amd64 -o /var/tmp/kubebuilder
39+
chmod +x /var/tmp/kubebuilder
40+
sudo mv /var/tmp/kubebuilder /usr/local/bin/
4041
make test
4142
4243
- name: Login to GitHub Container Registry

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.so
77
*.dylib
88
bin
9+
testbin/*
910

1011
# Test binary, build with `go test -c`
1112
*.test

Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ COPY main.go main.go
2828
COPY controllers/ controllers/
2929

3030
# Build
31-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
31+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
3232

3333
# Use distroless as minimal base image to package the manager binary
3434
# Refer to https://github.com/GoogleContainerTools/distroless for more details
3535
FROM gcr.io/distroless/static:nonroot
3636
WORKDIR /
37-
COPY --chown=nonroot:nonroot --from=builder /workspace/manager .
38-
# user nonroot has uid 65532
39-
USER 65532
37+
COPY --from=builder /workspace/manager .
38+
USER 65532:65532
39+
4040
ENTRYPOINT ["/manager"]

Makefile

+91-38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

22
# Image URL to use all building/pushing image targets
33
IMG ?= controller:latest
4-
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
5-
CRD_OPTIONS ?= "crd:trivialVersions=true"
4+
# Previously we produced CRDs that work back to Kubernetes 1.11 (no version conversion),
5+
# but now we'll support only 1.16+.
6+
CRD_OPTIONS ?= "crd"
67

78
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
89
ifeq (,$(shell go env GOBIN))
@@ -11,63 +12,114 @@ else
1112
GOBIN=$(shell go env GOBIN)
1213
endif
1314

14-
CONTROLLER_GEN ?= sigs.k8s.io/controller-tools/cmd/[email protected]
15+
# Setting SHELL to bash allows bash commands to be executed by recipes.
16+
# This is a requirement for 'setup-envtest.sh' in the test target.
17+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
18+
SHELL = /usr/bin/env bash -o pipefail
19+
.SHELLFLAGS = -ec
1520

16-
all: manager
21+
all: build
1722

18-
# Run tests
19-
test: generate fmt vet manifests
20-
go test ./... -coverprofile cover.out
23+
##@ General
2124

22-
# Build manager binary
23-
manager: generate fmt vet
24-
go build -o bin/manager main.go
25+
# The help target prints out all targets with their descriptions organized
26+
# beneath their categories. The categories are represented by '##@' and the
27+
# target descriptions by '##'. The awk commands is responsible for reading the
28+
# entire set of makefiles included in this invocation, looking for lines of the
29+
# file as xyz: ## something, and then pretty-format the target and help. Then,
30+
# if there's a line with ##@ something, that gets pretty-printed as a category.
31+
# More info on the usage of ANSI control characters for terminal formatting:
32+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
33+
# More info on the awk command:
34+
# http://linuxcommand.org/lc3_adv_awk.php
2535

26-
# Run against the configured Kubernetes cluster in ~/.kube/config
27-
run: generate fmt vet manifests
28-
go run ./main.go
36+
help: ## Display this help.
37+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
2938

30-
# Install CRDs into a cluster
31-
install: manifests
32-
kustomize build config/crd | kubectl apply -f -
39+
##@ Development
3340

34-
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
35-
deploy: manifests
36-
cd config/manager && kustomize edit set image controller=${IMG}
37-
kustomize build config/default | kubectl apply -f -
41+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
42+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
3843

39-
# Generate manifests e.g. CRD, RBAC etc.
40-
manifests:
41-
go run $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
44+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
45+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
4246

43-
# Run go fmt against code
44-
fmt:
47+
fmt: ## Run go fmt against code.
4548
go fmt ./...
4649

47-
# Run go vet against code
48-
vet:
50+
vet: ## Run go vet against code.
4951
go vet ./...
5052

51-
# Generate code
52-
generate:
53-
go run $(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."
53+
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
54+
ENVTEST = $(shell pwd)/bin/setup-envtest
55+
ENVTEST_K8S_VERSION ?= 1.26.1
56+
57+
testenv:
58+
mkdir -p ${ENVTEST_ASSETS_DIR}
59+
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
5460

55-
# Create api directory. Circumvents issue with kubebuilder without CRDs
56-
api:
57-
mkdir api
61+
test: manifests generate fmt vet testenv ## Run tests.
62+
KUBEBUILDER_ASSETS="$(shell ${ENVTEST} use ${ENVTEST_K8S_VERSION} --bin-dir ${ENVTEST_ASSETS_DIR} -p path)" go test ./... -coverprofile cover.out
63+
64+
##@ Build
5865

5966
# Build the docker image
6067
DOCKER_BIN ?= docker
6168
VERSION ?= latest
6269
LABELS ?= --label org.opencontainers.image.licenses="Apache-2.0" \
6370
--label org.opencontainers.image.vendor="Google LLC" \
6471
--label org.opencontainers.image.version="${VERSION}"
65-
docker-build: test api
66-
${DOCKER_BIN} build ${LABELS} . -t ${IMG}
72+
73+
docker-build: test
74+
${DOCKER_BIN} build ${DOCKER_FLAGS} ${LABELS} . -t ${IMG}
6775

6876
# Push the docker image
6977
docker-push:
70-
${DOCKER_BIN} push ${IMG}
78+
${DOCKER_BIN} push ${DOCKER_FLAGS} ${IMG}
79+
80+
build: generate fmt vet ## Build manager binary.
81+
go build -o bin/manager main.go
82+
83+
run: manifests generate fmt vet ## Run a controller from your host.
84+
go run ./main.go
85+
86+
##@ Deployment
87+
88+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
89+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
90+
91+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
92+
$(KUSTOMIZE) build config/crd | kubectl delete -f -
93+
94+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
95+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
96+
$(KUSTOMIZE) build config/default | kubectl apply -f -
97+
98+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
99+
$(KUSTOMIZE) build config/default | kubectl delete -f -
100+
101+
102+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
103+
controller-gen: ## Download controller-gen locally if necessary.
104+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
105+
106+
KUSTOMIZE = $(shell pwd)/bin/kustomize
107+
kustomize: ## Download kustomize locally if necessary.
108+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
109+
110+
# go-get-tool will 'go get' any package $2 and install it to $1.
111+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
112+
define go-get-tool
113+
@[ -f $(1) ] || { \
114+
set -e ;\
115+
TMP_DIR=$$(mktemp -d) ;\
116+
cd $$TMP_DIR ;\
117+
go mod init tmp ;\
118+
echo "Downloading $(2)" ;\
119+
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
120+
rm -rf $$TMP_DIR ;\
121+
}
122+
endef
71123

72124
# Used for autoneg project releases
73125
#
@@ -77,8 +129,9 @@ RELEASE_IMG ?= ghcr.io/googlecloudplatform/gke-autoneg-controller/gke-autoneg-co
77129

78130
# Make deployment manifests but do not deploy
79131
autoneg-manifests: manifests
80-
cd config/manager && kustomize edit set image controller=${RELEASE_IMG}:${VERSION}
81-
kustomize build config/default > deploy/autoneg.yaml
132+
cd config/manager && $(KUSTOMIZE) edit set image controller=${RELEASE_IMG}:${VERSION}
133+
cp hack/boilerplate.bash.txt deploy/autoneg.yaml
134+
$(KUSTOMIZE) build config/default >> deploy/autoneg.yaml
82135

83136
# Make release image
84137
release-image: docker-build

PROJECT

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
version: "2"
2-
domain: google.com
1+
domain: controller.autoneg.dev
2+
layout:
3+
- go.kubebuilder.io/v3
4+
projectName: gke-autoneg-controller
35
repo: github.com/GoogleCloudPlatform/gke-autoneg-controller
6+
resources:
7+
- controller: true
8+
group: core
9+
kind: Service
10+
path: k8s.io/api/core/v1
11+
version: v1
12+
version: "3"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Lastly, on each cluster in your project where you'd like to install `autoneg` (v
115115
```
116116
kubectl apply -f deploy/autoneg.yaml
117117
118-
kubectl annotate sa -n autoneg-system autoneg \
118+
kubectl annotate sa -n autoneg-system autoneg-controller-manager \
119119
iam.gke.io/gcp-service-account=autoneg-system@${PROJECT_ID}.iam.gserviceaccount.com
120120
```
121121
This will create all the Kubernetes resources required to support `autoneg` and annotate the default service account in the `autoneg-system` namespace to associate a GCP service account using [Workload Identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity).

config/certmanager/certificate.yaml

-38
This file was deleted.

config/default/kustomization.yaml

+18-16
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,29 @@ commonLabels:
2727
app: autoneg
2828

2929
bases:
30-
# - ../crd
30+
#- ../crd
3131
- ../rbac
3232
- ../manager
33-
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
33+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
34+
# crd/kustomization.yaml
3435
#- ../webhook
3536
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
3637
#- ../certmanager
38+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
39+
#- ../prometheus
3740

3841
patchesStrategicMerge:
39-
# Protect the /metrics endpoint by putting it behind auth.
40-
# Only one of manager_auth_proxy_patch.yaml and
41-
# manager_prometheus_metrics_patch.yaml should be enabled.
42+
# Protect the /metrics endpoint by putting it behind auth.
43+
# If you want your controller-manager to expose the /metrics
44+
# endpoint w/o any authn/z, please comment the following line.
4245
- manager_auth_proxy_patch.yaml
43-
# If you want your controller-manager to expose the /metrics
44-
# endpoint w/o any authn/z, uncomment the following line and
45-
# comment manager_auth_proxy_patch.yaml.
46-
# Only one of manager_auth_proxy_patch.yaml and
47-
# manager_prometheus_metrics_patch.yaml should be enabled.
48-
#- manager_prometheus_metrics_patch.yaml
4946

50-
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
47+
# Mount the controller config file for loading manager configurations
48+
# through a ComponentConfig type
49+
#- manager_config_patch.yaml
50+
51+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
52+
# crd/kustomization.yaml
5153
#- manager_webhook_patch.yaml
5254

5355
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
@@ -61,16 +63,16 @@ vars:
6163
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
6264
# objref:
6365
# kind: Certificate
64-
# group: certmanager.k8s.io
65-
# version: v1alpha1
66+
# group: cert-manager.io
67+
# version: v1
6668
# name: serving-cert # this name should match the one in certificate.yaml
6769
# fieldref:
6870
# fieldpath: metadata.namespace
6971
#- name: CERTIFICATE_NAME
7072
# objref:
7173
# kind: Certificate
72-
# group: certmanager.k8s.io
73-
# version: v1alpha1
74+
# group: cert-manager.io
75+
# version: v1
7476
# name: serving-cert # this name should match the one in certificate.yaml
7577
#- name: SERVICE_NAMESPACE # namespace of the service
7678
# objref:

config/default/manager_auth_proxy_patch.yaml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 Google LLC
1+
# Copyright 2021 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# This patch inject a sidecar container which is a HTTP proxy for the controller manager,
16-
# it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
15+
# This patch inject a sidecar container which is a HTTP proxy for the
16+
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
1717
apiVersion: apps/v1
1818
kind: Deployment
1919
metadata:
@@ -38,5 +38,6 @@ spec:
3838
allowPrivilegeEscalation: false
3939
- name: manager
4040
args:
41-
- "--metrics-addr=127.0.0.1:8080"
42-
- "--enable-leader-election"
41+
- "--health-probe-bind-address=:8081"
42+
- "--metrics-bind-address=127.0.0.1:8080"
43+
- "--leader-elect"

config/default/manager_webhook_patch.yaml renamed to config/default/manager_config_patch.yaml

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 Google LLC
1+
# Copyright 2021 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -22,16 +22,13 @@ spec:
2222
spec:
2323
containers:
2424
- name: manager
25-
ports:
26-
- containerPort: 443
27-
name: webhook-server
28-
protocol: TCP
25+
args:
26+
- "--config=controller_manager_config.yaml"
2927
volumeMounts:
30-
- mountPath: /tmp/k8s-webhook-server/serving-certs
31-
name: cert
32-
readOnly: true
28+
- name: manager-config
29+
mountPath: /controller_manager_config.yaml
30+
subPath: controller_manager_config.yaml
3331
volumes:
34-
- name: cert
35-
secret:
36-
defaultMode: 420
37-
secretName: webhook-server-cert
32+
- name: manager-config
33+
configMap:
34+
name: manager-config

0 commit comments

Comments
 (0)