Skip to content
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

✨ Allow passing the assets path bin via the env config #1214

Merged
merged 1 commit into from Nov 23, 2020

Conversation

camilamacedo86
Copy link
Member

@camilamacedo86 camilamacedo86 commented Oct 10, 2020

Description

  • Allow passing the assets path bin via the env config
        By("bootstrapping test environment")
	testEnv = &envtest.Environment{
		CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
                BinaryAssetsDirectory: "filepath.Join("..", "bin")" // See here
	}

The priority here is:

  1. the specific envvars for each binary: TEST_ASSET_KUBE_APISERVER, TEST_ASSET_ETCD, TEST_ASSET_KUBECTL
  2. the path for all env var: KUBEBUILDER_ASSETS
  3. the value set via the structure in this case Environment{}
  4. the default value which is currently /usr/local/kubebuilder/bin

Motivation

Allow passing the bin path to simplify the required implementation to config the EnvTest when the bin is not in the default path. Currently, if a developer would like to try to get the bins from a specific dir if they exist would be required an implementation such as:

var _ = BeforeSuite(func(done Done) {
	logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))

	By("bootstrapping test environment")
	testEnv = &envtest.Environment{
		CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
	}

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

	var err error
	cfg, err = testEnv.Start()
	Expect(err).ToNot(HaveOccurred())
	Expect(cfg).ToNot(BeNil())

	err = crewv1.AddToScheme(scheme.Scheme)
	Expect(err).NotTo(HaveOccurred())

	// +kubebuilder:scaffold:scheme

	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
	Expect(err).ToNot(HaveOccurred())
	Expect(k8sClient).ToNot(BeNil())

	close(done)
}, 60)

var _ = AfterSuite(func() {
	By("tearing down the test environment")
	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
}

With this change we can just:

var _ = BeforeSuite(func(done Done) {
	logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))

	By("bootstrapping test environment")
	testEnv = &envtest.Environment{
		CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
                BinaryAssetsDirectory: "filepath.Join("..", "bin")" // See here
	}

	var err error
	cfg, err = testEnv.Start()
	Expect(err).ToNot(HaveOccurred())
	Expect(cfg).ToNot(BeNil())

	err = crewv1.AddToScheme(scheme.Scheme)
	Expect(err).NotTo(HaveOccurred())

	// +kubebuilder:scaffold:scheme

	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
	Expect(err).ToNot(HaveOccurred())
	Expect(k8sClient).ToNot(BeNil())

	close(done)
}, 60)

var _ = AfterSuite(func() {
	By("tearing down the test environment")
	err := testEnv.Stop()
	Expect(err).ToNot(HaveOccurred())
})

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 10, 2020
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 10, 2020
@camilamacedo86 camilamacedo86 force-pushed the envtest-bin branch 2 times, most recently from 1ca5a31 to aa49d4f Compare October 10, 2020 14:17
@camilamacedo86 camilamacedo86 requested review from mengqiy and mariantalla and removed request for mengqiy October 12, 2020 14:58
@camilamacedo86
Copy link
Member Author

/test pull-controller-runtime-test-master

@camilamacedo86
Copy link
Member Author

/test pull-controller-runtime-test-master

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 17, 2020
@camilamacedo86 camilamacedo86 force-pushed the envtest-bin branch 4 times, most recently from 8450022 to 8d02d33 Compare October 17, 2020 14:08
@camilamacedo86 camilamacedo86 changed the title ✨ allow to pass the assets path via the environment config ✨ Allow pass the assets path bin via the env config Nov 11, 2020
@camilamacedo86 camilamacedo86 changed the title ✨ Allow pass the assets path bin via the env config ✨ Allow to pass the assets path bin via the env config Nov 11, 2020
@camilamacedo86 camilamacedo86 changed the title ✨ Allow to pass the assets path bin via the env config ✨ Allow passing the assets path bin via the env config Nov 11, 2020
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 13, 2020
@camilamacedo86 camilamacedo86 force-pushed the envtest-bin branch 3 times, most recently from 362e9ad to 24a75dd Compare November 13, 2020 11:48
Copy link
Contributor

@DirectXMan12 DirectXMan12 left a comment

Choose a reason for hiding this comment

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

EDIT: Overall, I think the priority should change a bit -- environment variables are intended as overrides, so I think they should be on top. Otherwise, if I store things elsewhere on my system, or want to test with a different version, I can't

@@ -113,6 +116,10 @@ type Environment struct {
// values are merged.
CRDDirectoryPaths []string

// AbsBinaryDirectory is the path where the binaries required for the envtest are
// locate in the environment
AbsBinaryDirectory string
Copy link
Contributor

Choose a reason for hiding this comment

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

This name is a bit unclear. Can we make this TestBinariesDirectory or BinaryAssetsDirectory or something?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@camilamacedo86
Copy link
Member Author

The suggestions made are applied now.

assetPath := os.Getenv(envKubebuilderPath)
if assetPath == "" {
assetPath = defaultKubebuilderPath
// getBinAssetPath will return the path for the binary informed
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// getBinAssetPath will return the path for the binary informed
// getBinAssetPath returns a path for binary from the following list of locations,
// ordered by precedence:
// 1. KUBEBUILDER_ASSETS
// 2. Environment.BinaryAssetsDirectory
// 3. The default path, "/usr/local/kubebuilder/bin"

@@ -113,6 +117,10 @@ type Environment struct {
// values are merged.
CRDDirectoryPaths []string

// BinaryAssetsDirectory is the path where the binaries required for the envtest are
// locate in the environment
Copy link
Contributor

@estroz estroz Nov 23, 2020

Choose a reason for hiding this comment

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

Suggested change
// locate in the environment
// located in the local environment. This field can be overridden by setting KUBEBUILDER_ASSETS.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 23, 2020
@camilamacedo86
Copy link
Member Author

Hi @estroz,

Thank you for the review. All done 👍

Copy link
Contributor

@estroz estroz left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 23, 2020
@DirectXMan12
Copy link
Contributor

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: camilamacedo86, coderanger, DirectXMan12, estroz

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 the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 23, 2020
@k8s-ci-robot k8s-ci-robot merged commit f3831c2 into kubernetes-sigs:master Nov 23, 2020
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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants