Skip to content

Implement the Gateway API shim ("gateway-shim" controller) - #4158

Merged
jetstack-bot merged 8 commits into
cert-manager:masterfrom
maelvls:add-gateway-api-split-controller
Jul 15, 2021
Merged

jetstack-bot merged 8 commits into
cert-manager:masterfrom
maelvls:add-gateway-api-split-controller

Conversation

@maelvls

@maelvls maelvls commented Jun 29, 2021 •

Copy link
Copy Markdown
Member

In #4121, Jake implemented the support for the Gateway object for the "ingress shim" mechanism proposed as described in #4119. Jake decided to rename "ingress shim" to "certificate shim" the object that we create that that serves as a "shim" for bootstrapping the Secrets inside the TLS block of an Ingress.

This PR refactors the new "certificate shim" to be two separate controllers instead of a single one, as proposed by @munnerz:

Could we adjust this to be two distinct controllers, but where there is duplicate logic, factor that into standalone library functions instead? This will be easier to maintain long term, including when/if we ever create alternate ingress-shim's too (rather than having to constantly add more complex switching logic into this one controller)

Tasks:

Archived discussion: from "everything in the `controller` struct" to `SyncFnFor`

Instead of using a single struct, I use simple functions that clearly define their dependency. I want to avoid the "struct bloat" that makes it really hard to comprehend what is depending on what.

Instead of relying on a bloated controller struct, the common Sync injects its dependencies. The only caveat is that the number of arguments for Sync is quite big:

func SyncFnFor(
	rec record.EventRecorder,
    log logr.Logger,
    cmClient clientset.Interface,
    cmLister cmlisters.CertificateLister,
	defaults controller.IngressShimOptions,          // 🔥 So many arguments! 🤯
) func(context.Context, metav1.Object) error {
    return func(ctx context.Context, ingressLike metav1.Object) error {
        // The reconciliation logic.
    }
}

Pros:

  1. Each function indicates clearly what they depend on which improves readability,
  2. Way easier to test each function independently,
  3. No more controller struct bloat where we don't know which field is used where.

Cons:

  1. Very long function declaration (which I dislike, but I rather have that than a bloated controller struct that makes it really hard to know what is depending on what),
  2. Inconsistency with the rest of the codebase,
  3. Due to the first-order function, the Sync is indented twice.

Important notes for the reader

  • I removed the ClusterIssuer and Issuer informers that were in the controller since (1) they were not used, and (2) no event handler was set up. I think these were copy-pasted from another controller and we forgot to remove them. As you can see in the old controller.go, no event handler was set:

    // register handler functions
    ingressInformer.Informer().AddEventHandler(&controllerpkg.QueuingEventHandler{Queue: c.queue})
    certificatesInformer.Informer().AddEventHandler(&controllerpkg.BlockingEventHandler{WorkFunc: c.certificateDeleted})
  • I changed the way the logs are injected, which may change the way logs "look like" (e.g., I have to check whether the name of the controller still shows).

    Before, c.log was configured with the controller name, but the actual Sync function was not using c.log:

    // In controller.go:
    c.log = logf.FromContext(ctx.RootContext, ControllerName)
    
    // In sync.go:
    log := logf.WithResource(logf.FromContext(ctx), ing)
    ctx = logf.NewContext(ctx, log)

    After, we don't use logf.FromContext anymore and just pass the logger as an argument to SyncFnFor.

Test it!

Make sure you are running cert-manager with the flag --controllers=*,gateway-shim (the gateway shim controller is disabled by default) and that you have installed the Gateway API CRDs. To setup both:

./devel/setup-e2e-deps.sh

Then, create a Gateway. The issuer foo does not have to exist:

kubectl apply -f- <<EOF
apiVersion: networking.x-k8s.io/v1alpha1
kind: Gateway
metadata:
  name: example
  annotations:
    cert-manager.io/issuer: foo
spec:
  gatewayClassName: foo
  listeners:
    - hostname: example.com
      port: 443
      protocol: HTTPS
      routes:
        kind: HTTPRoute
        selector:
          matchLabels:
            app: foo
      tls:
        mode: Terminate
        certificateRef:
          name: example-com-tls
          kind: Secret
          group: core
    - hostname: www.example.com
      port: 443
      protocol: HTTPS
      routes:
        kind: HTTPRoute
        selector:
          matchLabels:
            app: foo
      tls:
        mode: Terminate
        certificateRef:
          name: example-com-tls
          kind: Secret
          group: core
EOF

You should now see a Certificate named example-com-tls:

# kubectl get cert example-com-tls -oyaml
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: example-com-tls
  ownerReferences:
  - apiVersion: networking.x-k8s.io/v1alpha1
    controller: true
    kind: Gateway
    name: example
spec:
  dnsNames:
  - example.com
  - www.example.com
  issuerRef:
    group: cert-manager.io
    kind: Issuer
    name: foo
  secretName: example-com-tls
  usages:
  - digital signature
  - key encipherment

/kind feature

Added a new optional controller: gateway-shim. cert-manager now supports automatic
creation of ACME certificates for the sig-network Gateway API Gateway, when annotated
similarly to existing support for Ingresses.

@jetstack-bot jetstack-bot added kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 29, 2021
@maelvls
maelvls marked this pull request as draft June 29, 2021 14:16
@jetstack-bot jetstack-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 29, 2021
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch from 81393de to 7d63d34 Compare June 29, 2021 18:58
@maelvls

maelvls commented Jun 30, 2021 •

Copy link
Copy Markdown
Member Author

@munnerz Is that what you had in mind? Or should each controller also have its own Sync?

@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch 2 times, most recently from 95a61e2 to 05f6017 Compare July 1, 2021 17:23
@jetstack-bot jetstack-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 5, 2021
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch 2 times, most recently from 55b230f to 8de3c60 Compare July 5, 2021 14:52
@jetstack-bot jetstack-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 5, 2021
@maelvls
maelvls marked this pull request as ready for review July 6, 2021 08:16
@jetstack-bot jetstack-bot added area/testing Issues relating to testing needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jul 6, 2021
Comment thread hack/bin/deps.bzl Resolved
@jetstack-bot jetstack-bot added the area/deploy Indicates a PR modifies deployment configuration label Jul 6, 2021
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch 2 times, most recently from 59d7fda to 06febbe Compare July 7, 2021 09:23
@jetstack-bot jetstack-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 7, 2021
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch 2 times, most recently from f03388a to 4de31bf Compare July 7, 2021 09:43
@maelvls maelvls changed the title Implement the Gateway API shim as a separate controller WIP: Implement the Gateway API shim as a separate controller Jul 7, 2021
@jetstack-bot jetstack-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 7, 2021
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch from 4de31bf to d855ea2 Compare July 7, 2021 11:30
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch from c2af666 to ecaf9ba Compare July 15, 2021 18:25
maelvls and others added 7 commits July 15, 2021 20:34
Note that the gateway-shim is only half the work for supporting the
Gateway API in cert-manager. The other half is the HTTP01 solver
support, which is still worked on.

The Gateway API in cert-manager is releases as an experimental feature
and needs to be enabled manually with the following flag:

  --controllers=*,gateway-shim

All the annotations supported by ingress-shim are also supported by
gateway-shim, with some exceptions:

  "acme.cert-manager.io/http01-ingress-class"

This annotation is not supported on the Gateway resource. Although the
Gateway resource also has a "gatewayClass" field, we will need to add
another field instead of "ingress-class" to avoid confusion with the
ingress-shim.

  "acme.cert-manager.io/http01-edit-in-place"

This annotation is not supported because it is specific to some ingress
controllers like ingress-gce.

  "kubernetes.io/tls-acme"

This annotation is not supported because it is a behavior inherited from
kube-lego and we chose not to keep this behavior with the Gateway API.

Unlike the ingress-shim, you can reuse the same Secret name in multiple
TLS configurations on the same Gateway resource.

The ingress-shim now shows the exact location of the duplicate
secretName when the user gives the same secretName in two separate TLS
blocks.

Signed-off-by: Maël Valais <[email protected]>
Co-authored-by: Jake Sanders <[email protected]>
The Gateway CRD has to be installed, meaning that the CRDs may be
installed after cert-manager. We don't want cert-manager to crash in
that case; instead, we let the user know that cert-manager will keep
retrying looking for the CRDs with this message on startup:

  controller.go:181] cert-manager/controller/build-context "msg"="the
  Gateway API CRDs do not seem to be present, cert-manager will keep
  retrying watching for them"

The user then sees the following message printed (using an exponential
back-off):

  reflector.go:167: Failed to watch *v1alpha1.Gateway: failed to list
  *v1alpha1.Gateway: the server could not find the requested resource
  (get gateways.networking.x-k8s.io)

Signed-off-by: Maël Valais <[email protected]>
@maelvls
maelvls force-pushed the add-gateway-api-split-controller branch from ecaf9ba to 368c765 Compare July 15, 2021 18:36
@irbekrm

irbekrm commented Jul 15, 2021

Copy link
Copy Markdown
Contributor

This looks good to me - glad we'll have support for the Gateway API now!

I'm adding an lgm with a hold - I see that the PR is still 'WIP', but I guess the last item is not strictly related to this code as such.
Also triggering all the old k8s e2e tests (the latest e2e failure is cluster networking/dns again it seems)

/test pull-cert-manager-e2e-v1-21
/test pull-cert-manager-e2e-v1-20
/test pull-cert-manager-e2e-v1-19
/test pull-cert-manager-e2e-v1-18
/test pull-cert-manager-e2e-v1-17
/test pull-cert-manager-e2e-v1-16
/test pull-cert-manager-upgrade-v1-21

/approve
/hold
/lgtm

@jetstack-bot jetstack-bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Jul 15, 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

@maelvls maelvls changed the title WIP: Implement the Gateway API shim as a separate controller Implement the Gateway API shim ("gateway-shim" controller) Jul 15, 2021
@jetstack-bot jetstack-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 15, 2021
@maelvls

maelvls commented Jul 15, 2021

Copy link
Copy Markdown
Member Author

/unhold

🔥

@jetstack-bot jetstack-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 15, 2021
@jetstack-bot
jetstack-bot merged commit 1b7e706 into cert-manager:master Jul 15, 2021
@jetstack-bot jetstack-bot added this to the v1.5 milestone Jul 15, 2021
@irbekrm

irbekrm commented Jul 15, 2021

Copy link
Copy Markdown
Contributor

🥳 🥳 seems like a good time for an alpha release?

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/deploy Indicates a PR modifies deployment configuration area/testing Issues relating to testing dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants