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

images/kube-webhook-certgen/rootfs: add support for patching APIService objects #7641

Merged
merged 11 commits into from
Sep 26, 2021

Conversation

invidian
Copy link
Member

@invidian invidian commented Sep 15, 2021

What this PR does / why we need it:

This PR adds ability to inject created CA bundle to APIService objects, in addition to the existing ValidatingWebhookConfiguration and MutatingWebhookConfiguration, which is helpful, e.g. when one wants to use extension API server without insecureSkipTLSVerify: true.

This mainly allows deploying APIService in a secure way without e.g. dependency on cert-manager, which in some environments might be undesirable.

Note, that right now this PR includes changes from PR #7630.

Do note that changes to the code includes some uncommon things, like wrappedError struct to 100% preserve existing behavior, I didn't know if it was actually required, but at least right now they should be easier to clean up.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

How Has This Been Tested?

There is decent set of unit tests implemented and invidian/kube-webhook-certgen:controller-v1.0.0-61-g36229c197 Docker image has been used for testing https://github.com/newrelic/helm-charts/tree/gsanchez/metrics-adapter-chart.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@k8s-ci-robot
Copy link
Contributor

@invidian: This issue is currently awaiting triage.

If Ingress contributors determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority labels Sep 15, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @invidian. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 15, 2021
@rikatz
Copy link
Contributor

rikatz commented Sep 16, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Sep 16, 2021
@rikatz
Copy link
Contributor

rikatz commented Sep 16, 2021

damn this is big.

Thanks @invidian I will put this on my review queue ASAP!

Initially only from some to preserve existing behavior.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
So we don't call log.Fatal in so many places, which makes code testable.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
So initialize top-level contexts in tests and CLI, then pass them around
all the way down, so there is an ability e.g. to add timeouts to patch
operations, if needed and to follow general conventions.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
APIService object is very similar to MutatingWebhookConfiguration and
ValidatingWebhookConfiguration objects, so support for patching it
shouldn't be too much of a burden.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
So old function PatchWebhookConfigurations can be unexported and CLI can
be extended to also support patching APIService.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
PatchObjects should be now used instead.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
To ignore manually built binaries during development process.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
By adding a PatchConfig and Patch function, it is now possible to test
logic of flag validation, which was previously tied to CLI options.

This commit adds nice set of tests covering existing logic.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
Those strings will be changed anyway in future commits, so at first we
can properly capitalize used names.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
As logic for creating a CA certificate and patching an object is almost
the same for both webhook configuration and API services, this commit
adds support to kube-webhook-certgen CLI to also patch APIService
objects, so they can be served over TLS as well.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
k8s.k8s.patchWebhookConfigurations() always dereferences it and we do
not do a nil check, so the code may panic in some conditions, so it's
safer to just pass it by value, as it's just a wrapped string.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
@invidian invidian force-pushed the invidian/improvements branch from 36229c1 to d573087 Compare September 16, 2021 21:01
@invidian
Copy link
Member Author

Thanks @invidian I will put this on my review queue ASAP!

Thanks @rikatz! I this is not super urgent or anything on my side, but it would be nice to not having it stale.

damn this is big.

Well, code required some creative work to keep it the same, add tests to existing functionality and make it extensible for new one. Hopefully another person adding features or fixing bugs in it will have a bit easier 😅

Also rebased after #7630 merge.

// PatchWebhookConfigurations will patch validatingWebhook and mutatingWebhook clientConfig configurations with
// the provided ca data. If failurePolicy is provided, patch all webhooks with this value
func (k8s *k8s) PatchWebhookConfigurations(
func (k8s *k8s) patchWebhookConfigurations(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: As you are unexporting the function (still wondering why!) you should change the comment as well

Copy link
Member Author

Choose a reason for hiding this comment

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

still wondering why

To reduce the surface of the package and to promote use of newly introduced and more flexible PatchObjects function. Though this is a breaking change on Go module level, so I can revert this change if you like.

you should change the comment as well

Probably, but then some of my linters yell, that comment is not capitalized, so I recently just started leaving those docs as they are.

Please suggest a desired change and I'll apply it, as I don't have a preference here.

@rikatz
Copy link
Contributor

rikatz commented Sep 19, 2021

@invidian overall lgtm, cool to see this project been extended :)

Some comments:

  • I have left two minor comments, one about if we want really to extend this to other clientsets (other than k8s), otherwise using I'm not sure about that interface
  • One nit about unexported functions.

Also, I was discussing with sig-net to turn this into an independent project without success, so if you don't mind we can keep the development here. I would suggest to add you as an approver/reviewer of the code of this program (we can add you into OWNERS file after you get membership in k8s org).

One last thing, maybe we want in a future to add some e2e tests into this using github actions :D

Thanks

@invidian
Copy link
Member Author

Thanks for a timely review @rikatz, highly appreciated. It would indeed be nice to plug this code into CI to ensure no regressions, if you plan to maintain it. However, I'll need to get familiar with existing setup for the project to adopt the standards also for this sub-directory.

Once we have that, we can perhaps add some e2e tests as well.

Also, I was discussing with sig-net to turn this into an independent project without success, so if you don't mind we can keep the development here. I would suggest to add you as an approver/reviewer of the code of this program (we can add you into OWNERS file after you get membership in k8s org).

I'm happy to apply for membership and become a reviewer here, I guess there shouldn't be too much work involved. Would you like to sponsor me in that?

@rikatz
Copy link
Contributor

rikatz commented Sep 19, 2021

Thanks for a timely review @rikatz, highly appreciated. It would indeed be nice to plug this code into CI to ensure no regressions, if you plan to maintain it. However, I'll need to get familiar with existing setup for the project to adopt the standards also for this sub-directory.

Once we have that, we can perhaps add some e2e tests as well.

Also, I was discussing with sig-net to turn this into an independent project without success, so if you don't mind we can keep the development here. I would suggest to add you as an approver/reviewer of the code of this program (we can add you into OWNERS file after you get membership in k8s org).

I'm happy to apply for membership and become a reviewer here, I guess there shouldn't be too much work involved. Would you like to sponsor me in that?

Yes sure, I can sponsor you for that :)

@invidian
Copy link
Member Author

I'm sorry @rikatz, but I think I won't be able to add CI or e2e tests for this code. I've been doing several attempts of trying to figure out how the existing CI setup works and I find it very hard to extend in a reasonable way. I think the following points are significant here:

  1. I don't see how make test is triggered on CI, I assume this is done somehow via Prow.
  2. All scripts and targets are closely tied to PKG variable, pointing to k8s.io/ingress-nginx by default, which because of 1. is hard to reasonably override or extend.
  3. Existing setup for e2e tests is very complex. Here is the rough flow:
1. GitHub Actions calls 'make kind-e2e-test'.
2. 'make kind-e2e-test' ONLY calls 'test/e2e/run.sh'.
3. 'test/e2e/run.sh' runs:
  3a. Creates kind cluster.
  3b. Calls 'make clean-image'.
  3c. Calls 'make build':
    - Calls 'build/build.sh' via 'build/run-in-docker.sh':
      - Calls 'go build'
  3d. Calls 'make image':
    - Calls 'make clean-image again'
      - Calls 'docker build'.
  3e. Calls 'make image' on e2e-image:
    - Calls 'make e2e-test-binary' on root Makefile:
      - Calls 'ginkgo build' via 'build/run-in-docker.sh'.
      - Calls 'docker build'.
  3f. Calls 'make e2e-test'.
4. 'make e2e-test' ONLY calls 'build/run-e2e-suite.sh'.
5. 'build/run-e2e-suite.sh' creates a Pod with e2e image.
6. e2e image calls 'ginkgo' with some obscure /e2e.test binary.

Note, that this is only to entry the actual test logic written in Go.

I could just drop a completely independent file to .github/workflows directory with some simplified tests, but I don't know if this is something acceptable. Please let me know how we can move those things forward.

Regarding the membership in K8s org, I found another sponsor, so I will be applying for it today.

@rikatz
Copy link
Contributor

rikatz commented Sep 26, 2021

Yeah, so about the CI we need to improve it. Components are really tied. What we CAN do right now is just verify if the image builds fine, and maybe create a kind cluster just to check if things gets patched. Anyway, we can look at this as a follow up.

Thanks for all the clarifications
/approve
/hold
Will just finish reviewing and merge this. We should probably want to promote the generated image for this one as well (and maybe announce in README.md the correct path, later!)

Thanks

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Sep 26, 2021
@rikatz
Copy link
Contributor

rikatz commented Sep 26, 2021

/lgtm
/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 Sep 26, 2021
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 26, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: invidian, rikatz

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 9acf62d into kubernetes:main Sep 26, 2021
@invidian invidian deleted the invidian/improvements branch September 26, 2021 19:21
@invidian
Copy link
Member Author

Thanks for merging!

Will just finish reviewing and merge this. We should probably want to promote the generated image for this one as well (and maybe announce in README.md the correct path, later!)

Yeah, having it "released" would be nice :)

Yeah, so about the CI we need to improve it. Components are really tied. What we CAN do right now is just verify if the image builds fine, and maybe create a kind cluster just to check if things gets patched. Anyway, we can look at this as a follow up.

Sure, I'll have a look into that.

invidian added a commit to newrelic-forks/ingress-nginx that referenced this pull request Sep 27, 2021
As a follow up to PR kubernetes#7641, this commit adds some basic e2e tests for
kube-webhook-certgen image.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
@invidian
Copy link
Member Author

Created #7717 for basic CI.

k8s-ci-robot pushed a commit that referenced this pull request Oct 10, 2021
As a follow up to PR #7641, this commit adds some basic e2e tests for
kube-webhook-certgen image.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
invidian added a commit to newrelic-forks/ingress-nginx that referenced this pull request Oct 25, 2021
To add myself as a reviewer as discussed in kubernetes#7641.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
k8s-ci-robot pushed a commit that referenced this pull request Oct 25, 2021
* OWNERS_ALIASES: add ingress-nginx-kube-webhook-certgen-reviewers

For extra kube-webhook-certgen reviewers.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen: add separate owners

To add myself as a reviewer as discussed in #7641.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
rchshld pushed a commit to joomcode/ingress-nginx that referenced this pull request May 19, 2023
…ce objects (kubernetes#7641)

* images/kube-webhook-certgen/rootfs/pkg/k8s: return err from functions

Initially only from some to preserve existing behavior.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs: make patching return error

So we don't call log.Fatal in so many places, which makes code testable.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/pkg/k8s: require context

So initialize top-level contexts in tests and CLI, then pass them around
all the way down, so there is an ability e.g. to add timeouts to patch
operations, if needed and to follow general conventions.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/pkg/k8s: support patching APIService

APIService object is very similar to MutatingWebhookConfiguration and
ValidatingWebhookConfiguration objects, so support for patching it
shouldn't be too much of a burden.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/cmd: use new patch API

So old function PatchWebhookConfigurations can be unexported and CLI can
be extended to also support patching APIService.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/pkg/k8s: unexport old patch function

PatchObjects should be now used instead.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs: add .gitignore

To ignore manually built binaries during development process.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/cmd: test patching

By adding a PatchConfig and Patch function, it is now possible to test
logic of flag validation, which was previously tied to CLI options.

This commit adds nice set of tests covering existing logic.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/cmd: improve formatting

Those strings will be changed anyway in future commits, so at first we
can properly capitalize used names.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs/cmd: support patching APIService

As logic for creating a CA certificate and patching an object is almost
the same for both webhook configuration and API services, this commit
adds support to kube-webhook-certgen CLI to also patch APIService
objects, so they can be served over TLS as well.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen/rootfs: pass failure policy by value

k8s.k8s.patchWebhookConfigurations() always dereferences it and we do
not do a nil check, so the code may panic in some conditions, so it's
safer to just pass it by value, as it's just a wrapped string.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
rchshld pushed a commit to joomcode/ingress-nginx that referenced this pull request May 19, 2023
As a follow up to PR kubernetes#7641, this commit adds some basic e2e tests for
kube-webhook-certgen image.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
rchshld pushed a commit to joomcode/ingress-nginx that referenced this pull request May 19, 2023
)

* OWNERS_ALIASES: add ingress-nginx-kube-webhook-certgen-reviewers

For extra kube-webhook-certgen reviewers.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>

* images/kube-webhook-certgen: add separate owners

To add myself as a reviewer as discussed in kubernetes#7641.

Signed-off-by: Mateusz Gozdek <mgozdek@microsoft.com>
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. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-priority needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants