Skip to content

Commit

Permalink
✨ add kubebuilder/tooling and controller setup to use binaries from t…
Browse files Browse the repository at this point in the history
…he project bin
  • Loading branch information
Camila Macedo committed Oct 10, 2020
1 parent 5031c94 commit 2375fbf
Show file tree
Hide file tree
Showing 32 changed files with 779 additions and 52 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Expand Up @@ -13,10 +13,12 @@ docs/book/book/

# skip bin
bin/*
testbin/*
testdata/project-v3-addon/testbin/*
testdata/project-v3-multigroup/testbin/*
testdata/project-v3/testbin/*
testdata/project-v3-addon/bin/*
testdata/project-v3-multigroup/bin/*
testdata/project-v3/bin/*
testdata/project-v2-addon/bin/*
testdata/project-v2-multigroup/bin/*
testdata/project-v2/bin/*
docs/book/src/multiversion-tutorial/testdata/project/bin/*
docs/book/src/cronjob-tutorial/testdata/project/bin/*

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -42,11 +42,14 @@ help: ## Display this help
.PHONY: build
build: ## Build the project locally
go build -o bin/kubebuilder ./cmd
go build -o bin/tooling ./tooling/main.go


.PHONY: install
install: ## Build and install the binary with the current source code. Use it to test your changes locally.
make build
cp ./bin/kubebuilder $(shell go env GOPATH)/bin/kubebuilder
cp ./bin/tooling $(shell go env GOPATH)/bin/tooling

##@ Development

Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/quick-start.md
Expand Up @@ -27,7 +27,7 @@ arch=$(go env GOARCH)
curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | tar -xz -C /tmp/
```

If you are using a Kubebuilder plugin version less than version `v3+`, you must configure the Kubernetes binaries required for the [envtest][envtest], run:
If you are using a Kubebuilder CLI version =< `v2.3.1`, you must configure the Kubernetes binaries required for the [envtest][envtest], run:

```bash
# move to a long-term location and put it on your path
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -88,6 +88,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down Expand Up @@ -120,6 +121,7 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down
Expand Up @@ -34,8 +34,8 @@ type SuiteTest struct {
file.BoilerplateMixin
file.ResourceMixin

// CRDDirectoryRelativePath define the Path for the CRD when it is multigroup
CRDDirectoryRelativePath string
// RelativePath define the Path for the root when it is multigroup
RelativePath string
}

// SetTemplateDefaults implements file.Template
Expand All @@ -56,9 +56,9 @@ func (f *SuiteTest) SetTemplateDefaults() error {

// If is multigroup the path needs to be ../../ since it has
// the group dir.
f.CRDDirectoryRelativePath = `".."`
if f.MultiGroup {
f.CRDDirectoryRelativePath = `"..", ".."`
f.RelativePath = `".."`
if f.MultiGroup && f.Resource.Group != "" {
f.RelativePath = `"..", ".."`
}

return nil
Expand Down Expand Up @@ -148,9 +148,12 @@ var _ = BeforeSuite(func(done Done) {
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
CRDDirectoryPaths: []string{filepath.Join({{ .RelativePath }}, "config", "crd", "bases")},
}
By("configuring env test binaries")
configEnvTestBinaries()
var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -170,4 +173,31 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
// configEnvTestBinaries will check if the required binaries are in the bin directory
// of the project and configure to use them.
func configEnvTestBinaries() {
binPath := filepath.Join({{ .RelativePath }}, "bin")
if hasEnvTestBinaries(binPath) {
os.Setenv("KUBEBUILDER_ASSETS", binPath)
}
}
// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found
// in the path informed
func hasEnvTestBinaries(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) {
return false
}
return true
}
`
20 changes: 18 additions & 2 deletions pkg/plugin/v2/scaffolds/internal/templates/makefile.go
Expand Up @@ -70,8 +70,24 @@ endif
all: manager
# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out
test: generate fmt vet manifests tooling
$(TOOLING); go test ./... -coverprofile cover.out
# get the kubebuilder tooling
tooling:
ifeq (, $(shell which tooling))
@{ \
set -e ;\
TOOLING_TMP_DIR=$$(mktemp -d) ;\
cd $$TOOLING_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kubebuilder/tooling ;\
rm -rf $$TOOLING_TMP_DIR ;\
}
TOOLING=$(GOBIN)/tooling
else
TOOLING=$(shell which tooling)
endif
# Build manager binary
manager: generate fmt vet
Expand Down
9 changes: 4 additions & 5 deletions pkg/plugin/v3/scaffolds/init.go
Expand Up @@ -108,11 +108,10 @@ func (s *initScaffolder) scaffold() error {
&templates.Main{},
&templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
&templates.Makefile{
Image: imageName,
BoilerplatePath: s.boilerplatePath,
ControllerToolsVersion: ControllerToolsVersion,
KustomizeVersion: KustomizeVersion,
ControllerRuntimeVersion: ControllerRuntimeVersion,
Image: imageName,
BoilerplatePath: s.boilerplatePath,
ControllerToolsVersion: ControllerToolsVersion,
KustomizeVersion: KustomizeVersion,
},
&templates.Dockerfile{},
&templates.DockerignoreFile{},
Expand Down
Expand Up @@ -34,8 +34,8 @@ type SuiteTest struct {
file.BoilerplateMixin
file.ResourceMixin

// CRDDirectoryRelativePath define the Path for the CRD when it is multigroup
CRDDirectoryRelativePath string
// RelativePath define the Path for the root when it is multigroup
RelativePath string
}

// SetTemplateDefaults implements file.Template
Expand All @@ -56,9 +56,9 @@ func (f *SuiteTest) SetTemplateDefaults() error {

// If is multigroup the path needs to be ../../ since it has
// the group dir.
f.CRDDirectoryRelativePath = `".."`
f.RelativePath = `".."`
if f.MultiGroup && f.Resource.Group != "" {
f.CRDDirectoryRelativePath = `"..", ".."`
f.RelativePath = `"..", ".."`
}

return nil
Expand Down Expand Up @@ -148,9 +148,12 @@ var _ = BeforeSuite(func(done Done) {
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
CRDDirectoryPaths: []string{filepath.Join({{ .RelativePath }}, "config", "crd", "bases")},
}
By("configuring env test binaries")
configEnvTestBinaries()
var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -170,4 +173,31 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
// configEnvTestBinaries will check if the required binaries are in the bin directory
// of the project and configure to use them.
func configEnvTestBinaries() {
binPath := filepath.Join({{ .RelativePath }}, "bin")
if hasEnvTestBinaries(binPath) {
os.Setenv("KUBEBUILDER_ASSETS", binPath)
}
}
// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found
// in the path informed
func hasEnvTestBinaries(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) {
return false
}
return true
}
`
1 change: 0 additions & 1 deletion pkg/plugin/v3/scaffolds/internal/templates/gitignore.go
Expand Up @@ -46,7 +46,6 @@ const gitignoreTemplate = `
*.so
*.dylib
bin
testbin/*
# Test binary, build with ` + "`go test -c`" + `
*.test
Expand Down
25 changes: 18 additions & 7 deletions pkg/plugin/v3/scaffolds/internal/templates/makefile.go
Expand Up @@ -34,8 +34,6 @@ type Makefile struct {
ControllerToolsVersion string
// Kustomize version to use in the project
KustomizeVersion string
// ControllerRuntimeVersion version to be used to download the envtest setup script
ControllerRuntimeVersion string
}

// SetTemplateDefaults implements input.Template
Expand Down Expand Up @@ -72,11 +70,24 @@ endif
all: manager
# Run tests
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: generate fmt vet manifests
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/{{ .ControllerRuntimeVersion }}/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
test: generate fmt vet manifests tooling
$(TOOLING); go test ./... -coverprofile cover.out
# get the kubebuilder tooling
tooling:
ifeq (, $(shell which tooling))
@{ \
set -e ;\
TOOLING_TMP_DIR=$$(mktemp -d) ;\
cd $$TOOLING_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kubebuilder/tooling ;\
rm -rf $$TOOLING_TMP_DIR ;\
}
TOOLING=$(GOBIN)/tooling
else
TOOLING=$(shell which tooling)
endif
# Build manager binary
manager: generate fmt vet
Expand Down
20 changes: 18 additions & 2 deletions testdata/project-v2-addon/Makefile
Expand Up @@ -14,8 +14,24 @@ endif
all: manager

# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out
test: generate fmt vet manifests tooling
$(TOOLING); go test ./... -coverprofile cover.out

# get the kubebuilder tooling
tooling:
ifeq (, $(shell which tooling))
@{ \
set -e ;\
TOOLING_TMP_DIR=$$(mktemp -d) ;\
cd $$TOOLING_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kubebuilder/tooling ;\
rm -rf $$TOOLING_TMP_DIR ;\
}
TOOLING=$(GOBIN)/tooling
else
TOOLING=$(shell which tooling)
endif

# Build manager binary
manager: generate fmt vet
Expand Down
31 changes: 31 additions & 0 deletions testdata/project-v2-addon/controllers/suite_test.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -57,6 +58,9 @@ var _ = BeforeSuite(func(done Done) {
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
}

By("configuring env test binaries")
configEnvTestBinaries()

var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -85,3 +89,30 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})

// configEnvTestBinaries will check if the required binaries are in the bin directory
// of the project and configure to use them.
func configEnvTestBinaries() {
binPath := filepath.Join("..", "bin")
if hasEnvTestBinaries(binPath) {
os.Setenv("KUBEBUILDER_ASSETS", binPath)
}
}

// hasEnvTestBinaries will return true when the kubebuilder tools binaries are found
// in the path informed
func hasEnvTestBinaries(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "etcd")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "kube-apiserver")); os.IsNotExist(err) {
return false
}
if _, err := os.Stat(filepath.Join(path, "kubectl")); os.IsNotExist(err) {
return false
}
return true
}
20 changes: 18 additions & 2 deletions testdata/project-v2-multigroup/Makefile
Expand Up @@ -14,8 +14,24 @@ endif
all: manager

# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out
test: generate fmt vet manifests tooling
$(TOOLING); go test ./... -coverprofile cover.out

# get the kubebuilder tooling
tooling:
ifeq (, $(shell which tooling))
@{ \
set -e ;\
TOOLING_TMP_DIR=$$(mktemp -d) ;\
cd $$TOOLING_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kubebuilder/tooling ;\
rm -rf $$TOOLING_TMP_DIR ;\
}
TOOLING=$(GOBIN)/tooling
else
TOOLING=$(shell which tooling)
endif

# Build manager binary
manager: generate fmt vet
Expand Down

0 comments on commit 2375fbf

Please sign in to comment.