Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduce Conftest
Search
yuhara
September 08, 2020
Technology
560
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Introduce Conftest
yuhara
September 08, 2020
More Decks by yuhara
See All by yuhara
managed_Kubernetes_on-premises.pdf
yuhara
1
890
Other Decks in Technology
See All in Technology
プロダクト開発から業務改善コンサルまで。事業全体へ「染み出す」ことで広がるエンジニアの可能性
ham0215
0
130
「エンジニア進化論」2028年の開発完全自動化、エンジニアはどう進化するか
cyberagentdevelopers
PRO
6
5.2k
エンジニアリング戦略の作り方 / Crafting Engineering Strategy
iwashi86
21
6.9k
MCP Appsを作ってみよう
iwamot
PRO
4
660
SONiCの統計情報を取得したい
sonic
0
170
ACE-Step-1.5で見る 音楽生成AIのしくみと“破綻だけ直す”Retake機能の開発【zennfes spring 2026 登壇資料】
personabb
1
480
【Snowflake Summit 2026 Recap!!】Snowflake Summit Deep Dive: Security & Governance
civitaspo
1
190
GitHub Copilot 最新アップデート – 「一歩先」の実践活用術
moulongzhang
3
820
新しいVibe Codingと”自走”について
watany
6
330
日本 Fintech 未来予測レポート 2027〜2028年(オリジナル版)
8maki
0
2.2k
【Cyber-sec+】経営層を"動かす"ための考え方
hssh2_bin
0
190
Kubernetesにおける学習基盤とLLMOpsの概要
ry
1
310
Featured
See All Featured
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
190
RailsConf 2023
tenderlove
30
1.5k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
940
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Faster Mobile Websites
deanohume
310
31k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
410
Exploring anti-patterns in Rails
aemeredith
3
410
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
How to build a perfect <img>
jonoalderson
1
5.6k
Building AI with AI
inesmontani
PRO
1
1.1k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
Transcript
CloudNative Days Tokyo 2020 RegoによるOPAポリシーの開発と Conftestを活用したポリシーチェックの実現 Merpay
SRE Takaaki Yuhara
Agenda Conftest利用の背景 Regoで記述したポリシーとConftestの活用 ポリシーの開発とテスト 01 02 03
Conftest利用の背景 Regoで記述したポリシーとConftestの活用 ポリシーの開発とテスト 01 02 03
メルペイのシステム概要 • マイクロサービスアーキテクチャ • Google Cloud Platform(GCP)を利用 • アプリケーションはKubernetes上で稼働 •
マイクロサービスはそれぞれ下記が割り当てられる ◦ GCP Project ◦ Kubernetes Namespace
構成とフローの概略図 Kubernetes Project Project A Project B Service A Team
Service B Team Repository(GCP) Namespace A Namespace B Repository(k8s) Cloud Spanner Cloud Spanner Cloud Pub/Sub BigQuery Kubernetes Cluster CircleCI YAML HCL (Terraform)
Production Readiness Checklist • サービスを安全にリリースするためのチェックリスト • マイクロサービスのリリース前にチェックを行う • 例) ◦
Kubernetes ▪ CPUやMemoryのrequestsとlimits ▪ preStop ▪ livenessProbe / readinessProbe ▪ runAsNonRoot ▪ HorizontalPodAutoscaler ▪ PodDisruptionBudget ◦ GCP ▪ Databaseのバックアップ ▪ Cloud StorageのObject Lifecycle Management
Open Policy Agent(OPA) https://www.openpolicyagent.org/docs/latest/ • OSSの汎用ポリシーエンジン • CNCFのincubatingのプロジェクト • Regoというポリシー記述言語で
ポリシーを書く • ポリシーと入力されたデータを評価し結 果を返す
Gatekeeper • Gatekeeper ◦ OPAをKubernetes上で使えるよう にしたもの ◦ KubernetesのAdmission Controllerと連携してOPAを利用 ◦
Webhook先をOPAにすることで API Server側でポリシーチェック https://www.openpolicyagent.org/docs/latest/kubernetes-introduction/
Gatekeeper • ConstraintTemplateとConstraint というCRDでポリシーを定義 • Regoによるポリシーは ConstraintTemplate内に記述 https://github.com/open-policy-agent/gatekeeper
Conftest • Regoで記述したポリシーと入力データをコマンドラインで検証するツール • CI上で使われることを想定している • リモートにあるRegoのポリシーを利用できる • 様々なフォーマットに対応 ❏
YAML ❏ JSON ❏ INI ❏ TOML ❏ HOCON ❏ HCL ❏ HCL1 ❏ CUE ❏ Dockerfile ❏ EDN ❏ VCL ❏ XML ❏ Jsonnet
なぜConftestを使い始めたか • もともとCIでterraform planやkubectl validate/dry-runでの失敗を通知していて、ポ リシーチェックも導入しやすかった • Conftestが様々なフォーマットに対応している ◦ GCPのクラウドリソースはHCL形式のTerraformで管理
◦ KubernetesのマニフェストはYAMLで管理 • KubernetesではRegoで記述したポリシーをCIとAPI Server側で両側面でチェックで きる ◦ CI時のチェックはConftest ◦ API Server側でのチェックはGatekeeper
Conftest利用の背景 Regoで記述したポリシーとConftestの活用 ポリシーの開発とテスト 01 02 03
テストシナリオ • 例)HorizontalPodAutoscalerのspec.minReplicasが3以上であること apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: php-apache
spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: php-apache minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: <以下、省略> minReplicasが省略されていたり、 値が3未満の場合は事前に気付きたい
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } • 例)HorizontalPodAutoscalerのspec.minReplicasが3以上であること
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } package名は任意。
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } ルール名は deny, violation, warn を使うことができる。 deny, violationではルールに合致するとExit codeが1, warnではルールに合致してもExit codeが0 ルール名をviolation_xxxなどのように指定してもOK
デバッグ時の出力 内部でdata.<package名>.<ルール名> という形で扱われているためルール名を分けておくと便利
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } ルールに合致した場合に返す値
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } 入力データ(input)のkindがHorizontalPodAutoscalerであるか評価 (Gatekeeperでは入力データが input.review.objectなのでConftestとGatekeeper両方でポリシーを使う場合は入 力データを評価するような判定を入れると良い)
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } input.spec.minReplicas が3以上であればgoodという変数にtrueが代入。3未満ならfalseが代入される。
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } パイプの左のgoodという変数に値を渡しています。つまりsatisfiedは要素が1つのarrayとなります。ただし minReplicasが省略されている場合はgoodが空なのでsatisfiedは要素が0のarrayです。 satisfied minReplicasが3以上 [true] minReplicasが3未満 [false] minReplicasが省略 []
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } any関数でsatisfied内に1つでもtrueがある場合はtrueとなる。 satisfied any(satisfied) minReplicasが3以上 [true] true minReplicasが3未満 [false] false minReplicasが省略 [] false
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } spec.minReplicasが3以上でない場合を条件として合致させたいため、否定のnotを使用 satisfied any(satisfied) not any(satisfied) minReplicasが3以上 [true] true false minReplicasが3未満 [false] false true minReplicasが省略 [] false true
Regoでポリシーを記述 package hpa description := "This is production readiness check
itme, please check http://~" violation_min_replicas_is_not_set[{"msg": msg, "description": description}] { input.kind = "HorizontalPodAutoscaler" satisfied := [good | good := input.spec.minReplicas >= 3] not any(satisfied) msg := sprintf("minReplicas in HorizontalPodAutoscaler %s must be set", [input.metadata.name]) } 最後にmsgを生成
コマンド実行時の出力(JSON) [ { "filename": "sample-hpa.yaml", "warnings": [], "failures": [ {
"msg": "minReplicas in HorizontalPodAutoscaler sample must be set", "metadata": { "description": "This is a production readiness check item, please check https://~" } } ], "successes": [] } ] $ conftest test --policy policy/hpa --namespace hpa --input yaml --output json sample-hpa.yaml ConftestをCIに組み込んで、 適切な情報をディベロッパーにフィードバック
Agenda Conftest利用の背景 Regoで記述したポリシーとConftestの活用 ポリシーの開発とテスト 01 02 03
ポリシーの開発とテスト • OPAではポリシーをコードで開発、運用できる(Policy as Code) • Regoのポリシーをテストするフレームワークがある • テスト用のルールはtest_xxxとする •
withキーワードを使って入力データをMockのデータに置き換えできる package hpa msg := "minReplicas in HorizontalPodAutoscaler sample must be set" test_min_replicas_less_than_three { violation_min_replicas_is_not_set[{"msg": msg, "description": description}] with input as { "kind": "HorizontalPodAutoscaler", "metadata": {"name": "php-apache"}, "spec": {"maxReplicas": 10, "minReplicas": 1} } }
テスト実行 • opa testコマンドを使ってテストを実行 • --coverage --format=jsonオプションをつけるとカバレッジも報告される $ opa test
-v hpa.rego hpa_test.rego data.hpa.test_min_replicas_less_than_three: PASS (2.977518ms) -------------------------------------------------------------------------------- PASS: 1/1 $ opa test --coverage --format=json hpa.rego hpa_test.rego | jq .coverage 60
まとめ • Production Readiness Checklistを適切にチェックし安全にリリースするためにOpen Policy AgentのConftestを利用し始めた • KubernetesのYAML manifestやTerraformのHCL等をCIでチェック
• Regoで記述したポリシーのテストもCIで実施
• 本日の内容は、下記のブログでも紹介しています https://engineering.mercari.com/blog/entry/introduce_conftest/