Skip to content

feat(cozystack-api): Implement hot reload for ApplicationDefinition changes #1960

@kvaps

Description

@kvaps

Summary

Currently, when ApplicationDefinition resources change, cozystack-controller triggers a full restart of the cozystack-api pod by patching the pod template annotation. This causes:

  1. Race condition at startup — if CRD doesn't exist yet, apiserver waits up to 30 minutes
  2. Downtime — full process restart on every CRD change
  3. External dependency — relies on controller to manage apiserver lifecycle

Proposal

Implement hot reload inside apiserver following the pattern from apiextensions-apiserver:

  • apiserver watches ApplicationDefinition directly via controller-runtime informer
  • On changes, atomically updates REST handlers using atomic.Value
  • Updates OpenAPI schema dynamically via OpenAPIVersionedService.UpdateSpec()
  • No process restart required

Implementation outline

  1. Create DynamicResourceHandler with atomic storage for REST handlers
  2. Create HotReloadController that watches CRD and computes diffs
  3. Remove blocking retry loop in Complete() — start with empty config
  4. Remove restart logic from cozystackresource_controller.go

Benefits

  • Zero downtime on CRD changes
  • Faster startup (no waiting for CRD)
  • Simpler architecture (no external restart trigger)
  • Better resilience

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions