Repository navigation
Cleanup goroutine management - #4243
Conversation
|
Hi @inteon. Thanks for your PR. I'm waiting for a jetstack or cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
|
/ok-to-test |
9d162ab to
c49bb11
Compare
SgtCoDFish
left a comment
There was a problem hiding this comment.
I've not done a full review, but found a couple of typos 👍
c49bb11 to
75915fd
Compare
Signed-off-by: Inteon <[email protected]>
Signed-off-by: Inteon <[email protected]>
Signed-off-by: Inteon <[email protected]>
Signed-off-by: Inteon <[email protected]>
Signed-off-by: Inteon <[email protected]>
75915fd to
632459c
Compare
wallrj
left a comment
There was a problem hiding this comment.
I tried running the controller locally on my laptop to see how it would exit after ctrl-c.
richard improved_go_routines ~ projects cert-manager cert-manager bazel-bin/cmd/controller/controller_/controller --kubeconfig ~/.kube/config
I0727 17:57:43.504762 886925 controller.go:81] cert-manager/controller "msg"="enabled controllers: [certificaterequests-approver certificaterequests-issuer-acme certificaterequests-issuer-ca certificaterequests-issuer-selfsigned certificaterequests-issuer-vault certificaterequests-issuer-venafi certificates-issuing certificates-key-manager certificates-metrics certificates-readiness certificates-request-manager certificates-revision-manager certificates-trigger challenges clusterissuers ingress-shim issuers orders]"
I0727 17:57:43.505747 886925 leaderelection.go:243] attempting to acquire leader lease kube-system/cert-manager-controller...
I0727 17:57:43.541148 886925 leaderelection.go:253] successfully acquired lease kube-system/cert-manager-controller
E0727 17:57:43.645834 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Certificate: failed to list *v1.Certificate: the server could not find the requested resource (get certificates.cert-manager.io)
E0727 17:57:43.645838 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.CertificateRequest: failed to list *v1.CertificateRequest: the server could not find the requested resource (get certificaterequests.cert-manager.io)
E0727 17:57:43.645919 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.ClusterIssuer: failed to list *v1.ClusterIssuer: the server could not find the requested resource (get clusterissuers.cert-manager.io)
E0727 17:57:43.645991 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Challenge: failed to list *v1.Challenge: the server could not find the requested resource (get challenges.acme.cert-manager.io)
E0727 17:57:43.646043 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Issuer: failed to list *v1.Issuer: the server could not find the requested resource (get issuers.cert-manager.io)
W0727 17:57:43.646124 886925 warnings.go:70] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
E0727 17:57:43.646225 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Order: failed to list *v1.Order: the server could not find the requested resource (get orders.acme.cert-manager.io)
W0727 17:57:43.651424 886925 warnings.go:70] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
E0727 17:57:44.689568 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Issuer: failed to list *v1.Issuer: the server could not find the requested resource (get issuers.cert-manager.io)
E0727 17:57:44.690006 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.ClusterIssuer: failed to list *v1.ClusterIssuer: the server could not find the requested resource (get clusterissuers.cert-manager.io)
E0727 17:57:44.840039 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Challenge: failed to list *v1.Challenge: the server could not find the requested resource (get challenges.acme.cert-manager.io)
E0727 17:57:44.914405 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Certificate: failed to list *v1.Certificate: the server could not find the requested resource (get certificates.cert-manager.io)
E0727 17:57:45.174561 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.CertificateRequest: failed to list *v1.CertificateRequest: the server could not find the requested resource (get certificaterequests.cert-manager.io)
E0727 17:57:45.221546 886925 reflector.go:138] external/io_k8s_client_go/tools/cache/reflector.go:167: Failed to watch *v1.Order: failed to list *v1.Order: the server could not find the requested resource (get orders.acme.cert-manager.io)
^CI0727 17:57:46.131476 886925 controller.go:347] cert-manager/controller/leader-election "msg"="leader election lost"
richard improved_go_routines ~ projects cert-manager cert-manager 1 echo $?
1
I expected it to exit with 0 because it's configured with the Graceful error handling option. What do you expect?
The We should probably check if the context is canceled when the function is called, and only exit if it is not canceled. |
Signed-off-by: Inteon <[email protected]>
…ion ReleaseOnCancel works Signed-off-by: Inteon <[email protected]>
Signed-off-by: Inteon <[email protected]>
|
UPDATE: I did some more refactoring in You can compare this locally (master vs this PR-branch): bazel run //cmd/controller:controller -- --kubeconfig ~/.kube/config
# ctrl+c -> cancel context
bazel run //cmd/controller:controller -- --kubeconfig ~/.kube/config
# on master there will now be a long leader election conflict, on this PR-branch; leader election should be instantaneous |
|
Excellent addition!! I always had to wait for 10-20 seconds for the leader election to time out when restarting the pod... Now I won't have to wait 🙏🙏 |
wallrj
left a comment
There was a problem hiding this comment.
Thanks @inteon
There's quite a lot to absorb in this PR and I found the diff quite difficult to read...it's difficult for me to be sure that we're not changing any important behaviour with all these changes.
Is there any danger of deadlocks, while waiting for all these goroutines to exit? And if there were a deadlock after the leaderelection lock had been released, is there a danger that users (who have scaled up the default deployments) might end up with multiple cert-manager controllers competing to reconcile the same CRs? How bad would that be?
It'd be nice if you could add some comments to the code explaining what precautions you've taken to ensure that the ReleaseOnCancel will only happen after all the other goroutines have exited.
| ctx.Metrics.Shutdown(metricsServer) | ||
| os.Exit(0) | ||
| select { | ||
| case <-rootCtx.Done(): // Exit early if the Elected channel gets closed because we are shutting down. |
There was a problem hiding this comment.
| case <-rootCtx.Done(): // Exit early if the Elected channel gets closed because we are shutting down. | |
| case <-rootCtx.Done(): // Exit early if we are shutting down or if any of the preceding goroutines have exited with failure. |
That makes me think that the errgroup thing isn't quite right....because what we want is for all the goroutines to exit if any of them exit (not just those that exit with an error). So should we consider making them all exit with a error of some kind? Do you know what I mean?
There was a problem hiding this comment.
https://pkg.go.dev/golang.org/x/sync/errgroup#Group.Wait
"Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them."
The errgroup will wait for all goroutines to return, not just those that returned an error. But in case an error is returned, the linked context will be canceled. If the context is canceled (either because of an error or a interrupt signal), we want all goroutines to return -> this is something we have to check for ourselves in our goroutine's implementation by passing the errgroup context & adding them to all select cases.
There was a problem hiding this comment.
Yeah, but I was thinking of the case where one of the goroutines exits early with nil.....for some reason.
Perhaps if I examined the code, I'd see that that can't ever happen, but nevertheless, the expectation is that all the goroutines are running, otherwise cert-manager will not be functioning properly.
So ideally, we'd use something like errgroup, but where the exit of one goroutine (with error or nil) would immediatly cancel the group context so that the other goroutines can also exit cleanly.
If you agree with my logic, I don't propose that it should be done in this PR, but perhaps it should be considered for a future PR.
Signed-off-by: Inteon <[email protected]>
If the main context is canceled (= the stopChannel is closed), this will cause all sub-contexts to also be canceled. eg.: The Leader Election Logic itself has to use a context.TODO() to update the leases, because the context it was using before got canceled: https://github.com/kubernetes/client-go/blob/4841142cdc4ba44627994f8c25da76c2be2cdb5d/tools/leaderelection/leaderelection.go#L294-L312 I did not add any other logic to make sure that all goroutines have exited. I don't think it is necessary to do that yet. |
wallrj
left a comment
There was a problem hiding this comment.
Thanks for sorting all this out @inteon
It looks fine to me.
bazel run //cmd/controller -- --kubeconfig ~/.kube/config -v 2
INFO: Analyzed target //cmd/controller:controller (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //cmd/controller:controller up-to-date:
bazel-bin/cmd/controller/controller_/controller
INFO: Elapsed time: 0.291s, Critical Path: 0.08s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
I0728 15:32:49.220335 1000596 start.go:75] cert-manager "msg"="starting controller" "git-commit"="{STABLE_BUILD_GIT_COMMIT}" "version"="{STABLE_DOCKER_TAG}"
I0728 15:32:49.224991 1000596 controller.go:259] cert-manager/controller/build-context "msg"="configured acme dns01 nameservers" "nameservers"=["127.0.0.53:53"]
I0728 15:32:49.225794 1000596 controller.go:84] cert-manager/controller "msg"="enabled controllers: [certificaterequests-approver certificaterequests-issuer-acme certificaterequests-issuer-ca certificaterequests-issuer-selfsigned certificaterequests-issuer-vault certificaterequests-issuer-venafi certificates-issuing certificates-key-manager certificates-metrics certificates-readiness certificates-request-manager certificates-revision-manager certificates-trigger challenges clusterissuers ingress-shim issuers orders]"
I0728 15:32:49.226153 1000596 controller.go:114] cert-manager/controller "msg"="starting leader election"
I0728 15:32:49.226210 1000596 controller.go:104] cert-manager/controller "msg"="starting metrics server" "address"={"IP":"::","Port":9402,"Zone":""}
I0728 15:32:49.226551 1000596 leaderelection.go:243] attempting to acquire leader lease kube-system/cert-manager-controller...
I0728 15:32:49.343855 1000596 leaderelection.go:253] successfully acquired lease kube-system/cert-manager-controller
I0728 15:32:49.344073 1000596 controller.go:162] cert-manager/controller "msg"="not starting controller as it's disabled" "controller"="gateway-shim"
I0728 15:32:49.344403 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificaterequests-issuer-acme"
I0728 15:32:49.344589 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificaterequests-issuer-vault"
I0728 15:32:49.344589 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificaterequests-issuer-selfsigned"
I0728 15:32:49.344755 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-issuing"
I0728 15:32:49.344821 1000596 controller.go:162] cert-manager/controller "msg"="not starting controller as it's disabled" "controller"="certificatesigningrequests-issuer-vault"
I0728 15:32:49.344869 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-key-manager"
I0728 15:32:49.345010 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="issuers"
I0728 15:32:49.345069 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="orders"
I0728 15:32:49.345098 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="ingress-shim"
I0728 15:32:49.345163 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificaterequests-issuer-venafi"
I0728 15:32:49.345105 1000596 controller.go:162] cert-manager/controller "msg"="not starting controller as it's disabled" "controller"="certificatesigningrequests-issuer-ca"
I0728 15:32:49.345281 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificaterequests-approver"
I0728 15:32:49.345298 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-readiness"
I0728 15:32:49.345371 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-request-manager"
I0728 15:32:49.345415 1000596 controller.go:162] cert-manager/controller "msg"="not starting controller as it's disabled" "controller"="certificatesigningrequests-issuer-selfsigned"
I0728 15:32:49.345441 1000596 controller.go:162] cert-manager/controller "msg"="not starting controller as it's disabled" "controller"="certificatesigningrequests-issuer-venafi"
I0728 15:32:49.345462 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-trigger"
I0728 15:32:49.345421 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-revision-manager"
I0728 15:32:49.446780 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="challenges"
I0728 15:32:49.446877 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificaterequests-issuer-ca"
I0728 15:32:49.446972 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="certificates-metrics"
I0728 15:32:49.447062 1000596 controller.go:185] cert-manager/controller "msg"="starting controller" "controller"="clusterissuers"
W0728 15:32:49.451647 1000596 warnings.go:70] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
W0728 15:32:49.462963 1000596 warnings.go:70] networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
q
^CI0728 15:32:59.014775 1000596 controller.go:126] cert-manager/controller/certificates-revision-manager "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014846 1000596 controller.go:126] cert-manager/controller/certificates-readiness "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014865 1000596 controller.go:126] cert-manager/controller/certificaterequests-issuer-vault "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014888 1000596 controller.go:126] cert-manager/controller/certificates-issuing "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014901 1000596 controller.go:126] cert-manager/controller/clusterissuers "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014915 1000596 controller.go:126] cert-manager/controller/certificaterequests-issuer-selfsigned "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014928 1000596 controller.go:126] cert-manager/controller/ingress-shim "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014816 1000596 controller.go:126] cert-manager/controller/certificaterequests-issuer-acme "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014956 1000596 controller.go:126] cert-manager/controller/certificaterequests-approver "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014965 1000596 controller.go:126] cert-manager/controller/certificates-key-manager "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014972 1000596 controller.go:126] cert-manager/controller/challenges "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014860 1000596 controller.go:126] cert-manager/controller/orders "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014903 1000596 controller.go:126] cert-manager/controller/issuers "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014822 1000596 controller.go:126] cert-manager/controller/certificates-trigger "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014816 1000596 controller.go:126] cert-manager/controller/certificaterequests-issuer-venafi "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.015018 1000596 controller.go:126] cert-manager/controller/certificates-metrics "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014931 1000596 controller.go:126] cert-manager/controller/certificates-request-manager "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.014948 1000596 controller.go:126] cert-manager/controller/certificaterequests-issuer-ca "msg"="shutting down queue as workqueue signaled shutdown"
I0728 15:32:59.029572 1000596 controller.go:202] cert-manager/controller "msg"="control loops exited"
/lgtm
/approve
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: inteon, wallrj The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
@wallrj could you cherry pick this pr in the 1.4 release? We faced an issue that the cainjector hung before the it got elected, if the manager stops with error. This issue might be related to this pr after investigating. |
@yanxingponyai We're releasing cert-manager 1.5 today, which will include this code. Will you be able to use that? Or at least test the 1.5 release to find out if it does indeed solve your issue. Then we might consider backporting it to 1.4. |
Given the size of this patch, I'd be reluctant to see this back-ported into a patch release for risk of causing issues if there are any edge cases we have not caught... |
What this PR does / why we need it:
In the cert-manager repo, a lot of different methods are utilized for managing go routines.
Some of them do not properly wait for the goroutine to finish before exiting the program.
This might cause some logic to not run or only run sometimes.
This PR also cleans up the "management of goroutines", mostly by introducing "errgroup".
Release note:
/kind cleanup