k8s.io/client-go/pkg/api/v1 was retired, how to parse event.Object?

327 Views Asked by At

I am watching pods and get event from channel ResultChan(), now I want to parse event.Object. I saw a piece of code like this: event.Object.(*apiv1.Pod), please see the below code, but unfortunately k8s.io/client-go/pkg/api/v1 is retired, this piece of code doesn't work.

My question is, how to get Pod information from event.Object? What new API can access or parse event.Object?

import (
    apiv1 "k8s.io/client-go/pkg/api/v1"
)
    watcher, err := k8sClient.CoreV1().Pods(namespace).Watch(ctx, metav1.ListOptions{
        LabelSelector: labels,
    })

    event := <-watcher.ResultChan():

    pod, ok := event.Object.(*apiv1.Pod)

1

There are 1 best solutions below

0
On BEST ANSWER

It seems that with version 5 there was indeed breaking API change:

Moved pkg/api and pkg/apis to k8s.io/api. Other kubernetes repositories also import types from there, so they are composable with client-go.

So no instead of the k8s.io/client-go/pkg/api/v1 you can use k8s.io/api/core/v1 :

import ( 
    corev1  "http://k8s.io/api/core/v1"  
    "k8s.io/apimachinery/pkg/api/errors"
    metav1  "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"  
    "k8s.io/client-go/tools/clientcmd"
)