Skip to content

Commit ceb4646

Browse files
kavirajkstevesg
andauthored
Add ability to support strict JSON unmarshal for limits (#4298)
* Add ability to support strict JSON unmarshal for `limits` This is the default behaviour for YAML now. ``` in := `{"unknown_fields": 100}` l := cortex.Limits{} fmt.Println(yaml.UnmarshalStrict([]byte(in), &l)) // yaml: unmarshal errors: // line 1: field unknown_fields not found in type validation.plain ``` This PR adds same behaviour if unmarshalled from JSON input as well. Signed-off-by: Kaviraj <[email protected]> * Add changelog Signed-off-by: Kaviraj <[email protected]> * Update CHANGELOG.md Co-authored-by: Steve Simpson <[email protected]> Signed-off-by: Kaviraj <[email protected]> Co-authored-by: Steve Simpson <[email protected]>
1 parent 8e84b22 commit ceb4646

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## master / unreleased
44

5+
* [CHANGE] Enable strict JSON unmarshal for `pkg/util/validation.Limits` struct. The custom `UnmarshalJSON()` will now fail if the input has unknown fields. #4298
56
* [CHANGE] Cortex chunks storage has been deprecated and it's now in maintenance mode: all Cortex users are encouraged to migrate to the blocks storage. No new features will be added to the chunks storage. The default Cortex configuration still runs the chunks engine; please check out the [blocks storage doc](https://cortexmetrics.io/docs/blocks-storage/) on how to configure Cortex to run with the blocks storage. #4268
67
* [CHANGE] The example Kubernetes manifests (stored at `k8s/`) have been removed due to a lack of proper support and maintenance. #4268
78
* [CHANGE] Querier / ruler: deprecated `-store.query-chunk-limit` CLI flag (and its respective YAML config option `max_chunks_per_query`) in favour of `-querier.max-fetched-chunks-per-query` (and its respective YAML config option `max_fetched_chunks_per_query`). The new limit specifies the maximum number of chunks that can be fetched in a single query from ingesters and long-term storage: the total number of actual fetched chunks could be 2x the limit, being independently applied when querying ingesters and long-term storage. #4125

pkg/util/validation/limits.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package validation
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"flag"
@@ -229,7 +230,10 @@ func (l *Limits) UnmarshalJSON(data []byte) error {
229230
}
230231

231232
type plain Limits
232-
return json.Unmarshal(data, (*plain)(l))
233+
dec := json.NewDecoder(bytes.NewReader(data))
234+
dec.DisallowUnknownFields()
235+
236+
return dec.Decode((*plain)(l))
233237
}
234238

235239
func (l *Limits) copyNotificationIntegrationLimits(defaults NotificationRateLimitMap) {

pkg/util/validation/limits_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package validation
33
import (
44
"encoding/json"
55
"reflect"
6+
"strings"
67
"testing"
78
"time"
89

@@ -165,6 +166,15 @@ func TestLimitsLoadingFromJson(t *testing.T) {
165166

166167
assert.Equal(t, 0.5, l.IngestionRate, "from json")
167168
assert.Equal(t, 100, l.MaxLabelNameLength, "from defaults")
169+
170+
// Unmarshal should fail if input contains unknown struct fields and
171+
// the decoder flag `json.Decoder.DisallowUnknownFields()` is set
172+
inp = `{"unknown_fields": 100}`
173+
l = Limits{}
174+
dec := json.NewDecoder(strings.NewReader(inp))
175+
dec.DisallowUnknownFields()
176+
err = dec.Decode(&l)
177+
assert.Error(t, err)
168178
}
169179

170180
func TestLimitsTagsYamlMatchJson(t *testing.T) {

0 commit comments

Comments
 (0)