Skip to content
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
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.77.1

### Fixed

- Fixed a panic when parsing incomplete rules using relaxed mode.

## v0.77.0

### Added
Expand Down
19 changes: 9 additions & 10 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,12 @@ func (p Parser) parseRule(node *yaml.Node, offsetLine, offsetColumn int, content
}
}

for _, entry := range []struct {
part *yaml.Node
key string
}{
{key: labelsKey, part: labelsNode},
{key: annotationsKey, part: annotationsNode},
} {
if entry.part != nil && !isTag(entry.part.ShortTag(), mapTag) {
return invalidValueError(lines, entry.part.Line+offsetLine, entry.key, describeTag(mapTag), describeTag(entry.part.ShortTag()))
}
if (recordPart != nil || alertPart != nil) && exprPart != nil && labelsNode != nil && !isTag(labelsNode.ShortTag(), mapTag) {
return invalidValueError(lines, labelsNode.Line+offsetLine, labelsKey, describeTag(mapTag), describeTag(labelsNode.ShortTag()))
}

if alertPart != nil && exprPart != nil && annotationsNode != nil && !isTag(annotationsNode.ShortTag(), mapTag) {
return invalidValueError(lines, annotationsNode.Line+offsetLine, annotationsKey, describeTag(mapTag), describeTag(annotationsNode.ShortTag()))
}

if ok, perr, plines := validateStringMap(labelsKey, labelsNodes, offsetLine, lines); !ok {
Expand Down Expand Up @@ -631,6 +627,9 @@ type yamlMap struct {
}

func mappingNodes(node *yaml.Node) []yamlMap {
if node.Kind != yaml.MappingNode {
return nil
}
m := make([]yamlMap, 0, len(node.Content)/2)
for i := 0; i < len(node.Content); i += 2 {
m = append(m, yamlMap{key: node.Content[i], val: node.Content[i+1]})
Expand Down
39 changes: 37 additions & 2 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2942,6 +2942,8 @@ groups:
- name: foo
rules:
- labels: !!binary "SGVsbG8sIFdvcmxkIQ=="
record: foo
expr: foo
`),
strict: true,
output: parser.File{
Expand All @@ -2950,7 +2952,7 @@ groups:
Name: "foo",
Rules: []parser.Rule{
{
Lines: diags.LineRange{First: 8, Last: 8},
Lines: diags.LineRange{First: 8, Last: 10},
Error: parser.ParseError{
Line: 8,
Err: errors.New("labels value must be a mapping, got binary data instead"),
Expand Down Expand Up @@ -3564,6 +3566,8 @@ groups:
- name: foo
rules:
- labels: !!binary "SGVsbG8sIFdvcmxkIQ=="
record: foo
expr: foo
`),
strict: true,
output: parser.File{
Expand All @@ -3572,7 +3576,7 @@ groups:
Name: "foo",
Rules: []parser.Rule{
{
Lines: diags.LineRange{First: 5, Last: 5},
Lines: diags.LineRange{First: 5, Last: 7},
Error: parser.ParseError{
Line: 5,
Err: errors.New("labels value must be a mapping, got binary data instead"),
Expand Down Expand Up @@ -5131,6 +5135,37 @@ groups:
},
},
},
{
input: []byte(`
apiVersion: v1
kind: ConfigMap
metadata:
labels:
shard: "0"
total-shards: "1"
name: shard-jobs
namespace: foo
data:
jobs.yaml: |
jobs:
- name: foo
interval: 1m
queries:
- name: xxx
help: ''
labels:
- account_id
- bucket
- cache_status
mtls_identity:
cert_path: /etc/identity/tls.crt
key_path: /etc/identity/tls.key
`),
output: parser.File{
Groups: nil,
IsRelaxed: true,
},
},
}

alwaysEqual := cmp.Comparer(func(_, _ any) bool { return true })
Expand Down
Loading