nolar / kopf

A Python framework to write Kubernetes operators in just a few lines of code

Home Page:https://kopf.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attach to container start event

majkrzak opened this issue · comments

Keywords

events pods

Problem

I'm trying to attach to the container start event:

- apiVersion: v1
  count: 2
  eventTime: null
  firstTimestamp: "2024-02-03T12:19:33Z"
  involvedObject:
    apiVersion: v1
    fieldPath: spec.containers{main2}
    kind: Pod
    name: test-7d644695c9-hh9fn
    namespace: default
    resourceVersion: "3123810"
    uid: cac86574-81cf-440d-8f95-ad71f5849f0f
  kind: Event
  lastTimestamp: "2024-02-03T12:20:34Z"
  message: Started container main2
  metadata:
    creationTimestamp: "2024-02-03T12:19:33Z"
    name: test-7d644695c9-hh9fn.17b0599925b195f8
    namespace: default
    resourceVersion: "3123856"
    uid: e693730e-0bf5-45bf-bc8c-32a8bcfa3be4
  reason: Started
  reportingComponent: kubelet
  reportingInstance: k3s
  source:
    component: kubelet
    host: k3s
  type: Normal

When hooking to the on.event('pods') I don't see similar data received anywhere. I tired also with approaching the problem by observing the status.containerStatuses of the pod, but this either is not called at all, or called with old==new multiple times. Depending on the solution used. One of the examples bellow:

[2024-02-03 13:26:11,022] kopf.objects         [INFO    ] [default/test-7d644695c9-gbw97] Updating is processed: 1 succeeded; 0 failed.
{'name': 'main', 'state': {'running': {'startedAt': '2024-02-03T12:26:09Z'}}, 'lastState': {}, 'ready': True, 'restartCount': 0, 'image': 'docker.io/library/busybox:latest', 'imageID': 'docker.io/library/busybox@sha256:6d9ac9237a84afe1516540f40a0fafdc86859b2141954b4d643af7066d598b74', 'containerID': 'containerd://eff26917d139077a943b9346fc368d5c5ffab372de7cb9e7f9d27afebf588471', 'started': True}
{'name': 'main', 'state': {'running': {'startedAt': '2024-02-03T12:26:09Z'}}, 'lastState': {}, 'ready': True, 'restartCount': 0, 'image': 'docker.io/library/busybox:latest', 'imageID': 'docker.io/library/busybox@sha256:6d9ac9237a84afe1516540f40a0fafdc86859b2141954b4d643af7066d598b74', 'containerID': 'containerd://eff26917d139077a943b9346fc368d5c5ffab372de7cb9e7f9d27afebf588471', 'started': True}
@kopf.on.update(
    "pods",
    annotations={API_GROUP: kopf.PRESENT},
    field="status",
    new=lambda x, **_: (x["containerStatuses"][0]["started"] is True),
)
def foo(new, old, **_):
    print(old["containerStatuses"][0])
    print(new["containerStatuses"][0])
    print("XXXXXXXXXXXXXXXXXXXXXXXXX")

Adding old=lambda x, **_: not (x[0]["started"] is True), causes handling only the first "proper" and not the subsequent ones.

[2024-02-03 13:40:28,595] kopf._core.engines.a [INFO    ] Initial authentication has finished.
{'name': 'main', 'state': {'waiting': {'reason': 'ContainerCreating'}}, 'lastState': {}, 'ready': False, 'restartCount': 0, 'image': 'busybox:latest', 'imageID': '', 'started': False}
{'name': 'main', 'state': {'running': {'startedAt': '2024-02-03T12:40:34Z'}}, 'lastState': {}, 'ready': True, 'restartCount': 0, 'image': 'docker.io/library/busybox:latest', 'imageID': 'docker.io/library/busybox@sha256:6d9ac9237a84afe1516540f40a0fafdc86859b2141954b4d643af7066d598b74', 'containerID': 'containerd://d9e817121ac293ab06ee66d9b4ac8b1dd9849fa3ec57f805280f61fc506b704c', 'started': True}