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

Support for single quotes in YAML image value #1008

Merged
merged 1 commit into from
Mar 15, 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
7 changes: 4 additions & 3 deletions cluster/kubernetes/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
}
indent := matches[1]

optq :=`["']?` // An optional single or double quote
// Replace the container images
// Parse out all the container blocks
containersRE := regexp.MustCompile(`(?m:^` + indent + `containers:\s*(?:#.*)*$(?:\n(?:` + indent + `[-\s#].*)?)*)`)
// Parse out an individual container blog
containerRE := regexp.MustCompile(`(?m:` + indent + `-.*(?:\n(?:` + indent + `\s+.*)?)*)`)
// Parse out the image ID
imageRE := regexp.MustCompile(`(` + indent + `[-\s]\s*"?image"?:\s*)"?(?:[\w\.\-/:]+\s*?)*"?([\t\f #]+.*)?`)
imageRE := regexp.MustCompile(`(` + indent + `[-\s]\s*` + optq + `image` + optq + `:\s*)` + optq + `(?:[\w\.\-/:]+\s*?)*` + optq + `([\t\f #]+.*)?`)
imageReplacement := fmt.Sprintf("${1}%s${2}", maybeQuote(newImage.String()))
// Find the block of container specs
newDef = containersRE.ReplaceAllStringFunc(newDef, func(containers string) string {
Expand All @@ -159,7 +160,7 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)

// The name we want is that under `metadata:`, which will *probably* be the first one
replacedName := false
replaceRCNameRE := regexp.MustCompile(`(\s+"?name"?:\s*)"?(?:[\w\.\-/:]+\s*?)"?([\t\f #]+.*)`)
replaceRCNameRE := regexp.MustCompile(`(\s+` + optq + `name` + optq + `:\s*)` + optq + `(?:[\w\.\-/:]+\s*?)` + optq + `([\t\f #]+.*)`)
replaceRCNameRE.ReplaceAllStringFunc(newDef, func(found string) string {
if replacedName {
return found
Expand All @@ -175,7 +176,7 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
replaceLabelsRE := multilineRE(
`((?: selector| labels):.*)`,
`((?: ){2,4}name:.*)`,
`((?: ){2,4}version:\s*) (?:"?[-\w]+"?)(\s.*)`,
`((?: ){2,4}version:\s*) (?:` + optq + `[-\w]+` + optq + `)(\s.*)`,
)
replaceLabels := fmt.Sprintf("$1\n$2\n$3 %s$4", maybeQuote(newImage.Tag))
newDef = replaceLabelsRE.ReplaceAllString(newDef, replaceLabels)
Expand Down
40 changes: 39 additions & 1 deletion cluster/kubernetes/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func testUpdate(t *testing.T, u update) {
fmt.Fprintln(os.Stderr, "Failed:", u.name)
t.Fatalf("Did not get expected result:\n\n%s\n\nInstead got:\n\n%s", u.caseOut, manifest)
}

}

func TestUpdates(t *testing.T) {
Expand All @@ -49,6 +48,7 @@ func TestUpdates(t *testing.T) {
{"minimal dockerhub image name", case5container, case5image, case5, case5out},
{"reordered keys", case6containers, case6image, case6, case6out},
{"from prod", case7containers, case7image, case7, case7out},
{"single quotes", case8containers, case8image, case8, case8out},
} {
testUpdate(t, c)
}
Expand Down Expand Up @@ -534,3 +534,41 @@ spec:
- name: FLUENTD_CONF
value: fluent.conf
`

const case8 = `---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: weave
spec:
replicas: 1
template:
metadata:
labels:
name: weave
spec:
containers:
- name: weave
image: 'weaveworks/weave-kube:2.2.0'
`

const case8image = "weaveworks/weave-kube:2.2.1"

var case8containers = []string{"weave"}

const case8out = `---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: weave
spec:
replicas: 1
template:
metadata:
labels:
name: weave
spec:
containers:
- name: weave
image: weaveworks/weave-kube:2.2.1
`