Skip to content

Commit

Permalink
Always use correct tool version (#13)
Browse files Browse the repository at this point in the history
Use go dependency management to download and run common build tools like
controller-gen, ginkgo and kustomize. This will ensure that the correct
version of the tool is used. As a bonus, we can use tools like
dependabot to automatically track and propose updated to our tools.

Under the hood this works by using a custom go module with a single
dependency (the tool). `go run` is used to load and execute the tool at
runtime for that module. Golang caches the compiled version of the tool
and invalidates the cache when a new version of the tool is desired.

This PR does not change how envtest is setup, which is a bit more
complicated.

This PR also aligns the module name with the git repo.

Signed-off-by: Scott Andrews <andrewssc@vmware.com>
  • Loading branch information
scothis committed Aug 19, 2021
1 parent 38252c3 commit ff2d31c
Show file tree
Hide file tree
Showing 19 changed files with 1,466 additions and 57 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ updates:
update-types:
- version-update:semver-major
- version-update:semver-minor
- package-ecosystem: gomod
directory: "/hack/controller-gen"
schedule:
interval: daily
- package-ecosystem: gomod
directory: "/hack/ginkgo"
schedule:
interval: daily
- package-ecosystem: gomod
directory: "/hack/kustomize"
schedule:
interval: daily
43 changes: 10 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ else
GOBIN=$(shell go env GOBIN)
endif

CONTROLLER_GEN ?= go run -modfile hack/controller-gen/go.mod sigs.k8s.io/controller-tools/cmd/controller-gen
GINKGO ?= go run -modfile hack/ginkgo/go.mod github.com/onsi/ginkgo/ginkgo
KUSTOMIZE ?= go run -modfile hack/kustomize/go.mod sigs.k8s.io/kustomize/kustomize/v3

# 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.
Expand All @@ -37,10 +41,10 @@ help: ## Display this help.

##@ Development

manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
manifests: ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

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

fmt: ## Run go fmt against code.
Expand All @@ -50,7 +54,7 @@ vet: ## Run go vet against code.
go vet ./...

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ginkgo ## Run tests.
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); $(GINKGO) -v ./... -coverprofile cover.out
Expand All @@ -71,42 +75,15 @@ docker-push: ## Push docker image with the manager.

##@ Deployment

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

uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
uninstall: manifests ## 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.
deploy: manifests ## 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)

GINKGO = $(shell pwd)/bin/ginkgo
ginkgo: ## Download ginkgo locally if necessary.
$(call go-get-tool,$(GINKGO),github.com/onsi/ginkgo/ginkgo@v1.16.4)

# 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
6 changes: 3 additions & 3 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ layout:
- go.kubebuilder.io/v3
multigroup: true
projectName: service-binding-operator
repo: github.com/kubernetes-sigs/service-binding
repo: github.com/k8s-service-bindings/implementation
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: service.binding
kind: ServiceBinding
path: github.com/kubernetes-sigs/service-binding/apis/v1alpha2
path: github.com/k8s-service-bindings/implementation/apis/v1alpha2
version: v1alpha2
- api:
crdVersion: v1
controller: true
domain: service.binding
kind: ClusterApplicationResourceMapping
path: github.com/kubernetes-sigs/service-binding/apis/v1alpha2
path: github.com/k8s-service-bindings/implementation/apis/v1alpha2
version: v1alpha2
version: "3"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ spec:
singular: clusterapplicationresourcemapping
scope: Cluster
versions:
- name: v1alpha2
- additionalPrinterColumns:
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha2
schema:
openAPIV3Schema:
description: ClusterApplicationResourceMapping is the Schema for the clusterapplicationresourcemappings
Expand All @@ -38,20 +42,49 @@ spec:
description: ClusterApplicationResourceMappingSpec defines the desired
state of ClusterApplicationResourceMapping
properties:
foo:
description: Foo is an example field of ClusterApplicationResourceMapping.
Edit clusterapplicationresourcemapping_types.go to remove/update
type: string
type: object
status:
description: ClusterApplicationResourceMappingStatus defines the observed
state of ClusterApplicationResourceMapping
versions:
description: Versions is the collection of versions for a given resource,
with mappings.
items:
description: ClusterApplicationResourceMappingVersion defines the
mapping for a specific version of an application resource.
properties:
containers:
description: Containers is the collection of JSONPaths that
container configuration may be written to.
items:
type: string
type: array
envs:
description: Envs is the collection of JSONPaths that env configuration
may be written to.
items:
type: string
type: array
version:
description: Version is the version of the application resource
that this mapping is for.
type: string
volumeMounts:
description: VolumeMounts is the collection of JSONPaths that
volume mount configuration may be written to.
items:
type: string
type: array
volumes:
description: Volumes is the JSONPath that volume configuration
must be written to.
type: string
required:
- version
- volumes
type: object
type: array
type: object
type: object
served: true
storage: true
subresources:
status: {}
subresources: {}
status:
acceptedNames:
kind: ""
Expand Down
Loading

0 comments on commit ff2d31c

Please sign in to comment.