Skip to content

Commit 8948315

Browse files
committed
*: update kubebuilder from v2 to v3 #113
[summary] go mod init github.com/radondb/radondb-mysql-kubernetes kubebuilder init --domain=radondb.com --owner "RadonDB" kubebuilder create api --group mysql --version v1alpha1 --kind Cluster --resource=true --controller=true make manifests
1 parent ed59b60 commit 8948315

Some content is hidden

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

42 files changed

+855
-480
lines changed

.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

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.15 as builder
2+
FROM golang:1.16 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests
@@ -15,13 +15,13 @@ COPY api/ api/
1515
COPY controllers/ controllers/
1616

1717
# Build
18-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
1919

2020
# Use distroless as minimal base image to package the manager binary
2121
# Refer to https://github.com/GoogleContainerTools/distroless for more details
2222
FROM gcr.io/distroless/static:nonroot
2323
WORKDIR /
2424
COPY --from=builder /workspace/manager .
25-
USER nonroot:nonroot
25+
USER 65532:65532
2626

2727
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Image URL to use all building/pushing image targets
33
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.8.3/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/[email protected] ;\
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/[email protected])
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/[email protected])
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

PROJECT

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
domain: radondb.com
2+
layout:
3+
- go.kubebuilder.io/v3
4+
projectName: radondb-mysql-kubernetes
25
repo: github.com/radondb/radondb-mysql-kubernetes
36
resources:
4-
- group: mysql
7+
- api:
8+
crdVersion: v1
9+
namespaced: true
10+
controller: true
11+
domain: radondb.com
12+
group: mysql
513
kind: Cluster
14+
path: github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1
615
version: v1alpha1
7-
version: "2"
16+
version: "3"

api/v1alpha1/cluster_types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
2+
Copyright 2021 RadonDB.
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.
@@ -28,7 +28,7 @@ type ClusterSpec struct {
2828
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
2929
// Important: Run "make" to regenerate code after modifying this file
3030

31-
// Foo is an example field of Cluster. Edit Cluster_types.go to remove/update
31+
// Foo is an example field of Cluster. Edit cluster_types.go to remove/update
3232
Foo string `json:"foo,omitempty"`
3333
}
3434

@@ -38,7 +38,8 @@ type ClusterStatus struct {
3838
// Important: Run "make" to regenerate code after modifying this file
3939
}
4040

41-
// +kubebuilder:object:root=true
41+
//+kubebuilder:object:root=true
42+
//+kubebuilder:subresource:status
4243

4344
// Cluster is the Schema for the clusters API
4445
type Cluster struct {
@@ -49,7 +50,7 @@ type Cluster struct {
4950
Status ClusterStatus `json:"status,omitempty"`
5051
}
5152

52-
// +kubebuilder:object:root=true
53+
//+kubebuilder:object:root=true
5354

5455
// ClusterList contains a list of Cluster
5556
type ClusterList struct {

api/v1alpha1/groupversion_info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
2+
Copyright 2021 RadonDB.
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 v1alpha1 contains API Schema definitions for the mysql v1alpha1 API group
18-
// +kubebuilder:object:generate=true
19-
// +groupName=mysql.radondb.com
18+
//+kubebuilder:object:generate=true
19+
//+groupName=mysql.radondb.com
2020
package v1alpha1
2121

2222
import (

api/v1alpha1/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.

config/certmanager/certificate.yaml

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

config/certmanager/kustomization.yaml

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

config/certmanager/kustomizeconfig.yaml

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.4.1
8+
creationTimestamp: null
9+
name: clusters.mysql.radondb.com
10+
spec:
11+
group: mysql.radondb.com
12+
names:
13+
kind: Cluster
14+
listKind: ClusterList
15+
plural: clusters
16+
singular: cluster
17+
scope: Namespaced
18+
versions:
19+
- name: v1alpha1
20+
schema:
21+
openAPIV3Schema:
22+
description: Cluster is the Schema for the clusters API
23+
properties:
24+
apiVersion:
25+
description: 'APIVersion defines the versioned schema of this representation
26+
of an object. Servers should convert recognized schemas to the latest
27+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
28+
type: string
29+
kind:
30+
description: 'Kind is a string value representing the REST resource this
31+
object represents. Servers may infer this from the endpoint the client
32+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
33+
type: string
34+
metadata:
35+
type: object
36+
spec:
37+
description: ClusterSpec defines the desired state of Cluster
38+
properties:
39+
foo:
40+
description: Foo is an example field of Cluster. Edit cluster_types.go
41+
to remove/update
42+
type: string
43+
type: object
44+
status:
45+
description: ClusterStatus defines the observed state of Cluster
46+
type: object
47+
type: object
48+
served: true
49+
storage: true
50+
subresources:
51+
status: {}
52+
status:
53+
acceptedNames:
54+
kind: ""
55+
plural: ""
56+
conditions: []
57+
storedVersions: []

config/crd/kustomization.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
# It should be run by config/default
44
resources:
55
- bases/mysql.radondb.com_clusters.yaml
6-
# +kubebuilder:scaffold:crdkustomizeresource
6+
#+kubebuilder:scaffold:crdkustomizeresource
77

88
patchesStrategicMerge:
99
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1010
# patches here are for enabling the conversion webhook for each CRD
1111
#- patches/webhook_in_clusters.yaml
12-
# +kubebuilder:scaffold:crdkustomizewebhookpatch
12+
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1313

1414
# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
1515
# patches here are for enabling the CA injection for each CRD
1616
#- patches/cainjection_in_clusters.yaml
17-
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
17+
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
1818

1919
# the following config is for teaching kustomize how to do kustomization for CRDs.
2020
configurations:

0 commit comments

Comments
 (0)