This is a Kubernetes controller built with Go that manages nginx deployments through Custom Resource Definitions (CRDs). The controller creates and manages nginx pods based on MyNginx custom resources.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ MyNginx CRD │───▶│ Controller │───▶│ Nginx Deployment│
│ │ │ (Reconciler) │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
│ ┌─────────────────┐ │
└─────────────▶│ ConfigMap │ │
│ (HTML content)│ │
└─────────────────┘ │
│ ┌─────────────────┐ │
└─────────────▶│ Secret │──────────────┘
│ (TLS config) │
└─────────────────┘
- Location: api/v1/mynginx_types.go
- Purpose: Defines the MyNginx resource schema
- Current Spec Fields:
replicas(int32): Number of nginx pod replicas
- Location: internal/controller/mynginx_controller.go
- Purpose: Reconciles MyNginx resources to desired state
- Key Functions:
Reconcile(): Main reconciliation loopreconcileDeployment(): Ensures nginx deployment existsreconcileDelete(): Handles resource cleanup with finalizers
- Location: cmd/main.go
- Purpose: Sets up and starts the controller manager
# Generate code and manifests
make
# Run tests
make test
# Build the controller
make build
# Run locally (requires kubeconfig)
make run
# Deploy to cluster
make deploy
# Remove from cluster
make undeployapi/v1/: API definitions and typesconfig/: Kubernetes manifests and kustomizationinternal/controller/: Controller implementationtest/: Test files (unit and e2e)
- Code Generation: Use Go best practices for Kubernetes controllers
- CRD Updates: Remember to run
makeafter modifying api/v1/mynginx_types.go - RBAC: Update controller comments for new permissions
- Testing: Consider both unit tests and e2e test implications
- Versioning: This is v1 API - breaking changes need new version
- Update
MyNginxSpecin api/v1/mynginx_types.go - Run
maketo regenerate code - Update controller logic in internal/controller/mynginx_controller.go
- Add tests in internal/controller/mynginx_controller_test.go
The README indicates these features are planned but not yet implemented:
- ConfigMap reference for custom HTML content
- Secret reference for TLS configuration
- Status field updates for error conditions
- Proper reconciliation on Secret changes (currently ignored for ConfigMap)
# Install required tools (if not present)
make install-tools
# Setup test environment
make envtest
# Run with verbose logging
make run ARGS="--log-level=debug"- Controller logs are structured - use
--log-level=debugfor detailed output - Check CRD generation:
kubectl describe crd mynginxes.webapp.mynginx.amandahla.xyz - Inspect controller RBAC:
kubectl describe clusterrole mynginx-controller-role
- 2025-09-19: Refactored finalizer handling and ensureDeployment logic
- 2025-09-01: Initial controller implementation with basic deployment management
- Implement ConfigMap integration for custom HTML content
- Add Secret integration for TLS configuration
- Implement proper status reporting
- Add comprehensive error handling
- Improve test coverage
- Kubernetes 1.32+
- controller-runtime v0.20.2
- Go 1.23.0+