|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What is Rollout Controller? |
| 6 | + |
| 7 | +A Kubernetes controller for managing application rollouts with support for health checks, gates, and bake time. It integrates tightly with Flux CD for GitOps workflows, providing progressive delivery capabilities. |
| 8 | + |
| 9 | +## Key Features |
| 10 | + |
| 11 | +- **Flux ImagePolicy Integration**: Reads available releases from Flux ImagePolicy |
| 12 | +- **Multiple Rollout Support**: Kustomizations can be managed by multiple rollouts using rollout-specific annotations |
| 13 | +- **Health Check Integration**: Monitor application health during rollouts |
| 14 | +- **Rollout Gates**: Control deployment progression with custom gates |
| 15 | +- **Bake Time**: Wait for a specified duration before considering a deployment successful |
| 16 | +- **Version History**: Maintains version history with configurable retention policies |
| 17 | +- **Gate Bypass**: Emergency deployment support via annotations |
| 18 | + |
| 19 | +## Common Development Commands |
| 20 | + |
| 21 | +```bash |
| 22 | +# Generate code after modifying CRD types |
| 23 | +make manifests # Generate CRD YAML from Go types |
| 24 | +make generate # Generate DeepCopy methods |
| 25 | + |
| 26 | +# Code quality |
| 27 | +make fmt # go fmt |
| 28 | +make vet # go vet |
| 29 | +make lint # gofmt + govet |
| 30 | + |
| 31 | +# Testing |
| 32 | +make test # Run unit tests (Ginkgo/Gomega) |
| 33 | +make test-e2e # Run e2e tests on Kind cluster |
| 34 | + |
| 35 | +# Building |
| 36 | +make build # Build binary |
| 37 | +make docker-build # Build container image |
| 38 | +make docker-push # Push to registry |
| 39 | + |
| 40 | +# Deployment |
| 41 | +make install # Install CRDs to cluster |
| 42 | +make deploy # Deploy controller to cluster |
| 43 | +make uninstall # Remove CRDs and controller |
| 44 | +make run # Run locally (without cluster deployment) |
| 45 | +``` |
| 46 | + |
| 47 | +## Development Workflow |
| 48 | + |
| 49 | +1. Modify CRD type definitions in `api/v1alpha1/*_types.go` |
| 50 | +2. Run `make manifests generate` to update CRDs and DeepCopy methods |
| 51 | +3. Implement reconciliation logic in `internal/controller/*_controller.go` |
| 52 | +4. Write tests in `internal/controller/*_controller_test.go` |
| 53 | +5. Run `make test` to verify |
| 54 | +6. Deploy with `make docker-build docker-push deploy IMG=registry/image:tag` |
| 55 | + |
| 56 | +## Flux CD Integration |
| 57 | + |
| 58 | +### Annotation-Based Resource Patching |
| 59 | + |
| 60 | +**OCIRepository** - managed by single rollout: |
| 61 | +```yaml |
| 62 | +metadata: |
| 63 | + annotations: |
| 64 | + rollout.kuberik.com/rollout: "my-app-rollout" |
| 65 | +spec: |
| 66 | + ref: |
| 67 | + tag: "1.0.0" # Updated by rollout controller |
| 68 | +``` |
| 69 | +
|
| 70 | +**Kustomization** - multiple rollouts can manage different variables: |
| 71 | +```yaml |
| 72 | +metadata: |
| 73 | + annotations: |
| 74 | + rollout.kuberik.com/substitute.IMAGE_TAG.from: "frontend-rollout" |
| 75 | + rollout.kuberik.com/substitute.VERSION.from: "backend-rollout" |
| 76 | +spec: |
| 77 | + postBuild: |
| 78 | + substitute: |
| 79 | + IMAGE_TAG: "1.0.0" # Managed by frontend-rollout |
| 80 | + VERSION: "2.0.0" # Managed by backend-rollout |
| 81 | +``` |
| 82 | +
|
| 83 | +### ImagePolicy Workflow |
| 84 | +
|
| 85 | +1. Flux ImageRepository scans OCI registry |
| 86 | +2. Flux ImagePolicy filters releases with semver/regex |
| 87 | +3. Rollout Controller reads `latestRef` from ImagePolicy status |
| 88 | +4. Controller patches OCIRepository/Kustomization resources |
| 89 | +5. Flux reconciles changes and deploys |
| 90 | + |
| 91 | +## CRDs Managed |
| 92 | + |
| 93 | +- **Rollout**: Main resource defining deployment strategy |
| 94 | +- **RolloutGate**: Conditions that must pass before deployment |
| 95 | +- **RolloutSchedule**: Scheduled deployment configurations |
| 96 | +- **HealthCheck**: Health status from various sources |
| 97 | +- **ClusterRolloutSchedule**: Cross-cluster schedules |
| 98 | + |
| 99 | +## Rollout Spec Example |
| 100 | + |
| 101 | +```yaml |
| 102 | +apiVersion: kuberik.com/v1alpha1 |
| 103 | +kind: Rollout |
| 104 | +metadata: |
| 105 | + name: my-app-rollout |
| 106 | +spec: |
| 107 | + releasesImagePolicy: |
| 108 | + name: my-app-policy |
| 109 | +
|
| 110 | + versionHistoryLimit: 10 |
| 111 | + releaseUpdateInterval: "5m" |
| 112 | +
|
| 113 | + # Bake time for health checks |
| 114 | + bakeTime: "10m" |
| 115 | + healthCheckSelector: |
| 116 | + matchLabels: |
| 117 | + app: myapp |
| 118 | +``` |
| 119 | + |
| 120 | +## Important Annotations |
| 121 | + |
| 122 | +```yaml |
| 123 | +# OCIRepository |
| 124 | +rollout.kuberik.com/rollout: "rollout-name" |
| 125 | +
|
| 126 | +# Kustomization |
| 127 | +rollout.kuberik.com/substitute.<VAR>.from: "rollout-name" |
| 128 | +
|
| 129 | +# Gate bypass for emergency deployments |
| 130 | +rollout.kuberik.com/bypass-gates: "v1.2.3" |
| 131 | +``` |
| 132 | + |
| 133 | +## Common Patterns |
| 134 | + |
| 135 | +### Controller Reconciliation |
| 136 | + |
| 137 | +```go |
| 138 | +func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 139 | + // 1. Get resource (with IgnoreNotFound for deleted resources) |
| 140 | + rollout := &kuberikv1alpha1.Rollout{} |
| 141 | + if err := r.Get(ctx, req.NamespacedName, rollout); err != nil { |
| 142 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 143 | + } |
| 144 | +
|
| 145 | + // 2. Add finalizer if needed |
| 146 | + // 3. Perform business logic |
| 147 | + // 4. Update resource status with conditions |
| 148 | + // 5. Return Result with optional RequeueAfter |
| 149 | +
|
| 150 | + return ctrl.Result{RequeueAfter: 30 * time.Second}, nil |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +**Result Options:** |
| 155 | +- `ctrl.Result{}` - Stop, don't requeue |
| 156 | +- `ctrl.Result{Requeue: true}` - Retry immediately |
| 157 | +- `ctrl.Result{RequeueAfter: 5*time.Second}` - Retry after delay |
| 158 | + |
| 159 | +### Testing with Ginkgo/Gomega |
| 160 | + |
| 161 | +```go |
| 162 | +var _ = Describe("RolloutController", func() { |
| 163 | + Context("when rollout is created", func() { |
| 164 | + It("should update status", func() { |
| 165 | + rollout := &kuberikv1alpha1.Rollout{...} |
| 166 | + Expect(k8sClient.Create(ctx, rollout)).To(Succeed()) |
| 167 | +
|
| 168 | + Eventually(func() string { |
| 169 | + err := k8sClient.Get(ctx, key, rollout) |
| 170 | + if err != nil { |
| 171 | + return "" |
| 172 | + } |
| 173 | + return rollout.Status.Phase |
| 174 | + }).Should(Equal("Ready")) |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
| 178 | +``` |
| 179 | + |
| 180 | +### Event Recording |
| 181 | + |
| 182 | +```go |
| 183 | +r.Recorder.Event(rollout, corev1.EventTypeNormal, "RolloutSucceeded", "Deployment completed") |
| 184 | +r.Recorder.Event(rollout, corev1.EventTypeWarning, "GatesFailing", "Required gates not passing") |
| 185 | +``` |
| 186 | + |
| 187 | +### Status Subresource Pattern |
| 188 | + |
| 189 | +```go |
| 190 | +r.Client.Status().Update(ctx, resource) // Update status |
| 191 | +r.Client.Update(ctx, resource) // Update spec/metadata |
| 192 | +``` |
| 193 | + |
| 194 | +## Dependencies |
| 195 | + |
| 196 | +Key dependencies: |
| 197 | +- `github.com/fluxcd/image-reflector-controller/api` - ImagePolicy |
| 198 | +- `github.com/fluxcd/kustomize-controller/api` - Kustomization |
| 199 | +- `github.com/fluxcd/source-controller/api` - OCIRepository |
| 200 | +- `github.com/google/go-containerregistry` - OCI image operations |
| 201 | +- `sigs.k8s.io/controller-runtime` - Kubebuilder framework |
| 202 | + |
| 203 | +## Debugging |
| 204 | + |
| 205 | +```bash |
| 206 | +# Controller logs |
| 207 | +kubectl logs -n kuberik-system deployment/rollout-controller -f |
| 208 | +
|
| 209 | +# Events |
| 210 | +kubectl get events --sort-by='.lastTimestamp' |
| 211 | +
|
| 212 | +# Describe resources |
| 213 | +kubectl describe rollout <name> |
| 214 | +kubectl describe rolloutgate <name> |
| 215 | +kubectl describe healthcheck <name> |
| 216 | +``` |
0 commit comments