diff --git a/Makefile b/Makefile index 85ee5199d07..d13032c81a1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ #!/usr/bin/env bash - # Copyright 2019 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -68,7 +67,7 @@ generate: generate-testdata ## Update/generate all mock data. You should run thi .PHONY: generate-testdata generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder - ./generate_testdata.sh + ./test/testdata/generate.sh .PHONY: lint lint: golangci-lint ## Run golangci-lint linter @@ -87,26 +86,35 @@ golangci-lint: ##@ Tests -.PHONY: go-test -go-test: ## Run the unit test - go test -race -v ./cmd/... ./pkg/... ./plugins/... - .PHONY: test -test: ## Run the unit tests (used in the CI) - ./test.sh +test: test-unit test-integration test-testdata ## Run the unit and integration tests (used in the CI) + +.PHONY: test-unit +test-unit: ## Run the unit tests + go test -race -v ./pkg/... .PHONY: test-coverage -test-coverage: ## Run coveralls - # remove all coverage files if exists - - rm -rf *.out - # run the go tests and gen the file coverage-all used to do the integration with coverrals.io - go test -race -failfast -tags=integration -coverprofile=coverage-all.out ./cmd/... ./pkg/... ./plugins/... +test-coverage: ## Run unit tests creating the output to report coverage + - rm -rf *.out # Remove all coverage files if exists + go test -race -failfast -tags=integration -coverprofile=coverage-all.out ./pkg/... -.PHONY: test-e2e-local -test-e2e-local: ## It will run the script to install kind and run e2e tests - ## To keep the same kind cluster between test runs, use `SKIP_KIND_CLEANUP=1 make test-e2e-local` - ./test_e2e_local.sh +.PHONY: test-integration +test-integration: ## Run the integration tests + ./test/integration.sh .PHONY: check-testdata check-testdata: ## Run the script to ensure that the testdata is updated - ./check_testdata.sh + ./test/testdata/check.sh + +.PHONY: test-testdata +test-testdata: ## Run the tests of the testdata directory + ./test/testdata/test.sh + +.PHONY: test-e2e-local +test-e2e-local: ## Run the end-to-end tests locally + ## To keep the same kind cluster between test runs, use `SKIP_KIND_CLEANUP=1 make test-e2e-local` + ./test/e2e/local.sh + +.PHONY: test-e2e-ci +test-e2e-ci: ## Run the end-to-end tests (used in the CI)` + ./test/e2e/ci.sh diff --git a/common.sh b/common.sh deleted file mode 100644 index 9340312eaa0..00000000000 --- a/common.sh +++ /dev/null @@ -1,204 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2018 The Kubernetes Authors. -# -# 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. - -set -o errexit -set -o nounset -set -o pipefail - -# check if modules are enabled -MODULES_ENABLED="" -if go mod edit -json &>/dev/null ; then - MODULES_ENABLED="1" -fi - -MOD_OPT="" -MODULES_OPT=${MODULES_OPT:-""} -if [[ -n "${MODULES_OPT}" && -n "${MODULES_ENABLED}" ]]; then - MOD_OPT="-mod=${MODULES_OPT}" -fi - - -# Enable tracing in this script off by setting the TRACE variable in your -# environment to any value: -# -# $ TRACE=1 test.sh -TRACE=${TRACE:-""} -if [ -n "$TRACE" ]; then - set -x -fi - -# By setting INJECT_KB_VERSION variable in your environment, KB will be compiled -# with this version. This is to assist testing functionality which depends on -# version .e.g gopkg.toml generation. -# -# $ INJECT_KB_VERSION=0.1.7 test.sh -INJECT_KB_VERSION=${INJECT_KB_VERSION:-unknown} - -# Make sure, we run in the root of the repo and -# therefore run the tests on all packages -base_dir="$( cd "$(dirname "$0")/" && pwd )" -cd "$base_dir" || { - echo "Cannot cd to '$base_dir'. Aborting." >&2 - exit 1 -} - -go_workspace='' -export GOPATH=${GOPATH:-$(go env GOPATH)} -for p in ${GOPATH//:/ }; do - if [[ $PWD/ = $p/* ]]; then - go_workspace=$p - fi -done - -k8s_version=1.16.4 -goarch=amd64 -goos="unknown" - -if [[ "$OSTYPE" == "linux-gnu" ]]; then - goos="linux" -elif [[ "$OSTYPE" == "darwin"* ]]; then - goos="darwin" -fi - -if [[ "$goos" == "unknown" ]]; then - echo "OS '$OSTYPE' not supported. Aborting." >&2 - exit 1 -fi - -# Turn colors in this script off by setting the NO_COLOR variable in your -# environment to any value: -# -# $ NO_COLOR=1 test.sh -NO_COLOR=${NO_COLOR:-""} -if [ -z "$NO_COLOR" ]; then - header=$'\e[1;33m' - reset=$'\e[0m' -else - header='' - reset='' -fi - -function header_text { - echo "$header$*$reset" -} - -rc=0 -tmp_root=/tmp - -kb_root_dir=$tmp_root/kubebuilder -kb_orig=$(pwd) - -# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable -# in your environment to any value: -# -# $ SKIP_FETCH_TOOLS=1 ./test.sh -# -# If you skip fetching tools, this script will use the tools already on your -# machine, but rebuild the kubebuilder and kubebuilder-bin binaries. -SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""} - -function prepare_staging_dir { - header_text "preparing staging dir" - - if [ -z "$SKIP_FETCH_TOOLS" ]; then - rm -rf "$kb_root_dir" - else - rm -f "$kb_root_dir/bin/kubebuilder" - fi -} - -# fetch k8s API gen tools and make it available under kb_root_dir/bin. -function fetch_tools { - if [ -z "$SKIP_FETCH_TOOLS" ]; then - fetch_kb_tools - fi -} - -function fetch_kb_tools { - header_text "fetching kb tools" - kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz" - kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name" - - kb_tools_archive_path="$tmp_root/$kb_tools_archive_name" - if [ ! -f $kb_tools_archive_path ]; then - curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path" - fi - tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/" -} - -function build_kb { - header_text "building kubebuilder" - - if [ "$INJECT_KB_VERSION" = "unknown" ]; then - opts="" - else - # Injects the version into the cmd/version.go file - opts=-ldflags "-X sigs.k8s.io/kubebuilder/v3/cmd.kubeBuilderVersion=$INJECT_KB_VERSION" - fi - - GO111MODULE=on go build $opts -o $kb_root_dir/bin/kubebuilder ./cmd -} - -function install_kind { - header_text "Checking for kind" - if ! is_installed kind ; then - header_text "Installing kind" - KIND_DIR=$(mktemp -d) - pushd $KIND_DIR - GO111MODULE=on go get sigs.k8s.io/kind@v0.7.0 - popd - fi -} - -function is_installed { - if command -v $1 &>/dev/null; then - return 0 - fi - return 1 -} - -function prepare_testdir_under_gopath { - kb_test_dir=$kb_root_dir/test - header_text "preparing test directory $kb_test_dir" - rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir" - header_text "running kubebuilder commands in test directory $kb_test_dir" -} - -function setup_envs { - header_text "setting up env vars" - - # Setup env vars - export PATH=$kb_root_dir/bin:$PATH - export TEST_ASSET_KUBECTL=$kb_root_dir/bin/kubectl - export TEST_ASSET_KUBE_APISERVER=$kb_root_dir/bin/kube-apiserver - export TEST_ASSET_ETCD=$kb_root_dir/bin/etcd - export TEST_DEP=$kb_root_dir/init_project -} - -function cache_project { - header_text "caching initialized projects" - if [ -d "$TEST_DEP" ]; then - rm -rf "$TEST_DEP" - fi - mkdir -p "$TEST_DEP" - cp -r $PWD/* $TEST_DEP -} - -function dump_project { - header_text "restoring cached project" - if [ -d "$TEST_DEP" ]; then - cp -r $TEST_DEP/* . - fi -} diff --git a/pkg/plugins/golang/v2/scaffolds/internal/templates/api/types.go b/pkg/plugins/golang/v2/scaffolds/internal/templates/api/types.go index 675f278a802..a20dfd9ff82 100644 --- a/pkg/plugins/golang/v2/scaffolds/internal/templates/api/types.go +++ b/pkg/plugins/golang/v2/scaffolds/internal/templates/api/types.go @@ -87,7 +87,7 @@ type {{ .Resource.Kind }}Status struct { //+kubebuilder:object:root=true //+kubebuilder:subresource:status -{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }} +{{ if not .Resource.Namespaced }}//+kubebuilder:resource:scope=Cluster{{ end }} // {{ .Resource.Kind }} is the Schema for the {{ .Resource.Plural }} API type {{ .Resource.Kind }} struct { diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/api/types.go b/pkg/plugins/golang/v3/scaffolds/internal/templates/api/types.go index 8147a5ad5f7..27614182577 100644 --- a/pkg/plugins/golang/v3/scaffolds/internal/templates/api/types.go +++ b/pkg/plugins/golang/v3/scaffolds/internal/templates/api/types.go @@ -91,7 +91,7 @@ type {{ .Resource.Kind }}Status struct { //+kubebuilder:object:root=true //+kubebuilder:subresource:status -{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }} +{{ if not .Resource.Namespaced }}//+kubebuilder:resource:scope=Cluster{{ end }} // {{ .Resource.Kind }} is the Schema for the {{ .Resource.Plural }} API type {{ .Resource.Kind }} struct { diff --git a/plugins/addon/controller.go b/plugins/addon/controller.go index 0c70e80242f..1c1dea013ce 100644 --- a/plugins/addon/controller.go +++ b/plugins/addon/controller.go @@ -56,7 +56,7 @@ var _ reconcile.Reconciler = &{{ .Resource.Kind }}Reconciler{} // {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object type {{ .Resource.Kind }}Reconciler struct { client.Client - Log logr.Logger + Log logr.Logger Scheme *runtime.Scheme declarative.Reconciler diff --git a/plugins/addon/type.go b/plugins/addon/type.go index 4046a190d38..f7d67ac0e70 100644 --- a/plugins/addon/type.go +++ b/plugins/addon/type.go @@ -76,7 +76,9 @@ type {{.Resource.Kind}}Status struct { //+kubebuilder:object:root=true //+kubebuilder:subresource:status -{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }} +{{- if not .Resource.Namespaced }} +//+kubebuilder:resource:scope=Cluster +{{- end }} // {{.Resource.Kind}} is the Schema for the {{ .Resource.Plural }} API type {{.Resource.Kind}} struct { @@ -110,7 +112,9 @@ func (o *{{.Resource.Kind}}) SetCommonStatus(s addonv1alpha1.CommonStatus) { } //+kubebuilder:object:root=true -{{ if not .Resource.Namespaced }} //+kubebuilder:resource:scope=Cluster {{ end }} +{{- if not .Resource.Namespaced }} +//+kubebuilder:resource:scope=Cluster +{{- end }} // {{.Resource.Kind}}List contains a list of {{.Resource.Kind}} type {{.Resource.Kind}}List struct { diff --git a/test.sh b/test.sh index 32324a680de..3f17ef45342 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + # Copyright 2018 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,148 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -o errexit -set -o nounset -set -o pipefail - -source common.sh - -export TRACE=1 -export GO111MODULE=on - -function test_init_project { - header_text "performing init project" - go mod init kubebuilder.io/test - kubebuilder init --domain example.com <<< "n" -} - -function test_make_project { - header_text "running make in project" - make -} - -function test_create_api_controller { - header_text "performing creating api and controller" - kubebuilder create api --group insect --version v1beta1 --kind Bee --namespaced false <&2 + exit 1 +fi + +# Turn colors in this script off by setting the NO_COLOR variable in your +# environment to any value: +# +# $ NO_COLOR=1 test.sh +NO_COLOR=${NO_COLOR:-""} +if [ -z "$NO_COLOR" ]; then + header=$'\e[1;33m' + reset=$'\e[0m' +else + header='' + reset='' +fi + +function header_text { + echo "$header$*$reset" +} + +tmp_root=/tmp +kb_root_dir=$tmp_root/kubebuilder + +# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable +# in your environment to any value: +# +# $ SKIP_FETCH_TOOLS=1 ./test.sh +# +# If you skip fetching tools, this script will use the tools already on your +# machine, but rebuild the kubebuilder and kubebuilder-bin binaries. +SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""} + +# Remove previously built binary and fetched tools if they need to be fetched again +function prepare_staging_dir { + header_text "Preparing staging dir" + + if [ -z "$SKIP_FETCH_TOOLS" ]; then + rm -rf "$kb_root_dir" + else + rm -f "$kb_root_dir/bin/kubebuilder" + fi +} + +# Build kubebuilder +function build_kb { + header_text "Building kubebuilder" + go build -o $kb_root_dir/bin/kubebuilder ./cmd + kb=$kb_root_dir/bin/kubebuilder +} + +# Fetch k8s API gen tools and make it available under kb_root_dir/bin. +function fetch_tools { + if [ -z "$SKIP_FETCH_TOOLS" ]; then + header_text "Fetching kb tools" + kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz" + kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name" + + kb_tools_archive_path="$tmp_root/$kb_tools_archive_name" + if [ ! -f $kb_tools_archive_path ]; then + curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path" + fi + tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/" + fi + + export KUBEBUILDER_ASSETS=$kb_root_dir/bin/ +} + +# Installing kind in a temporal dir if no previously installed +function install_kind { + header_text "Checking if kind is installed" + if ! is_installed kind ; then + header_text "Kind not found, installing kind" + pushd $(mktemp -d) + GO111MODULE=on go get sigs.k8s.io/kind@v0.7.0 + popd + fi +} + +# Check if a program is previously installed +function is_installed { + if command -v $1 &>/dev/null; then + return 0 + fi + return 1 +} diff --git a/scripts/setup.sh b/test/e2e/ci.sh similarity index 52% rename from scripts/setup.sh rename to test/e2e/ci.sh index 9878fc51614..e09ae41b8e0 100755 --- a/scripts/setup.sh +++ b/test/e2e/ci.sh @@ -1,6 +1,6 @@ -#!/bin/sh +#!/usr/bin/env bash -# Copyright 2019 The Kubernetes Authors. +# Copyright 2018 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,15 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -K8S_VERSION=$1 -KIND_NAME="kind" -if [ $# -gt 1 ]; then - KIND_NAME=$2 -fi +source "$(dirname "$0")/../common.sh" +source "$(dirname "$0")/setup.sh" -export GO111MODULE=on +kind_cluster="kind" +create_cluster ${KIND_K8S_VERSION} $kind_cluster +trap delete_cluster EXIT -# setup go module to create the cluster - -# You can use --image flag to specify the cluster version you want, e.g --image=kindest/node:v1.13.6, the supported version are listed at https://hub.docker.com/r/kindest/node/tags -kind create cluster -v 4 --name $KIND_NAME --retain --wait=1m --config test/kind-config.yaml --image=kindest/node:$K8S_VERSION +test_cluster diff --git a/test/kind-config.yaml b/test/e2e/kind-config.yaml similarity index 100% rename from test/kind-config.yaml rename to test/e2e/kind-config.yaml diff --git a/test_e2e_local.sh b/test/e2e/local.sh similarity index 53% rename from test_e2e_local.sh rename to test/e2e/local.sh index 90527a2ebf7..0d0d82e0249 100755 --- a/test_e2e_local.sh +++ b/test/e2e/local.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + # Copyright 2018 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,39 +14,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -o errexit -set -o nounset -set -o pipefail - -source common.sh - -export TRACE=1 -export GO111MODULE=on -export KIND_K8S_VERSION=${KIND_K8S_VERSION:-v1.18.0} - -fetch_tools -install_kind -build_kb - -setup_envs +source "$(dirname "$0")/../common.sh" +source "$(dirname "$0")/setup.sh" -export KIND_CLUSTER=local-kubebuilder-e2e -if ! kind get clusters | grep -q $KIND_CLUSTER ; then - source "$(pwd)/scripts/setup.sh" ${KIND_K8S_VERSION} $KIND_CLUSTER +kind_cluster=local-kubebuilder-e2e +create_cluster ${KIND_K8S_VERSION:-v1.18.0} $kind_cluster +if [ -z "${SKIP_KIND_CLEANUP:-}" ]; then + trap delete_cluster EXIT fi -kind export kubeconfig --kubeconfig $tmp_root/kubeconfig --name $KIND_CLUSTER +kind export kubeconfig --kubeconfig $tmp_root/kubeconfig --name $kind_cluster export KUBECONFIG=$tmp_root/kubeconfig -# remove running containers on exit -function cleanup() { - kind delete cluster --name $KIND_CLUSTER -} - -if [ -z "${SKIP_KIND_CLEANUP:-}" ]; then - trap cleanup EXIT -fi - -# when changing these commands, make sure to keep in sync with ./test_e2e.sh -go test ./test/e2e/v2 -v -ginkgo.v -go test ./test/e2e/v3 -v -ginkgo.v -timeout 20m +test_cluster -v -ginkgo.v diff --git a/test/e2e/setup.sh b/test/e2e/setup.sh new file mode 100755 index 00000000000..e0146ebe73f --- /dev/null +++ b/test/e2e/setup.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +# Copyright 2019 The Kubernetes Authors. +# +# 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. + +build_kb +export PATH=$kb_root_dir/bin:$PATH +fetch_tools +install_kind + +# Creates a kind cluster given a k8s version and a cluster name. +# +# Usage: +# +# create_cluster +function create_cluster { + if ! kind get clusters | grep -q $2 ; then + kind create cluster -v 4 --name $2 --retain --wait=1m --config $(dirname "$0")/kind-config.yaml --image=kindest/node:$1 + fi +} + +# Deletes a kind cluster by cluster name. The kind cluster needs to be defined as a variable instead of an argument +# so that this function can be used with `trap` +# +# Usage: +# +# kind_cluster= +# delete_cluster +function delete_cluster { + kind delete cluster --name $kind_cluster +} + +function test_cluster { + local flags="$@" + + go test $(dirname "$0")/v2 $flags + go test $(dirname "$0")/v3 $flags -timeout 20m +} diff --git a/test/integration.sh b/test/integration.sh new file mode 100755 index 00000000000..8f987de4c98 --- /dev/null +++ b/test/integration.sh @@ -0,0 +1,126 @@ +#!/usr/bin/env bash + +# Copyright 2018 The Kubernetes Authors. +# +# 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. + +source $(dirname "$0")/common.sh + +header_text "Running kubebuilder integration tests" + +build_kb +pushd . + +kb_test_dir=$kb_root_dir/test +kb_test_cache_dir=$kb_root_dir/cache + +function prepare_test_dir { + header_text "Preparing test directory $kb_test_dir" + rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir" + header_text "Running kubebuilder commands in test directory $kb_test_dir" +} + +function cache_project { + header_text "Caching project '$1'" + if [ -d "$kb_test_cache_dir/$1" ]; then + rm -rf "$kb_test_cache_dir/$1" + fi + mkdir -p "$kb_test_cache_dir/$1" + cp -r $PWD/* $kb_test_cache_dir/$1 +} + +function dump_project { + header_text "Restoring cached project '$1'" + if [ -d "$kb_test_cache_dir/$1" ]; then + cp -r $kb_test_cache_dir/$1/* . + fi +} + + +function test_init_project { + header_text "Init project" + go mod init kubebuilder.io/test + $kb init --domain example.com <<< "n" +} + +function test_make_project { + header_text "Running make" + make +} + +function test_create_api_controller { + header_text "Creating api and controller" + $kb create api --group insect --version v1beta1 --kind Bee --namespaced false < -# -scaffold_test_project() { +function scaffold_test_project { local project=$1 shift local init_flags="$@" - local testdata_dir=$(pwd)/testdata - mkdir -p ./testdata/$project - rm -rf ./testdata/$project/* - pushd . - cd testdata/$project - local kb=$testdata_dir/../bin/kubebuilder + local testdata_dir="$(dirname "$0")/../../testdata" + mkdir -p $testdata_dir/$project + rm -rf $testdata_dir/$project/* + pushd $testdata_dir/$project # Remove tool binaries for projects of version 2, which don't have locally-configured binaries, - # so the correct versions are used. + # so the correct versions are used. Also, webhooks in version 2 don't have --make flag + webhook_make="--make=false" if [[ $init_flags =~ --project-version=2 ]]; then rm -f "$(command -v controller-gen)" rm -f "$(command -v kustomize)" + webhook_make="" fi header_text "Generating project ${project} with flags: ${init_flags}" @@ -58,58 +51,54 @@ scaffold_test_project() { header_text 'Creating APIs ...' $kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false $kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false --force - $kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation + $kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation $webhook_make + if [ $project == "project-v3" ]; then + $kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --make=false --force + fi $kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false - $kb create webhook --group crew --version v1 --kind FirstMate --conversion + $kb create webhook --group crew --version v1 --kind FirstMate --conversion $webhook_make $kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false - $kb create webhook --group crew --version v1 --kind Admiral --defaulting + $kb create webhook --group crew --version v1 --kind Admiral --defaulting $webhook_make $kb create api --group crew --version v1 --kind Laker --controller=true --resource=false --make=false - if [ $project == "project-v3" ]; then - $kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --force - fi elif [[ $project =~ multigroup ]]; then header_text 'Switching to multigroup layout ...' $kb edit --multigroup=true header_text 'Creating APIs ...' $kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false - $kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation + $kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation $webhook_make $kb create api --group ship --version v1beta1 --kind Frigate --controller=true --resource=true --make=false - $kb create webhook --group ship --version v1beta1 --kind Frigate --conversion + $kb create webhook --group ship --version v1beta1 --kind Frigate --conversion $webhook_make $kb create api --group ship --version v1 --kind Destroyer --controller=true --resource=true --namespaced=false --make=false - $kb create webhook --group ship --version v1 --kind Destroyer --defaulting + $kb create webhook --group ship --version v1 --kind Destroyer --defaulting $webhook_make $kb create api --group ship --version v2alpha1 --kind Cruiser --controller=true --resource=true --namespaced=false --make=false - $kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation + $kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation $webhook_make $kb create api --group sea-creatures --version v1beta1 --kind Kraken --controller=true --resource=true --make=false $kb create api --group sea-creatures --version v1beta2 --kind Leviathan --controller=true --resource=true --make=false $kb create api --group foo.policy --version v1 --kind HealthCheckPolicy --controller=true --resource=true --make=false $kb create api --group apps --version v1 --kind Pod --controller=true --resource=false --make=false if [ $project == "project-v3-multigroup" ]; then $kb create api --version v1 --kind Lakers --controller=true --resource=true --make=false - $kb create webhook --version v1 --kind Lakers --defaulting --programmatic-validation + $kb create webhook --version v1 --kind Lakers --defaulting --programmatic-validation --make=false fi elif [[ $project =~ addon ]]; then header_text 'enabling --pattern flag ...' export KUBEBUILDER_ENABLE_PLUGINS=1 header_text 'Creating APIs ...' - $kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --pattern=addon + $kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false --pattern=addon $kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false --pattern=addon $kb create api --group crew --version v1 --kind Admiral --controller=true --resource=true --namespaced=false --make=false --pattern=addon unset KUBEBUILDER_ENABLE_PLUGINS fi - make all test + make generate manifests rm -f go.sum - rm -rf ./bin ./testbin + popd } -set -e - -export GO111MODULE=on -export PATH="$PATH:$(go env GOPATH)/bin" - build_kb + # Project version 2 uses plugin go/v2 (default). scaffold_test_project project-v2 --project-version=2 scaffold_test_project project-v2-multigroup --project-version=2 diff --git a/test/testdata/test.sh b/test/testdata/test.sh new file mode 100755 index 00000000000..aeee4057ce7 --- /dev/null +++ b/test/testdata/test.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +# Copyright 2018 The Kubernetes Authors. +# +# 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. + +source "$(dirname "$0")/../common.sh" + +export KUBEBUILDER_ASSETS=$kb_root_dir/bin/ + +# Executes the test of the testdata directories +function test_project { + rm -f "$(command -v controller-gen)" + rm -f "$(command -v kustomize)" + + header_text "Performing tests in dir $1" + pushd "$(dirname "$0")/../../testdata/$1" + make test + popd +} + +prepare_staging_dir +fetch_tools + +# Test project v2 +test_project project-v2 +test_project project-v2-multigroup +test_project project-v2-addon + +# Test project v3 +test_project project-v3 +test_project project-v3-multigroup +test_project project-v3-addon +test_project project-v3-config diff --git a/test_e2e.sh b/test_e2e.sh old mode 100755 new mode 100644 index 51993c1037a..62564499911 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + # Copyright 2018 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,28 +14,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -o errexit -set -o nounset -set -o pipefail - -source common.sh - -export TRACE=1 -export GO111MODULE=on - -fetch_tools -install_kind -build_kb - -setup_envs - -source "$(pwd)/scripts/setup.sh" ${KIND_K8S_VERSION} - -# remove running containers on exit -function cleanup() { - kind delete cluster -} - -trap cleanup EXIT -go test ./test/e2e/v2 -go test ./test/e2e/v3 -timeout 20m +./test/e2e/ci.sh diff --git a/testdata/project-v2-addon/go.mod b/testdata/project-v2-addon/go.mod index 158ead9deb9..c853a92bb0f 100644 --- a/testdata/project-v2-addon/go.mod +++ b/testdata/project-v2-addon/go.mod @@ -4,8 +4,6 @@ go 1.13 require ( github.com/go-logr/logr v0.1.0 - github.com/onsi/ginkgo v1.12.1 - github.com/onsi/gomega v1.10.1 k8s.io/apimachinery v0.18.6 k8s.io/client-go v0.18.6 sigs.k8s.io/controller-runtime v0.6.4 diff --git a/testdata/project-v2-multigroup/go.mod b/testdata/project-v2-multigroup/go.mod index 2aeef58e0be..651aac9c1fc 100644 --- a/testdata/project-v2-multigroup/go.mod +++ b/testdata/project-v2-multigroup/go.mod @@ -4,8 +4,6 @@ go 1.13 require ( github.com/go-logr/logr v0.1.0 - github.com/onsi/ginkgo v1.12.1 - github.com/onsi/gomega v1.10.1 k8s.io/apimachinery v0.18.6 k8s.io/client-go v0.18.6 sigs.k8s.io/controller-runtime v0.6.4 diff --git a/testdata/project-v2/go.mod b/testdata/project-v2/go.mod index d12efdcca14..57b94c83092 100644 --- a/testdata/project-v2/go.mod +++ b/testdata/project-v2/go.mod @@ -4,8 +4,6 @@ go 1.13 require ( github.com/go-logr/logr v0.1.0 - github.com/onsi/ginkgo v1.12.1 - github.com/onsi/gomega v1.10.1 k8s.io/apimachinery v0.18.6 k8s.io/client-go v0.18.6 sigs.k8s.io/controller-runtime v0.6.4 diff --git a/testdata/project-v3-addon/go.mod b/testdata/project-v3-addon/go.mod index e64120517df..9419f0ce644 100644 --- a/testdata/project-v3-addon/go.mod +++ b/testdata/project-v3-addon/go.mod @@ -4,8 +4,6 @@ go 1.15 require ( github.com/go-logr/logr v0.3.0 - github.com/onsi/ginkgo v1.14.1 - github.com/onsi/gomega v1.10.2 k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.0 diff --git a/testdata/project-v3-config/go.mod b/testdata/project-v3-config/go.mod index f3957e4e5a8..17104c35fcb 100644 --- a/testdata/project-v3-config/go.mod +++ b/testdata/project-v3-config/go.mod @@ -4,9 +4,6 @@ go 1.15 require ( github.com/go-logr/logr v0.3.0 - github.com/onsi/ginkgo v1.14.1 - github.com/onsi/gomega v1.10.2 - k8s.io/api v0.19.2 k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.0 diff --git a/testdata/project-v3-multigroup/go.mod b/testdata/project-v3-multigroup/go.mod index 20900109e2c..bdad249d4f4 100644 --- a/testdata/project-v3-multigroup/go.mod +++ b/testdata/project-v3-multigroup/go.mod @@ -4,9 +4,6 @@ go 1.15 require ( github.com/go-logr/logr v0.3.0 - github.com/onsi/ginkgo v1.14.1 - github.com/onsi/gomega v1.10.2 - k8s.io/api v0.19.2 k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.0 diff --git a/testdata/project-v3/api/v1/webhook_suite_test.go b/testdata/project-v3/api/v1/webhook_suite_test.go index 21a406c4c13..1dabe80a540 100644 --- a/testdata/project-v3/api/v1/webhook_suite_test.go +++ b/testdata/project-v3/api/v1/webhook_suite_test.go @@ -109,10 +109,10 @@ var _ = BeforeSuite(func() { err = (&Captain{}).SetupWebhookWithManager(mgr) Expect(err).NotTo(HaveOccurred()) - err = (&Admiral{}).SetupWebhookWithManager(mgr) + err = (&Captain{}).SetupWebhookWithManager(mgr) Expect(err).NotTo(HaveOccurred()) - err = (&Captain{}).SetupWebhookWithManager(mgr) + err = (&Admiral{}).SetupWebhookWithManager(mgr) Expect(err).NotTo(HaveOccurred()) //+kubebuilder:scaffold:webhook diff --git a/testdata/project-v3/go.mod b/testdata/project-v3/go.mod index 3a6c1779c1d..85b8c51e260 100644 --- a/testdata/project-v3/go.mod +++ b/testdata/project-v3/go.mod @@ -4,9 +4,6 @@ go 1.15 require ( github.com/go-logr/logr v0.3.0 - github.com/onsi/ginkgo v1.14.1 - github.com/onsi/gomega v1.10.2 - k8s.io/api v0.19.2 k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.0 diff --git a/testdata/project-v3/main.go b/testdata/project-v3/main.go index 0ae83837744..fc10f63f98e 100644 --- a/testdata/project-v3/main.go +++ b/testdata/project-v3/main.go @@ -98,6 +98,10 @@ func main() { setupLog.Error(err, "unable to create webhook", "webhook", "Captain") os.Exit(1) } + if err = (&crewv1.Captain{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "Captain") + os.Exit(1) + } if err = (&controllers.FirstMateReconciler{ Client: mgr.GetClient(), Log: ctrl.Log.WithName("controllers").WithName("FirstMate"), @@ -130,10 +134,6 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "Laker") os.Exit(1) } - if err = (&crewv1.Captain{}).SetupWebhookWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create webhook", "webhook", "Captain") - os.Exit(1) - } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {