Skip to content

Commit 3289cbf

Browse files
committed
Migrate Operator code to kubebuilder v3
1 parent ecd1356 commit 3289cbf

80 files changed

Lines changed: 31311 additions & 27078 deletions

File tree

Some content is hidden

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

operator/.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore all files which are not go type
3+
!**/*.go
4+
!**/*.mod
5+
!**/*.sum

operator/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.so
77
*.dylib
88
bin
9+
testbin/*
910

1011
# Test binary, build with `go test -c`
1112
*.test

operator/.helmignore

Lines changed: 0 additions & 38 deletions
This file was deleted.

operator/Makefile

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
# Image URL to use all building/pushing image targets
3-
IMG ?= securecodebox/operator:latest
3+
IMG ?= controller:latest
44
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
5-
CRD_OPTIONS ?= "crd:trivialVersions=true"
5+
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
66

77
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
88
ifeq (,$(shell go env GOBIN))
@@ -11,70 +11,98 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14-
all: manager
14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
1519

16-
# Run tests
17-
test: generate fmt vet manifests
18-
go test ./... -coverprofile cover.out
20+
all: build
1921

20-
# Build manager binary
21-
manager: generate fmt vet
22-
go build -o bin/manager main.go
23-
24-
# Run against the configured Kubernetes cluster in ~/.kube/config
25-
run: generate fmt vet manifests
26-
go run ./main.go
22+
##@ General
2723

28-
# Install CRDs into a cluster
29-
install: manifests
30-
kustomize build config/crd | kubectl apply -f -
24+
# The help target prints out all targets with their descriptions organized
25+
# beneath their categories. The categories are represented by '##@' and the
26+
# target descriptions by '##'. The awk commands is responsible for reading the
27+
# entire set of makefiles included in this invocation, looking for lines of the
28+
# file as xyz: ## something, and then pretty-format the target and help. Then,
29+
# if there's a line with ##@ something, that gets pretty-printed as a category.
30+
# More info on the usage of ANSI control characters for terminal formatting:
31+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
32+
# More info on the awk command:
33+
# http://linuxcommand.org/lc3_adv_awk.php
3134

32-
# Uninstall CRDs from a cluster
33-
uninstall: manifests
34-
kustomize build config/crd | kubectl delete -f -
35+
help: ## Display this help.
36+
@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)
3537

36-
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
37-
deploy: manifests
38-
cd config/manager && kustomize edit set image controller=${IMG}
39-
kustomize build config/default | kubectl apply -f -
38+
##@ Development
4039

41-
# Generate manifests e.g. CRD, RBAC etc.
42-
manifests: controller-gen
40+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
4341
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
4442

45-
# Run go fmt against code
46-
fmt:
43+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
44+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
45+
46+
fmt: ## Run go fmt against code.
4747
go fmt ./...
4848

49-
# Run go vet against code
50-
vet:
49+
vet: ## Run go vet against code.
5150
go vet ./...
5251

53-
# Generate code
54-
generate: controller-gen
55-
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
52+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
53+
test: manifests generate fmt vet ## Run tests.
54+
mkdir -p ${ENVTEST_ASSETS_DIR}
55+
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.2/hack/setup-envtest.sh
56+
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
57+
58+
##@ Build
59+
60+
build: generate fmt vet ## Build manager binary.
61+
go build -o bin/manager main.go
62+
63+
run: manifests generate fmt vet ## Run a controller from your host.
64+
go run ./main.go
5665

57-
# Build the docker image
58-
docker-build: test
59-
docker build . -t ${IMG}
66+
docker-build: test ## Build docker image with the manager.
67+
docker build -t ${IMG} .
6068

61-
# Push the docker image
62-
docker-push:
69+
docker-push: ## Push docker image with the manager.
6370
docker push ${IMG}
6471

65-
# find or download controller-gen
66-
# download controller-gen if necessary
67-
controller-gen:
68-
ifeq (, $(shell which controller-gen))
69-
@{ \
70-
set -e ;\
71-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
72-
cd $$CONTROLLER_GEN_TMP_DIR ;\
73-
go mod init tmp ;\
74-
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.4 ;\
75-
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
76-
}
77-
CONTROLLER_GEN=$(GOBIN)/controller-gen
78-
else
79-
CONTROLLER_GEN=$(shell which controller-gen)
80-
endif
72+
##@ Deployment
73+
74+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
75+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
76+
77+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
78+
$(KUSTOMIZE) build config/crd | kubectl delete -f -
79+
80+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
81+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
82+
$(KUSTOMIZE) build config/default | kubectl apply -f -
83+
84+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
85+
$(KUSTOMIZE) build config/default | kubectl delete -f -
86+
87+
88+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
89+
controller-gen: ## Download controller-gen locally if necessary.
90+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
91+
92+
KUSTOMIZE = $(shell pwd)/bin/kustomize
93+
kustomize: ## Download kustomize locally if necessary.
94+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
95+
96+
# go-get-tool will 'go get' any package $2 and install it to $1.
97+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
98+
define go-get-tool
99+
@[ -f $(1) ] || { \
100+
set -e ;\
101+
TMP_DIR=$$(mktemp -d) ;\
102+
cd $$TMP_DIR ;\
103+
go mod init tmp ;\
104+
echo "Downloading $(2)" ;\
105+
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
106+
rm -rf $$TMP_DIR ;\
107+
}
108+
endef

operator/PROJECT

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
domain: securecodebox.io
2+
layout:
3+
- go.kubebuilder.io/v3
24
multigroup: true
5+
projectName: operator
36
repo: github.com/secureCodeBox/secureCodeBox
47
resources:
5-
- group: execution
8+
- api:
9+
crdVersion: v1
10+
namespaced: true
11+
controller: true
12+
domain: securecodebox.io
13+
group: execution
614
kind: Scan
15+
path: github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1
716
version: v1
8-
- group: execution
17+
- api:
18+
crdVersion: v1
19+
namespaced: true
20+
domain: securecodebox.io
21+
group: execution
922
kind: ScanType
23+
path: github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1
1024
version: v1
11-
- group: execution
12-
kind: PersistenceProvider
13-
version: v1
14-
- group: execution
25+
- api:
26+
crdVersion: v1
27+
namespaced: true
28+
domain: securecodebox.io
29+
group: execution
1530
kind: ParseDefinition
31+
path: github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1
1632
version: v1
17-
- group: execution
18-
kind: ScheduledScan
33+
- api:
34+
crdVersion: v1
35+
namespaced: true
36+
domain: securecodebox.io
37+
group: execution
38+
kind: ScanCompletionHook
39+
path: github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1
1940
version: v1
20-
- group: cascading
41+
- api:
42+
crdVersion: v1
43+
namespaced: true
44+
domain: securecodebox.io
45+
group: cascading
2146
kind: CascadingRule
47+
path: github.com/secureCodeBox/secureCodeBox/operator/apis/cascading/v1
48+
version: v1
49+
- api:
50+
crdVersion: v1
51+
namespaced: true
52+
controller: true
53+
domain: securecodebox.io
54+
group: execution
55+
kind: ScheduledScan
56+
path: github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1
2257
version: v1
23-
version: "2"
58+
version: "3"

operator/apis/cascading/v1/groupversion_info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2020 iteratec GmbH.
2+
Copyright 2021.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -15,8 +15,8 @@ limitations under the License.
1515
*/
1616

1717
// Package v1 contains API Schema definitions for the cascading v1 API group
18-
// +kubebuilder:object:generate=true
19-
// +groupName=cascading.securecodebox.io
18+
//+kubebuilder:object:generate=true
19+
//+groupName=cascading.securecodebox.io
2020
package v1
2121

2222
import (

operator/apis/cascading/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

operator/apis/execution/v1/groupversion_info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2020 iteratec GmbH.
2+
Copyright 2021.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -15,8 +15,8 @@ limitations under the License.
1515
*/
1616

1717
// Package v1 contains API Schema definitions for the execution v1 API group
18-
// +kubebuilder:object:generate=true
19-
// +groupName=execution.securecodebox.io
18+
//+kubebuilder:object:generate=true
19+
//+groupName=execution.securecodebox.io
2020
package v1
2121

2222
import (
File renamed without changes.

operator/apis/execution/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)