Skip to content

Commit 0876aa3

Browse files
Rakshith-Rmergify[bot]
authored andcommitted
add initial kubebuilder project setup
kubebuilder init --domain openshift.io Signed-off-by: Rakshith R <rar@redhat.com>
1 parent 6161d73 commit 0876aa3

26 files changed

+1451
-0
lines changed

.dockerignore

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

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin
9+
testbin/*
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+
*.swp
24+
*.swo
25+
*~

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build the manager binary
2+
FROM golang:1.16 as builder
3+
4+
WORKDIR /workspace
5+
# Copy the Go Modules manifests
6+
COPY go.mod go.mod
7+
COPY go.sum go.sum
8+
# cache deps before building and copying source so that we don't need to re-download as much
9+
# and so that source changes don't invalidate our downloaded layer
10+
RUN go mod download
11+
12+
# Copy the go source
13+
COPY main.go main.go
14+
COPY api/ api/
15+
COPY controllers/ controllers/
16+
17+
# Build
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
19+
20+
# Use distroless as minimal base image to package the manager binary
21+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
22+
FROM gcr.io/distroless/static:nonroot
23+
WORKDIR /
24+
COPY --from=builder /workspace/manager .
25+
USER 65532:65532
26+
27+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.22
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+
# 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
19+
20+
.PHONY: all
21+
all: build
22+
23+
##@ General
24+
25+
# The help target prints out all targets with their descriptions organized
26+
# beneath their categories. The categories are represented by '##@' and the
27+
# target descriptions by '##'. The awk commands is responsible for reading the
28+
# entire set of makefiles included in this invocation, looking for lines of the
29+
# file as xyz: ## something, and then pretty-format the target and help. Then,
30+
# if there's a line with ##@ something, that gets pretty-printed as a category.
31+
# More info on the usage of ANSI control characters for terminal formatting:
32+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
33+
# More info on the awk command:
34+
# http://linuxcommand.org/lc3_adv_awk.php
35+
36+
.PHONY: help
37+
help: ## Display this help.
38+
@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)
39+
40+
##@ Development
41+
42+
.PHONY: manifests
43+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
44+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
45+
46+
.PHONY: generate
47+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
48+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
49+
50+
.PHONY: fmt
51+
fmt: ## Run go fmt against code.
52+
go fmt ./...
53+
54+
.PHONY: vet
55+
vet: ## Run go vet against code.
56+
go vet ./...
57+
58+
.PHONY: test
59+
test: manifests generate fmt vet envtest ## Run tests.
60+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
61+
62+
##@ Build
63+
64+
.PHONY: build
65+
build: generate fmt vet ## Build manager binary.
66+
go build -o bin/manager main.go
67+
68+
.PHONY: run
69+
run: manifests generate fmt vet ## Run a controller from your host.
70+
go run ./main.go
71+
72+
.PHONY: docker-build
73+
docker-build: test ## Build docker image with the manager.
74+
docker build -t ${IMG} .
75+
76+
.PHONY: docker-push
77+
docker-push: ## Push docker image with the manager.
78+
docker push ${IMG}
79+
80+
##@ Deployment
81+
82+
ifndef ignore-not-found
83+
ignore-not-found = false
84+
endif
85+
86+
.PHONY: install
87+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
88+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
89+
90+
.PHONY: uninstall
91+
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.
92+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
93+
94+
.PHONY: deploy
95+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
96+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
97+
$(KUSTOMIZE) build config/default | kubectl apply -f -
98+
99+
.PHONY: undeploy
100+
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.
101+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
102+
103+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
104+
.PHONY: controller-gen
105+
controller-gen: ## Download controller-gen locally if necessary.
106+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0)
107+
108+
KUSTOMIZE = $(shell pwd)/bin/kustomize
109+
.PHONY: kustomize
110+
kustomize: ## Download kustomize locally if necessary.
111+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
112+
113+
ENVTEST = $(shell pwd)/bin/setup-envtest
114+
.PHONY: envtest
115+
envtest: ## Download envtest-setup locally if necessary.
116+
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
117+
118+
# go-get-tool will 'go get' any package $2 and install it to $1.
119+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
120+
define go-get-tool
121+
@[ -f $(1) ] || { \
122+
set -e ;\
123+
TMP_DIR=$$(mktemp -d) ;\
124+
cd $$TMP_DIR ;\
125+
go mod init tmp ;\
126+
echo "Downloading $(2)" ;\
127+
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
128+
rm -rf $$TMP_DIR ;\
129+
}
130+
endef

PROJECT

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
domain: openshift.io
2+
layout:
3+
- go.kubebuilder.io/v3
4+
projectName: kubernetes-csi-addons
5+
repo: github.com/csi-addons/kubernetes-csi-addons
6+
version: "3"

config/default/kustomization.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Adds namespace to all resources.
2+
namespace: kubernetes-csi-addons-system
3+
4+
# Value of this field is prepended to the
5+
# names of all resources, e.g. a deployment named
6+
# "wordpress" becomes "alices-wordpress".
7+
# Note that it should also match with the prefix (text before '-') of the namespace
8+
# field above.
9+
namePrefix: kubernetes-csi-addons-
10+
11+
# Labels to add to all resources and selectors.
12+
#commonLabels:
13+
# someName: someValue
14+
15+
bases:
16+
- ../crd
17+
- ../rbac
18+
- ../manager
19+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
20+
# crd/kustomization.yaml
21+
#- ../webhook
22+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
23+
#- ../certmanager
24+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
25+
#- ../prometheus
26+
27+
patchesStrategicMerge:
28+
# Protect the /metrics endpoint by putting it behind auth.
29+
# If you want your controller-manager to expose the /metrics
30+
# endpoint w/o any authn/z, please comment the following line.
31+
- manager_auth_proxy_patch.yaml
32+
33+
# Mount the controller config file for loading manager configurations
34+
# through a ComponentConfig type
35+
#- manager_config_patch.yaml
36+
37+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
38+
# crd/kustomization.yaml
39+
#- manager_webhook_patch.yaml
40+
41+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
42+
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
43+
# 'CERTMANAGER' needs to be enabled to use ca injection
44+
#- webhookcainjection_patch.yaml
45+
46+
# the following config is for teaching kustomize how to do var substitution
47+
vars:
48+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
49+
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
50+
# objref:
51+
# kind: Certificate
52+
# group: cert-manager.io
53+
# version: v1
54+
# name: serving-cert # this name should match the one in certificate.yaml
55+
# fieldref:
56+
# fieldpath: metadata.namespace
57+
#- name: CERTIFICATE_NAME
58+
# objref:
59+
# kind: Certificate
60+
# group: cert-manager.io
61+
# version: v1
62+
# name: serving-cert # this name should match the one in certificate.yaml
63+
#- name: SERVICE_NAMESPACE # namespace of the service
64+
# objref:
65+
# kind: Service
66+
# version: v1
67+
# name: webhook-service
68+
# fieldref:
69+
# fieldpath: metadata.namespace
70+
#- name: SERVICE_NAME
71+
# objref:
72+
# kind: Service
73+
# version: v1
74+
# name: webhook-service
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This patch inject a sidecar container which is a HTTP proxy for the
2+
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
3+
apiVersion: apps/v1
4+
kind: Deployment
5+
metadata:
6+
name: controller-manager
7+
namespace: system
8+
spec:
9+
template:
10+
spec:
11+
containers:
12+
- name: kube-rbac-proxy
13+
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
14+
args:
15+
- "--secure-listen-address=0.0.0.0:8443"
16+
- "--upstream=http://127.0.0.1:8080/"
17+
- "--logtostderr=true"
18+
- "--v=10"
19+
ports:
20+
- containerPort: 8443
21+
protocol: TCP
22+
name: https
23+
- name: manager
24+
args:
25+
- "--health-probe-bind-address=:8081"
26+
- "--metrics-bind-address=127.0.0.1:8080"
27+
- "--leader-elect"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- name: manager
11+
args:
12+
- "--config=controller_manager_config.yaml"
13+
volumeMounts:
14+
- name: manager-config
15+
mountPath: /controller_manager_config.yaml
16+
subPath: controller_manager_config.yaml
17+
volumes:
18+
- name: manager-config
19+
configMap:
20+
name: manager-config
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: controller-runtime.sigs.k8s.io/v1alpha1
2+
kind: ControllerManagerConfig
3+
health:
4+
healthProbeBindAddress: :8081
5+
metrics:
6+
bindAddress: 127.0.0.1:8080
7+
webhook:
8+
port: 9443
9+
leaderElection:
10+
leaderElect: true
11+
resourceName: e8cd140a.openshift.io

config/manager/kustomization.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resources:
2+
- manager.yaml
3+
4+
generatorOptions:
5+
disableNameSuffixHash: true
6+
7+
configMapGenerator:
8+
- name: manager-config
9+
files:
10+
- controller_manager_config.yaml

0 commit comments

Comments
 (0)