Skip to content

Commit ce6bea9

Browse files
committed
Update GuestbookOperator with kubebuiler v3
This commit update guestbook-operator with kubebuilder v3 and update walkthrough docs.
1 parent cac4a6c commit ce6bea9

36 files changed

+407
-241
lines changed

docs/addon/walkthrough/README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This walkthrough is for creating an operator to run the [guestbook](https://gith
77
Install the following depenencies:
88

99
- [kubebuilder](https://book.kubebuilder.io/quick-start.html#installation) (tested with 3.1.0)
10-
- [kustomize](https://kubectl.docs.kubernetes.io/installation/kustomize/) (tested with v2.0.3)
10+
- [kustomize](https://kubectl.docs.kubernetes.io/installation/kustomize/) (tested with v3.8.7)
1111
- docker
1212
- kubectl
1313
- golang (>=1.11 for go modules)
@@ -298,7 +298,7 @@ EOF
298298
3. Reference the patch by adding `manager_resource_patch.yaml` to the `patches` section of `config/default/kustomization.yaml`:
299299

300300
```yaml
301-
patches:
301+
patchesStrategicMerge:
302302
- manager_resource_patch.yaml
303303
# ... existing patches
304304
```
@@ -309,38 +309,36 @@ This is requried to run kubectl in the container.
309309
and run in a slim container:
310310

311311
```Dockerfile
312-
FROM ubuntu:latest as kubectl
313-
RUN apt-get update
314-
RUN apt-get install -y curl
315-
RUN curl -fsSL https://dl.k8s.io/release/v1.13.4/bin/linux/amd64/kubectl > /usr/bin/kubectl
316-
RUN chmod a+rx /usr/bin/kubectl
317-
318312
# Build the manager binary
319-
FROM golang:1.15 as builder
313+
FROM golang:1.16 as builder
320314

321315
# Copy in the go src
322-
WORKDIR /go/src/guestbook-operator
316+
WORKDIR /workspace
323317
# Copy the Go Modules manifests
324318
COPY go.mod go.mod
325319
COPY go.sum go.sum
326320
# cache deps before building and copying source so that we don't need to re-download as much
327321
# and so that source changes don't invalidate our downloaded layer
328322
RUN go mod download
329-
330323
COPY vendor/ vendor/
324+
331325
COPY main.go main.go
332326
COPY api/ api/
333327
COPY controllers/ controllers/
328+
COPY channels/ channels/
329+
RUN chmod -R a+rx channels/
334330

335331
# Build
336332
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
337333

338334
# Copy the operator and dependencies into a thin image
339335
FROM gcr.io/distroless/static:latest
340336
WORKDIR /
341-
COPY --from=builder /go/src/guestbook-operator/manager .
342-
COPY --from=kubectl /usr/bin/kubectl /usr/bin/kubectl
343-
COPY channels/ channels/
337+
COPY --from=builder /workspace/manager .
338+
COPY --from=builder /workspace/channels/ channels/
339+
340+
USER 65532:65532
341+
344342
ENTRYPOINT ["./manager"]
345343
```
346344

@@ -426,7 +424,7 @@ You can verify that the operator has created the `kubernetes-guestbook`
426424
deployment:
427425

428426
e.g. `kubectl get pods -n guestbook-operator-system` or
429-
`kubectl get deploy -n guestbook-operator-system`.
427+
`kubectl get deployments -n guestbook-operator-system`.
430428

431429
## Manifest simplification: Automatic labels
432430

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
FROM ubuntu:latest as kubectl
2-
RUN apt-get update
3-
RUN apt-get install -y curl
4-
RUN curl -fsSL https://dl.k8s.io/release/v1.13.4/bin/linux/amd64/kubectl > /usr/bin/kubectl
5-
RUN chmod a+rx /usr/bin/kubectl
6-
71
# Build the manager binary
8-
FROM golang:1.13 as builder
2+
FROM golang:1.16 as builder
93

104
# Copy in the go src
11-
WORKDIR /go/src/guestbook-operator
5+
WORKDIR /workspace
126
# Copy the Go Modules manifests
137
COPY go.mod go.mod
148
COPY go.sum go.sum
159
# cache deps before building and copying source so that we don't need to re-download as much
1610
# and so that source changes don't invalidate our downloaded layer
1711
RUN go mod download
18-
1912
COPY vendor/ vendor/
13+
2014
COPY main.go main.go
2115
COPY api/ api/
2216
COPY controllers/ controllers/
17+
COPY channels/ channels/
18+
RUN chmod -R a+rx channels/
2319

2420
# Build
2521
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
2622

2723
# Copy the operator and dependencies into a thin image
2824
FROM gcr.io/distroless/static:latest
2925
WORKDIR /
30-
COPY --from=builder /go/src/guestbook-operator/manager .
31-
COPY --from=kubectl /usr/bin/kubectl /usr/bin/kubectl
32-
COPY channels/ channels/
26+
COPY --from=builder /workspace/manager .
27+
COPY --from=builder /workspace/channels/ channels/
28+
29+
USER 65532:65532
30+
3331
ENTRYPOINT ["./manager"]

examples/guestbook-operator/Makefile

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Image URL to use all building/pushing image targets
33
IMG ?= controller:latest
44
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
5-
CRD_OPTIONS ?= "crd:trivialVersions=true"
5+
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
66

77
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
88
ifeq (,$(shell go env GOBIN))
@@ -11,80 +11,101 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14-
all: manager
14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
1519

16-
# Run tests
17-
test: generate fmt vet manifests
18-
go test ./... -coverprofile cover.out
20+
all: build
1921

20-
# Build manager binary
21-
manager: generate fmt vet
22-
go build -o bin/manager main.go
23-
24-
# Run against the configured Kubernetes cluster in ~/.kube/config
25-
run: generate fmt vet manifests
26-
go run ./main.go
22+
##@ General
2723

28-
# Install CRDs into a cluster
29-
install: manifests
30-
kustomize build config/crd | kubectl apply -f -
24+
# The help target prints out all targets with their descriptions organized
25+
# beneath their categories. The categories are represented by '##@' and the
26+
# target descriptions by '##'. The awk commands is responsible for reading the
27+
# entire set of makefiles included in this invocation, looking for lines of the
28+
# file as xyz: ## something, and then pretty-format the target and help. Then,
29+
# if there's a line with ##@ something, that gets pretty-printed as a category.
30+
# More info on the usage of ANSI control characters for terminal formatting:
31+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
32+
# More info on the awk command:
33+
# http://linuxcommand.org/lc3_adv_awk.php
3134

32-
# Uninstall CRDs from a cluster
33-
uninstall: manifests
34-
kustomize build config/crd | kubectl delete -f -
35+
help: ## Display this help.
36+
@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)
3537

36-
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
37-
deploy: manifests
38-
cd config/manager && kustomize edit set image controller=${IMG}
39-
kustomize build config/default | kubectl apply -f -
38+
##@ Development
4039

41-
# Teardown controller in the configured Kubernetes cluster in ~/.kube/config
42-
# This command does things that can't always re-run so the exit codes are ignored
43-
teardown: manifests
44-
kustomize build config/default | kubectl delete -f - || true
45-
46-
# Generate manifests e.g. CRD, RBAC etc.
47-
manifests: controller-gen
40+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
4841
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
4942

50-
# Run go fmt against code
51-
fmt:
43+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
44+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
45+
46+
fmt: ## Run go fmt against code.
5247
go fmt ./...
5348

54-
# Run go vet against code
55-
vet:
49+
vet: ## Run go vet against code.
5650
go vet ./...
5751

58-
# Run go mod vendor
52+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
53+
test: manifests generate fmt vet ## Run tests.
54+
mkdir -p ${ENVTEST_ASSETS_DIR}
55+
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
56+
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
57+
58+
##@ Build
59+
60+
build: generate fmt vet ## Build manager binary.
61+
go build -o bin/manager main.go
62+
63+
run: manifests generate fmt vet ## Run a controller from your host.
64+
go run ./main.go
65+
66+
docker-build: vendor test ## Build docker image with the manager.
67+
docker build -t ${IMG} .
68+
5969
vendor:
6070
go mod vendor
6171

62-
# Generate code
63-
generate: controller-gen
64-
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."
72+
docker-push: ## Push docker image with the manager.
73+
docker push ${IMG}
6574

66-
# Build the docker image
67-
docker-build: vendor test
68-
docker build . -t ${IMG}
75+
##@ Deployment
6976

70-
# Push the docker image
71-
docker-push:
72-
docker push ${IMG}
77+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
78+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
7379

74-
# find or download controller-gen
75-
# download controller-gen if necessary
76-
controller-gen:
77-
ifeq (, $(shell which controller-gen))
78-
@{ \
79-
set -e ;\
80-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
81-
cd $$CONTROLLER_GEN_TMP_DIR ;\
82-
go mod init tmp ;\
83-
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
84-
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
85-
}
86-
CONTROLLER_GEN=$(GOBIN)/controller-gen
87-
else
88-
CONTROLLER_GEN=$(shell which controller-gen)
89-
endif
80+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
81+
$(KUSTOMIZE) build config/crd | kubectl delete -f -
82+
83+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
84+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
85+
$(KUSTOMIZE) build config/default | kubectl apply -f -
86+
87+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
88+
$(KUSTOMIZE) build config/default | kubectl delete -f -
89+
90+
91+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
92+
controller-gen: ## Download controller-gen locally if necessary.
93+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
94+
95+
KUSTOMIZE = $(shell pwd)/bin/kustomize
96+
kustomize: ## Download kustomize locally if necessary.
97+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
9098

99+
# go-get-tool will 'go get' any package $2 and install it to $1.
100+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
101+
define go-get-tool
102+
@[ -f $(1) ] || { \
103+
set -e ;\
104+
TMP_DIR=$$(mktemp -d) ;\
105+
cd $$TMP_DIR ;\
106+
go mod init tmp ;\
107+
echo "Downloading $(2)" ;\
108+
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
109+
rm -rf $$TMP_DIR ;\
110+
}
111+
endef

examples/guestbook-operator/PROJECT

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
domain: example.org
2-
repo: sigs.k8s.io/kubebuilder-declarative-pattern
2+
layout:
3+
- go.kubebuilder.io/v3
4+
- declarative.go.kubebuilder.io/v1
5+
plugins:
6+
declarative.go.kubebuilder.io/v1:
7+
resources:
8+
- domain: example.org
9+
group: addons
10+
kind: Guestbook
11+
version: v1alpha1
12+
projectName: guestbook-operator
13+
repo: example.org/guestbook-operator
314
resources:
4-
- group: addons
15+
- api:
16+
crdVersion: v1
17+
namespaced: true
18+
controller: true
19+
domain: example.org
20+
group: addons
521
kind: Guestbook
22+
path: example.org/guestbook-operator/api/v1alpha1
623
version: v1alpha1
7-
version: "2"
24+
version: "3"

0 commit comments

Comments
 (0)