Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: remove duplicate labelfile events #31

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"

"bscp.io/pkg/logs"
Expand Down Expand Up @@ -193,7 +194,7 @@ func initLabelsFromEnv() {
conf.Labels = util.MergeLabels(conf.Labels, labels)
}

func watchLabelsFile(ctx context.Context, path string) (chan ReloadMessage, error) {
func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]string) (chan ReloadMessage, error) {
watchChan := make(chan ReloadMessage)
v := viper.New()
v.SetConfigFile(path)
Expand All @@ -205,6 +206,7 @@ func watchLabelsFile(ctx context.Context, path string) (chan ReloadMessage, erro
return nil, fmt.Errorf("add watcher for %s failed, err: %s", path, err.Error())
}
go func() {
oldLabels := oldLabels
for {
select {
case <-ctx.Done():
Expand All @@ -228,20 +230,27 @@ func watchLabelsFile(ctx context.Context, path string) (chan ReloadMessage, erro
continue
}

logs.Infof("labels file %s changed, try reset labels", path)
if err := v.ReadInConfig(); err != nil {
msg.Error = fmt.Errorf("read labels file failed, err: %s", err.Error())
watchChan <- msg
continue
}

labels := make(map[string]string)
if err := v.Unmarshal(&labels); err != nil {
msg.Error = fmt.Errorf("unmarshal labels file failed, err: %s", err.Error())
watchChan <- msg
continue
}

if reflect.DeepEqual(labels, oldLabels) {
continue
}

logs.Infof("labels file %s changed, try reset labels, old: %s, new: %s", path, oldLabels, labels)
msg.Labels = labels
watchChan <- msg
oldLabels = labels
case err := <-watcher.Errors:
logs.Errorf("watcher error: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func refineLabelsFile(ctx context.Context, path string, confLabels map[string]st
return nil, fmt.Errorf("read labels file failed, err: %s", err.Error())
}

reloadChan, err := watchLabelsFile(ctx, absPath)
reloadChan, err := watchLabelsFile(ctx, absPath, labelsFromFile)
if err != nil {
return nil, fmt.Errorf("watch labels file failed, err: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
bscp.io v0.0.0-00010101000000-000000000000
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20230912015319-acb7495967f5
github.com/fsnotify/fsnotify v1.7.0
github.com/prometheus/client_golang v1.16.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
Expand All @@ -22,7 +23,6 @@ require (
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/gobwas/glob v0.2.3 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -407,7 +407,6 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down