Webhookの生成

Kubernetesには、Admission Webhookと呼ばれる拡張機能があります。 これは特定のリソースを作成・更新する際にWebhook APIを呼び出し、バリデーションやリソースの書き換えをおこなうための機能です。

kubebuilderコマンドでは、以下の3種類のオプションで生成するWebhookを指定できます。

  • --programmatic-validation: リソースのバリデーションをおこなうためのWebhook
  • --defaulting: リソースのフィールドにデフォルト値を設定するためのWebhook
  • --conversion: カスタムリソースのバージョンアップ時にリソースの変換をおこなうためのWebhook

ここでは--programmatic-validation--defaultingを指定して、MarkdownViewリソース用のWebhookを生成してみましょう。

注意: kindにはPodやDeploymentなどの既存のリソースを指定できません。

$ kubebuilder create webhook --group view --version v1 --kind MarkdownView --programmatic-validation --defaulting
$ make manifests

以下のファイルが新たに追加されました。

├── api
│    └── v1
│        ├── markdownview_webhook.go
│        └── webhook_suite_test.go
└── config
     ├── certmanager
     │    ├── certificate.yaml
     │    ├── kustomization.yaml
     │    └── kustomizeconfig.yaml
     ├── default
     │    ├── manager_webhook_patch.yaml
     │    └── webhookcainjection_patch.yaml
     └── webhook
         ├── kustomization.yaml
         ├── kustomizeconfig.yaml
         ├── manifests.yaml
         └── service.yaml

api/v1

markdownview_webhook.goがWebhook実装の雛形になります。 このファイルにWebhookの実装を追加していくことになります。

config/certmanager

Admission Webhook機能を利用するためには証明書が必要となります。 cert-managerを利用して証明書を発行するためのカスタムリソースが生成されています。

config/webhook

config/webhook下は、Webhook機能を利用するために必要なマニフェストファイルです。 manifests.yamlファイルはmake manifestsファイルで自動生成されるため、基本的に手動で編集する必要はありません。

cmd/main.go

cmd/main.goには、以下のようなWebhookの初期化をおこなうためのコードが追加されています。

main.go

if err = (&viewv1.MarkdownView{}).SetupWebhookWithManager(mgr); err != nil {
    setupLog.Error(err, "unable to create webhook", "webhook", "MarkdownView")
    os.Exit(1)
}

kustomization.yamlの編集

Kubebuilderコマンドで生成した直後の状態では、make manifestsコマンドでマニフェストを生成しても、Webhook機能が利用できるようにはなっていません。

config/default/kustomization.yamlファイルを編集する必要があります。

生成直後のkustomization.yamlは、resources../webhook../certmanager, patchesStrategicMergemanager_webhook_patch.yamlwebhookcainjection_patch.yaml, replacements がコメントアウトされていますが、これらのコメントを外します。

kustomization.yaml

resources:
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
- ../certmanager
patchesStrategicMerge:
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
- manager_webhook_patch.yaml

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
# 'CERTMANAGER' needs to be enabled to use ca injection
- webhookcainjection_patch.yaml
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
# Uncomment the following replacements to add the cert-manager CA injection annotations
replacements:
  - source: # Add cert-manager annotation to ValidatingWebhookConfiguration, MutatingWebhookConfiguration and CRDs
      kind: Certificate
      group: cert-manager.io
      version: v1
      name: serving-cert # this name should match the one in certificate.yaml
      fieldPath: .metadata.namespace # namespace of the certificate CR
    targets:
      - select:
          kind: ValidatingWebhookConfiguration
        fieldPaths:
          - .metadata.annotations.[cert-manager.io/inject-ca-from]
        options:
          delimiter: '/'
          index: 0
          create: true
      - select:
          kind: MutatingWebhookConfiguration
        fieldPaths:
          - .metadata.annotations.[cert-manager.io/inject-ca-from]
        options:
          delimiter: '/'
          index: 0
          create: true
      - select:
          kind: CustomResourceDefinition
        fieldPaths:
          - .metadata.annotations.[cert-manager.io/inject-ca-from]
        options:
          delimiter: '/'
          index: 0
          create: true
  - source:
      kind: Certificate
      group: cert-manager.io
      version: v1
      name: serving-cert # this name should match the one in certificate.yaml
      fieldPath: .metadata.name
    targets:
      - select:
          kind: ValidatingWebhookConfiguration
        fieldPaths:
          - .metadata.annotations.[cert-manager.io/inject-ca-from]
        options:
          delimiter: '/'
          index: 1
          create: true
      - select:
          kind: MutatingWebhookConfiguration
        fieldPaths:
          - .metadata.annotations.[cert-manager.io/inject-ca-from]
        options:
          delimiter: '/'
          index: 1
          create: true
      - select:
          kind: CustomResourceDefinition
        fieldPaths:
          - .metadata.annotations.[cert-manager.io/inject-ca-from]
        options:
          delimiter: '/'
          index: 1
          create: true
  - source: # Add cert-manager annotation to the webhook Service
      kind: Service
      version: v1
      name: webhook-service
      fieldPath: .metadata.name # namespace of the service
    targets:
      - select:
          kind: Certificate
          group: cert-manager.io
          version: v1
        fieldPaths:
          - .spec.dnsNames.0
          - .spec.dnsNames.1
        options:
          delimiter: '.'
          index: 0
          create: true
  - source:
      kind: Service
      version: v1
      name: webhook-service
      fieldPath: .metadata.namespace # namespace of the service
    targets:
      - select:
          kind: Certificate
          group: cert-manager.io
          version: v1
        fieldPaths:
          - .spec.dnsNames.0
          - .spec.dnsNames.1
        options:
          delimiter: '.'
          index: 1
          create: true

results matching ""

    No results matching ""