feat: Only write on changes

This commit is contained in:
Dries De Peuter 2023-06-01 12:40:19 +02:00
parent bbd55ad23e
commit 2ceb595dc3
No known key found for this signature in database
3 changed files with 15 additions and 2 deletions

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -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)