kopf
kopf copied to clipboard
Handling observedGeneration in status subresource
Keywords
subresource observedGeneration
Problem
In my update handler for a custom resource, I'd like to set the spec.observedGeneration
field to match the latest metadata.generation
, as I understand to be good practice.
My CRD defines status
as a subresource:
spec:
group: mycrd.mygroup
versions:
- name: v1
served: true
storage: true
subresources:
status: {}
My understanding is that this means the status
subresource can be updated without affecting the metadata.generation
value, and I believe #313 introduced the functionality to kopf to support this.
In my handler code, I return observedGeneration
on successful completion:
@kopf.on.update('mycrds')
@kopf.on.create('mycrds')
def template_validation(body, name, namespace, **kwargs):
# Processing omitted for brevity
return {
'observedGeneration': body.metadata.get('generation'),
'valid': 'true',
'error': None
}
However, there must be another update happening here because I always find that metadata.generation
is 1 higher than status.observedGeneration
. In fact, every update to my resource increments metadata.generation
by 2 and leaves status.observedGeneration
1 lower.
My guess as to what's happening is that the completion of the handler bumps metadata.generation
, pushing it beyond the value I'd retrieved for updating status.observedGeneration
. But if kopf is correctly using the Status subresource as suggested by #313 then I'd expect the values to match. Am I mistaken about how this should work, or is there something clever I can do to get them to match? I could of course do this:
'observedGeneration': body.metadata.get('generation') + 1,
but it feels messy, and it doesn't get around the fact that my metadata.generation
is increasing by 2 (rather than 1) whenever I manually apply an update.