-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bump kubebuilder version from v2
to v3
#40
Changes from 2 commits
3680ffe
abc08ba
80d4bd9
6da13d4
cf93c7c
fc6f132
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
bin/ | ||
config/ | ||
.git/ | ||
hack/ | ||
.gitignore | ||
LICENSE | ||
Makefile | ||
PROJECT | ||
README.md | ||
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file | ||
# Ignore all files which are not go type | ||
!**/*.go | ||
!**/*.mod | ||
!**/*.sum |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ vendor/ | |
*.so | ||
*.dylib | ||
bin | ||
testbin/* | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
# Build the manager binary | ||
FROM golang:1.14.0-buster AS builder | ||
FROM golang:1.16 AS builder | ||
|
||
WORKDIR /workspace | ||
|
||
# Copy the Go Modules manifests | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
# cache deps before building and copying source so that we don't need to re-download as much | ||
# and so that source changes don't invalidate our downloaded layer | ||
RUN go mod download | ||
|
||
# Copy the go source | ||
COPY pkg/ pkg/ | ||
COPY cmd/ cmd/ | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager cmd/spanner-autoscaler/main.go | ||
|
||
FROM gcr.io/distroless/static:nonroot | ||
# Build | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager cmd/spanner-autoscaler/main.go | ||
|
||
# Use distroless as minimal base image to package the manager binary | ||
# Refer to https://github.com/GoogleContainerTools/distroless for more details | ||
FROM gcr.io/distroless/static:nonroot | ||
WORKDIR / | ||
COPY --from=builder /workspace/manager . | ||
USER nonroot:nonroot | ||
USER 65532:65532 | ||
tkuchiki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ENTRYPOINT ["/manager"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,107 @@ | ||
GOOS := $(shell go env GOOS) | ||
GOARCH := $(shell go env GOARCH) | ||
|
||
# Image URL to use all building/pushing image targets | ||
IMG ?= mercari/spanner-autoscaler:latest | ||
rustycl0ck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion) | ||
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false" | ||
|
||
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) | ||
ifeq (,$(shell go env GOBIN)) | ||
GOBIN=$(shell go env GOPATH)/bin | ||
else | ||
GOBIN=$(shell go env GOBIN) | ||
endif | ||
|
||
# Setting SHELL to bash allows bash commands to be executed by recipes. | ||
# This is a requirement for 'setup-envtest.sh' in the test target. | ||
# Options are set to exit when a recipe line exits non-zero or a piped command fails. | ||
SHELL = /usr/bin/env bash -o pipefail | ||
.SHELLFLAGS = -ec | ||
|
||
all: build | ||
|
||
KUBEBUILDER_VERSION ?= 2.1.0 | ||
##@ General | ||
|
||
BIN_DIR := $(shell pwd)/bin | ||
# The help target prints out all targets with their descriptions organized | ||
# beneath their categories. The categories are represented by '##@' and the | ||
# target descriptions by '##'. The awk commands is responsible for reading the | ||
# entire set of makefiles included in this invocation, looking for lines of the | ||
# file as xyz: ## something, and then pretty-format the target and help. Then, | ||
# if there's a line with ##@ something, that gets pretty-printed as a category. | ||
# More info on the usage of ANSI control characters for terminal formatting: | ||
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters | ||
# More info on the awk command: | ||
# http://linuxcommand.org/lc3_adv_awk.php | ||
|
||
KUBEBUILDER_ASSETS_DIR := ${BIN_DIR}/kubebuilder | ||
KUBEBUILDER_ASSETS := ${KUBEBUILDER_ASSETS_DIR}/bin | ||
help: ## Display this help. | ||
@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) | ||
|
||
CONTROLLER_GEN := bin/controller-gen | ||
KIND := bin/kind | ||
KUBECTL := bin/kubectl | ||
SKAFFOLD := bin/skaffold | ||
KPT := bin/kpt | ||
##@ Development | ||
|
||
KIND_NODE_VERSION := 1.18.2 | ||
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. | ||
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases | ||
|
||
GO_TEST ?= go test | ||
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. | ||
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." | ||
|
||
all: manager | ||
fmt: ## Run go fmt against code. | ||
go fmt ./... | ||
|
||
vet: ## Run go vet against code. | ||
go vet ./... | ||
|
||
.PHONY: test | ||
test: kubebuilder | ||
@KUBEBUILDER_ASSETS=${KUBEBUILDER_ASSETS} $(GO_TEST) -v -race ./... | ||
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin | ||
test: manifests generate fmt vet ## Run tests. | ||
mkdir -p ${ENVTEST_ASSETS_DIR} | ||
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 | ||
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out | ||
|
||
# Run tests | ||
coverage: generate fmt vet manifests | ||
@KUBEBUILDER_ASSETS=${KUBEBUILDER_ASSETS} $(GO_TEST) -v -race ./... -covermode=atomic -coverprofile=cover.out | ||
##@ Build | ||
|
||
# Build manager binary | ||
manager: generate fmt vet | ||
build: generate fmt vet ## Build manager binary. | ||
go build -o bin/manager cmd/spanner-autoscaler/main.go | ||
|
||
# Install CRDs into a cluster | ||
install: manifests | ||
kustomize build config/crd | kubectl apply -f - | ||
run: manifests generate fmt vet ## Run a controller from your host. | ||
go run ./main.go | ||
|
||
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config | ||
deploy: manifests | ||
cd config/manager && kustomize edit set image controller=${IMG} | ||
kustomize build config/default | kubectl apply -f - | ||
docker-build: test ## Build docker image with the manager. | ||
docker build -t ${IMG} . | ||
|
||
.PHONY: fmt | ||
fmt: ## Run go fmt against code | ||
go fmt ./... | ||
docker-push: ## Push docker image with the manager. | ||
docker push ${IMG} | ||
|
||
.PHONY: vet | ||
vet: ## Run go vet against code | ||
go vet ./... | ||
##@ Deployment | ||
|
||
.PHONY: lint | ||
lint: lint/golangci-lint ## Run all linters. | ||
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. | ||
$(KUSTOMIZE) build config/crd | kubectl apply -f - | ||
|
||
.PHONY: tools/golangci-lint | ||
tools/golangci-lint: # go get 'golangci-lint' binary | ||
ifeq (, $(shell which golangci-lint)) | ||
GO111MODULE=off go get -u github.com/golangci/golangci-lint/cmd/golangci-lint | ||
endif | ||
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. | ||
$(KUSTOMIZE) build config/crd | kubectl delete -f - | ||
|
||
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. | ||
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} | ||
$(KUSTOMIZE) build config/default | kubectl apply -f - | ||
|
||
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. | ||
$(KUSTOMIZE) build config/default | kubectl delete -f - | ||
|
||
|
||
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen | ||
controller-gen: ## Download controller-gen locally if necessary. | ||
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1) | ||
|
||
KUSTOMIZE = $(shell pwd)/bin/kustomize | ||
kustomize: ## Download kustomize locally if necessary. | ||
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7) | ||
|
||
.PHONY: lint/golangci-lint | ||
lint/golangci-lint: tools/golangci-lint .golangci.yml ## Run golangci-lint. | ||
golangci-lint run | ||
|
||
# Build the docker image | ||
docker-build: | ||
docker image build . -t ${IMG} | ||
|
||
# Push the docker image | ||
docker-push: | ||
docker image push ${IMG} | ||
|
||
kubebuilder: ${KUBEBUILDER_ASSETS}/kubebuilder | ||
${KUBEBUILDER_ASSETS}/kubebuilder: | ||
@{ \ | ||
set -e ;\ | ||
TMP_DIR=./tmp/kubebuilder ;\ | ||
mkdir -p $$TMP_DIR ;\ | ||
curl -sSL https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${KUBEBUILDER_VERSION}/kubebuilder_${KUBEBUILDER_VERSION}_${GOOS}_${GOARCH}.tar.gz | tar -xz -C $$TMP_DIR ;\ | ||
mv $$TMP_DIR/kubebuilder_${KUBEBUILDER_VERSION}_${GOOS}_${GOARCH} ${KUBEBUILDER_ASSETS_DIR} ;\ | ||
rm -rf $$TMP_DIR ;\ | ||
} | ||
|
||
.PHONY: run | ||
run: $(SKAFFOLD) | ||
# NOTE: since skaffold uses kind executable from PATH directly, so this overrides PATH to use project local executable. | ||
@PATH=$${PWD}/bin:$${PATH} $(SKAFFOLD) dev --filename=./skaffold/skaffold.yaml | ||
|
||
.PHONY: manifests | ||
manifests: $(CONTROLLER_GEN) | ||
@$(CONTROLLER_GEN) crd:trivialVersions=true rbac:roleName=manager-role webhook paths="./pkg/api/..." output:crd:artifacts:config=config/crd/bases | ||
|
||
.PHONY: deepcopy | ||
deepcopy: $(CONTROLLER_GEN) | ||
@$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..." | ||
|
||
controlller-gen: $(CONTROLLER_GEN) | ||
$(CONTROLLER_GEN): go.sum | ||
@go build -o $(CONTROLLER_GEN) sigs.k8s.io/controller-tools/cmd/controller-gen | ||
|
||
kind: $(KIND) | ||
$(KIND): go.sum | ||
@go build -o ./bin/kind sigs.k8s.io/kind | ||
|
||
kubectl: $(KUBECTL) | ||
$(KUBECTL): .kubectl-version | ||
@curl -Lso $(KUBECTL) https://storage.googleapis.com/kubernetes-release/release/$(shell cat .kubectl-version)/bin/$(GOOS)/$(GOARCH)/kubectl | ||
@chmod +x $(KUBECTL) | ||
|
||
skaffold: $(SKAFFOLD) | ||
$(SKAFFOLD): .skaffold-version | ||
@curl -Lso $(SKAFFOLD) https://storage.googleapis.com/skaffold/releases/$(shell cat .skaffold-version)/skaffold-$(GOOS)-$(GOARCH) | ||
@chmod +x $(SKAFFOLD) | ||
|
||
kpt: $(KPT) | ||
$(KPT): go.sum | ||
@go build -o $(KPT) github.com/GoogleContainerTools/kpt | ||
|
||
.PHONY: kind-cluster | ||
kind-cluster: $(KIND) $(KUBECTL) | ||
@$(KIND) delete cluster --name spanner-autoscaler | ||
@$(KIND) create cluster --name spanner-autoscaler --image kindest/node:v${KIND_NODE_VERSION} | ||
@$(KUBECTL) apply -f ./config/crd/bases/spanner.mercari.com_spannerautoscalers.yaml | ||
@$(KUBECTL) apply -f ./kind/namespace.yaml | ||
@$(KUBECTL) apply -f ./kind/rbac | ||
|
||
.PHONY: kpt/crd | ||
kpt/crd: manifests kpt | ||
@kustomize build config/crd > ./kpt/crd/spannerautoscalers.spanner.mercari.com.yaml | ||
Comment on lines
-64
to
-132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason why you removed these make tasks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ref
Let me know what you think and which (or all) targets should be ported over. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks!
OK! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the |
||
# go-get-tool will 'go get' any package $2 and install it to $1. | ||
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) | ||
define go-get-tool | ||
@[ -f $(1) ] || { \ | ||
set -e ;\ | ||
TMP_DIR=$$(mktemp -d) ;\ | ||
cd $$TMP_DIR ;\ | ||
go mod init tmp ;\ | ||
echo "Downloading $(2)" ;\ | ||
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ | ||
rm -rf $$TMP_DIR ;\ | ||
} | ||
endef |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
version: "2" | ||
version: "3" | ||
domain: mercari.com | ||
layout: | ||
- go.kubebuilder.io/v3 | ||
projectName: spanner-autoscaler | ||
repo: github.com/mercari/spanner-autoscaler | ||
resources: | ||
- group: spanner | ||
version: v1alpha1 | ||
kind: SpannerAutoscaler | ||
controller: true | ||
domain: mercari.com | ||
path: github.com/mercari/spanner-autoscaler/api/v1alpha1 | ||
api: | ||
crdVersion: v1 | ||
namespaced: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import ( | |
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
@@ -46,29 +47,28 @@ var ( | |
|
||
func init() { | ||
//nolint:errcheck | ||
clientgoscheme.AddToScheme(scheme) | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
|
||
//nolint:errcheck | ||
spannerv1alpha1.AddToScheme(scheme) | ||
utilruntime.Must(spannerv1alpha1.AddToScheme(scheme)) | ||
|
||
// +kubebuilder:scaffold:scheme | ||
} | ||
|
||
var ( | ||
metricsAddr = flag.String("metrics-addr", "localhost:8090", "The address the metric endpoint binds to.") | ||
healthzAddr = flag.String("healthz-addr", "localhost:8091", "The address the healthz endpoint binds to.") | ||
scaleDownInterval = flag.Duration("scale-down-interval", 55*time.Minute, "The scale down interval.") | ||
metricsAddr = flag.String("metrics-bind-address", ":8080", "The address the metric endpoint binds to.") | ||
probeAddr = flag.String("health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") | ||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't have a strong motivation, it might be better to use the same flags and ports. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were the default values generated by Ref: https://book.kubebuilder.io/migration/manually_migration_guide_v2_v3.html#rename-the-manager-flags There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understood, thanks! |
||
enableLeaderElection = flag.Bool("leader-elect", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.") | ||
scaleDownInterval = flag.Duration("scale-down-interval", 55*time.Minute, "The scale down interval.") | ||
) | ||
|
||
const ( | ||
exitCode = 1 | ||
enableLeaderElection = true | ||
leaderElectionID = "spanner-autoscaler-leader-election" | ||
|
||
healthzEndpoint = "/healthz" | ||
readyzEndpoint = "/readyz" | ||
healthzName = "healthz" | ||
readyzName = "readyz" | ||
exitCode = 1 | ||
leaderElectionID = "spanner-autoscaler-leader-election" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was created by default with the hash of repo name: |
||
healthzEndpoint = "/healthz" | ||
readyzEndpoint = "/readyz" | ||
healthzName = "healthz" | ||
readyzName = "readyz" | ||
) | ||
|
||
func main() { | ||
|
@@ -112,7 +112,7 @@ func run() error { | |
log.V(1).Info( | ||
"flags", | ||
"metricsAddr", metricsAddr, | ||
"helthzAddr", healthzAddr, | ||
"probeAddr", probeAddr, | ||
"scaleDownInterval", scaleDownInterval, | ||
) | ||
|
||
|
@@ -123,10 +123,10 @@ func run() error { | |
|
||
mgr, err := ctrlmanager.New(cfg, ctrlmanager.Options{ | ||
Scheme: scheme, | ||
LeaderElection: enableLeaderElection, | ||
LeaderElection: *enableLeaderElection, | ||
LeaderElectionID: leaderElectionID, | ||
MetricsBindAddress: *metricsAddr, | ||
HealthProbeBindAddress: *healthzAddr, | ||
HealthProbeBindAddress: *probeAddr, | ||
ReadinessEndpointName: readyzEndpoint, | ||
LivenessEndpointName: healthzEndpoint, | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not need to remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just kept the default file in this case because I think it looked cleaner to have an allow-list instead have a block-list (block-list has to be updated whenever a new kind of file is added to the project). Let me know if you prefer to have both and I can add them back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following configures seem to be correct.
You can check the following command.
!**/*.go
doesn't work, so I choose!pkg
and!cmd
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent catch!! Thank you 🙇♂️ , I was not aware that this is an open bug in docker. I have now fixed it as per your recommendation since
!
does not work with wild cards.References:
.dockerignore
does not do what it claims kubernetes-sigs/kubebuilder#2073