Skip to content

Commit 54f018f

Browse files
committed
Switch to kubebuilder.
1 parent f063792 commit 54f018f

File tree

65 files changed

+1557
-1068
lines changed

Some content is hidden

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

65 files changed

+1557
-1068
lines changed

.golangci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
- typecheck
40+
- unconvert
41+
- unparam
42+
- unused
43+
44+
linters-settings:
45+
revive:
46+
rules:
47+
- 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.23 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,13 +12,17 @@ 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/controller/ internal/controller/
1618
COPY pkg/ pkg/
1719

1820
# Build
19-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
21+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
22+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
23+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
24+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
25+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
2026

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

Makefile

Lines changed: 139 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
# VERSION defines the project version for the bundle.
2-
# Update this value when you upgrade the version of your project.
3-
# To re-generate a bundle for another specific version without changing the standard setup, you can:
4-
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
5-
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
6-
VERSION ?= 0.0.1
7-
8-
# CHANNELS define the bundle channels used in the bundle.
9-
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
10-
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
11-
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
12-
# - use environment variables to overwrite this value (e.g export CHANNELS="preview,fast,stable")
13-
ifneq ($(origin CHANNELS), undefined)
14-
BUNDLE_CHANNELS := --channels=$(CHANNELS)
15-
endif
16-
17-
# DEFAULT_CHANNEL defines the default channel used in the bundle.
18-
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
19-
# To re-generate a bundle for any other default channel without changing the default setup, you can:
20-
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
21-
# - use environment variables to overwrite this value (e.g export DEFAULT_CHANNEL="stable")
22-
ifneq ($(origin DEFAULT_CHANNEL), undefined)
23-
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
24-
endif
25-
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
26-
27-
# BUNDLE_IMG defines the image:tag used for the bundle.
28-
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
29-
BUNDLE_IMG ?= controller-bundle:$(VERSION)
30-
311
# Image URL to use all building/pushing image targets
322
IMG ?= controller:latest
3+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
4+
ENVTEST_K8S_VERSION = 1.31.0
335

346
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
357
ifeq (,$(shell go env GOBIN))
@@ -38,22 +10,25 @@ else
3810
GOBIN=$(shell go env GOBIN)
3911
endif
4012

41-
## Location to install dependencies to
42-
LOCALBIN ?= $(shell pwd)/bin
43-
$(LOCALBIN):
44-
mkdir -p $(LOCALBIN)
45-
13+
# CONTAINER_TOOL defines the container tool to be used for building images.
14+
# Be aware that the target commands are only tested with Docker which is
15+
# scaffolded by default. However, you might want to replace it to use other
16+
# tools. (i.e. podman)
17+
CONTAINER_TOOL ?= docker
4618

47-
ENVTEST ?= $(LOCALBIN)/setup-envtest
48-
ENVTEST_K8S_VERSION = 1.31.0
19+
# Setting SHELL to bash allows bash commands to be executed by recipes.
20+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
21+
SHELL = /usr/bin/env bash -o pipefail
22+
.SHELLFLAGS = -ec
4923

24+
.PHONY: all
5025
all: build
5126

5227
##@ General
5328

5429
# The help target prints out all targets with their descriptions organized
5530
# beneath their categories. The categories are represented by '##@' and the
56-
# target descriptions by '##'. The awk commands is responsible for reading the
31+
# target descriptions by '##'. The awk command is responsible for reading the
5732
# entire set of makefiles included in this invocation, looking for lines of the
5833
# file as xyz: ## something, and then pretty-format the target and help. Then,
5934
# if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -62,97 +37,169 @@ all: build
6237
# More info on the awk command:
6338
# http://linuxcommand.org/lc3_adv_awk.php
6439

40+
.PHONY: help
6541
help: ## Display this help.
6642
@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)
6743

6844
##@ Development
6945

46+
.PHONY: manifests
7047
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
71-
$(CONTROLLER_GEN) rbac:roleName=manager-role webhook crd paths="./..." output:crd:artifacts:config=config/crd/bases
48+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
7249

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

54+
.PHONY: fmt
7655
fmt: ## Run go fmt against code.
7756
go fmt ./...
7857

58+
.PHONY: vet
7959
vet: ## Run go vet against code.
8060
go vet ./...
8161

82-
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
62+
.PHONY: test
8363
test: manifests generate fmt vet envtest ## Run tests.
84-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
64+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
65+
66+
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
67+
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
68+
test-e2e:
69+
go test ./test/e2e/ -v -ginkgo.v
70+
71+
.PHONY: lint
72+
lint: golangci-lint ## Run golangci-lint linter
73+
$(GOLANGCI_LINT) run
74+
75+
.PHONY: lint-fix
76+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
77+
$(GOLANGCI_LINT) run --fix
8578

8679
##@ Build
8780

88-
build: generate fmt vet ## Build manager binary.
89-
go build -o bin/manager main.go
81+
.PHONY: build
82+
build: manifests generate fmt vet ## Build manager binary.
83+
go build -o bin/manager cmd/main.go
9084

85+
.PHONY: run
9186
run: manifests generate fmt vet ## Run a controller from your host.
92-
go run ./main.go
87+
go run ./cmd/main.go
9388

94-
docker-build: test ## Build docker image with the manager.
95-
docker build -t ${IMG} .
89+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
90+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
91+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
92+
.PHONY: docker-build
93+
docker-build: ## Build docker image with the manager.
94+
$(CONTAINER_TOOL) build -t ${IMG} .
9695

96+
.PHONY: docker-push
9797
docker-push: ## Push docker image with the manager.
98-
docker push ${IMG}
98+
$(CONTAINER_TOOL) push ${IMG}
99+
100+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
101+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
102+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
103+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
104+
# - 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)
105+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
106+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
107+
.PHONY: docker-buildx
108+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
109+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
110+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
111+
- $(CONTAINER_TOOL) buildx create --name gitpoller-controller-builder
112+
$(CONTAINER_TOOL) buildx use gitpoller-controller-builder
113+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
114+
- $(CONTAINER_TOOL) buildx rm gitpoller-controller-builder
115+
rm Dockerfile.cross
116+
117+
.PHONY: build-installer
118+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
119+
mkdir -p dist
120+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
121+
$(KUSTOMIZE) build config/default > dist/install.yaml
99122

100123
##@ Deployment
101124

125+
ifndef ignore-not-found
126+
ignore-not-found = false
127+
endif
128+
129+
.PHONY: install
102130
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
103-
$(KUSTOMIZE) build config/crd | kubectl apply -f -
131+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
104132

105-
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
106-
$(KUSTOMIZE) build config/crd | kubectl delete -f -
133+
.PHONY: uninstall
134+
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.
135+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
107136

137+
.PHONY: deploy
108138
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
109139
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
110-
$(KUSTOMIZE) build config/default | kubectl apply -f -
140+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
111141

112-
release: manifests kustomize ## build a release file containing the necessary resources to deploy.
142+
.PHONY: release
143+
release: manifests kustomize ## Write out a release file with the correct image.
113144
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
114-
$(KUSTOMIZE) build config/default > release-$(VERSION).yaml
115-
116-
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
117-
$(KUSTOMIZE) build config/default | kubectl delete -f -
118-
119-
120-
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
121-
controller-gen: ## Download controller-gen locally if necessary.
122-
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
123-
124-
KUSTOMIZE = $(shell pwd)/bin/kustomize
125-
kustomize: ## Download kustomize locally if necessary.
126-
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
127-
128-
# go-get-tool will 'go install' any package $2 and install it to $1.
129-
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
130-
define go-get-tool
131-
@[ -f $(1) ] || { \
132-
set -e ;\
133-
TMP_DIR=$$(mktemp -d) ;\
134-
cd $$TMP_DIR ;\
135-
go mod init tmp ;\
136-
echo "Downloading $(2)" ;\
137-
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
138-
rm -rf $$TMP_DIR ;\
139-
}
140-
endef
145+
$(KUSTOMIZE) build config/default > release.yaml
141146

142-
.PHONY: bundle ## Generate bundle manifests and metadata, then validate generated files.
143-
bundle: manifests kustomize
144-
operator-sdk generate kustomize manifests -q
145-
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
146-
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
147-
operator-sdk bundle validate ./bundle
147+
.PHONY: undeploy
148+
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.
149+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
148150

149-
.PHONY: bundle-build ## Build the bundle image.
150-
bundle-build:
151-
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
151+
##@ Dependencies
152152

153-
.PHONY: envtest
154-
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
155-
$(ENVTEST): $(LOCALBIN)
156-
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
153+
## Location to install dependencies to
154+
LOCALBIN ?= $(shell pwd)/bin
155+
$(LOCALBIN):
156+
mkdir -p $(LOCALBIN)
157+
158+
## Tool Binaries
159+
KUBECTL ?= kubectl
160+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
161+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
162+
ENVTEST ?= $(LOCALBIN)/setup-envtest
163+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
164+
165+
## Tool Versions
166+
KUSTOMIZE_VERSION ?= v5.4.3
167+
CONTROLLER_TOOLS_VERSION ?= v0.16.1
168+
ENVTEST_VERSION ?= release-0.19
169+
GOLANGCI_LINT_VERSION ?= v1.59.1
157170

171+
.PHONY: kustomize
172+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
173+
$(KUSTOMIZE): $(LOCALBIN)
174+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
158175

176+
.PHONY: controller-gen
177+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
178+
$(CONTROLLER_GEN): $(LOCALBIN)
179+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
180+
181+
.PHONY: envtest
182+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
183+
$(ENVTEST): $(LOCALBIN)
184+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
185+
186+
.PHONY: golangci-lint
187+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
188+
$(GOLANGCI_LINT): $(LOCALBIN)
189+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
190+
191+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
192+
# $1 - target path with name of binary
193+
# $2 - package url which can be installed
194+
# $3 - specific version of package
195+
define go-install-tool
196+
@[ -f "$(1)-$(3)" ] || { \
197+
set -e; \
198+
package=$(2)@$(3) ;\
199+
echo "Downloading $${package}" ;\
200+
rm -f $(1) || true ;\
201+
GOBIN=$(LOCALBIN) go install $${package} ;\
202+
mv $(1) $(1)-$(3) ;\
203+
} ;\
204+
ln -sf $(1)-$(3) $(1)
205+
endef

PROJECT

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
15
domain: gitops.tools
2-
layout: go.kubebuilder.io/v3
3-
plugins:
4-
manifests.sdk.operatorframework.io/v2: {}
5-
scorecard.sdk.operatorframework.io/v2: {}
6+
layout:
7+
- go.kubebuilder.io/v4
68
projectName: gitpoller-controller
79
repo: github.com/gitops-tools/gitpoller-controller
810
resources:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ There's an additional `release` target that will generate a file `release-<versi
154154

155155
## Development
156156

157-
This is an [operator-sdk](https://sdk.operatorframework.io/) derived controller.
157+
This is a kubebuilder derived controller.
158158

159159
## Roadmap
160160

0 commit comments

Comments
 (0)