Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Fix multiline lock reason yaml corruption #978

Merged
merged 1 commit into from
Mar 7, 2018
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
2 changes: 1 addition & 1 deletion cluster/kubernetes/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func updateAnnotations(def []byte, tagAll string, f func(map[string]string) map[
// TODO: This should handle potentially different indentation.
// TODO: There's probably a more elegant regex-ey way to do this in one pass.
replaced := false
annotationsRE := regexp.MustCompile(`(?m:\n annotations:\s*(?:#.*)*(?:\n .*)*$)`)
annotationsRE := regexp.MustCompile(`(?m:\n annotations:\s*(?:#.*)*(?:\n .*|\n)*$)`)
newDef := annotationsRE.ReplaceAllStringFunc(string(def), func(found string) string {
if !replaced {
replaced = true
Expand Down
30 changes: 27 additions & 3 deletions cluster/kubernetes/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kubernetes

import (
"bytes"
"fmt"
"strings"
"testing"
"text/template"

Expand Down Expand Up @@ -96,6 +98,22 @@ func TestUpdatePolicies(t *testing.T) {
Remove: policy.Set{policy.Automated: "true"},
},
},
{
name: "multiline",
in: map[string]string{"flux.weave.works/locked_msg": "|-\n first\n second"},
out: nil,
update: policy.Update{
Remove: policy.Set{policy.LockedMsg: "foo"},
},
},
{
name: "multiline with empty line",
in: map[string]string{"flux.weave.works/locked_msg": "|-\n first\n\n third"},
out: nil,
update: policy.Update{
Remove: policy.Set{policy.LockedMsg: "foo"},
},
},
} {
caseIn := templToString(t, annotationsTemplate, c.in)
caseOut := templToString(t, annotationsTemplate, c.out)
Expand All @@ -113,7 +131,7 @@ apiVersion: extensions/v1beta1
kind: Deployment
metadata: # comment really close to the war zone
{{with .}}annotations:{{range $k, $v := .}}
{{$k}}: {{printf "%q" $v}}{{end}}
{{$k}}: {{printf "%s" $v}}{{end}}
{{end}}name: nginx
spec:
replicas: 1
Expand All @@ -129,9 +147,15 @@ spec:
- containerPort: 80
`))

func templToString(t *testing.T, templ *template.Template, data interface{}) string {
func templToString(t *testing.T, templ *template.Template, annotations map[string]string) string {
for k, v := range annotations {
// Don't wrap multilines
if !strings.HasPrefix(v, "|") {
annotations[k] = fmt.Sprintf("%q", v)
}
}
out := &bytes.Buffer{}
err := templ.Execute(out, data)
err := templ.Execute(out, annotations)
if err != nil {
t.Fatal(err)
}
Expand Down