How open-policy-agent/kube-mgmt works
Project type and purpose
• This is a backend Kubernetes‐native management service (no UI) for loading Open Policy Agent (OPA) policies and data and replicating Kubernetes resources into OPA.
• Written in Go, packaged as a CLI (cmd/kube-mgmt), with a Helm chart (charts/opa-kube-mgmt) for deployment into a K8s cluster.
File structure
• Top-level directories:
• cmd/kube-mgmt: entry point and flag parsing
• pkg/ & internal/: core logic for watching ConfigMaps, Kubernetes resources, and interacting with OPA
• charts/opa-kube-mgmt: Helm chart templates for Deployment, Service, RBAC, WebhookConfiguration, etc.
• test/, examples/: e2e fixtures, unit tests, usage examples
• Key config files: justfile, skaffold.yaml, Helm values schemas.
• CI pipeline in .github/workflows builds Docker images & publishes Helm chart.
README for additional insights
• kube-mgmt watches ConfigMaps labeled for policy/data and replicates arbitrary K8s resources.
• It uses the Kubernetes API (via client‐go) to LIST/WATCH ConfigMaps and resources.
• It calls OPA’s HTTP API (PUT/DELETE for policies, PUT/PATCH for data).
• It supports Admission Control as a validating webhook.
Main components
a. Kubernetes Control Plane
• API Server (authenticates & authorizes kube-mgmt’s service account)
• etcd (persistent store for K8s resources) – optional detail
b. kube-mgmt Pod
• Watches ConfigMaps & specified resources via K8s client
• Internals:
• ConfigMap watcher → policy/data loader
• Resource replicator → caches objects
• OPA client → PUT/DELETE v1/policy and PUT/PATCH v1/data
c. OPA Pod or sidecar
• Exposes REST API (/v1/data, /v1/policy, /health, /metrics)
• Hosts admission webhook server (via Kubernetes WebhookConfiguration)
d. Kubernetes Resources
• ConfigMaps (labeled openpolicyagent.org/policy=rego or openpolicyagent.org/data=opa)
• Namespaced & cluster‐scoped resources (Pods, Services, CRDs) being replicated
e. External clients
• Kubernetes API Server calling OPA for admission decisions
• Other clients querying OPA for policy decisions (optional)
Architectural patterns and design principles
• Event‐driven design: uses K8s watch API to react to resource changes
• Single responsibility: kube-mgmt focuses solely on syncing policies/data and replication
• Declarative deployment: Helm chart expresses Deployment, RBAC, Service, WebhookConfiguration
• Observability: exposes /metrics endpoint for Prometheus, /health for liveness/readiness
Detail level
• ConfigMap → annotation update loop
• Flag/config schema influencing behavior (--namespaces, --replicate, --replicate-path)