使用 secret 进行 ValidatingWebhookConfiguration

Use secret for ValidatingWebhookConfiguration

为了使我的 ValidatingWebhookConfiguration 正常工作,我必须执行一系列 openssl 命令,然后我将证书颁发机构复制粘贴(或 sed)到我的 deploy.yaml 文件中,其中定义了我的 webhook。但这并不是真的干净。我知道我可以将我的 CA 放在一个秘密中,但是我如何在 ValidatingWebhookConfiguration 中评估这个秘密?

apiVersion: admissionregistration.k8s.io/v1beta1
kind: ValidatingWebhookConfiguration
metadata:
  name: webhook-test
webhooks:
- name: my.webhook.frick
  failurePolicy: Fail
  clientConfig:
    caBundle: CA_BUNDLE_THAT_I_HAVE_TO_PASTE_BY_HAND
    service:
      name: validating-svc
      namespace: default
      path: /services/validate
  rules:
      ...

所有 openssl 命令:

openssl genrsa -out certs/ca.key 2048;
openssl req -new -x509 -key certs/ca.key -out certs/ca.crt -config certs/ca_config.txt
openssl genrsa -out certs/chris.pem 2048;
openssl req -new -key certs/chris.pem -subj "/CN=validating-svc.default.svc" -out certs/chris.csr -config certs/chris_config.txt;
openssl x509 -req -in certs/chris.csr -CA certs/ca.crt -CAkey certs/ca.key -CAcreateserial -out certs/chris-crt.pem;
export CA_BUNDLE=$(cat certs/ca.crt | base64 | tr -d '\n'); # Copy paste in deploy.yaml

长运行的目标是用helm打包我的webhook项目。

您可以有一个控制器,用于将 CA 捆绑包注入 webhook 的 ValidatingWebhookConfiguration 和 MutatingWebhookConfiguration 资源,以允许 Kubernetes API 服务器“信任”webhook API 服务器。 ca injector of cert manager does exactly that and you can use it as a reference because the source code 是开源的。