vault-sync-operator

Vault Sync Operator Development Summary

Project Overview

Successfully created a complete Kubernetes operator called vault-sync-operator that automatically syncs Kubernetes secrets to HashiCorp Vault using annotations on Deployments and Secrets.

Key Features Implemented

  1. Dual Sync Modes: Watches both Kubernetes Deployments and Secrets for specific annotations and syncs secrets to Vault
  2. Automatic Secret Synchronization: Supports both deployment-based and direct secret synchronization
  3. Vault Kubernetes Authentication: Uses Vault’s Kubernetes auth backend for secure authentication
  4. Selective Key Synchronization: Allows choosing specific keys from secrets to sync
  5. Key Prefixing: Supports adding prefixes to secret keys when storing in Vault
  6. Cleanup on Deletion: Automatically removes secrets from Vault when deployments or secrets are deleted
  7. Finalizer Management: Uses Kubernetes finalizers to ensure proper cleanup

Project Structure

vault-sync-operator/
├── cmd/
│   └── main.go                 # Main application entry point
├── internal/
│   ├── controller/
│   │   ├── deployment_controller.go  # Deployment reconciler logic
│   │   ├── secret_controller.go      # Secret reconciler logic
│   │   └── sync_common.go           # Shared sync functionality
│   ├── vault/
│   │   ├── client.go           # Vault client with K8s auth
│   │   └── health.go           # Vault health checks
│   ├── goruntime/
│   │   ├── config.go           # Go runtime optimization
│   │   └── config_test.go      # Runtime configuration tests
│   └── metrics/
│       └── metrics.go          # Prometheus metrics
├── config/
│   ├── default/               # Kustomize default configuration
│   ├── manager/               # Manager deployment configuration
│   ├── rbac/                  # RBAC permissions
├── docs/                      # Documentation
│   ├── README.md              # Documentation index
│   ├── PROJECT_SUMMARY.md     # Complete project summary
│   ├── DEPLOYMENT.md          # Deployment guide
│   ├── VAULT-SETUP-GUIDE.md   # Vault configuration guide
│   ├── VAULT-AUTH-TROUBLESHOOTING.md # Auth troubleshooting
│   ├── VAULT-ADDRESS-CONFIGURATION.md # Address configuration
│   ├── VM-DEPLOYMENT-README.md # VM deployment guide
│   ├── multi-cluster-deployment.md # Multi-cluster guide
│   ├── performance-optimizations.md # Performance guide
│   ├── secret-rotation-detection.md # Secret rotation
│   └── ci-cd-pipeline.md      # CI/CD documentation
├── examples/                  # Example deployment files
├── scripts/
│   ├── setup-vault.sh         # Vault configuration script
│   ├── dev.sh                 # Development environment setup
│   ├── deploy-on-vm.sh        # VM deployment script
│   ├── build-vm-manifests.sh  # VM manifest builder
│   ├── validate-and-package.sh # Manifest validation
│   └── validate-manifests.sh  # Manifest syntax validation
├── test/                      # Test files
│   └── suite_test.go          # Test suite setup
├── hack/
│   └── boilerplate.go.txt     # License header template
├── charts/                    # Helm chart
│   └── vault-sync-operator/   # Operator Helm chart
├── deploy/
│   └── manual/                # Manual deployment manifests
├── Dockerfile                 # Container image build
├── Makefile                   # Build and deployment targets
├── go.mod                     # Go module dependencies
└── README.md                  # Main documentation

Core Components

1. Vault Client (internal/vault/client.go)

2. Deployment Controller (internal/controller/deployment_controller.go)

3. Main Application (cmd/main.go)

Annotations Used

Annotation Required Description Example
vault-sync.io/path Yes Vault storage path (enables sync) "secret/data/my-app"
vault-sync.io/secrets No Custom secret configuration JSON See examples

Note: The presence of vault-sync.io/path automatically enables vault sync on both Deployments and Secrets. The vault-sync.io/secrets annotation is optional and only needed for selective key syncing or prefixing.

Example Usage

Deployment-Based Sync

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  annotations:
    vault-sync.io/path: "secret/data/my-app"
    # Optional: vault-sync.io/secrets for custom configuration
spec:
  # ... deployment spec

Direct Secret Sync

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
  annotations:
    vault-sync.io/path: "secret/data/my-secret"
    # Optional: vault-sync.io/secrets for custom configuration
type: Opaque
data:
  key1: dmFsdWUx  # base64 encoded value

Security Features

  1. RBAC Permissions: Required permissions for deployments and secrets (read/watch/update for annotations and finalizers)
  2. Service Account Authentication: Uses Kubernetes service account tokens
  3. Vault Policies: Configurable Vault policies for least privilege access
  4. Secure Communication: TLS support for Vault communication

Deployment

The operator supports multiple deployment methods:

  1. Helm Chart (Recommended) - Most flexible and production-ready
  2. Kustomize - Good for GitOps workflows
  3. Manual kubectl - Simple direct deployment

See the Deployment Guide for detailed installation instructions.

Configuration Options

The operator supports various configuration flags:

Testing and Examples

Production Features

  1. Comprehensive Monitoring: Prometheus metrics for sync operations, errors, and performance tracking
  2. Robust Error Handling: Enhanced error handling with retry logic and detailed logging for troubleshooting
  3. Multi-tenancy Support: Namespace-based isolation and configurable Vault paths for secure multi-tenant usage
  4. Automated CI/CD: Continuous integration and automated releases via GitHub Actions
  5. Container Optimization: Runtime optimization specifically tuned for Kubernetes environments
  6. Health Monitoring: Built-in Vault connectivity and authentication health probes
  7. Secret Rotation Detection: Automatic detection and synchronization of secret changes
  8. Multi-cluster Support: Complete documentation and deployment patterns for multi-cluster environments

Prerequisites

The operator is production-ready for deployment in Kubernetes environments with HashiCorp Vault integration.

Monitoring and Observability

Health and Readiness Checks

Prometheus Metrics

The operator exposes comprehensive metrics on port :8080:

Sync Operations

Error Tracking

Authentication

Error Handling

Comprehensive error detection and reporting for:

All errors are logged with structured context and tracked via Prometheus metrics for monitoring and alerting

Container Runtime Optimization

The operator is optimized for Kubernetes environments with automatic Go runtime configuration:

Runtime Metrics

Implementation Details

1. Safety Features

2. Secret Generator Compatibility

3. Multi-Cluster Architecture

Multi-Cluster Benefits: