gitops-drift-agent
GitOps ドリフト修復エージェント
Kubernetes クラスターと GitOps リポジトリに接続し、未承認の状態ドリフトを特定し、安全な修復戦略を生成し、自動化された API パッチまたはプルリクエストを実行する、本番グレードの自律エージェント。
目次
Related MCP server: kube-lint-mcp
概要
GitOps ドリフト修復エージェントは、Kubernetes リソースのライブ状態を宣言された GitOps ソース・オブ・トゥルースと継続的に監視する、自律的かつポリシー駆動型のプラットフォームです。ドリフトが検出されると、エージェントは適用可能な修復ポリシーを評価し、最小限の JSON パッチ操作を計算し、Kubernetes API を介して直接適用するか、GitOps リポジトリに対してプルリクエストを開きます。すべての操作には完全な監査証跡が付随します。
基本原則
最小限の影響範囲: パッチは可能な限り小さな差分として計算され、リソース全体の置き換えは行いません
ポリシー優先: すべての修復アクションは、リスク階層を持つ設定可能かつバージョン管理されたポリシーによってゲートキープされます
不変の監査ログ: すべての決定、検出、変更は暗号化コンテキストとともに記録されます
GitOps ネイティブ: エージェント自体が GitOps によって管理され、リポジトリに PR を発行します
MCP 対応: すべての機能を MCP ツールとして公開し、LLM エージェントとの統合を可能にします
アーキテクチャ
┌─────────────────────────────────────────────────────────────────────┐
│ GitOps Drift Remediation Agent │
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌────────────────────────┐ │
│ │ AST Differ │───▶│ Policy Engine│───▶│ Patch Engine │ │
│ │ │ │ │ │ │ │
│ │ • Deep diff │ │ • Risk tiers │ │ • JSON Patch RFC 6902 │ │
│ │ • Field │ │ • Allow/deny │ │ • K8s API apply │ │
│ │ tracking │ │ • Dry-run │ │ • PR generation │ │
│ │ • Severity │ │ • Approvals │ │ • Rollback support │ │
│ └─────────────┘ └──────────────┘ └────────────────────────┘ │
│ │ │ │ │
│ └──────────────────┴───────────────────────┘ │
│ │ │
│ ┌────────▼────────┐ │
│ │ Audit Logger │ │
│ │ │ │
│ │ • Structured │ │
│ │ JSON logs │ │
│ │ • Event chain │ │
│ │ • Pino backend │ │
│ └─────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ MCP Server │ │
│ │ detect_drift │ list_policies │ remediate │ get_audit_trail │ │
│ └──────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│ │
▼ ▼
┌─────────────┐ ┌───────────────┐
│ Kubernetes │ │ GitOps Repo │
│ Cluster(s) │ │ (Git/GitHub) │
└─────────────┘ └───────────────┘機能
機能 | 説明 |
ドリフト検出 | ライブと望ましい Kubernetes リソース状態の深い AST ベースの差分比較 |
リスク分類 | フィールドパスごとの自動重大度スコアリング(重大 / 高 / 中 / 低) |
ポリシーエンジン | 許可/拒否ルール、ドライラン、承認ゲートを備えた宣言的かつバージョン管理されたポリシー |
パッチエンジン | K8s サーバーサイド適用をサポートする RFC 6902 JSON パッチ計算 |
GitOps PR | ドリフト差分と修復根拠を含む自動プルリクエスト生成 |
監査証跡 | 完全な決定チェーンを備えた不変の構造化監査ログ |
MCP サーバー | AI エージェントオーケストレーション用にすべてのエージェント機能を MCP ツールとして公開 |
CLI | 対話型および自動運用のためのフル機能 CLI |
インストール
前提条件
Node.js >= 20.0.0
ターゲットクラスターへのアクセスが設定された kubectl
GitOps リポジトリ用の Git 認証情報(PR モード用)
ソースからのインストール
git clone https://github.com/your-org/gitops-drift-remediation-agent.git
cd gitops-drift-remediation-agent
npm install
npm run build
npm link # optional: makes `drift-agent` available globally設定
すべての設定は環境変数または設定ファイルを介して渡されます。
環境変数
変数 | 必須 | デフォルト | 説明 |
| いいえ |
| kubeconfig ファイルへのパス |
| いいえ | current-context | 使用する Kubernetes コンテキスト |
| いいえ | — | PR モード用の GitOps リポジトリ URL |
| いいえ |
| PR のターゲットブランチ |
| いいえ | — | Git プロバイダートークン(GitHub/GitLab) |
| いいえ |
| 修復ポリシー設定へのパス |
| いいえ |
| 監査ログ出力パス |
| いいえ |
| ログレベル(debug/info/warn/error) |
| いいえ |
| グローバルドライランモード |
| いいえ |
| MCP サーバー HTTP ポート |
| いいえ |
| カンマ区切りの名前空間フィルター |
使用方法
CLI コマンド
# Detect drift across all namespaces
drift-agent detect --namespace production --output json
# Detect and auto-remediate with policy gate
drift-agent remediate --namespace production --policy strict --dry-run
# List active policies
drift-agent policy list
# Show audit trail for a resource
drift-agent audit --resource deployments/my-app --namespace production
# Start MCP server
drift-agent mcp-server --port 3000プログラム API
import { AstDiffer } from './src/detector/ast-differ';
import { RemediationPolicy } from './src/policy/remediation-policy';
import { PatchEngine } from './src/remediator/patch-engine';
const differ = new AstDiffer();
const drifts = await differ.detectDrift(liveResource, desiredResource);
const policy = new RemediationPolicy(policyConfig);
const decision = await policy.evaluate(drifts, resourceContext);
if (decision.approved) {
const engine = new PatchEngine(k8sClient);
await engine.applyRemediation(decision.patches, resourceRef);
}MCP サーバー
エージェントは MCP(Model Context Protocol)サーバーを公開し、すべてのエージェント機能を LLM ベースのエージェント(Claude、GPT-4 など)向けのツールとして利用可能にします。
サーバーの起動
drift-agent mcp-server --port 3000
# or
npm run mcp:server利用可能な MCP ツール
ツール | 説明 |
| リソースまたは名前空間のドリフトを検出 |
| 設定済みのすべての修復ポリシーを一覧表示 |
| 特定のポリシーに対してドリフトを評価 |
| 計算された修復パッチを適用 |
| リソースの監査イベントを取得 |
| ドリフト修復用の GitOps PR を生成 |
| 以前に適用した修復をロールバック |
MCP クライアント設定
{
"mcpServers": {
"gitops-drift-agent": {
"url": "http://localhost:3000/mcp",
"transport": "http"
}
}
}ポリシーエンジン
ポリシーは宣言的に定義され、修復ライフサイクルのすべての側面を制御します。
ポリシー構造
apiVersion: drift.gitops.io/v1
kind: RemediationPolicy
metadata:
name: production-strict
spec:
riskTier: high
autoRemediate: false
requireApproval: true
dryRunFirst: true
rules:
- field: "spec.replicas"
action: restore
severity: high
- field: "spec.template.spec.containers[*].image"
action: block
severity: critical
excludeFields:
- "metadata.annotations['kubectl.kubernetes.io/last-applied-configuration']"
- "metadata.resourceVersion"
- "metadata.uid"監査とテレメトリ
すべてのエージェントアクションは構造化 JSON 形式で記録されます:
{
"timestamp": "2024-06-01T12:00:00.000Z",
"eventId": "evt_01J0ABC123",
"eventType": "DRIFT_DETECTED",
"severity": "high",
"resource": {
"kind": "Deployment",
"name": "my-app",
"namespace": "production",
"apiVersion": "apps/v1"
},
"drift": {
"field": "spec.replicas",
"desired": 3,
"live": 1,
"changeType": "edited"
},
"policy": {
"name": "production-strict",
"decision": "remediate",
"riskTier": "high"
},
"actor": {
"agentVersion": "1.0.0",
"kubeContext": "prod-cluster"
}
}開発
# Install dependencies
npm install
# Run in development mode (ts-node)
npm run dev -- detect --namespace default
# Type check only
npm run typecheck
# Lint
npm run lint
# Format
npm run format
# Build
npm run buildテスト
# Run all tests
npm test
# Run with coverage
npm run test:coverage
# Run in watch mode
npm run test:watchテストは tests/ の下に整理され、Jest と ts-jest を使用します。Kubernetes クライアントとファイルシステム操作にはモックが提供されています。
セキュリティに関する考慮事項
最小権限: エージェントはターゲットリソースに対して
get、list、watch、patchの権限のみを必要とし、deleteやcreateは決して必要としませんデフォルトでドライラン: すべてのポリシーティアは、明示的に有効化されるまでデフォルトでドライランになります
承認ゲート: 高リスクおよび重大リスクの変更には、ポリシーによる明示的な承認が必要です
監査の不変性: 監査ログは追記専用です。ログのローテーションは外部で処理されます
シークレットのマスキング: シークレットリソースの値は、ログと PR で常に編集されます
kubeconfig の分離: エージェントは kubeconfig を決して変更しません
貢献
CONTRIBUTING.md を参照してください。すべての貢献には以下が必要です:
80% 以上のカバレッジでテストスイートに合格すること
新しい lint 警告がないこと
新しい変更パスに対する監査ログエントリ
新しい修復アクションに対するポリシー評価
ライセンス
Apache 2.0 — LICENSE を参照してください。
This server cannot be deployed
Maintenance
Related MCP Connectors
Governed MCP gateway: one endpoint for your tools, with credential custody and audit log.
AI-native git hosting — repos, PRs, issues, CI gates, and AI code review over MCP (60 tools).
One MCP endpoint for your company's tools, with per-person sign-in, policy and audit.
Authenticated MCP server for ClearPolicy policy and compliance workflows.
Related MCP Servers
- AlicenseNot gradedqualityCmaintenanceProvides MCP multi-cluster Kubernetes management and operations. It can be integrated as an SDK into your own project and includes nearly 50 built-in tools covering common DevOps and development scenarios. Supports both standard and CRD resources.151MIT
- AlicenseNot gradedqualityCmaintenanceMCP server to lint and validate Kubernetes-related manifests(Helm, FluxCD, ArgoCD, Kustomize, etc.)59 PyPIMIT
- AlicenseCqualityBmaintenanceA policy-aware MCP server for GitHub and GitHub Actions that enables safe AI-assisted infrastructure workflows—inspecting repositories, preparing branches and pull requests, and constrained remote mutations behind explicit preview-bound approval tokens.18MIT
- AlicenseNot gradedqualityCmaintenanceProvides MCP tools for fetching GitHub PR diffs, searching codebases, running unit tests, and linting code, enabling automated code review and CI/CD workflows.MIT