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

feat: avoid importing kubectl deps when using ApplysetApplier #331

Merged
merged 1 commit into from
May 5, 2023

Conversation

yuwenma
Copy link
Contributor

@yuwenma yuwenma commented May 3, 2023

What this PR does / why we need it:
This PR allows k-d-p operators to not import the kubectl dependent libraries if using ApplysetApplier.

CI change and test coverage:
Some Test files are tagged with without_kubectl_deps specifically. We added a new CI test run in the dev/test to make sure those tests are run with the without_kubectl_deps tag

CGO_ENABLED=0 go test -tags without_kubectl_deps -count=1 -v ./...

usage:

Take Guestbook as an example,

// examples/guestbook-operator/controllers/guestbook_controller.go
applier := applier.NewApplySetApplier(metav1.PatchOptions{}, metav1.DeleteOptions{}, applier.ApplysetOptions{})
return r.Reconciler.Init(mgr, &api.Guestbook{},
        ...
        declarative.WithApplier(applier),
		declarative.WithApplyPrune())

go run -tags without_kubectl_deps main.go

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

yuwenma commented May 3, 2023

/assign @justinsb

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 3, 2023
@yuwenma yuwenma force-pushed the no-kubectl branch 3 times, most recently from 2eb6c43 to 1966900 Compare May 4, 2023 00:09
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 4, 2023
@yuwenma yuwenma force-pushed the no-kubectl branch 3 times, most recently from e27854e to 55e9816 Compare May 4, 2023 00:46
Copy link
Contributor

@justinsb justinsb left a comment

Choose a reason for hiding this comment

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

Looks good, but I think if we can avoid go:build without_kubectl_deps except in one file that builds the default object, I think this PR gets simpler.

@@ -1,3 +1,6 @@
//go:build without_kubectl_deps
// +build without_kubectl_deps
Copy link
Contributor

Choose a reason for hiding this comment

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

Dumb question: why is this one needed? I would have expected only to see go:build !without_kubectl_deps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, because this test needs to import and use the ApplysetApplier (see code). Without this building tag, go test won't successfully import the applier package.

Copy link
Contributor

Choose a reason for hiding this comment

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

The Applyset applier should always be present, I think.

pkg/patterns/declarative/pkg/applier/applylib.go Outdated Show resolved Hide resolved
@@ -1,3 +1,6 @@
//go:build !without_kubectl_deps
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we do this by applier, because there are two "dependencies" on kubectl - there is execing kubectl, and there is calling into the kubectl code.

So maybe we have two tags: without_kubectl_applier (or without_exec_applier) and without_direct_applier ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing! Yeah, I was thinking about to define the tags for 3 kinds rather than 2, but on my side I don't have a dedicated use case that need to distinguish exec and direct kubectl.

pkg/patterns/declarative/pkg/applier/golden_test.go Outdated Show resolved Hide resolved
@yuwenma yuwenma force-pushed the no-kubectl branch 5 times, most recently from 3f15434 to 3cb08fe Compare May 4, 2023 21:52
@yuwenma
Copy link
Contributor Author

yuwenma commented May 4, 2023

Looks good, but I think if we can avoid go:build without_kubectl_deps except in one file that builds the default object, I think this PR gets simpler.

qq: but I think this will cause the without_kubectl_deps test to call the test cases that need the kubectl dep (which cause undefined error because the direct.go and exec.go are excluded).
e.g.
run go test -tags without_direct_applier,without_exec_applier will include test with_kubectl_test.go:

// assuming I commented out the tags
//go:build !without_exec_applier || !without_direct_applier
// +build !without_exec_applier !without_direct_applier

func TestSimpleReconciler(t *testing.T) {
	Key := "direct"
	t.Run(Key, func(t *testing.T) {
		testharness.RunGoldenTests(t, "testdata/reconcile/"+Key+"/", func(h *testharness.Harness, testdir string) {
            // ! NewDirectApplier raises undeclared error
			testSimpleReconciler(h, testdir, applier.NewDirectApplier(), status.NewBasic(nil)) 
		})
	})
}

@@ -1,3 +1,6 @@
//go:build without_direct_applier && !without_exec_applier
Copy link
Contributor

Choose a reason for hiding this comment

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

So I would only expect to see these negated (except for globals.go). Otherwise (in this case) users must specify without_direct_applier to get the exec applier.

So I would expect to see this to be //go:build !without_exec_applier

@@ -0,0 +1,23 @@
//go:build without_exec_applier && without_direct_applier
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes sense, this is the one file where I would expect to see these not negated, because we want complete coverage of all the build tags in the various global_....go files.

pkg/patterns/declarative/pkg/applier/applylib_test.go Outdated Show resolved Hide resolved
@yuwenma yuwenma force-pushed the no-kubectl branch 2 times, most recently from 1384f65 to 15664d9 Compare May 5, 2023 17:27
applier := applier.NewApplySetApplier(
metav1.PatchOptions{FieldManager: "kdp-test"}, metav1.DeleteOptions{}, applier.ApplysetOptions{})

applier := applier.DefaultApplier
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tolerant build tag. Any applier should pass this test.

Comment on lines +1 to +2
//go:build !without_exec_applier || !without_direct_applier
// +build !without_exec_applier !without_direct_applier
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this file contains kubectl deps.

Comment on lines +1 to +2
//go:build !without_exec_applier || !without_direct_applier
// +build !without_exec_applier !without_direct_applier
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file contains kubectl deps. Tag to allow skipping importing.

Comment on lines +1 to +2
//go:build !without_exec_applier
// +build !without_exec_applier
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file contains exec.go methods

pkg/patterns/declarative/pkg/applier/applylib_test.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 5, 2023
@justinsb
Copy link
Contributor

justinsb commented May 5, 2023

Thanks @yuwenma - lgtm!

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 5, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb, yuwenma

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 merged commit 4cd1d8f into kubernetes-sigs:master May 5, 2023
3 checks passed
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.

None yet

3 participants