In the implementation of informer
, one can provide event handlers like below.
podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs {
// When a new pod gets created
AddFunc: func(obj interface{}) {
k8s.handleAddPod(obj)
},
// When a pod gets updated
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
k8s.handleUpdatePod(oldObj, newObj)
},
// When a pod gets deleted
DeleteFunc: func(obj interface{}) {
k8s.handleDeletePod(obj)
},
})
This is an example event handler for processing the Pod
related events. So far, I have written the handler as below; however, it is failing to read the object.
I am not able to typecast the obj interface{}
into v1.Pod
and an attempt to read it failing. The value of flag ok
is coming out false
. Can someone please suggest whats missing in this code?
func (k8s *K8S) handleAddPod(obj interface{}) {
pod, ok := obj.(v1.Pod)
if ok {
log.Debug("Status: " + string(pod.Status.Phase))
log.Debug("Pod added: " + pod.Name + " " + pod.DeletionTimestamp.String())
}
}
I could read the values and the content of the obj
using this code.
func (k8s *K8S) handleAddPod(obj interface{}) {
pod, ok := obj.(*v1.Pod) // Added * here.
if ok {
log.Debug("Status: " + string(pod.Status.Phase))
log.Debug("Pod added: " + pod.Name + " " + pod.DeletionTimestamp.String())
}
}