From 2ceb595dc365b57325efd6b94fb20e6a98529ab8 Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Thu, 1 Jun 2023 12:40:19 +0200 Subject: [PATCH] feat: Only write on changes --- go.mod | 1 + go.sum | 2 ++ server/main.go | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e65c2ac..7f2cf07 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( require ( github.com/bep/debounce v1.2.1 github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davegardnerisme/deephash v0.0.0-20210406090112-6d072427d830 github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/go-logr/logr v1.2.3 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect diff --git a/go.sum b/go.sum index a4e905f..f8dd32f 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davegardnerisme/deephash v0.0.0-20210406090112-6d072427d830 h1:gn7TsPBQ3HoEUaa8oBLbMalIBPf3eeQb7W/8kK3gERk= +github.com/davegardnerisme/deephash v0.0.0-20210406090112-6d072427d830/go.mod h1:ToGe2SdaElKXzEmYLttAgFHy0exxh0wyq9zG7ZjjjYM= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= diff --git a/server/main.go b/server/main.go index c1e49e6..b5e7d12 100644 --- a/server/main.go +++ b/server/main.go @@ -23,6 +23,8 @@ import ( "github.com/bep/debounce" "k8s.io/client-go/tools/clientcmd" + + "github.com/davegardnerisme/deephash" ) var kubeconfig string @@ -32,6 +34,7 @@ var id string var leaseLockName string func main() { + klog.InitFlags(nil) if home := homedir.HomeDir(); home != "" { flag.StringVar(&kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") } else { @@ -41,7 +44,6 @@ func main() { flag.StringVar(&cmName, "configmap", "well-known-generated", "") flag.StringVar(&id, "id", os.Getenv("POD_NAME"), "the holder identity name") flag.StringVar(&leaseLockName, "lease-lock-name", "well-known", "the lease lock resource name") - flag.Parse() // creates the in-cluster config @@ -138,13 +140,14 @@ func loop(ctx context.Context, clientset *kubernetes.Clientset) { } debounced := debounce.New(500 * time.Millisecond) + hash := []byte{} for event := range watch.ResultChan() { svc, ok := event.Object.(*v1.Service) if !ok { continue } - klog.Infof("Change detected on %s", svc.GetName()) + klog.V(1).Infof("Change detected on %s", svc.GetName()) debounced(func() { reg, err := discoverData(clientset, namespace) @@ -153,6 +156,13 @@ func loop(ctx context.Context, clientset *kubernetes.Clientset) { return } + newHash := deephash.Hash(reg) + if string(hash) == string(newHash) { + klog.V(1).Info("No changes detected") + return + } + hash = newHash + klog.Info("Writing configmap") if err := updateConfigMap(ctx, clientset, reg); err != nil { klog.Error(err)