Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An incremental approach to implement an admission controller

An incremental approach to implement an admission controller

勉強会で発表した,Kubernetesのadmission webhookを実装する為の知識と,
実際にenvtestを使用するcontrollerの実装,
clusterが立っていない環境でtest clusterを立ち上げるDEMOを行った.
Kubebuilderを使わず,controller-runtimeを直に叩いて動かした.

Drumato

May 14, 2021
Tweet

More Decks by Drumato

Other Decks in Programming

Transcript

  1. Index • Background ◦ Admission Controlとは • Admission Controllerを開発する上で気にしなければならないこと •

    速習envtest ◦ testを動かすDEMO • deployについて考える ◦ kubebuilder projectを解析して構成要素を抽出する
  2. Background • Kubernetesはいくつかのextension patternを用意している ◦ Kubernetes自体のrebuildやcluster re-constructionをしなくても拡張可能 • Patterns ◦

    kubelet plugins … network, device, storage, container runtime ◦ kubectl plugins … e.g. adding some CLI commands ◦ access control … 本発表の対象 ◦ operator ◦ custom API server ◦ scheduler … PFNの方が資料を公開していた ◦ authentication
  3. Admission control • Kubernetesで作成可能な,API requestに対するhook pointの一つ • k8s objectの作成/更新/削除で発火し,Webhook Requestを出してくれる

    ◦ Webhook Serverを立てて,決められたformatのResponseを返すよう実装 することでAdmission Controlを実現 • 以下の2種類が存在 ◦ Validation … object fieldsを検証する ◦ Mutation … object fieldsを変更する ▪ Validationより前にhandlerが呼び出される • topolvmでは最低限のvalidationみたいなのをやってる
  4. Required works are so complicated... • manifests ◦ cert-manager ◦

    {Mutating, Validating}WebhookConfiguration ◦ service manifest ▪ WebhookConfiguration等はclientConfig.serviceを指定,webhook requestはそのsvcに向かって送信される ◦ manager deployment • CRDの場合はCRD manifestsも増える • 幸いKubebuilder等を使えば1から手書きしなくても良い • 一方,kustomizeのoverlaysを使ったり,詳細にconfigurationしようとすると 結局理解し,用意しなければならない
  5. 速習envtest • controller-runtimeが提供する,Custom Controllerのtest環境を作るpackage ◦ k8s control planeのうちapiserver/etcdを利用してtest用clusterを作ってくれる • BDD

    frameworkのginkgo/gomegaと連携することで快適にtest ◦ 全test specが走る前にinitializationを実行できる ▪ そこでTLS証明書を読み込んだりできる ▪ WebhookConfigurationのベタ書きもできる • cert-managerやmanifestsを用意せずに動く
  6. Core type admission controller on envtest • admission.Handler interfaceを実装する構造体を定義する ◦

    CREATE/UPDATE/DELETE が全部func Handle()にかかってくる • それぞれ実装したいlogicを追加する • cplane binariesを適当に落とす ◦ envtest.Environment.Start()によって起動される ◦ 今回はKubebuilderに同梱されているものを使います • openssl等で証明書を作成し,func BeforeSuite()で読み込む ◦ cert-managerを使わない為に必要
  7. kustomize build#Namespace • <project-dir>-system というNameを持つNamespaceができる • このnsにdeployされるresources ◦ {Webhook, Metrics}

    Service ◦ {Validating, Mutating}WebhookConfiguration ◦ Manager deployment ◦ Certificate, Issuer ◦ Role, RoleBind
  8. 所感 • Core typesに対するAdmission controlに限って言えば,Kubebuilder無しでも 難しくない ◦ 実際にはdeployのことも考えると使ったほうが良い • k8s

    clusterに乗っけるsoftwareでも,envtestのようなpkgの利用 で"incremental approach"が可能 • kustomize buildを直で読んでいったり,kubectl describeすると何が起こって いるかわかって便利 • TLS何もわかってない
  9. References • Programming Kubernetes • Dynamic Admission Control • The

    Kubebuilder book • kubernetes-sigs/controller-runtime