-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[VTAdmin API] Replace underscores with dashes in cluster ID #15722
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/spf13/viper" | ||
|
@@ -149,6 +150,8 @@ func LoadConfig(r io.Reader, configType string) (cfg *Config, id string, err err | |
return nil, "", ErrNoConfigID | ||
} | ||
|
||
id = formatID(id) | ||
|
||
tmp := map[string]string{} | ||
if err := v.Unmarshal(&tmp); err != nil { | ||
return nil, id, err | ||
|
@@ -164,6 +167,7 @@ func LoadConfig(r io.Reader, configType string) (cfg *Config, id string, err err | |
if err := cfg.unmarshalMap(tmp); err != nil { | ||
return nil, id, err | ||
} | ||
cfg.ID = id | ||
|
||
return cfg, id, nil | ||
} | ||
|
@@ -237,7 +241,7 @@ func (cfg *Config) MarshalJSON() ([]byte, error) { | |
// config. Neither the caller or the argument are modified in any way. | ||
func (cfg Config) Merge(override Config) Config { | ||
merged := Config{ | ||
ID: cfg.ID, | ||
ID: formatID(cfg.ID), | ||
Name: cfg.Name, | ||
DiscoveryImpl: cfg.DiscoveryImpl, | ||
DiscoveryFlagsByImpl: map[string]map[string]string{}, | ||
|
@@ -255,7 +259,7 @@ func (cfg Config) Merge(override Config) Config { | |
} | ||
|
||
if override.ID != "" { | ||
merged.ID = override.ID | ||
merged.ID = formatID(override.ID) | ||
} | ||
|
||
if override.Name != "" { | ||
|
@@ -284,6 +288,13 @@ func (cfg Config) Merge(override Config) Config { | |
return merged | ||
} | ||
|
||
func formatID(id string) string { | ||
// gRPC can't process custom resolver names with underscores | ||
id = strings.Replace(id, "_", "-", -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're here, we could address the wider issue I think just as easily. We could replace any invalid chars with dashes, or remove them as well. You can see the regex to replace any unaccepted chars with dashes here: https://go.dev/play/p/qmHmZ_KSnMI If you wanted to do that, you probably just want to make the regexp a package variable so that we don't have to compile it every time (although I don't imagine we hit this very often so probably not a practical issue even if we did). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought about this a bit more and IMO: auto-fixing is a breaking change (what happens if I have a deployment with clusters but the alternative is less magical, since users are having the names changed out silently beneath them, so we should maybe just do input validation and include an entry in the release notes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree that it's a breaking change - did you see my latest comment? I agree with input validation! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that plan looks good to me! |
||
|
||
return id | ||
} | ||
|
||
func mergeStringMap(base map[string]string, override map[string]string) { | ||
for k, v := range override { | ||
base[k] = v | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we do this further up the call stack (when we parse a config) so we don't need to do this operation every time we merge two configs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! 😄