Skip to content

🏃 setup-envtest.sh: standalone script for setting up envtest #1092

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

Merged
merged 1 commit into from
Aug 3, 2020

Conversation

joelanford
Copy link
Member

@joelanford joelanford commented Aug 3, 2020

envtest is a test framework provided by controller-runtime that simplifies running integration tests for controllers.

Currently, it is not straightforward to setup envtest without also setting up kubebuilder. However, Kubebuilder is not a prerequisite to use envtest.

This PR splits out the envtest setup code from controller-runtime's CI scripts such that users could easily download and use the envtest setup code in their own CI scripts.

It would work like this:

source <(curl -sfL https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh)

envtest_assets_dir=$(pwd)/test/assets
fetch_envtest_tools "$envtest_assets_dir"
setup_envtest_env "$envtest_assets_dir"

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 3, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: joelanford

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 3, 2020
@joelanford
Copy link
Member Author

@joelanford
Copy link
Member Author

/hold

I realized there are a few more places to integrate this.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 3, 2020
@joelanford
Copy link
Member Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 3, 2020
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set -e
set -o errexit
set -o nounset
set -o pipefail

}

# fetch k8s API gen tools and make it available under envtest_root_dir/bin.
function fetch_envtest_tools {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about checking and returning early if SKIP_FETCH_TOOLS is already set?

goarch=amd64
goos="unknown"

if [[ "$OSTYPE" == "linux-gnu" ]]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [[ "$OSTYPE" == "linux-gnu" ]]; then
if [[ "$OSTYPE" == "linux"* ]]; then

Comment on lines +31 to +33
k8s_version=1.16.4
goarch=amd64
goos="unknown"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about passing these in? So they're not hardcoded and they can be set dynamically when we call fetch_envtest_tools?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about:

  • Auto-detecting GOOS and GOARCH by running go env GOOS and go env GOARCH?
  • Defaulting, but allow overriding the k8s version?
    ENVTEST_K8S_VERSION="${ENVTEST_K8S_VERSION:-1.16.4}"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me

Comment on lines +21 to +23
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
export KUBEBUILDER_ASSETS=$1/bin
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
export KUBEBUILDER_ASSETS=$1/bin
fi
if [[ -z "${KUBEBUILDER_ASSETS}" ]]; then
header_text "setting up kubebuilder-tools@${k8s_version} env vars"
export KUBEBUILDER_ASSETS=$1/bin
fi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is KUBEBUILDER_ASSETS preferable to what we were doing before

export PATH=${tmp_root}/kubebuilder/bin:$PATH
  export TEST_ASSET_KUBECTL=${tmp_root}/kubebuilder/bin/kubectl
  export TEST_ASSET_KUBE_APISERVER=${tmp_root}/kubebuilder/bin/kube-apiserver
  export TEST_ASSET_ETCD=${tmp_root}/kubebuilder/bin/etcd

Copy link
Member Author

@joelanford joelanford Aug 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed calls to header_text because that lives in common.sh, and I didn't want to force users to download two scripts for this.

If the header text is important, I could copy that code into this script. But I figured callers of these functions could call header_text.

For environment variables, I opted to keep the existing environment setup as what's in master, but I'm not opposed to making this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's copy it, it's great to see some styled output when running it

return 0
fi
fi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
header_text "fetching envtest-tools@${k8s_version}"

fi

setup_envs
header_text "setting up envtest environment"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
header_text "setting up envtest environment"
header_text "setting up envtest@${k8s_version}"

Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally and it worked 👍

$ . ../sigs.k8s.io/controller-runtime/hack/setup-envtest.sh && envtest_assets_dir=$(pwd)/test/assets && fetch_envtest_tools “$envtest_assets_dir” && setup_envtest_env “$envtest_assets_dir”
camilamacedo@Camilas-MacBook-Pro ~/go/src/envtest $ make test
/Users/camilamacedo/go/bin/controller-gen object:headerFile=“hack/boilerplate.go.txt” paths=“./...”
go fmt ./...
go vet ./...
/Users/camilamacedo/go/bin/controller-gen “crd:trivialVersions=true” rbac:roleName=manager-role webhook paths=“./...” output:crd:artifacts:config=config/crd/bases
go test ./... -coverprofile cover.out
?  	github.com/example-inc/memcached-operator	[no test files]
?  	github.com/example-inc/memcached-operator/api/v1beta1	[no test files]
ok 	github.com/example-inc/memcached-operator/controllers	4.988s	coverage: 0.0% of statements
camilamacedo@Camilas-MacBook-Pro ~/go/src/envtest $

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 3, 2020
@k8s-ci-robot k8s-ci-robot merged commit 8734ac8 into kubernetes-sigs:master Aug 3, 2020
@joelanford joelanford deleted the setup-envtest branch August 3, 2020 17:56
joelanford added a commit to joelanford/controller-runtime that referenced this pull request Aug 3, 2020
joelanford added a commit to joelanford/controller-runtime that referenced this pull request Aug 3, 2020
joelanford added a commit to joelanford/controller-runtime that referenced this pull request Aug 4, 2020
joelanford added a commit to joelanford/controller-runtime that referenced this pull request Aug 4, 2020
k8s-ci-robot added a commit that referenced this pull request Aug 4, 2020
🏃 hack/setup-envtest.sh: follow-up from #1092
kevindelgado pushed a commit to kevindelgado/controller-runtime that referenced this pull request Sep 15, 2020
alkar added a commit to utilitywarehouse/kube-applier that referenced this pull request Dec 15, 2020
kubebuilder is not an actual dependency and is only installed in order
to pull the binary assets required for the envtest package. This change
pulls a controller-runtime script that helps with installing these
assets, independently of kubebuilder. This means that we don't depend on
the latter to release a new version which has pulled updates from
upstream.

This is apparently the recommended method (also how kubebuilder sets up
new projects), see:
 - kubernetes-sigs/controller-runtime#1092
 - https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v3/Makefile#L17-L21

Signed-off-by: Dimitrios Karagiannis <[email protected]>
alkar added a commit to utilitywarehouse/kube-applier that referenced this pull request Dec 15, 2020
kubebuilder is not an actual dependency and is only installed in order
to pull the binary assets required for the envtest package. This change
pulls a controller-runtime script that helps with installing these
assets, independently of kubebuilder. This means that we don't depend on
the latter to release a new version which has pulled updates from
upstream.

This is apparently the recommended method (also how kubebuilder sets up
new projects), see:
 - kubernetes-sigs/controller-runtime#1092
 - https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v3/Makefile#L17-L21

Signed-off-by: Dimitrios Karagiannis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants