kopf icon indicating copy to clipboard operation
kopf copied to clipboard

kopf cr status update

Open odra opened this issue 3 years ago • 8 comments

Keywords

status update

Problem

Does the framework provide a way for CR status update?

My operator creates a pod and the custom resource needs to keep track/be in sync with the pod status.

Thanks.

odra avatar Jan 04 '22 13:01 odra

Hi @odra ,

If I read your question right, you can just return a dict on your handlers and it will be set on your CRD's status field.

Alternatively, you can use the patch variable.

Cheers !

Frankkkkk avatar Feb 10 '22 13:02 Frankkkkk

>>> If I read your question right, you can just return a dict on your handlers and it will be set on your CRD's status field.

I have a handler that us watching for batchv1.jobs resources and it needs to update the CR from that event handler.

I don't think returning a dict from that function will update the CR status, correct?

>>> Alternatively, you can use the patch variable.

Does that mean I need to invoke a kubernetes client method to patch my CR status?

odra avatar Feb 10 '22 16:02 odra

I think it might be easier with this sample python code:

@kopf.on.event('batchv1.jobs')
def handler_job(event, status, **kwargs):
    conditions = status.get('conditions', [])
    if len(conditions) == 0:
        # TODO: update custom resource status that job is still running
        return

    condition = conditions[0]
    condition_type = condition.get('type')
    condition_status = condition.get('status')

    if condition_type != 'Complete' and condition_status != 'True':
         # TODO: update custom resource status that job is still running
        return

    # TODO: update custom resource status that job is done

I need to update a CR status from the above function.

odra avatar Feb 10 '22 17:02 odra

I have a handler that us watching for batchv1.jobs resources and it needs to update the CR from that event handler. I don't think returning a dict from that function will update the CR status, correct?

Correct, it will not. If you need to watch a resource kind A and modify a resource of kind B, then you do it by patching it with any client library you like, explicitly. Kopf does not provide such functionality. A few names to start with: kubernetes (the "official client library"), pykube-ng (just nice and easy to use).

nolar avatar Feb 10 '22 22:02 nolar

Pykorm is a good lib too :P

Frankkkkk avatar Feb 10 '22 22:02 Frankkkkk

Thanks, that makes sense, but let me raise another question: why isn't a client available from the framework, such as kopf.client? Is it just to provide some level of flexibility to users so they choose whatever they want to use?

odra avatar Feb 10 '22 23:02 odra

First, making own client requires effort and time investment, which I cannot afford. Especially for later maintenance.

Second, making own client implies that it is different or better than the existing ones. I see nothing innovative which can be added to this topic, except maybe for Pydantic-based approach a la FastAPI and alike — but see point 1.

Flexibility for users is also on the list, but not as the primary goal or reason.

nolar avatar Feb 10 '22 23:02 nolar

I totally agree with you point about not creating a new client but my question was more oriented to choosing an existing one and wrapping it up in the framework itself.

odra avatar Feb 11 '22 00:02 odra