How Controller policies behave in jsPolicy
mainWhen a jsPolicy is configured with type: Controller, it behaves like a standard Kubernetes controller.
Key behaviors include:
- Reconciliation: The policy logic is called during each reconcile cycle.
- Watch Events: The controller receives watch events directly from Kubernetes, allowing it to react immediately to cluster changes or enforce policies on existing objects.
- Concurrency: jsPolicy ensures only one instance of a specific policy runs at a time to prevent race conditions.
- Event Lifecycle:
- On startup, a controller receives a
CREATEevent for every existing resource that matches its selectors. - On subsequent changes, it receives
CREATEevents for new or modified objects andDELETEevents for deleted objects. - During informer re-syncs,
CREATEevents are re-emitted for all existing resources.
- On startup, a controller receives a
- Selectors: Controller policies support
objectSelectorandnamespaceSelectorto limit the scope of the controller.
apiVersion: policy.jspolicy.com/v1beta1
kind: JsPolicy
metadata:
name: "state.resource.example"
spec:
type: Controller
objectSelector:
matchLabels:
create-rq: "true"
javascript: |
// Policy logic goes here