func (sm *Manager) servicesWatcher(ctx context.Context) error {
	var wg sync.WaitGroup
	rw, err := watchtools.NewRetryWatcher("1", &cache.ListWatch{
		WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
			return sm.clientSet.CoreV1().Services(v1.NamespaceAll).Watch(ctx, metav1.ListOptions{})
		},
	})
	if err != nil {
		return fmt.Errorf("error creating services watcher: %s", err.Error())
	}
	go func() {
		<-sm.signalChan
		rw.Stop()
	}()
	ch := rw.ResultChan()
	log.Infoln("Beginning watching services for type: LoadBalancer in all namespaces")
	for event := range ch {
		sm.countServiceWatchEvent.With(prometheus.Labels{"type": string(event.Type)}).Add(1)
		switch event.Type {
		case watch.Added, watch.Modified:
			svc, ok := event.Object.(*v1.Service)
			if !ok {
				return fmt.Errorf("Unable to parse Kubernetes services from API watcher")
			}
			if svc.Annotations["kube-vip.io/ignore"] == "true" {
				log.Infof("Service [%s] has an ignore annotation for kube-vip", svc.Name)
				break
			}
			if svc.Spec.LoadBalancerIP == "" {
				log.Infof("Service [%s] has been addded/modified, it has no assigned external addresses", svc.Name)
			} else {
				log.Infof("Service [%s] has been addded/modified, it has an assigned external addresses [%s]", svc.Name, svc.Spec.LoadBalancerIP)
				wg.Add(1)
				err = sm.syncServices(svc, &wg)
				if err != nil {
					log.Error(err)
				}
				wg.Wait()
			}
		case watch.Deleted:
			svc, ok := event.Object.(*v1.Service)
			if !ok {
				return fmt.Errorf("Unable to parse Kubernetes services from API watcher")
			}
			if svc.Annotations["kube-vip.io/ignore"] == "true" {
				log.Infof("Service [%s] has an ignore annotation for kube-vip", svc.Name)
				break
			}
			err = sm.stopService(string(svc.UID))
			if err != nil {
				log.Error(err)
			}
			err = sm.deleteService(string(svc.UID))
			if err != nil {
				log.Error(err)
			}
			log.Infof("Service [%s] has been deleted", svc.Name)
		case watch.Bookmark:
		case watch.Error:
			log.Error("Error attempting to watch Kubernetes services")
			errObject := apierrors.FromObject(event.Object)
			statusErr, ok := errObject.(*apierrors.StatusError)
			if !ok {
				log.Errorf(spew.Sprintf("Received an error which is not *metav1.Status but %#+v", event.Object))
			}
			status := statusErr.ErrStatus
			log.Errorf("%v", status)
		default:
		}
	}
	log.Warnln("Stopping watching services for type: LoadBalancer in all namespaces")
	return nil
}
This file handles the watching of a services endpoints and updates a load balancers endpoint configurations accordingly
package manager
import (
	"context"
	"encoding/base64"
	"fmt"
	"os"
	"strconv"
	"strings"
	"sync"
	"github.com/kube-vip/kube-vip/pkg/bgp"
	"github.com/prometheus/client_golang/prometheus"
	log "github.com/sirupsen/logrus"
	"github.com/davecgh/go-spew/spew"
	v1 "k8s.io/api/core/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	watchtools "k8s.io/client-go/tools/watch"
	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/client-go/tools/cache"
)