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

Commit

Permalink
Merge pull request #1008 from weaveworks/1000-yaml-single-quotes
Browse files Browse the repository at this point in the history
Support for single quotes in YAML image value
  • Loading branch information
rndstr authored Mar 15, 2018
2 parents b19e9b7 + 11e9bd2 commit b205a92
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
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
`

0 comments on commit b205a92

Please sign in to comment.