Skip to content

Commit d7944b9

Browse files
author
Michal Tichák
committed
[operator] OCTRL-1078 controller operator that is able to control all OCC tasks
1 parent adbed10 commit d7944b9

File tree

69 files changed

+9160
-0
lines changed

Some content is hidden

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

69 files changed

+9160
-0
lines changed

control-operator/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin
2+
cover.out
3+
/internal/controller/protos/generated

control-operator/Dockerfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Build the manager binary
2+
FROM golang:1.23 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+
RUN sed -i '\,replace github.com/AliceO2Group/Control,d' go.mod
11+
# cache deps before building and copying source so that we don't need to re-download as much
12+
# and so that source changes don't invalidate our downloaded layer
13+
RUN go mod download
14+
15+
# Copy the go source
16+
COPY cmd/main.go cmd/main.go
17+
COPY api/ api/
18+
COPY internal/controller/ internal/controller/
19+
20+
RUN go mod download github.com/AliceO2Group/Control
21+
# Build
22+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
23+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
24+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
25+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
26+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
27+
28+
# Use distroless as minimal base image to package the manager binary
29+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
30+
FROM gcr.io/distroless/static:nonroot
31+
WORKDIR /
32+
COPY --from=builder /workspace/manager .
33+
USER 65532:65532
34+
35+
ENTRYPOINT ["/manager"]

control-operator/Makefile

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
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 = "candidate,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=candidate,fast,stable)
12+
# - use environment variables to overwrite this value (e.g export CHANNELS="candidate,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+
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
28+
# This variable is used to construct full image tags for bundle and catalog images.
29+
#
30+
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31+
# alice.cern/operator-bundle:$VERSION and alice.cern/operator-catalog:$VERSION.
32+
IMAGE_TAG_BASE ?= teom/aliecs
33+
34+
# BUNDLE_IMG defines the image:tag used for the bundle.
35+
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
36+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
37+
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 ?= unknown
52+
53+
# Image URL to use all building/pushing image targets
54+
IMG ?= docker.io/teom/aliecs:latest
55+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
56+
ENVTEST_K8S_VERSION = 1.27.1
57+
58+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
59+
ifeq (,$(shell go env GOBIN))
60+
GOBIN=$(shell go env GOPATH)/bin
61+
else
62+
GOBIN=$(shell go env GOBIN)
63+
endif
64+
65+
# CONTAINER_TOOL defines the container tool to be used for building images.
66+
# Be aware that the target commands are only tested with Docker which is
67+
# scaffolded by default. However, you might want to replace it to use other
68+
# tools. (i.e. podman)
69+
CONTAINER_TOOL ?= docker
70+
71+
# Setting SHELL to bash allows bash commands to be executed by recipes.
72+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
73+
SHELL = /usr/bin/env bash -o pipefail
74+
.SHELLFLAGS = -ec
75+
76+
.PHONY: all
77+
all: build
78+
79+
##@ General
80+
81+
# The help target prints out all targets with their descriptions organized
82+
# beneath their categories. The categories are represented by '##@' and the
83+
# target descriptions by '##'. The awk commands is responsible for reading the
84+
# entire set of makefiles included in this invocation, looking for lines of the
85+
# file as xyz: ## something, and then pretty-format the target and help. Then,
86+
# if there's a line with ##@ something, that gets pretty-printed as a category.
87+
# More info on the usage of ANSI control characters for terminal formatting:
88+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
89+
# More info on the awk command:
90+
# http://linuxcommand.org/lc3_adv_awk.php
91+
92+
.PHONY: help
93+
help: ## Display this help.
94+
@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)
95+
96+
##@ Development
97+
98+
.PHONY: manifests
99+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
100+
# Note that the option maxDescLen=0 was added in the default scaffold in order to sort out the issue
101+
# Too long: must have at most 262144 bytes. By using kubectl apply to create / update resources an annotation
102+
# is created by K8s API to store the latest version of the resource ( kubectl.kubernetes.io/last-applied-configuration).
103+
# However, it has a size limit and if the CRD is too big with so many long descriptions as this one it will cause the failure.
104+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:maxDescLen=0 webhook paths="./..." output:crd:artifacts:config=config/crd/bases
105+
106+
# .PHONY: manifests
107+
# manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
108+
# $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
109+
110+
.PHONY: generate
111+
generate: controller-gen generate-proto ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
112+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
113+
114+
.PHONY: fmt
115+
fmt: ## Run go fmt against code.
116+
go fmt ./...
117+
118+
.PHONY: vet
119+
vet: ## Run go vet against code.
120+
go vet ./...
121+
122+
.PHONY: test
123+
test: manifests generate fmt vet envtest ## Run tests.
124+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
125+
126+
##@ Protobuf
127+
128+
.PHONY: generate-proto
129+
generate-proto: ## Generate protobuf and gRPC Go files from occ.proto.
130+
mkdir -p internal/controller/protos/generated
131+
protoc --go_out=internal/controller/protos/generated --go_opt=paths=source_relative --go-grpc_out=internal/controller/protos/generated --go-grpc_opt=paths=source_relative -I internal/controller/protos internal/controller/protos/occ.proto
132+
133+
.PHONY: clean-proto
134+
clean-proto: ## Remove generated protobuf and gRPC Go files.
135+
rm -rf internal/controller/protos/generated/*
136+
137+
##@ Build
138+
139+
.PHONY: build
140+
build: manifests generate fmt vet ## Build manager binary.
141+
go build -o bin/manager cmd/main.go
142+
143+
.PHONY: run
144+
run: manifests generate fmt vet ## Run a controller from your host.
145+
go run ./cmd/main.go
146+
147+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
148+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
149+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
150+
.PHONY: docker-build
151+
docker-build: test ## Build docker image with the manager.
152+
$(CONTAINER_TOOL) build -t ${IMG} .
153+
154+
.PHONY: docker-push
155+
docker-push: ## Push docker image with the manager.
156+
$(CONTAINER_TOOL) push ${IMG}
157+
158+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
159+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
160+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
161+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
162+
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
163+
# To properly provided solutions that supports more than one platform you should use this option.
164+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
165+
.PHONY: docker-buildx
166+
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
167+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
168+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
169+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
170+
$(CONTAINER_TOOL) buildx use project-v3-builder
171+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
172+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
173+
rm Dockerfile.cross
174+
175+
##@ Deployment
176+
177+
ifndef ignore-not-found
178+
ignore-not-found = false
179+
endif
180+
181+
.PHONY: install
182+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
183+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply --server-side -f -
184+
185+
.PHONY: uninstall
186+
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.
187+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
188+
189+
.PHONY: deploy
190+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
191+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
192+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f - --server-side
193+
194+
.PHONY: undeploy
195+
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.
196+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
197+
198+
##@ Build Dependencies
199+
200+
## Location to install dependencies to
201+
LOCALBIN ?= $(shell pwd)/bin
202+
$(LOCALBIN):
203+
mkdir -p $(LOCALBIN)
204+
205+
## Tool Binaries
206+
KUBECTL ?= kubectl
207+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
208+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
209+
ENVTEST ?= $(LOCALBIN)/setup-envtest
210+
211+
## Tool Versions
212+
KUSTOMIZE_VERSION ?= v5.0.1
213+
CONTROLLER_TOOLS_VERSION ?= v0.16.5
214+
215+
.PHONY: kustomize
216+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
217+
$(KUSTOMIZE): $(LOCALBIN)
218+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
219+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
220+
rm -rf $(LOCALBIN)/kustomize; \
221+
fi
222+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
223+
224+
.PHONY: controller-gen
225+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
226+
$(CONTROLLER_GEN): $(LOCALBIN)
227+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
228+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
229+
230+
.PHONY: envtest
231+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
232+
$(ENVTEST): $(LOCALBIN)
233+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
234+
235+
.PHONY: operator-sdk
236+
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
237+
operator-sdk: ## Download operator-sdk locally if necessary.
238+
ifeq (,$(wildcard $(OPERATOR_SDK)))
239+
ifeq (, $(shell which operator-sdk 2>/dev/null))
240+
@{ \
241+
set -e ;\
242+
mkdir -p $(dir $(OPERATOR_SDK)) ;\
243+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
244+
curl -sSLo $(OPERATOR_SDK) https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_$${OS}_$${ARCH} ;\
245+
chmod +x $(OPERATOR_SDK) ;\
246+
}
247+
else
248+
OPERATOR_SDK = $(shell which operator-sdk)
249+
endif
250+
endif
251+
252+
.PHONY: bundle
253+
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
254+
$(OPERATOR_SDK) generate kustomize manifests -q
255+
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
256+
$(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS)
257+
$(OPERATOR_SDK) bundle validate ./bundle
258+
259+
.PHONY: bundle-build
260+
bundle-build: ## Build the bundle image.
261+
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
262+
263+
.PHONY: bundle-push
264+
bundle-push: ## Push the bundle image.
265+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
266+
267+
.PHONY: opm
268+
OPM = ./bin/opm
269+
opm: ## Download opm locally if necessary.
270+
ifeq (,$(wildcard $(OPM)))
271+
ifeq (,$(shell which opm 2>/dev/null))
272+
@{ \
273+
set -e ;\
274+
mkdir -p $(dir $(OPM)) ;\
275+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
276+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.0/$${OS}-$${ARCH}-opm ;\
277+
chmod +x $(OPM) ;\
278+
}
279+
else
280+
OPM = $(shell which opm)
281+
endif
282+
endif
283+
284+
# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
285+
# These images MUST exist in a registry and be pull-able.
286+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
287+
288+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
289+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
290+
291+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
292+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
293+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
294+
endif
295+
296+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
297+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
298+
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
299+
.PHONY: catalog-build
300+
catalog-build: opm ## Build a catalog image.
301+
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
302+
303+
# Push the catalog image.
304+
.PHONY: catalog-push
305+
catalog-push: ## Push a catalog image.
306+
$(MAKE) docker-push IMG=$(CATALOG_IMG)

control-operator/PROJECT

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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: alice.cern
6+
layout:
7+
- go.kubebuilder.io/v4
8+
plugins:
9+
manifests.sdk.operatorframework.io/v2: {}
10+
scorecard.sdk.operatorframework.io/v2: {}
11+
projectName: operator
12+
repo: github.com/AliceO2Group/Control/operator
13+
resources:
14+
- api:
15+
crdVersion: v1
16+
namespaced: true
17+
controller: true
18+
domain: alice.cern
19+
group: aliecs
20+
kind: Task
21+
path: github.com/AliceO2Group/Control/operator/api/v1alpha1
22+
version: v1alpha1
23+
version: "3"

0 commit comments

Comments
 (0)