I have a question that i have encountered on my project. In my project, briefly when a user clicks to a button, a pod is created, does some operations and finally it is deleted. I should measure the pods running time and should decrease the duration from the credit of the user. I want to manage it externally. Is it possible to understand and manage when a new pod has been created and destroyed from outside of the pods? Thanks
The function I written is like following:
func WatchPods() {
clientset, err := utils.NewClient(true)//create k8s client
if err != nil {
//handle error
}
watchlist := cache.NewListWatchFromClient(
clientset.CoreV1().RESTClient(),
string(v1.ResourcePods),
"default",
fields.Everything(),
)
_, controller := cache.NewInformer(
watchlist,
&v1.Pod{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {//Calls when a new pod is created
pod := obj.(*v1.Pod)
//do whatever you want
}
},
DeleteFunc: func(obj interface{}) {//Calls when a new pod is deleted
pod := obj.(*v1.Pod)
//do whatever you want
},
},
)
stop := make(chan struct{})
defer close(stop)
controller.Run(stop)
}