Browse Source

Merge pull request #1008 from weaveworks/1000-yaml-single-quotes

Support for single quotes in YAML image value
Roland Schilter 1 year ago
parent
commit
b205a922e0
No account linked to committer's email address
2 changed files with 43 additions and 4 deletions
  1. 4
    3
      cluster/kubernetes/update.go
  2. 39
    1
      cluster/kubernetes/update_test.go

+ 4
- 3
cluster/kubernetes/update.go View File

@@ -126,13 +126,14 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
126 126
 	}
127 127
 	indent := matches[1]
128 128
 
129
+	optq :=`["']?` // An optional single or double quote
129 130
 	// Replace the container images
130 131
 	// Parse out all the container blocks
131 132
 	containersRE := regexp.MustCompile(`(?m:^` + indent + `containers:\s*(?:#.*)*$(?:\n(?:` + indent + `[-\s#].*)?)*)`)
132 133
 	// Parse out an individual container blog
133 134
 	containerRE := regexp.MustCompile(`(?m:` + indent + `-.*(?:\n(?:` + indent + `\s+.*)?)*)`)
134 135
 	// Parse out the image ID
135
-	imageRE := regexp.MustCompile(`(` + indent + `[-\s]\s*"?image"?:\s*)"?(?:[\w\.\-/:]+\s*?)*"?([\t\f #]+.*)?`)
136
+	imageRE := regexp.MustCompile(`(` + indent + `[-\s]\s*` + optq + `image` + optq + `:\s*)` + optq + `(?:[\w\.\-/:]+\s*?)*` + optq + `([\t\f #]+.*)?`)
136 137
 	imageReplacement := fmt.Sprintf("${1}%s${2}", maybeQuote(newImage.String()))
137 138
 	// Find the block of container specs
138 139
 	newDef = containersRE.ReplaceAllStringFunc(newDef, func(containers string) string {
@@ -159,7 +160,7 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
159 160
 
160 161
 	// The name we want is that under `metadata:`, which will *probably* be the first one
161 162
 	replacedName := false
162
-	replaceRCNameRE := regexp.MustCompile(`(\s+"?name"?:\s*)"?(?:[\w\.\-/:]+\s*?)"?([\t\f #]+.*)`)
163
+	replaceRCNameRE := regexp.MustCompile(`(\s+` + optq + `name` + optq + `:\s*)` + optq + `(?:[\w\.\-/:]+\s*?)` + optq + `([\t\f #]+.*)`)
163 164
 	replaceRCNameRE.ReplaceAllStringFunc(newDef, func(found string) string {
164 165
 		if replacedName {
165 166
 			return found
@@ -175,7 +176,7 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
175 176
 	replaceLabelsRE := multilineRE(
176 177
 		`((?:  selector|      labels):.*)`,
177 178
 		`((?:  ){2,4}name:.*)`,
178
-		`((?:  ){2,4}version:\s*) (?:"?[-\w]+"?)(\s.*)`,
179
+		`((?:  ){2,4}version:\s*) (?:` + optq + `[-\w]+` + optq + `)(\s.*)`,
179 180
 	)
180 181
 	replaceLabels := fmt.Sprintf("$1\n$2\n$3 %s$4", maybeQuote(newImage.Tag))
181 182
 	newDef = replaceLabelsRE.ReplaceAllString(newDef, replaceLabels)

+ 39
- 1
cluster/kubernetes/update_test.go View File

@@ -36,7 +36,6 @@ func testUpdate(t *testing.T, u update) {
36 36
 		fmt.Fprintln(os.Stderr, "Failed:", u.name)
37 37
 		t.Fatalf("Did not get expected result:\n\n%s\n\nInstead got:\n\n%s", u.caseOut, manifest)
38 38
 	}
39
-
40 39
 }
41 40
 
42 41
 func TestUpdates(t *testing.T) {
@@ -49,6 +48,7 @@ func TestUpdates(t *testing.T) {
49 48
 		{"minimal dockerhub image name", case5container, case5image, case5, case5out},
50 49
 		{"reordered keys", case6containers, case6image, case6, case6out},
51 50
 		{"from prod", case7containers, case7image, case7, case7out},
51
+		{"single quotes", case8containers, case8image, case8, case8out},
52 52
 	} {
53 53
 		testUpdate(t, c)
54 54
 	}
@@ -534,3 +534,41 @@ spec:
534 534
         - name: FLUENTD_CONF
535 535
           value: fluent.conf
536 536
 `
537
+
538
+const case8 = `---
539
+apiVersion: extensions/v1beta1
540
+kind: Deployment
541
+metadata:
542
+  name: weave
543
+spec:
544
+  replicas: 1
545
+  template:
546
+    metadata:
547
+      labels:
548
+        name: weave
549
+    spec:
550
+      containers:
551
+      - name: weave
552
+        image: 'weaveworks/weave-kube:2.2.0'
553
+`
554
+
555
+const case8image = "weaveworks/weave-kube:2.2.1"
556
+
557
+var case8containers = []string{"weave"}
558
+
559
+const case8out = `---
560
+apiVersion: extensions/v1beta1
561
+kind: Deployment
562
+metadata:
563
+  name: weave
564
+spec:
565
+  replicas: 1
566
+  template:
567
+    metadata:
568
+      labels:
569
+        name: weave
570
+    spec:
571
+      containers:
572
+      - name: weave
573
+        image: weaveworks/weave-kube:2.2.1
574
+`

Loading…
Cancel
Save