Skip to content

Commit 3d257c3

Browse files
committed
Implement basic operator
0 parents  commit 3d257c3

Some content is hidden

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

46 files changed

+2578
-0
lines changed

.dockerignore

+3
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

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

.golangci.yml

+47
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

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build the manager binary
2+
FROM golang:1.22 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

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Image URL to use all building/pushing image targets
2+
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
5+
6+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
7+
ifeq (,$(shell go env GOBIN))
8+
GOBIN=$(shell go env GOPATH)/bin
9+
else
10+
GOBIN=$(shell go env GOBIN)
11+
endif
12+
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
18+
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
23+
24+
.PHONY: all
25+
all: build
26+
27+
##@ General
28+
29+
# The help target prints out all targets with their descriptions organized
30+
# beneath their categories. The categories are represented by '##@' and the
31+
# target descriptions by '##'. The awk command is responsible for reading the
32+
# entire set of makefiles included in this invocation, looking for lines of the
33+
# file as xyz: ## something, and then pretty-format the target and help. Then,
34+
# if there's a line with ##@ something, that gets pretty-printed as a category.
35+
# More info on the usage of ANSI control characters for terminal formatting:
36+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
37+
# More info on the awk command:
38+
# http://linuxcommand.org/lc3_adv_awk.php
39+
40+
.PHONY: help
41+
help: ## Display this help.
42+
@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)
43+
44+
##@ Development
45+
46+
.PHONY: manifests
47+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
48+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
49+
50+
.PHONY: generate
51+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
52+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
53+
54+
.PHONY: fmt
55+
fmt: ## Run go fmt against code.
56+
go fmt ./...
57+
58+
.PHONY: vet
59+
vet: ## Run go vet against code.
60+
go vet ./...
61+
62+
.PHONY: test
63+
test: manifests generate fmt vet envtest ## Run tests.
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
78+
79+
##@ Build
80+
81+
.PHONY: build
82+
build: manifests generate fmt vet ## Build manager binary.
83+
go build -o bin/manager cmd/main.go
84+
85+
.PHONY: run
86+
run: manifests generate fmt vet ## Run a controller from your host.
87+
go run ./cmd/main.go
88+
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} .
95+
96+
.PHONY: docker-push
97+
docker-push: ## Push docker image with the manager.
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 memcached-operator-builder
112+
$(CONTAINER_TOOL) buildx use memcached-operator-builder
113+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
114+
- $(CONTAINER_TOOL) buildx rm memcached-operator-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
122+
123+
##@ Deployment
124+
125+
ifndef ignore-not-found
126+
ignore-not-found = false
127+
endif
128+
129+
.PHONY: install
130+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
131+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
132+
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 -
136+
137+
.PHONY: deploy
138+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
139+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
140+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
141+
142+
.PHONY: undeploy
143+
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.
144+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
145+
146+
##@ Dependencies
147+
148+
## Location to install dependencies to
149+
LOCALBIN ?= $(shell pwd)/bin
150+
$(LOCALBIN):
151+
mkdir -p $(LOCALBIN)
152+
153+
## Tool Binaries
154+
KUBECTL ?= kubectl
155+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
156+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
157+
ENVTEST ?= $(LOCALBIN)/setup-envtest
158+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
159+
160+
## Tool Versions
161+
KUSTOMIZE_VERSION ?= v5.4.3
162+
CONTROLLER_TOOLS_VERSION ?= v0.16.1
163+
ENVTEST_VERSION ?= release-0.19
164+
GOLANGCI_LINT_VERSION ?= v1.59.1
165+
166+
.PHONY: kustomize
167+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
168+
$(KUSTOMIZE): $(LOCALBIN)
169+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
170+
171+
.PHONY: controller-gen
172+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
173+
$(CONTROLLER_GEN): $(LOCALBIN)
174+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
175+
176+
.PHONY: envtest
177+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
178+
$(ENVTEST): $(LOCALBIN)
179+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
180+
181+
.PHONY: golangci-lint
182+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
183+
$(GOLANGCI_LINT): $(LOCALBIN)
184+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
185+
186+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
187+
# $1 - target path with name of binary
188+
# $2 - package url which can be installed
189+
# $3 - specific version of package
190+
define go-install-tool
191+
@[ -f "$(1)-$(3)" ] || { \
192+
set -e; \
193+
package=$(2)@$(3) ;\
194+
echo "Downloading $${package}" ;\
195+
rm -f $(1) || true ;\
196+
GOBIN=$(LOCALBIN) go install $${package} ;\
197+
mv $(1) $(1)-$(3) ;\
198+
} ;\
199+
ln -sf $(1)-$(3) $(1)
200+
endef

PROJECT

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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: example.com
6+
layout:
7+
- go.kubebuilder.io/v4
8+
projectName: memcached-operator
9+
repo: github.com/sagoresarker/memcached-operator
10+
resources:
11+
- api:
12+
crdVersion: v1
13+
namespaced: true
14+
controller: true
15+
domain: example.com
16+
group: cache
17+
kind: MemcachedDeployment
18+
path: github.com/sagoresarker/memcached-operator/api/v1
19+
version: v1
20+
version: "3"

0 commit comments

Comments
 (0)