使用 Helm 图表在 MutatingWebhookConfiguration 中设置 caBundle

Setting caBundle in MutatingWebhookConfiguration with Helm chart

MutatingWebhookConfigurationcaBundle 定义 here 为:

caBundle is a PEM encoded CA bundle which will be used to validate the webhook's server certificate. If unspecified, system trust roots on the apiserver are used.

我正在使用此命令获取 PEM 编码的 CA 包。

kubectl config view --raw --minify --flatten \ 
-o jsonpath='{.clusters[].cluster.certificate-authority-data}'

结果值保存在一个变量中,该变量在 sed 命令中用于替换 'template' YAML 中的 CA_BUNDLE 字符串,如下所示。

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
  name: WEBHOOK_APP
  labels:
    app: WEBHOOK_APP
webhooks:
  - name: com.demo.NAMESPACE.WEBHOOK_APP
    sideEffects: None
    admissionReviewVersions: ["v1", "v1beta1"]
    matchPolicy: Equivalent
    failurePolicy: Fail
    clientConfig:
      caBundle: CA_BUNDLE
      service:
        name: WEBHOOK_APP
        namespace: NAMESPACE
        path: "/mutate"
    rules:
      - operations: [ "CREATE", "UPDATE" ]
        apiGroups: [""]
        apiVersions: ["v1"]
        resources: ["pods"]
        scope: "*"

在Helm chart中传递CA_BUNDLE的方式是什么?

由于安全原因,无法直接从 helm chart 中的 env 变量读取变量,并且此功能未按照 this document 中的状态实现。

在 helm chart 中,你总是可以创建一个变量,例如myCAbundleVariablevalues.yaml 文件中,该文件将保存您的 PEM 编码 CA,然后在图表中使用此变量的值,如下所示:

webhooks:
  - ...
    clientConfig:
      caBundle: {{ .myCAbundleVariable }}

如果你想在 运行 helm 命令时传递值 'in runtime' 你可以使用 --set 参数。

因此您的 helm 命令将如下所示:

helm install ... --set myCAbundleVariable=$(kubectl config view --raw --minify --flatten \
-o jsonpath='{.clusters[].cluster.certificate-authority-data}')`

如果有帮助,请告诉我。