package informer
import (
"fmt"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
k8scache "k8s.io/client-go/tools/cache"
)
// PodController watches the name and namespace of pods that are added,
// deleted, or updated
type PodSvcController struct {
informerFactory informers.SharedInformerFactory
podInformer coreinformers.PodInformer
svcInformer coreinformers.ServiceInformer
ChangePod bool
ChangeSvc bool
}
// Run starts shared informers and waits for the shared informer cache to
// synchronize.
func (c *PodSvcController) Run(stopCh chan struct{}) error {
// Starts all the shared informers that have been created by the factory so
// far.
c.informerFactory.Start(stopCh)
// wait for the initial synchronization of the local cache.
if !k8scache.WaitForCacheSync(stopCh, c.podInformer.Informer().HasSynced) {
return fmt.Errorf("Failed to sync")
}
return nil
}
func (c *PodSvcController) podAdd(obj interface{}) {
//pod := obj.(*corev1.Pod)
c.ChangePod = true
//fmt.Println("POD ADD: ", pod.Namespace, pod.Name)
}
func (c *PodSvcController) podDelete(obj interface{}) {
//pod := obj.(*corev1.Pod)
c.ChangePod = true
//fmt.Println("POD DELETE: ", pod.Namespace, pod.Name)
}
func (c *PodSvcController) podUpdate(old, new interface{}) {
//oldPod := old.(*corev1.Pod)
//newPod := new.(*corev1.Pod)
c.ChangePod = true
//fmt.Println("POD UPDATED: ",
// oldPod.Namespace, oldPod.Name, newPod.Status.Phase)
}
func (c *PodSvcController) svcAdd(obj interface{}) {
//svc := obj.(*corev1.Service)
c.ChangeSvc = true
//fmt.Println("SVC ADD: ", svc.Namespace, svc.Name)
}
func (c *PodSvcController) svcDelete(obj interface{}) {
//svc := obj.(*corev1.Service)
c.ChangeSvc = true
//fmt.Println("SVC DELETE: ", svc.Namespace, svc.Name)
}
func (c *PodSvcController) svcUpdate(old, new interface{}) {
//oldSvc := old.(*corev1.Service)
//newSvc := new.(*corev1.Service)
c.ChangeSvc = true
//fmt.Println("POD UPDATED: ",
// oldSvc.Namespace, oldSvc.Name, newSvc.Status)
}
// NewPodLoggingController creates a PodLoggingController
func NewPodSvcController(informerFactory informers.SharedInformerFactory) *PodSvcController {
podInformer := informerFactory.Core().V1().Pods()
svcInformer := informerFactory.Core().V1().Services()
c := &PodSvcController{
informerFactory: informerFactory,
podInformer: podInformer,
svcInformer: svcInformer,
ChangePod: false,
ChangeSvc: false,
}
podInformer.Informer().AddEventHandler(
// We only care about Add and Delete event handlers.
k8scache.ResourceEventHandlerFuncs{
// Called on creation
AddFunc: c.podAdd,
// Called on resource update and every resyncPeriod on existing resources.
UpdateFunc: c.podUpdate,
// Called on resource deletion.
DeleteFunc: c.podDelete,
},
)
svcInformer.Informer().AddEventHandler(
// We only care about Add and Delete event handlers.
k8scache.ResourceEventHandlerFuncs{
// Called on creation
AddFunc: c.svcAdd,
// Called on resource update and every resyncPeriod on existing resources.
UpdateFunc: c.svcUpdate,
// Called on resource deletion.
DeleteFunc: c.svcDelete,
},
)
return c
}
No comments:
Post a Comment