kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.78k stars 2.9k forks source link

Events retrieved via the dynamic client when converted between unstructured.Unstructured and corev1.Event are missing involvedObject #1284

Closed TylerHelmuth closed 11 months ago

TylerHelmuth commented 11 months ago

I have a situation where a dynamic client is retrieving events in the cluster. The received events look like

{
    "Type": "ADDED",
    "Object": {
        "apiVersion": "events.k8s.io/v1",
        "deprecatedCount": 1,
        "deprecatedFirstTimestamp": "2023-07-20T20:27:06Z",
        "deprecatedLastTimestamp": "2023-07-20T20:27:06Z",
        "deprecatedSource": {
            "component": "replicaset-controller"
        },
        "eventTime": null,
        "kind": "Event",
        "metadata": {
            "creationTimestamp": "2023-07-20T20:27:06Z",
            "managedFields": [
                {
                    "apiVersion": "v1",
                    "fieldsType": "FieldsV1",
                    "fieldsV1": {
                        "f:count": {},
                        "f:firstTimestamp": {},
                        "f:involvedObject": {},
                        "f:lastTimestamp": {},
                        "f:message": {},
                        "f:reason": {},
                        "f:source": {
                            "f:component": {}
                        },
                        "f:type": {}
                    },
                    "manager": "kube-controller-manager",
                    "operation": "Update",
                    "time": "2023-07-20T20:27:06Z"
                }
            ],
            "name": "testing-refinery-7df6cbbdb9.1773ad4bde97125f",
            "namespace": "default",
            "resourceVersion": "34802",
            "uid": "31f2fde4-a1a1-4861-9b99-1c291ab600ee"
        },
        "note": "Created pod: testing-refinery-7df6cbbdb9-lh7c7",
        "reason": "SuccessfulCreate",
        "regarding": {
            "apiVersion": "apps/v1",
            "kind": "ReplicaSet",
            "name": "testing-refinery-7df6cbbdb9",
            "namespace": "default",
            "resourceVersion": "34777",
            "uid": "7f7fd90e-c747-4623-ac65-c32d7d9d4bb5"
        },
        "type": "Normal"
    }
}

This representation of the event uses regarding instead of involvedObject.

I try to convert these objects to corev1.Events:

func someFunction(e *watch.Event) {
    udata := e.Object.(*unstructured.Unstructured)
    unstructure := udata.UnstructuredContent()
    var event corev1.Event
    runtime.DefaultUnstructuredConverter.FromUnstructured(unstructure, &event)
        ...
}

and I am left with an event without an involvedObject:

{
    "kind": "Event",
    "apiVersion": "events.k8s.io/v1",
    "metadata": {
        "name": "testing-refinery-7df6cbbdb9.1773ad4bde97125f",
        "namespace": "default",
        "uid": "31f2fde4-a1a1-4861-9b99-1c291ab600ee",
        "resourceVersion": "34802",
        "creationTimestamp": "2023-07-20T20:27:06Z",
        "managedFields": [
            {
                "manager": "kube-controller-manager",
                "operation": "Update",
                "apiVersion": "v1",
                "time": "2023-07-20T20:27:06Z",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:count": {},
                    "f:firstTimestamp": {},
                    "f:involvedObject": {},
                    "f:lastTimestamp": {},
                    "f:message": {},
                    "f:reason": {},
                    "f:source": {
                        "f:component": {}
                    },
                    "f:type": {}
                }
            }
        ]
    },
    "involvedObject": {},
    "reason": "SuccessfulCreate",
    "source": {},
    "firstTimestamp": null,
    "lastTimestamp": null,
    "type": "Normal",
    "eventTime": null,
    "reportingComponent": "",
    "reportingInstance": ""
}

I believe the issue is that the event returned by the dynamic client has regarding instead of involvedObject like the corev1.Event struct expects.

Please let me know if this is actually an issue for the unstructured package.

liggitt commented 11 months ago

you are retrieving events in events.k8s.io/v1 version (corresponding to https://github.com/kubernetes/api/blob/86e8180c2c5fc26b0903248e2ec19bd30ee203bc/events/v1/types.go#L33) but then trying to coerce into v1 version objects (corresponding to https://github.com/kubernetes/api/blob/86e8180c2c5fc26b0903248e2ec19bd30ee203bc/core/v1/types.go#L6179)

You are correct the field names differ. For this to work properly, use the go types corresponding to the API group/version you are fetching (https://github.com/kubernetes/api/blob/86e8180c2c5fc26b0903248e2ec19bd30ee203bc/events/v1/types.go#L33).

TylerHelmuth commented 11 months ago

@liggitt thank for the explanation. Which version of the event should be preferred, v1 or events.k8s.io/v1?