Skip to content

Commit

Permalink
Update kubebuilder scaffolding and dependencies, use CRD v1 (#59)
Browse files Browse the repository at this point in the history
* update to latest kubebuilder scaffolding

bump dependencies

fix tests by creating valid service account token

bump golang to 1.17.3

fix issue with zap-devel flag

* bump controller-runtime to v0.10.2

* Use log from context kubernetes-sigs/kubebuilder#2181

* Tag boilerplate Makefile targets as .PHONY

* Add default-container annotation

* Add flag for ignore-not-found to make undeploy

* fix(scaffolds): better defaults

* Defer GinkgoRecover() in test goroutines

* ignore non-go files during Docker build

* bump golang to 1.17.5

* Apply suggestions from code review

Co-authored-by: Lukas Gross <35373687+grolu@users.noreply.github.com>

Co-authored-by: Lukas Gross <35373687+grolu@users.noreply.github.com>
  • Loading branch information
petersutter and grolu authored Apr 4, 2022
1 parent 0c5634d commit 1708f2f
Show file tree
Hide file tree
Showing 59 changed files with 2,223 additions and 1,452 deletions.
23 changes: 5 additions & 18 deletions .ci/check
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
#
# Copyright 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# SPDX-License-Identifier: Apache-2.0

set -o errexit
set -o pipefail
Expand All @@ -26,9 +16,6 @@ else
export SOURCE_PATH="$(readlink -f ${SOURCE_PATH})"
fi

# Install golangci-lint (linting tool)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.35.2

cd "$SOURCE_PATH"
export GOLANGCI_LINT_ADDITIONAL_FLAGS="--verbose --timeout 2m"

golangci-lint run ./... -E golint,whitespace,wsl --skip-files "zz_generated.*" --verbose --timeout 2m
"${SOURCE_PATH}"/hack/golangci-lint.sh
4 changes: 2 additions & 2 deletions .ci/pipeline_definitions
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ terminal-controller-manager:
image: 'eu.gcr.io/gardener-project/gardener/terminal-controller-manager'
steps:
check:
image: 'golang:1.16.6'
image: 'golang:1.17.5'
test:
image: 'golang:1.16.6'
image: 'golang:1.17.5'
jobs:
head-update:
traits:
Expand Down
32 changes: 5 additions & 27 deletions .ci/test
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
#
# Copyright 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# SPDX-License-Identifier: Apache-2.0

set -o errexit
set -o pipefail
Expand All @@ -26,18 +16,6 @@ else
export SOURCE_PATH="$(readlink -f ${SOURCE_PATH})"
fi

cd "$SOURCE_PATH"

TEST_BIN_DIR="$SOURCE_PATH/../dev/testbin"
mkdir -p ${TEST_BIN_DIR}

ENVTEST_ASSETS_DIR="$(realpath ${TEST_BIN_DIR})"

source "$SOURCE_PATH/hack/setup-envtest.sh"

fetch_envtest_tools ${ENVTEST_ASSETS_DIR}
setup_envtest_env ${ENVTEST_ASSETS_DIR}

echo "> Test"
export GO_TEST_ADDITIONAL_FLAGS="-race"

GO111MODULE=on go test ./... -race -coverprofile cover.out
"${SOURCE_PATH}"/hack/test-integration.sh
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore everything
**

# Exclude files relevant for build
!api/v1alpha1/*.go
!controllers/*.go
!test/*.go
!utils/*.go
!webhooks/*.go
!main.go
!go.mod
!go.sum
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

# Build the manager binary
FROM golang:1.16.6 as builder
FROM golang:1.17.5 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
194 changes: 122 additions & 72 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
# SPDX-License-Identifier: Apache-2.0

# Image URL to use all building/pushing image targets
IMG ?= eu.gcr.io/gardener-project/gardener/terminal-controller-manager:latest
IMG ?= eu.gcr.io/gardener-project/gardener/terminal-controller-manager

# Kube RBAC Proxy image to use
IMG_RBAC_PROXY ?= quay.io/brancz/kube-rbac-proxy:v0.8.0

# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"
REPO_ROOT := $(shell git rev-parse --show-toplevel)
VERSION := $(shell cat "$(REPO_ROOT)/VERSION")
EFFECTIVE_VERSION := $(VERSION)-$(shell git rev-parse HEAD)

CR_VERSION := $(shell go mod edit -json | jq -r '.Require[] | select(.Path=="sigs.k8s.io/controller-runtime") | .Version')

Expand All @@ -20,93 +21,142 @@ else
GOBIN=$(shell go env GOBIN)
endif

all: manager
# 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

# Run tests
test: generate lint manifests
go test ./... -coverprofile cover.out
.PHONY: all
all: build

# Build manager binary
manager: generate lint
go build -o bin/manager main.go
##@ General

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate lint
go run ./main.go
# 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

.PHONY: help
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%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate ClusterRole object.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./controllers/..." paths="./api/..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./controllers/..." paths="./api/..."

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -
.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -
.PHONY: lint
lint: $(GOPATH)/bin/golangci-lint ## Run golangci-lint against code.
golangci-lint run ./... -E golint,whitespace,wsl --skip-files "zz_generated.*"

.PHONY: test
test: manifests generate fmt lint ## Run tests.
@./hack/test-integration.sh

# Install example resources into a dev cluster
bootstrap-dev: install
.PHONY: bootstrap-dev
bootstrap-dev: install ## Install example resources into a dev cluster
kubectl apply -f config/samples/bootstrap/01_namespaces.yaml
kubectl apply -f config/samples/bootstrap/02_rbac.yaml

# Install example resources and gardener project into a dev cluster
bootstrap-dev-project: bootstrap-dev
.PHONY: bootstrap-dev-project
bootstrap-dev-project: bootstrap-dev ## Install example resources and gardener project into a dev cluster
kubectl apply -f config/samples/bootstrap/03_gardener-rbac.yaml
kubectl apply -f config/samples/bootstrap/04_gardener-project.yaml

apply-image: manifests
cd config/manager && kustomize edit set image "controller=${IMG}"
cd config/default && kustomize edit set image "quay.io/brancz/kube-rbac-proxy=${IMG_RBAC_PROXY}"
##@ Build

.PHONY: build
build: generate fmt lint ## Build manager binary.
go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt lint ## Run a controller from your host.
go run ./main.go

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t $(IMG):$(EFFECTIVE_VERSION) .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push $(IMG):$(EFFECTIVE_VERSION)

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

# Multi-cluster use case: Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy-rt: apply-image
.PHONY: uninstall
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.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy-rt
deploy-rt: apply-image kustomize ## Multi-cluster use case: Deploy controller in the configured Kubernetes cluster in ~/.kube/config
kustomize build config/overlay/multi-cluster/runtime | kubectl apply -f -

# Multi-cluster use case: Deploy crd, admission configurations etc. in the configured Kubernetes cluster
deploy-virtual: apply-image
.PHONY: deploy-virtual
deploy-virtual: apply-image kustomize ## Multi-cluster use case: Deploy crd, admission configurations etc. in the configured Kubernetes cluster
kustomize build config/overlay/multi-cluster/virtual-garden | kubectl apply -f -

# Single-cluster use case: Deploy crd, admission configurations, controller etc. in the configured Kubernetes cluster
deploy-singlecluster: apply-image
.PHONY: deploy-singlecluster
deploy-singlecluster: apply-image ## Single-cluster use case: Deploy crd, admission configurations, controller etc. in the configured Kubernetes cluster
kustomize build config/overlay/single-cluster | kubectl apply -f -

# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run golangci-lint against code
lint: $(GOPATH)/bin/golangci-lint
golangci-lint run ./... -E golint,whitespace,wsl --skip-files "zz_generated.*"
.PHONY: apply-image
apply-image: manifests kustomize ## Apply terminal controller and kube-rbac-proxy images according to the variables IMG and IMG_RBAC_PROXY
cd config/manager && $(KUSTOMIZE) edit set image "controller=${IMG}:${EFFECTIVE_VERSION}"
cd config/default && $(KUSTOMIZE) edit set image "quay.io/brancz/kube-rbac-proxy=${IMG_RBAC_PROXY}"

$(GOPATH)/bin/golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.35.2

# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."

# Build the docker image
docker-build: test
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

sync-setup-envtest:
@curl -sSLo hack/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/$(CR_VERSION)/hack/setup-envtest.sh

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.40.1

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
.PHONY: 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.7.0)

KUSTOMIZE = $(shell pwd)/bin/kustomize
.PHONY: kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v4@v4.4.1)

ENVTEST = $(shell pwd)/bin/setup-envtest
.PHONY: envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)

# 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

19 changes: 16 additions & 3 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
#
# SPDX-License-Identifier: Apache-2.0

version: "2"
domain: gardener.cloud
layout:
- go.kubebuilder.io/v3
projectName: terminal-controller-manager
repo: github.com/gardener/terminal-controller-manager
resources:
- group: extensions
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: gardener.cloud
group: dashboard
kind: Terminal
path: github.com/gardener/terminal-controller-manager/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
version: "3"
Loading

0 comments on commit 1708f2f

Please sign in to comment.