Skip to content

Configs API: Allow POST configs in YAML format. #2181

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

Merged
merged 8 commits into from
Mar 4, 2020

Conversation

Wing924
Copy link
Contributor

@Wing924 Wing924 commented Feb 25, 2020

What this PR does:

allow post configs API in YAML format
allow get configs API in YAML format

Which issue(s) this PR fixes:
Fixes #2170

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
fixes cortexproject#2170
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
@pracucci
Copy link
Contributor

Removed myself and Goutham as reviewers cause this is @jtlisi area of expertise. I can jump in as second reviewer, if necessary.

@Wing924
Copy link
Contributor Author

Wing924 commented Feb 28, 2020

@jtlisi Could you review this?

@@ -111,7 +113,7 @@ func (a *API) setConfig(w http.ResponseWriter, r *http.Request) {
logger := util.WithContext(r.Context(), util.Logger)

var cfg configs.Config
if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
if err := yaml.NewDecoder(r.Body).Decode(&cfg); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assuming JSON is a subset of YAML for this to work (works most of the time, but there are corner cases), can you use a content-type head to detect YAML? And have JSON be the default.

Wing924 added 2 commits March 2, 2020 13:18
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good job! I love this enhancement! I left few minor comments. Other than this, LGTM.

FormatYAML = "yaml"
)

func ParseConfigFormat(v string, defaultFormat string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to expose this function. Can be parseConfigFormat()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't expose this function, I can't write test against this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is tested within the same package pkg/configs/api/api_test.go so you shouldn't have any problem testing it even if unexported (because unexported functions are still visibile within the same package).

Copy link
Contributor

@pracucci pracucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Wing924 for your great contributions! LGTM with few nits that should be trivial to solve.

FormatYAML = "yaml"
)

func ParseConfigFormat(v string, defaultFormat string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is tested within the same package pkg/configs/api/api_test.go so you shouldn't have any problem testing it even if unexported (because unexported functions are still visibile within the same package).

Comment on lines 108 to 110
// should not be here
level.Error(logger).Log("msg", "unknown Format")
http.Error(w, err.Error(), http.StatusInternalServerError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// should not be here
level.Error(logger).Log("msg", "unknown Format")
http.Error(w, err.Error(), http.StatusInternalServerError)
// should never reach this point
level.Error(logger).Log("msg", "unexpected error detecting the config format")
http.Error(w, err.Error(), http.StatusInternalServerError)
Comment on lines 144 to 146
// should not be here
level.Error(logger).Log("msg", "unknown Format")
http.Error(w, err.Error(), http.StatusBadRequest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// should not be here
level.Error(logger).Log("msg", "unknown Format")
http.Error(w, err.Error(), http.StatusBadRequest)
// should never reach this point
level.Error(logger).Log("msg", "unexpected error detecting the config format")
http.Error(w, err.Error(), http.StatusInternalServerError)
Wing924 added 2 commits March 3, 2020 22:40
Signed-off-by: Wing924 <weihe924stephen@gmail.com>
@Wing924
Copy link
Contributor Author

Wing924 commented Mar 3, 2020

@pracucci since api_test.go use api_test package while api.go use api package, I created new api2_test.go to do test against private function.

@pracucci
Copy link
Contributor

pracucci commented Mar 3, 2020

@pracucci since api_test.go use api_test package while api.go use api package, I created new api2_test.go to do test against private function.

Hold on. Why "api_test.go use api_test package"? I think that's a mistake. It should just use the same package of api.go

Signed-off-by: Wing924 <weihe924stephen@gmail.com>
@Wing924
Copy link
Contributor Author

Wing924 commented Mar 3, 2020

@pracucci You're right. There is no reason to use api_test package.

Copy link
Contributor

@jtlisi jtlisi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pracucci pracucci merged commit 9789020 into cortexproject:master Mar 4, 2020
@Wing924 Wing924 deleted the config_api_yaml branch March 4, 2020 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 participants