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

fix: get kv list not work with label #59

Merged
merged 1 commit into from
Dec 28, 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
26 changes: 13 additions & 13 deletions cmd/bscp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func initFromCmdArgs() error {
}

func initLabelsFromEnv() {
labels := make(map[string]string)
envLabels := make(map[string]string)
// get multi labels from environment variables
envs := os.Environ()
for _, env := range envs {
Expand All @@ -230,10 +230,10 @@ func initLabelsFromEnv() {
continue
}
if strings.HasPrefix(k, envLabelsPrefix) && strings.TrimPrefix(k, envLabelsPrefix) != "" {
labels[strings.TrimPrefix(k, envLabelsPrefix)] = v
envLabels[strings.TrimPrefix(k, envLabelsPrefix)] = v
}
}
conf.Labels = util.MergeLabels(conf.Labels, labels)
conf.Labels = util.MergeLabels(conf.Labels, envLabels)
}

func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]string) (chan ReloadMessage, error) {
Expand Down Expand Up @@ -278,22 +278,22 @@ func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]stri
continue
}

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

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

logger.Info("labels file changed, try reset labels",
slog.String("file", path), slog.Any("old", oldLabels), slog.Any("new", labels))
msg.Labels = labels
slog.String("file", path), slog.Any("old", oldLabels), slog.Any("new", newLabels))
msg.Labels = newLabels
watchChan <- msg
oldLabels = labels
oldLabels = newLabels
case err := <-watcher.Errors:
logger.Error("watcher error", logger.ErrAttr(err))
}
Expand All @@ -305,21 +305,21 @@ func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]stri
func readLabelsFile(path string) (map[string]string, error) {
v := viper.New()
v.SetConfigFile(path)
labels := make(map[string]string)
fileLabels := make(map[string]string)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
logger.Warn("labels file not exist, skip read", slog.String("path", path))
return labels, nil
return fileLabels, nil
}
return nil, fmt.Errorf("stat labels file %s failed, err: %s", path, err.Error())
}
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("read labels file %s failed, err: %s", path, err.Error())
}
if err := v.Unmarshal(&labels); err != nil {
if err := v.Unmarshal(&fileLabels); err != nil {
return nil, fmt.Errorf("unmarshal labels file %s failed, err: %s", path, err.Error())
}
return labels, nil
return fileLabels, nil
}

// newTable 统一风格表格, 风格参考 kubectl
Expand Down
20 changes: 11 additions & 9 deletions cmd/bscp/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ var (
Use: "get",
Short: "Display app or kv resources",
Long: `Display app or kv resources`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// 设置日志等级, get 命令默认是 error
if logLevel == "" {
logLevel = "error"
}

level := logger.GetLevelByName(logLevel)
logger.SetLevel(level)

// 校验&反序列化 labels
if labelsStr != "" {
if err := json.Unmarshal([]byte(labelsStr), &labels); err != nil {
return fmt.Errorf("invalid labels: %w", err)
}
}

return nil
},
}

Expand Down Expand Up @@ -135,7 +144,7 @@ func runGetApp(args []string) error {
}

func runGetListKv(bscp client.Client, app string, match []string) error {
release, err := bscp.PullKvs(app, match)
release, err := bscp.PullKvs(app, match, client.WithAppLabels(labels))
if err != nil {
return err
}
Expand Down Expand Up @@ -169,13 +178,6 @@ func runGetListKv(bscp client.Client, app string, match []string) error {
}

func runGetKvValue(bscp client.Client, app, key string) error {
labels := map[string]string{}
if labelsStr != "" {
if err := json.Unmarshal([]byte(labelsStr), &labels); err != nil {
return fmt.Errorf("labels invalid: %w", err)
}
}

value, err := bscp.Get(app, key, client.WithAppLabels(labels))
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion cmd/bscp/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ var (
Use: "bscp",
Short: "bscp is a command line tool for blueking service config platform",
Long: `bscp is a command line tool for blueking service config platform`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// 设置日志等级
level := logger.GetLevelByName(logLevel)
logger.SetLevel(level)

return nil
},
}
)
Expand Down