Skip to content

ingress-shim: untangle logic for "looking for cert owners" - #4149

Merged
jetstack-bot merged 8 commits into
cert-manager:masterfrom
maelvls:refactor-ingress-shim
Jul 14, 2021
Merged

jetstack-bot merged 8 commits into
cert-manager:masterfrom
maelvls:refactor-ingress-shim

Conversation

@maelvls

@maelvls maelvls commented Jun 28, 2021 •

Copy link
Copy Markdown
Member
This PR blocks #4158.

While thinking about James's #4121 (comment), I came across a weirdness in the ingress-shim controller.

Anytime a Certificate is deleted, we want to trigger a resync of the Ingress that owns this Certificate. Let's imagine that the below Certificate gets deleted:

# This is the Certificate given to the certificateDeleted function:
kind: Certificate
metadata:
  namespace: foo
  ownerReferences:
  - apiVersion: networking.k8s.io/v1beta1
    blockOwnerDeletion: true
    controller: true
    kind: Ingress
    name: example
    uid: 7d3897c2-ce27-4144-883a-e1b5f89bd65a

The certificateDeleted wants to trigger a resync of the Ingress example on the same namespace foo.

But for some reason, certificateDeleted does way more than just looking at the owner reference: it calls to ingressesForCertificate which lists all the Ingresses (even outside of the Certificate's namespace).

(Note: I simplified a bit the code)

func (c *controller) certificateDeleted(obj interface{}) {
	crt, _ := obj.(*cmapi.Certificate)
	ings, _ := c.ingressesForCertificate(crt) // 🤔 huh??
	for _, ing := range ings {
		c.queue.Add(key)
	}
}

Why do we need to loop over the ingresses? We are not even filtering on the Certificate's namespace, which means we need to iterate over all Ingresses that exist on the cluster every time a Certificate gets an update.

Why not just look at the Certificate's owner references and trigger a "requeue" based on that? I am trying to find a security risk associated with that, but I can't find one.

It could look like this:

func (c *controller) certificateDeleted(obj interface{}) {
    crt, _ := obj.(*cmapi.Certificate)

    ref := metav1.GetControllerOf(crt)
    if ref == nil {
        // No controller should care about orphans being deleted or
        // updated.
        return
    }

    if ref.Kind != "Ingress" {
        return
    }
    c.queue.Add(crt.Namespace + "/" + ref.Name) // ✅ No lister and no loop used
}

Pros:

  1. No more reliance on the Ingress informer,

Cons:

  1. If the Ingress referenced in the ownerReference does not exist (in which case this Certificate should have been garbage-collected anyway), then the ingress.Sync will say that this Ingress does not exist.

Note that I also chose to avoid using the controller struct as a receiver, since it obfuscates what this function depends on. If this function was depending on a lister, we would like to see that immediately.

Before:

func (c *controller) certificateDeleted(obj interface{}) {
}

After:

func certificateDeleted(queue workqueue.RateLimitingInterface) func(obj interface{}) {
	return func(obj interface{}) {
	}
}

/kind cleanup
/area ingress-shim
/cc @munnerz

@jetstack-bot
jetstack-bot requested a review from munnerz June 28, 2021 17:25
@jetstack-bot jetstack-bot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. area/ingress-shim Indicates a PR or issue relates to the ingress-shim 'auto-certificate' component dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. 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. release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jun 28, 2021
@maelvls
maelvls force-pushed the refactor-ingress-shim branch 2 times, most recently from bd69fdc to bc8b7cf Compare June 28, 2021 17:34
@maelvls
maelvls force-pushed the refactor-ingress-shim branch 4 times, most recently from 687b63c to 23fce3d Compare June 28, 2021 18:14
@maelvls
maelvls force-pushed the refactor-ingress-shim branch from 23fce3d to 0dd4309 Compare July 7, 2021 10:09
@maelvls
maelvls force-pushed the refactor-ingress-shim branch from 0dd4309 to 75b9bd6 Compare July 7, 2021 11:27
@maelvls

maelvls commented Jul 7, 2021

Copy link
Copy Markdown
Member Author

@irbekrm I added a couple of comments to help the review process 😅

@maelvls

maelvls commented Jul 7, 2021

Copy link
Copy Markdown
Member Author

I just noticed there is 0 unit test around certificateUpdated, which means you will have to "trust me" for this one... I need to find a way to unit-test the queueing mechanism...

@james-w

james-w commented Jul 8, 2021

Copy link
Copy Markdown
Contributor

[I just read the description, not the code]

The first obvious question to me is whether we set the ingress as the owner of the cert
when the cert is created. If not then this won't find the ingress.

The second thing I noticed is that the previous code could find multiple ingresses,
whereas the new code will only find one. Is it possible for two ingress objects to
be associated with one certificate? If it is then just following the owner reference
wouldn't be sufficient.

@maelvls

maelvls commented Jul 8, 2021 •

Copy link
Copy Markdown
Member Author

The first obvious question to me is whether we set the ingress as the owner of the cert when the cert is created. If not then this won't find the ingress.

That's a good question, I assumed that it was the case. Looking at the creation of the Certificate "shim", it seems like we always do add the Ingress as an owner (and more specifically, a "controller object", which is an ownerReference with controller: true):

https://github.com/jetstack/cert-manager/blob/d1c7730e90084a0bab5b93ea0a8dec8190a2f5ed/pkg/controller/ingress-shim/sync.go#L167

The previous code could find multiple ingresses, whereas the new code will only find one. Is it possible for two ingress objects to be associated with one certificate? If it is then just following the owner reference wouldn't be sufficient.

Each Certificate can only be owned by a single Ingress, but one Ingress can be associated with multiple Certificate (which is fine).

Just to elaborate further, the old code relied solely on the ownerReference.UID:

when a certificate gets deleted {
    ingressUID := getIngressControllerOf(certificate).UID
    for every ingress in the cluster {
        if ingressUID == ingress.UID {
            reconcile this ingress
        }
    }
}

The new code uses the ownerReference.Name directly:

when a certificate gets deleted {
    ingressName := getIngressOwnerOf(certificate).Name
    reconcile this ingress
}

I can't think of any reason for not using ownerReference.Name directly. Let's remember that the role of certificateDeleted is only to reduce the delay before the Ingress gets reconciled (it is auto-reconciled every 10 hours anyways).

@james-w

james-w commented Jul 8, 2021

Copy link
Copy Markdown
Contributor

/assign @maelvls

@james-w

james-w commented Jul 8, 2021

Copy link
Copy Markdown
Contributor

Good explanation, thanks.

@maelvls
maelvls force-pushed the refactor-ingress-shim branch from 05378ab to 30ad337 Compare July 9, 2021 16:27
@maelvls

maelvls commented Jul 12, 2021

Copy link
Copy Markdown
Member Author

/test pull-cert-manager-bazel

@maelvls
maelvls force-pushed the refactor-ingress-shim branch from cf6f7e5 to 4759995 Compare July 12, 2021 10:43
@maelvls
maelvls force-pushed the refactor-ingress-shim branch 2 times, most recently from 3b165c4 to 67b48fb Compare July 12, 2021 15:28
@maelvls

maelvls commented Jul 12, 2021 •

Copy link
Copy Markdown
Member Author

I added unit tests around certificateDeleted:

  • an Ingress should be queued when it is created
  • an Ingress should be queued when it is updated
  • an Ingress should be queued when it is deleted
  • an Ingress should not be queued when its child Certificate is added
  • an Ingress should not be queued when its child Certificate is updated
  • an Ingress should be queued when its child Certificate is deleted (that's the test for certificateDeleted)

Doing so, I discovered that we did not only re-queue the parent ingress on the deletion of its child certificate, but also on creation and update.

The func certificateDeleted was being called on every possible event
(deleted, created, updated).

Signed-off-by: Maël Valais <[email protected]>
@maelvls
maelvls force-pushed the refactor-ingress-shim branch 2 times, most recently from 3a48870 to 570c81e Compare July 13, 2021 11:41

@irbekrm irbekrm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me as we already reviewed it together.
I've added a couple more questions, but happy to merge once the tests pass.

Thanks for adding the test cases- it also helps with understanding what the code is doing!

@maelvls
maelvls force-pushed the refactor-ingress-shim branch from 570c81e to 5d34571 Compare July 13, 2021 16:47
@maelvls
maelvls force-pushed the refactor-ingress-shim branch from 5d34571 to a7b2c25 Compare July 13, 2021 16:55
@maelvls
maelvls force-pushed the refactor-ingress-shim branch from a7b2c25 to b13b751 Compare July 13, 2021 17:06

@irbekrm irbekrm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the work on this @maelvls!

This looks good to me now.

/lgtm

Comment on lines +151 to +159
for {
// Get blocks until either (1) a key is returned, or (2) the
// queue is shut down.
gotKey, done := queue.Get()
if done {
break
}
gotKeys = append(gotKeys, gotKey.(string))
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This makes sense 👍🏼

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label Jul 14, 2021
@jetstack-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: irbekrm, maelvls

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

The pull request process is described here

Details 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

@irbekrm

irbekrm commented Jul 14, 2021

Copy link
Copy Markdown
Contributor

DNS- this is a known issue in the workers cluster

@irbekrm

irbekrm commented Jul 14, 2021

Copy link
Copy Markdown
Contributor

/retest

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. area/ingress-shim Indicates a PR or issue relates to the ingress-shim 'auto-certificate' component dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. 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