Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit a620e3f

Browse files
committed
chore: scaffold with kubebuilder
1 parent 0cc4f9f commit a620e3f

26 files changed

+1286
-0
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore build and test binaries.
3+
bin/

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin/*
9+
Dockerfile.cross
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Kubernetes Generated files - skip generated files, except for vendored files
18+
19+
!vendor/**/zz_generated.*
20+
21+
# editor and IDE paraphernalia
22+
.idea
23+
.vscode
24+
*.swp
25+
*.swo
26+
*~

Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build the manager binary
2+
FROM golang:1.20 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN go mod download
13+
14+
# Copy the go source
15+
COPY cmd/main.go cmd/main.go
16+
COPY api/ api/
17+
COPY internal/controller/ internal/controller/
18+
19+
# 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
25+
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
29+
WORKDIR /
30+
COPY --from=builder /workspace/manager .
31+
USER 65532:65532
32+
33+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= controller:latest
4+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5+
ENVTEST_K8S_VERSION = 1.28.0
6+
7+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8+
ifeq (,$(shell go env GOBIN))
9+
GOBIN=$(shell go env GOPATH)/bin
10+
else
11+
GOBIN=$(shell go env GOBIN)
12+
endif
13+
14+
# CONTAINER_TOOL defines the container tool to be used for building images.
15+
# Be aware that the target commands are only tested with Docker which is
16+
# scaffolded by default. However, you might want to replace it to use other
17+
# tools. (i.e. podman)
18+
CONTAINER_TOOL ?= docker
19+
20+
# Setting SHELL to bash allows bash commands to be executed by recipes.
21+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
22+
SHELL = /usr/bin/env bash -o pipefail
23+
.SHELLFLAGS = -ec
24+
25+
.PHONY: all
26+
all: build
27+
28+
##@ General
29+
30+
# The help target prints out all targets with their descriptions organized
31+
# beneath their categories. The categories are represented by '##@' and the
32+
# target descriptions by '##'. The awk command is responsible for reading the
33+
# entire set of makefiles included in this invocation, looking for lines of the
34+
# file as xyz: ## something, and then pretty-format the target and help. Then,
35+
# if there's a line with ##@ something, that gets pretty-printed as a category.
36+
# More info on the usage of ANSI control characters for terminal formatting:
37+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
38+
# More info on the awk command:
39+
# http://linuxcommand.org/lc3_adv_awk.php
40+
41+
.PHONY: help
42+
help: ## Display this help.
43+
@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)
44+
45+
##@ Development
46+
47+
.PHONY: manifests
48+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
49+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
50+
51+
.PHONY: generate
52+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
53+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
54+
55+
.PHONY: fmt
56+
fmt: ## Run go fmt against code.
57+
go fmt ./...
58+
59+
.PHONY: vet
60+
vet: ## Run go vet against code.
61+
go vet ./...
62+
63+
.PHONY: test
64+
test: manifests generate fmt vet envtest ## Run tests.
65+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
66+
67+
##@ Build
68+
69+
.PHONY: build
70+
build: manifests generate fmt vet ## Build manager binary.
71+
go build -o bin/manager cmd/main.go
72+
73+
.PHONY: run
74+
run: manifests generate fmt vet ## Run a controller from your host.
75+
go run ./cmd/main.go
76+
77+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
78+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
79+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
80+
.PHONY: docker-build
81+
docker-build: ## Build docker image with the manager.
82+
$(CONTAINER_TOOL) build -t ${IMG} .
83+
84+
.PHONY: docker-push
85+
docker-push: ## Push docker image with the manager.
86+
$(CONTAINER_TOOL) push ${IMG}
87+
88+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
89+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
90+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
91+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
92+
# - 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)
93+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
94+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
95+
.PHONY: docker-buildx
96+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
97+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
98+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
99+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
100+
$(CONTAINER_TOOL) buildx use project-v3-builder
101+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
102+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
103+
rm Dockerfile.cross
104+
105+
##@ Deployment
106+
107+
ifndef ignore-not-found
108+
ignore-not-found = false
109+
endif
110+
111+
.PHONY: install
112+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
113+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
114+
115+
.PHONY: uninstall
116+
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.
117+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
118+
119+
.PHONY: deploy
120+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
121+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
122+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
123+
124+
.PHONY: undeploy
125+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
126+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
127+
128+
##@ Build Dependencies
129+
130+
## Location to install dependencies to
131+
LOCALBIN ?= $(shell pwd)/bin
132+
$(LOCALBIN):
133+
mkdir -p $(LOCALBIN)
134+
135+
## Tool Binaries
136+
KUBECTL ?= kubectl
137+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
138+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
139+
ENVTEST ?= $(LOCALBIN)/setup-envtest
140+
141+
## Tool Versions
142+
KUSTOMIZE_VERSION ?= v5.1.1
143+
CONTROLLER_TOOLS_VERSION ?= v0.13.0
144+
145+
.PHONY: kustomize
146+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
147+
$(KUSTOMIZE): $(LOCALBIN)
148+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
149+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
150+
rm -rf $(LOCALBIN)/kustomize; \
151+
fi
152+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
153+
154+
.PHONY: controller-gen
155+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
156+
$(CONTROLLER_GEN): $(LOCALBIN)
157+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
158+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
159+
160+
.PHONY: envtest
161+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
162+
$(ENVTEST): $(LOCALBIN)
163+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

PROJECT

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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
5+
domain: app.spacelift.io
6+
layout:
7+
- go.kubebuilder.io/v4
8+
projectName: spacelift-operator
9+
repo: github.com/spacelift-io/spacelift-operator
10+
version: "3"

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# spacelift-operator
2+
// TODO(user): Add simple overview of use/purpose
3+
4+
## Description
5+
// TODO(user): An in-depth paragraph about your project and overview of use
6+
7+
## Getting Started
8+
You’ll need a Kubernetes cluster to run against. You can use [KIND](https://sigs.k8s.io/kind) to get a local cluster for testing, or run against a remote cluster.
9+
**Note:** Your controller will automatically use the current context in your kubeconfig file (i.e. whatever cluster `kubectl cluster-info` shows).
10+
11+
### Running on the cluster
12+
1. Install Instances of Custom Resources:
13+
14+
```sh
15+
kubectl apply -k config/samples/
16+
```
17+
18+
2. Build and push your image to the location specified by `IMG`:
19+
20+
```sh
21+
make docker-build docker-push IMG=<some-registry>/spacelift-operator:tag
22+
```
23+
24+
3. Deploy the controller to the cluster with the image specified by `IMG`:
25+
26+
```sh
27+
make deploy IMG=<some-registry>/spacelift-operator:tag
28+
```
29+
30+
### Uninstall CRDs
31+
To delete the CRDs from the cluster:
32+
33+
```sh
34+
make uninstall
35+
```
36+
37+
### Undeploy controller
38+
UnDeploy the controller from the cluster:
39+
40+
```sh
41+
make undeploy
42+
```
43+
44+
## Contributing
45+
// TODO(user): Add detailed information on how you would like others to contribute to this project
46+
47+
### How it works
48+
This project aims to follow the Kubernetes [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/).
49+
50+
It uses [Controllers](https://kubernetes.io/docs/concepts/architecture/controller/),
51+
which provide a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster.
52+
53+
### Test It Out
54+
1. Install the CRDs into the cluster:
55+
56+
```sh
57+
make install
58+
```
59+
60+
2. Run your controller (this will run in the foreground, so switch to a new terminal if you want to leave it running):
61+
62+
```sh
63+
make run
64+
```
65+
66+
**NOTE:** You can also run this in one step by running: `make install run`
67+
68+
### Modifying the API definitions
69+
If you are editing the API definitions, generate the manifests such as CRs or CRDs using:
70+
71+
```sh
72+
make manifests
73+
```
74+
75+
**NOTE:** Run `make --help` for more information on all potential `make` targets
76+
77+
More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)
78+
79+
## License
80+
81+
Copyright 2024.
82+
83+
Licensed under the Apache License, Version 2.0 (the "License");
84+
you may not use this file except in compliance with the License.
85+
You may obtain a copy of the License at
86+
87+
http://www.apache.org/licenses/LICENSE-2.0
88+
89+
Unless required by applicable law or agreed to in writing, software
90+
distributed under the License is distributed on an "AS IS" BASIS,
91+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
92+
See the License for the specific language governing permissions and
93+
limitations under the License.
94+

0 commit comments

Comments
 (0)