Skip to content

Support loading webhook config from versioned file - #4546

Merged
jetstack-bot merged 18 commits into
cert-manager:masterfrom
munnerz:webhook-config-api
Dec 14, 2021
Merged

jetstack-bot merged 18 commits into
cert-manager:masterfrom
munnerz:webhook-config-api

Conversation

@munnerz

@munnerz munnerz commented Oct 22, 2021

Copy link
Copy Markdown
Member

What this PR does / why we need it:

This PR adds support for loading configuration following the k8s.io/component-base recommendations (https://docs.google.com/document/d/1FdaEJUEh091qf5B98HM6_8MS764iXrxxigNIdwHYW9c/edit#heading=h.ctbveyrh07ch).

It adds a new type, WebhookConfiguration, which today contains a field for each of the current options we expose as flags.

This will allow us to e.g. rename fields, as well as pave the way for dynamically loading config (i.e. one day these types could be used as CRDs, with our various components watching for their own config objects).

Special notes for your reviewer:

Most of the complexity in this is within cmd/webhook and is there to handle 'layering' of configuration. In accordance with component-base recommendations, all options specified as flags on the CLI by the user will always take precedence over the equivalent field in the WebhookConfiguration object.

Release note:

Add support for loading webhook flags/options from a WebhookConfiguration file on disk

/cc @wallrj

@jetstack-bot
jetstack-bot requested a review from wallrj October 22, 2021 13:34
@jetstack-bot jetstack-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 22, 2021
@jetstack-bot

Copy link
Copy Markdown
Contributor

Hi @munnerz. Thanks for your PR.

I'm waiting for a jetstack 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.

Details

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.

@jetstack-bot jetstack-bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. area/api Indicates a PR directly modifies the 'pkg/apis' directory approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Oct 22, 2021
@munnerz
munnerz force-pushed the webhook-config-api branch from d7bcab4 to e2f0bf6 Compare October 22, 2021 13:34
@jetstack-bot jetstack-bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. and removed dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. labels Oct 22, 2021
@munnerz munnerz removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 22, 2021
@munnerz

munnerz commented Oct 22, 2021

Copy link
Copy Markdown
Member Author

/kind feature
/area api
/area webhook

@jetstack-bot jetstack-bot added kind/feature Categorizes issue or PR as related to a new feature. area/webhook Indicates a PR or issue relates to the webhook component and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 22, 2021
@munnerz

munnerz commented Oct 22, 2021

Copy link
Copy Markdown
Member Author

An example configuration that can now be loaded with --config path/to/file.yaml:

apiVersion: config.cert-manager.io/v1alpha1
kind: WebhookConfiguration
kubeConfig: path/to/kubeconfig/relative/to/config.yaml
tlsConfig:
  dynamic:
    secretNamespace: cert-manager
    secretName: webhook-tls
    dnsNames:
    - cert-manager-webhook
    - cert-manager-webhook.cert-manager
    - cert-manager-webhook.cert-manager.svc

@munnerz

munnerz commented Oct 22, 2021

Copy link
Copy Markdown
Member Author

/test all

@munnerz

munnerz commented Oct 22, 2021

Copy link
Copy Markdown
Member Author

/test pull-cert-manager-bazel

@jetstack-bot jetstack-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 27, 2021

@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.

I've read through the code and the upstream docs for component configuration- looks like a really useful change and it was an interesting read 👍🏼 Thanks for linking the upstream doc.
I've also tried to deploy with using with some options provided via WebhookConfiguration.

I've added a couple of comments, mostly questions.
A few more things:

  • I think it would be good to make using the WebhookConfiguration via helm- at the moment there is no option to specifiy volumes to be mounted to webhook. Arguably, we could maybe even add a configmap template for WebhookConfiguration that would probably also serve as documentation as to what fields there are

  • Assuming that we want to eventually deprecate the flags and only use the WebhookConfiguration, what would be the way forward with this? Do we just go with the standard way of deprecating all flags in favour of the WebhookConfiguration and should we aim for the WebhookConfiguration to be v1 at that point?

  • I wonder if there is some way how we could force the webhook to reload when the configmap's contents change. Or, perhaps we don't care about this because eventually we'll be watching the WebhookConfiguration? (I'm still not clear on how boostrapping will work in this setup)

Comment thread cmd/webhook/app/options/options.go Resolved
Comment thread cmd/webhook/app/options/options.go Resolved
Comment thread cmd/webhook/app/options/options.go Resolved
Comment thread internal/apis/config/register.go Resolved
Comment thread cmd/webhook/app/options/globalflags.go Resolved
@irbekrm

irbekrm commented Nov 1, 2021

Copy link
Copy Markdown
Contributor

/milestone v1.7

@jetstack-bot jetstack-bot added this to the v1.7 milestone Nov 1, 2021
@munnerz
munnerz force-pushed the webhook-config-api branch from e2f0bf6 to 0da2819 Compare November 9, 2021 12:54
@jetstack-bot jetstack-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 9, 2021
@munnerz

munnerz commented Nov 9, 2021

Copy link
Copy Markdown
Member Author

I think it would be good to make using the WebhookConfiguration via helm- at the moment there is no option to specifiy volumes to be mounted to webhook. Arguably, we could maybe even add a configmap template for WebhookConfiguration that would probably also serve as documentation as to what fields there are

I'm in two minds on this, largely because if we expose this as a CRD in future, then we won't be able to have users configure it via the Helm chart (as Helm doesn't play well with deploying instances of a CRD at the same time as installing the CRD itself). I'm +1 on making this easier, and naturally Helm does make sense, but given this is an optional feature for now I think it'd be easier to provide a ConfigMap example that is well documented, and have users create the ConfigMap if they do want to make use of this config API type (we can have the chart deploy with an optional: true configmap with a particular name to make this easy for users to use)

Assuming that we want to eventually deprecate the flags and only use the WebhookConfiguration, what would be the way forward with this? Do we just go with the standard way of deprecating all flags in favour of the WebhookConfiguration and should we aim for the WebhookConfiguration to be v1 at that point?

Yep that's the way I've seen k8s do it

I wonder if there is some way how we could force the webhook to reload when the configmap's contents change. Or, perhaps we don't care about this because eventually we'll be watching the WebhookConfiguration? (I'm still not clear on how boostrapping will work in this setup)

Yep this would be useful - for now given this is alpha, I've opted to go incrementally with all this stuff.

@irbekrm

irbekrm commented Nov 17, 2021

Copy link
Copy Markdown
Contributor

'm +1 on making this easier, and naturally Helm does make sense, but given this is an optional feature for now I think it'd be easier to provide a ConfigMap example that is well documented, and have users create the ConfigMap if they do want to make use of this config API type (we can have the chart deploy with an optional: true configmap with a particular name to make this easy for users to use)

ConfigMap makes sense, but I think we need to allow to specify a volume to be mounted for webhook deployment via Helm chart.

@munnerz

munnerz commented Nov 17, 2021

Copy link
Copy Markdown
Member Author

ConfigMap makes sense, but I think we need to allow to specify a volume to be mounted for webhook deployment via Helm chart.

My plan was to not expose this as a config option in the Helm chart, and instead update the Helm chart to always configure the webhook with a volume/volumeMount for a ConfigMap with a 'known name' such as cert-manager-webhook-config, but set it to optional: true - this way, all a user has to do to configure configuration is create this ConfigMap and it'll then be picked up by the webhook with no further changes. If the ConfigMap does not exist, the webhook will start up just fine as the volume is optional :)

@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.

I've played around with this PR for a while and it all looks good to me:

  • by default when deployed with Helm a configmap will be created, but will be empty and --config flag will not be passed
  • if .values.config is set the fields from there will be synced to the configmap and --config flag will be passed to webhook
  • since the contents of .values.config isn't validated and the apiversion needs to be set by user, adding a new api field should not break the helm values
  • with the static manifests, it is up to the users to create a configmap with the WebhookConfiguration, mount the volume and set the --config flag
  • at the moment there is no mechanism to ensure that webhook will reload the config when the contents of the configmap has changed. This may be solved in the future by having the webhook watch a WebhookConfiguration CR

Looks good to me, I'd be happy to lgtm except for the minor nit with apiversion in values file!

I guess we'll be also wanting to add to website docs how to configure this

# Flags will override options that are set here.
config:
# apiVersion: config.cert-manager.io/v1alpha1
# kind: WebhookConfiguration

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.

Nit: it may have been useful to put all possible fields here, but commented out so users don't have to look elsewhere to discover the structure

Comment thread deploy/charts/cert-manager/values.yaml Resolved
Comment thread deploy/charts/cert-manager/values.yaml Resolved

@wallrj wallrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't get the config file to work.
Here's how I tested:

$ ./devel/cluster/create.sh
$ ./devel/addon/gatewayapi/install.sh
$ ./devel/addon/certmanager/install.sh

# Then


$ cat ../values.yaml 
global:
  logLevel: 3
installCRDs: true
webhook:
  config:
    apiVersion: config.webhook.cert-manager.io/v1alpha1
    kind: WebhookConfiguration
    securePort: 8443
    enablePprof: true

$ helm upgrade cert-manager  bazel-bin/deploy/charts/cert-manager/cert-manager.tgz  --namespace cert-manager --create-namespace --values ../values.yaml --wait --install
Error: UPGRADE FAILED: timed out waiting for the condition

The webhook process logs an error:

kubectl -n cert-manager logs cert-manager-webhook-5c9bdf6fc8-gswwm 
E1202 17:17:32.653241       1 webhook.go:166] cert-manager/webhook "msg"="Failed to load webhook config file" "error"="failed to load webhook config file /var/cert-manager/config/config.yaml, error failed to decode: no kind \"WebhookConfiguration\" is registered for version \"config.webhook.cert-manager.io/v1alpha1\" in scheme \"pkg/runtime/scheme.go:100\""  "path"="/var/cert-manager/config/config.yaml"

And the configmap looks like this:

kubectl -n cert-manager get cm cert-manager-webhook  -o yaml
apiVersion: v1
data:
  config.yaml: |2

    apiVersion: config.webhook.cert-manager.io/v1alpha1
    enablePprof: true
    kind: WebhookConfiguration
    securePort: 8443
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: cert-manager
    meta.helm.sh/release-namespace: cert-manager
  creationTimestamp: "2021-12-02T17:16:35Z"
  labels:
    app: webhook
    app.kubernetes.io/component: webhook
    app.kubernetes.io/instance: cert-manager
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: webhook
  name: cert-manager-webhook
  namespace: cert-manager
  resourceVersion: "906"
  uid: 71c9599e-e537-4eea-999b-d12f7dd581f6

Any idea what I'm doing wrong?

@munnerz

munnerz commented Dec 2, 2021

Copy link
Copy Markdown
Member Author

@wallrj The API group should be webhook.config.cert-manager.io instead of config.webhook.cert-manager.io - Irbe noticed this during review and I've not addressed it yet 😅

@wallrj

wallrj commented Dec 2, 2021

Copy link
Copy Markdown
Member

Yep. That was it. It's working now:

kubectl -n cert-manager logs cert-manager-webhook-657bdc5ffb-9694b 
W1202 17:29:27.137527       1 client_config.go:615] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
W1202 17:29:27.138778       1 client_config.go:615] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
I1202 17:29:27.138882       1 webhook.go:76] cert-manager/webhook "msg"="using dynamic certificate generating using CA stored in Secret resource"  "secret_name"="cert-manager-webhook-ca" "secret_namespace"="cert-manager"
I1202 17:29:27.139181       1 server.go:141] cert-manager/webhook "msg"="listening for insecure healthz connections"  "address"=":6080"
I1202 17:29:27.139975       1 server.go:174] cert-manager/webhook "msg"="running go profiler on"  "address"="localhost:6060"
I1202 17:29:27.140040       1 server.go:205] cert-manager/webhook "msg"="listening for secure connections"  "address"=":8443"
I1202 17:29:27.141535       1 reflector.go:219] Starting reflector *v1.Secret (1m0s) from external/io_k8s_client_go/tools/cache/reflector.go:167
I1202 17:29:27.141561       1 reflector.go:255] Listing and watching *v1.Secret from external/io_k8s_client_go/tools/cache/reflector.go:167
I1202 17:29:28.214329       1 dynamic_source.go:273] cert-manager/webhook "msg"="Updated serving TLS certificate"  

@munnerz

munnerz commented Dec 3, 2021

Copy link
Copy Markdown
Member Author

by default when deployed with Helm a configmap will be created, but will be empty and --config flag will not be passed

Yeah I wonder if we're better to not create it at all... not sure. Needs some thought, but I think we can follow up later.

with the static manifests, it is up to the users to create a configmap with the WebhookConfiguration, mount the volume and set the --config flag

👍 correct!

at the moment there is no mechanism to ensure that webhook will reload the config when the contents of the configmap has changed. This may be solved in the future by having the webhook watch a WebhookConfiguration CR

Yeah we can look at a follow up for this. Either annotating pods with a hash of the config provided (which I think is a common Helm pattern) or improving the webhook to actually handle reloads when it changes 😄

@munnerz

munnerz commented Dec 3, 2021

Copy link
Copy Markdown
Member Author

Retesting after cert-manager/testing#612

/test pull-cert-manager-e2e-v1-22

@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.

I've read again through the code and it makes sense to me 👍🏼

We should probably add some docs for it on the website too.

Lgtm-ing with a hold so Richard can review

/hold
/lgtm

{{- end }}
{{ if not $config.tlsConfig -}}
{{- $tlsConfig := default $config.tlsConfig "" }}
{{ if or (not $config.tlsConfig) (and (not $tlsConfig.dynamic) (not $tlsConfig.filesystem) ) -}}

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.

Confusing, but it works 👍🏼

@jetstack-bot jetstack-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 3, 2021
@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 3, 2021

@wallrj wallrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I skimmed through the code and asked a few questions.
Nothing that should stop merging this, except to say that I think it's a bit premature to deprecate the securePort value of the Helm chart.

I'm not convinced that Helm users should care what port the webhook process listens on,
only what port the webhook Pod and perhaps the webhook service use,
neither of which can be changed from the WebhookConfiguration file.

Comment thread cmd/webhook/app/webhook.go Resolved
Comment thread cmd/webhook/app/webhook.go Resolved
port: 443
protocol: TCP
targetPort: {{ .Values.webhook.securePort }}
targetPort: "https"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is https a good name? I'd have thought it better to call it webhook to clearly distinguish it from metrics or pprof ports.
Perhaps this name was already used and can't be changed....I'll read on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was the name of the port previously - I am just changing the chart to use it to reference the port on the backend instead of hardcoding it into the service (which would complicate logic in this template): https://github.com/jetstack/cert-manager/pull/4546/files#diff-1eb646b61f7c20d254f00af700b9bea3f330419d0bb97b0234f58170aedabcdaR81

I think https makes sense as it makes it clear it is a secure service (IMO).

# rules or requiring NET_BIND_SERVICE capabilities to bind port numbers <1000.
# This should be uncommented and set as a default by the chart once we graduate
# the apiVersion of WebhookConfiguration past v1alpha1.
# securePort: 10250

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't think of a use case for wanting to change the listening port of the webhook process, when deployed with helm.

Only time I'd want to change this is when running the process locally on my laptop.

If run in a cluster, the process will always be run in a docker container and it won't matter what port the process listens on. Only matters which port is mapped to this in the PodTemplate, and then only on GKE private clusters.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The containerPort bit doesn't remap ports - it just 'names' a port on the Pod (for the Service to then send traffic to). So we do need a way for users to configure this (for ex private GKE clusters). Things like ECS (or maybe it was Lightsail? not sure) also have issues with 10250 if I recall correctly, due to pods running in hostNetwork there & a conflict with the local kubelet.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're right. Sorry. I was muddling Kubernetes networking with docker run -p port mapping. Thinking that Kubernetes always did some sort of port forwarding.

Comment thread deploy/charts/cert-manager/values.yaml Outdated
Comment on lines 328 to 329
# DEPRECATED: use webhook.config.securePort instead.
securePort: 10250

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems a bit premature to deprecate this.
I personally think that this is the correct field and that the Helm chart shouldn't attempt to configure either the --secure-port flag or the securePort field in the config file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It seems a bit premature to deprecate this.

👍 I'll remove this line

I personally think that this is the correct field and that the Helm chart shouldn't attempt to configure either the --secure-port flag or the securePort field in the config file.

Not 100% sure what you mean by this, but this isn't any kind of net-addition to the chart as we've always supported changing the listening port of the container (in order to allow people to workaround any environment specific quirks)

@munnerz

munnerz commented Dec 3, 2021

Copy link
Copy Markdown
Member Author

I'm not convinced that Helm users should care what port the webhook process listens on,
only what port the webhook Pod and perhaps the webhook service use,
neither of which can be changed from the WebhookConfiguration file.

It is possible to change the listen port of the process - that's exactly what securePort does. It's not possible to change the mapping of 443->securePort in the Service however.

@jetstack-bot jetstack-bot removed the lgtm Indicates that a PR is ready to be merged. label Dec 10, 2021

@wallrj wallrj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@munnerz

This looks good to me.
Many of my last review comments about securePort were mistaken (see below).

Some remaining questions:

  1. Will you be introducing a feature flag for this configuration file stuff?
  2. Is there any code here which has been copied from Kubernetes? And if so, perhaps we should add links to the upstream code: for our own benefit so that we can easily check upstream to see what improvements they've made in this area, and to give some copyright credit to the original authors.

Those could be addressed in later PRs if necessary (before next release) in which case unhold and merge this as-is.

/lgtm
/approve

# rules or requiring NET_BIND_SERVICE capabilities to bind port numbers <1000.
# This should be uncommented and set as a default by the chart once we graduate
# the apiVersion of WebhookConfiguration past v1alpha1.
# securePort: 10250

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're right. Sorry. I was muddling Kubernetes networking with docker run -p port mapping. Thinking that Kubernetes always did some sort of port forwarding.

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

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: irbekrm, munnerz, wallrj

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:
  • OWNERS [irbekrm,munnerz,wallrj]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@munnerz

munnerz commented Dec 14, 2021

Copy link
Copy Markdown
Member Author

Will you be introducing a feature flag for this configuration file stuff?

It was decided to not have a feature flag for this, given it requires explicitly enabling anyway to get this turned on. Also, when looking in the upstream project, there was not a feature flag for the config file work (only for the dynamic config work where the the configuration is read from an actual CRD in the apiserver)

Is there any code here which has been copied from Kubernetes? And if so, perhaps we should add links to the upstream code: for our own benefit so that we can easily check upstream to see what improvements they've made in this area, and to give some copyright credit to the original authors.

There's not any code that's been copied 1:1 - the majority of this is 'inspired by' the upstream work, and follows the component-base/configuration guidelines.

Thanks for all the reviews everyone!

/hold cancel

@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 Dec 14, 2021
@jetstack-bot
jetstack-bot merged commit 5894ed9 into cert-manager:master Dec 14, 2021
@munnerz
munnerz deleted the webhook-config-api branch December 14, 2021 10:09
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/api Indicates a PR directly modifies the 'pkg/apis' directory area/deploy Indicates a PR modifies deployment configuration area/webhook Indicates a PR or issue relates to the webhook component 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. ok-to-test 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