Skip to content

Commit e43305e

Browse files
authored
Update operator (#129)
* chore: bump to operator-sdk v1.39.2 & bump deps & other small improvements * fix: build * refac: replace 'patchesStrategicMerge' to 'patches' * refac: remove 'ControllerManagerConfiguration'
1 parent 2a2d767 commit e43305e

Some content is hidden

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

54 files changed

+1180
-1429
lines changed

.golangci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
run:
2+
timeout: 5m
3+
allow-parallel-runners: true
4+
5+
issues:
6+
# don't skip warning about doc comments
7+
# don't exclude the default set of lint
8+
exclude-use-default: false
9+
# restore some of the defaults
10+
# (fill in the rest as needed)
11+
exclude-rules:
12+
- path: "api/*"
13+
linters:
14+
- lll
15+
- path: "internal/*"
16+
linters:
17+
- dupl
18+
- lll
19+
linters:
20+
disable-all: true
21+
enable:
22+
- dupl
23+
- errcheck
24+
- exportloopref
25+
- ginkgolinter
26+
- goconst
27+
- gocyclo
28+
- gofmt
29+
- goimports
30+
- gosimple
31+
- govet
32+
- ineffassign
33+
- lll
34+
- misspell
35+
- nakedret
36+
- prealloc
37+
- revive
38+
- staticcheck
39+
- unconvert
40+
- unparam
41+
- unused
42+
43+
linters-settings:
44+
revive:
45+
rules:
46+
- name: comment-spacings

Dockerfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Build the manager binary
2-
FROM golang:1.19 as builder
2+
FROM golang:1.22 AS builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
35

46
WORKDIR /workspace
57
# Copy the Go Modules manifests
@@ -10,12 +12,16 @@ COPY go.sum go.sum
1012
RUN go mod download
1113

1214
# Copy the go source
13-
COPY main.go main.go
15+
COPY cmd/main.go cmd/main.go
1416
COPY api/ api/
15-
COPY controllers/ controllers/
17+
COPY internal/ internal/
1618

1719
# Build
18-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -a -o manager main.go
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
1925

2026
# Use distroless as minimal base image to package the manager binary
2127
# Refer to https://github.com/GoogleContainerTools/distroless for more details

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175

176176
END OF TERMS AND CONDITIONS
177177

178-
Copyright 2022 Adyanth H
178+
Copyright 2025 Adyanth H
179179

180180
Licensed under the Apache License, Version 2.0 (the "License");
181181
you may not use this file except in compliance with the License.

Makefile

Lines changed: 172 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,24 @@ IMAGE_TAG_BASE ?= adyanth/cloudflare-operator
3535
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
3636
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
3737

38+
# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command
39+
BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
40+
41+
# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests
42+
# You can enable this value if you would like to use SHA Based Digests
43+
# To enable set flag to true
44+
USE_IMAGE_DIGESTS ?= false
45+
ifeq ($(USE_IMAGE_DIGESTS), true)
46+
BUNDLE_GEN_FLAGS += --use-image-digests
47+
endif
48+
49+
# Set the Operator SDK version to use. By default, what is installed on the system is used.
50+
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
51+
OPERATOR_SDK_VERSION ?= v1.39.2
3852
# Image URL to use all building/pushing image targets
3953
IMG ?= $(IMAGE_TAG_BASE):$(VERSION)
4054
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
41-
ENVTEST_K8S_VERSION = 1.22
55+
ENVTEST_K8S_VERSION = 1.31.0
4256

4357
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
4458
ifeq (,$(shell go env GOBIN))
@@ -47,19 +61,25 @@ else
4761
GOBIN=$(shell go env GOBIN)
4862
endif
4963

64+
# CONTAINER_TOOL defines the container tool to be used for building images.
65+
# Be aware that the target commands are only tested with Docker which is
66+
# scaffolded by default. However, you might want to replace it to use other
67+
# tools. (i.e. podman)
68+
CONTAINER_TOOL ?= docker
69+
5070
# Setting SHELL to bash allows bash commands to be executed by recipes.
51-
# This is a requirement for 'setup-envtest.sh' in the test target.
5271
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
5372
SHELL = /usr/bin/env bash -o pipefail
5473
.SHELLFLAGS = -ec
5574

75+
.PHONY: all
5676
all: build
5777

5878
##@ General
5979

6080
# The help target prints out all targets with their descriptions organized
6181
# beneath their categories. The categories are represented by '##@' and the
62-
# target descriptions by '##'. The awk commands is responsible for reading the
82+
# target descriptions by '##'. The awk command is responsible for reading the
6383
# entire set of makefiles included in this invocation, looking for lines of the
6484
# file as xyz: ## something, and then pretty-format the target and help. Then,
6585
# if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -68,91 +88,191 @@ all: build
6888
# More info on the awk command:
6989
# http://linuxcommand.org/lc3_adv_awk.php
7090

91+
.PHONY: help
7192
help: ## Display this help.
7293
@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)
7394

7495
##@ Development
7596

97+
.PHONY: manifests
7698
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
7799
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
78100

101+
.PHONY: generate
79102
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
80103
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
81104

105+
.PHONY: fmt
82106
fmt: ## Run go fmt against code.
83107
go fmt ./...
84108

109+
.PHONY: vet
85110
vet: ## Run go vet against code.
86111
go vet ./...
87112

113+
.PHONY: test
88114
test: manifests generate fmt vet envtest ## Run tests.
89-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
115+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
116+
117+
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
118+
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
119+
test-e2e:
120+
go test ./test/e2e/ -v -ginkgo.v
121+
122+
.PHONY: lint
123+
lint: golangci-lint ## Run golangci-lint linter
124+
$(GOLANGCI_LINT) run
125+
126+
.PHONY: lint-fix
127+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
128+
$(GOLANGCI_LINT) run --fix
90129

91130
##@ Build
92131

93-
build: generate fmt vet ## Build manager binary.
94-
go build -o bin/manager main.go
132+
.PHONY: build
133+
build: manifests generate fmt vet ## Build manager binary.
134+
go build -o bin/manager cmd/main.go
95135

136+
.PHONY: run
96137
run: manifests generate fmt vet ## Run a controller from your host.
97-
go run ./main.go --cluster-resource-namespace=default
138+
go run ./cmd/main.go
98139

99-
docker-build: test ## Build docker image with the manager.
100-
docker build -t ${IMG} .
140+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
141+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
142+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
143+
.PHONY: docker-build
144+
docker-build: ## Build docker image with the manager.
145+
$(CONTAINER_TOOL) build -t ${IMG} .
101146

147+
.PHONY: docker-push
102148
docker-push: ## Push docker image with the manager.
103-
docker push ${IMG}
104-
105-
docker-multiarch-push: test ## Build and push for multiple architectures (amd64, arm and arm64)
106-
docker buildx build --push --platform linux/amd64,linux/arm,linux/arm64 --tag ${IMG} .
149+
$(CONTAINER_TOOL) push ${IMG}
150+
151+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
152+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
153+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
154+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
155+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
156+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
157+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
158+
.PHONY: docker-buildx
159+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
160+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
161+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
162+
- $(CONTAINER_TOOL) buildx create --name cloudflare-operator-builder
163+
$(CONTAINER_TOOL) buildx use cloudflare-operator-builder
164+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
165+
- $(CONTAINER_TOOL) buildx rm cloudflare-operator-builder
166+
rm Dockerfile.cross
167+
168+
.PHONY: build-installer
169+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
170+
mkdir -p dist
171+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
172+
$(KUSTOMIZE) build config/default > dist/install.yaml
107173

108174
##@ Deployment
109175

176+
ifndef ignore-not-found
177+
ignore-not-found = false
178+
endif
179+
180+
.PHONY: install
110181
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
111-
$(KUSTOMIZE) build config/crd | kubectl apply -f -
182+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
112183

113-
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
114-
$(KUSTOMIZE) build config/crd | kubectl delete -f -
184+
.PHONY: uninstall
185+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
186+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
115187

188+
.PHONY: deploy
116189
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
117190
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
118-
$(KUSTOMIZE) build config/default | kubectl apply -f -
119-
120-
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
121-
$(KUSTOMIZE) build config/default | kubectl delete -f -
122-
123-
124-
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
125-
controller-gen: ## Download controller-gen locally if necessary.
126-
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
127-
128-
KUSTOMIZE = $(shell pwd)/bin/kustomize
129-
kustomize: ## Download kustomize locally if necessary.
130-
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
131-
132-
ENVTEST = $(shell pwd)/bin/setup-envtest
133-
envtest: ## Download envtest-setup locally if necessary.
134-
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
135-
136-
# go-get-tool will 'go get' any package $2 and install it to $1.
137-
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
138-
define go-get-tool
139-
@[ -f $(1) ] || { \
140-
set -e ;\
141-
TMP_DIR=$$(mktemp -d) ;\
142-
cd $$TMP_DIR ;\
143-
go mod init tmp ;\
144-
echo "Downloading $(2)" ;\
145-
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
146-
rm -rf $$TMP_DIR ;\
147-
}
191+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
192+
193+
.PHONY: undeploy
194+
undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
195+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
196+
197+
##@ Dependencies
198+
199+
## Location to install dependencies to
200+
LOCALBIN ?= $(shell pwd)/bin
201+
$(LOCALBIN):
202+
mkdir -p $(LOCALBIN)
203+
204+
## Tool Binaries
205+
KUBECTL ?= kubectl
206+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
207+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
208+
ENVTEST ?= $(LOCALBIN)/setup-envtest
209+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
210+
211+
## Tool Versions
212+
KUSTOMIZE_VERSION ?= v5.4.3
213+
CONTROLLER_TOOLS_VERSION ?= v0.16.1
214+
ENVTEST_VERSION ?= release-0.19
215+
GOLANGCI_LINT_VERSION ?= v1.59.1
216+
217+
.PHONY: kustomize
218+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
219+
$(KUSTOMIZE): $(LOCALBIN)
220+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
221+
222+
.PHONY: controller-gen
223+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
224+
$(CONTROLLER_GEN): $(LOCALBIN)
225+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
226+
227+
.PHONY: envtest
228+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
229+
$(ENVTEST): $(LOCALBIN)
230+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
231+
232+
.PHONY: golangci-lint
233+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
234+
$(GOLANGCI_LINT): $(LOCALBIN)
235+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
236+
237+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
238+
# $1 - target path with name of binary
239+
# $2 - package url which can be installed
240+
# $3 - specific version of package
241+
define go-install-tool
242+
@[ -f "$(1)-$(3)" ] || { \
243+
set -e; \
244+
package=$(2)@$(3) ;\
245+
echo "Downloading $${package}" ;\
246+
rm -f $(1) || true ;\
247+
GOBIN=$(LOCALBIN) go install $${package} ;\
248+
mv $(1) $(1)-$(3) ;\
249+
} ;\
250+
ln -sf $(1)-$(3) $(1)
148251
endef
149252

253+
.PHONY: operator-sdk
254+
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
255+
operator-sdk: ## Download operator-sdk locally if necessary.
256+
ifeq (,$(wildcard $(OPERATOR_SDK)))
257+
ifeq (, $(shell which operator-sdk 2>/dev/null))
258+
@{ \
259+
set -e ;\
260+
mkdir -p $(dir $(OPERATOR_SDK)) ;\
261+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
262+
curl -sSLo $(OPERATOR_SDK) https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_$${OS}_$${ARCH} ;\
263+
chmod +x $(OPERATOR_SDK) ;\
264+
}
265+
else
266+
OPERATOR_SDK = $(shell which operator-sdk)
267+
endif
268+
endif
269+
150270
.PHONY: bundle
151-
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
152-
operator-sdk generate kustomize manifests -q
271+
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
272+
$(OPERATOR_SDK) generate kustomize manifests -q
153273
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
154-
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
155-
operator-sdk bundle validate ./bundle
274+
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
275+
$(OPERATOR_SDK) bundle validate ./bundle
156276

157277
.PHONY: bundle-build
158278
bundle-build: ## Build the bundle image.
@@ -163,15 +283,15 @@ bundle-push: ## Push the bundle image.
163283
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
164284

165285
.PHONY: opm
166-
OPM = ./bin/opm
286+
OPM = $(LOCALBIN)/opm
167287
opm: ## Download opm locally if necessary.
168288
ifeq (,$(wildcard $(OPM)))
169289
ifeq (,$(shell which opm 2>/dev/null))
170290
@{ \
171291
set -e ;\
172292
mkdir -p $(dir $(OPM)) ;\
173293
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
174-
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\
294+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
175295
chmod +x $(OPM) ;\
176296
}
177297
else

0 commit comments

Comments
 (0)