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
570
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
910
Other Decks in Technology
See All in Technology
【Oracle AI Spotlight ウェビナー】AWSか、Azureか、Google Cloudか。その議論にオラクルを含める意義。
oracle4engineer
PRO
2
240
Microsoft 365 Copilot chat -tekoälypalvelun tietosuojaongelmat
hponka
0
530
Nav2、Nav3 ... はたまた自作?〜 作って理解する Nav3 の設計意図 〜 / Nav2, Nav3 ... or Build Your Own? — Understanding Nav3's design intent by building it from scratch
yanzm
0
260
[DroidKaigi 2026] Making UI specifications visible: Android UI development in the AI agent era supported by Compose Screenshot Testing and galleries
syarihu
0
600
Oracle Cloud Infrastructure IaaS 新機能アップデート 2026/6 - 2026/8
oracle4engineer
PRO
0
120
Sigmaユーザーのための有用リソース一挙公開 & Sigmaで使えるMCP #sigma_ucj /useful-resources-for-sigma-computing-users-and-mcps-with-sigma
shinyaa31
0
150
深夜のクラウド懺悔室 1:29:300 or 1:0:0
kazzpapa3
0
190
GuardDuty 検知対応を DevOps Agent で効率化しようとしている話 / GuardDuty Investigations with DevOps Agent
masahirokawahara
1
330
Driving AI Adoption Using In-House GPUs to Serve Qwen
po3rin
2
460
プロダクトエンジニアに必要な「いい感じ」に作る能力 〜たくさん作れる時代に、どこまで作るかの決め方〜
jnishime_dresscode
2
1.4k
ブラウザアプリの継続的パフォーマンスモニタリング (序) / Continuous Browser Application Performance Monitoring; Act 1
moznion
0
110
少人数データチームのDevin活用実践事例
runandy16
2
450
Featured
See All Featured
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Facilitating Awesome Meetings
lara
57
7.1k
Skip the Path - Find Your Career Trail
mkilby
1
220
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
380
HDC tutorial
michielstock
2
850
Between Models and Reality
mayunak
4
450
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
970
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
400
How GitHub (no longer) Works
holman
316
150k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
490
Practical Orchestrator
shlominoach
191
12k
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/