diff --git a/cluster/kubernetes/update.go b/cluster/kubernetes/update.go index 24cfc85fb..41c842972 100644 --- a/cluster/kubernetes/update.go +++ b/cluster/kubernetes/update.go @@ -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 { @@ -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 @@ -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) diff --git a/cluster/kubernetes/update_test.go b/cluster/kubernetes/update_test.go index 2d12b3645..3b23ec061 100644 --- a/cluster/kubernetes/update_test.go +++ b/cluster/kubernetes/update_test.go @@ -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) { @@ -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) } @@ -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 +`