Skip to content

Commit 4cbca2e

Browse files
committed
Make munger begin/end less generic
Just force the beginMungeTag() endMungeTag() macros on users, by hiding it under the covers. It really simplies things for users.
1 parent 22fd8ac commit 4cbca2e

File tree

19 files changed

+105
-78
lines changed

19 files changed

+105
-78
lines changed

cmd/mungedocs/toc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func updateTOC(filePath string, markdown []byte) ([]byte, error) {
3939
return nil, err
4040
}
4141
lines := splitLines(markdown)
42-
updatedMarkdown, err := updateMacroBlock(lines, beginMungeTag(tocMungeTag), endMungeTag(tocMungeTag), string(toc))
42+
updatedMarkdown, err := updateMacroBlock(lines, tocMungeTag, string(toc))
4343
if err != nil {
4444
return nil, err
4545
}

cmd/mungedocs/unversioned_warning.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import "fmt"
2020

2121
const unversionedWarningTag = "UNVERSIONED_WARNING"
2222

23-
var beginUnversionedWarning = beginMungeTag(unversionedWarningTag)
24-
var endUnversionedWarning = endMungeTag(unversionedWarningTag)
25-
2623
const unversionedWarningFmt = `
2724
<!-- BEGIN STRIP_FOR_RELEASE -->
2825
@@ -65,8 +62,8 @@ func updateUnversionedWarning(file string, markdown []byte) ([]byte, error) {
6562
// No warnings on release branches
6663
return markdown, nil
6764
}
68-
if !hasMacroBlock(lines, beginUnversionedWarning, endUnversionedWarning) {
69-
lines = append([]string{beginUnversionedWarning, endUnversionedWarning}, lines...)
65+
if !hasMacroBlock(lines, unversionedWarningTag) {
66+
lines = prependMacroBlock(unversionedWarningTag, lines)
7067
}
71-
return updateMacroBlock(lines, beginUnversionedWarning, endUnversionedWarning, makeUnversionedWarning(file))
68+
return updateMacroBlock(lines, unversionedWarningTag, makeUnversionedWarning(file))
7269
}

cmd/mungedocs/unversioned_warning_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import (
2323
)
2424

2525
func TestUnversionedWarning(t *testing.T) {
26-
warningBlock := beginUnversionedWarning + "\n" + makeUnversionedWarning("filename.md") + "\n" + endUnversionedWarning + "\n"
26+
beginMark := beginMungeTag(unversionedWarningTag)
27+
endMark := endMungeTag(unversionedWarningTag)
28+
29+
warningBlock := beginMark + "\n" + makeUnversionedWarning("filename.md") + "\n" + endMark + "\n"
2730
var cases = []struct {
2831
in string
2932
out string
@@ -38,15 +41,15 @@ func TestUnversionedWarning(t *testing.T) {
3841
"Foo\n<!-- TAG IS_VERSIONED -->\nBar",
3942
},
4043
{
41-
beginUnversionedWarning + "\n" + endUnversionedWarning + "\n",
44+
beginMark + "\n" + endMark + "\n",
4245
warningBlock,
4346
},
4447
{
45-
beginUnversionedWarning + "\n" + "something\n" + endUnversionedWarning + "\n",
48+
beginMark + "\n" + "something\n" + endMark + "\n",
4649
warningBlock,
4750
},
4851
{
49-
"Foo\n" + beginUnversionedWarning + "\n" + endUnversionedWarning + "\nBar\n",
52+
"Foo\n" + beginMark + "\n" + endMark + "\nBar\n",
5053
"Foo\n" + warningBlock + "Bar\n",
5154
},
5255
{

cmd/mungedocs/util.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func splitLines(document []byte) []string {
3838
//
3939
// Delimiters should occupy own line.
4040
// Returns copy of document with modifications.
41-
func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]byte, error) {
41+
func updateMacroBlock(lines []string, token, insertThis string) ([]byte, error) {
42+
beginMark := beginMungeTag(token)
43+
endMark := endMungeTag(token)
4244
var buffer bytes.Buffer
4345
betweenBeginAndEnd := false
4446
for _, line := range lines {
@@ -85,15 +87,32 @@ func hasLine(lines []string, needle string) bool {
8587
return false
8688
}
8789

90+
// Add a macro block to the beginning of a set of lines
91+
func prependMacroBlock(token string, lines []string) []string {
92+
beginMark := beginMungeTag(token)
93+
endMark := endMungeTag(token)
94+
return append([]string{beginMark, endMark}, lines...)
95+
}
96+
97+
// Add a macro block to the end of a set of lines
98+
func appendMacroBlock(token string, lines []string) []string {
99+
beginMark := beginMungeTag(token)
100+
endMark := endMungeTag(token)
101+
return append(lines, beginMark, endMark)
102+
}
103+
88104
// Tests that a document, represented as a slice of lines, has a macro block.
89-
func hasMacroBlock(lines []string, begin string, end string) bool {
105+
func hasMacroBlock(lines []string, token string) bool {
106+
beginMark := beginMungeTag(token)
107+
endMark := endMungeTag(token)
108+
90109
foundBegin := false
91110
for _, line := range lines {
92111
trimmedLine := strings.Trim(line, " \n")
93112
switch {
94-
case !foundBegin && trimmedLine == begin:
113+
case !foundBegin && trimmedLine == beginMark:
95114
foundBegin = true
96-
case foundBegin && trimmedLine == end:
115+
case foundBegin && trimmedLine == endMark:
97116
return true
98117
}
99118
}

cmd/mungedocs/util_test.go

+33-25
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ import (
2424
)
2525

2626
func Test_updateMacroBlock(t *testing.T) {
27+
token := "TOKEN"
28+
BEGIN := beginMungeTag(token)
29+
END := endMungeTag(token)
30+
2731
var cases = []struct {
2832
in string
2933
out string
3034
}{
3135
{"", ""},
3236
{"Lorem ipsum\ndolor sit amet\n",
3337
"Lorem ipsum\ndolor sit amet\n"},
34-
{"Lorem ipsum \n BEGIN\ndolor\nEND\nsit amet\n",
35-
"Lorem ipsum \n BEGIN\nfoo\n\nEND\nsit amet\n"},
38+
{"Lorem ipsum \n " + BEGIN + "\ndolor\n" + END + "\nsit amet\n",
39+
"Lorem ipsum \n " + BEGIN + "\nfoo\n\n" + END + "\nsit amet\n"},
3640
}
3741
for _, c := range cases {
38-
actual, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo\n")
42+
actual, err := updateMacroBlock(splitLines([]byte(c.in)), "TOKEN", "foo\n")
3943
assert.NoError(t, err)
4044
if c.out != string(actual) {
4145
t.Errorf("Expected '%v' but got '%v'", c.out, string(actual))
@@ -44,20 +48,23 @@ func Test_updateMacroBlock(t *testing.T) {
4448
}
4549

4650
func Test_updateMacroBlock_errors(t *testing.T) {
51+
b := beginMungeTag("TOKEN")
52+
e := endMungeTag("TOKEN")
53+
4754
var cases = []struct {
4855
in string
4956
}{
50-
{"BEGIN\n"},
51-
{"blah\nBEGIN\nblah"},
52-
{"END\n"},
53-
{"blah\nEND\nblah\n"},
54-
{"END\nBEGIN"},
55-
{"BEGIN\nEND\nEND"},
56-
{"BEGIN\nBEGIN\nEND"},
57-
{"BEGIN\nBEGIN\nEND\nEND"},
57+
{b + "\n"},
58+
{"blah\n" + b + "\nblah"},
59+
{e + "\n"},
60+
{"blah\n" + e + "\nblah\n"},
61+
{e + "\n" + b},
62+
{b + "\n" + e + "\n" + e},
63+
{b + "\n" + b + "\n" + e},
64+
{b + "\n" + b + "\n" + e + "\n" + e},
5865
}
5966
for _, c := range cases {
60-
_, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo")
67+
_, err := updateMacroBlock(splitLines([]byte(c.in)), "TOKEN", "foo")
6168
assert.Error(t, err)
6269
}
6370
}
@@ -86,26 +93,27 @@ func TestHasLine(t *testing.T) {
8693
}
8794

8895
func TestHasMacroBlock(t *testing.T) {
96+
token := "<<<"
97+
b := beginMungeTag(token)
98+
e := endMungeTag(token)
8999
cases := []struct {
90100
lines []string
91-
begin string
92-
end string
93101
expected bool
94102
}{
95-
{[]string{"<<<", ">>>"}, "<<<", ">>>", true},
96-
{[]string{"<<<", "abc", ">>>"}, "<<<", ">>>", true},
97-
{[]string{"<<<", "<<<", "abc", ">>>"}, "<<<", ">>>", true},
98-
{[]string{"<<<", "abc", ">>>", ">>>"}, "<<<", ">>>", true},
99-
{[]string{"<<<", ">>>", "<<<", ">>>"}, "<<<", ">>>", true},
100-
{[]string{"<<<"}, "<<<", ">>>", false},
101-
{[]string{">>>"}, "<<<", ">>>", false},
102-
{[]string{"<<<", "abc"}, "<<<", ">>>", false},
103-
{[]string{"abc", ">>>"}, "<<<", ">>>", false},
103+
{[]string{b, e}, true},
104+
{[]string{b, "abc", e}, true},
105+
{[]string{b, b, "abc", e}, true},
106+
{[]string{b, "abc", e, e}, true},
107+
{[]string{b, e, b, e}, true},
108+
{[]string{b}, false},
109+
{[]string{e}, false},
110+
{[]string{b, "abc"}, false},
111+
{[]string{"abc", e}, false},
104112
}
105113

106114
for i, c := range cases {
107-
if hasMacroBlock(c.lines, c.begin, c.end) != c.expected {
108-
t.Errorf("case[%d]: %q,%q, expected %t, got %t", i, c.begin, c.end, c.expected, !c.expected)
115+
if hasMacroBlock(c.lines, token) != c.expected {
116+
t.Errorf("case[%d]: expected %t, got %t", i, c.expected, !c.expected)
109117
}
110118
}
111119
}

docs/admin/namespaces/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Use the file [`namespace-dev.json`](namespace-dev.json) which describes a develo
9999
```
100100

101101
[Download example](namespace-dev.json)
102-
<!-- END MUNGE: EXAMPLE -->
102+
<!-- END MUNGE: EXAMPLE namespace-dev.json -->
103103

104104
Create the development namespace using kubectl.
105105

docs/getting-started-guides/logging.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ spec:
7474
```
7575

7676
[Download example](../../examples/blog-logging/counter-pod.yaml)
77-
<!-- END MUNGE: EXAMPLE -->
77+
<!-- END MUNGE: EXAMPLE ../../examples/blog-logging/counter-pod.yaml -->
7878

7979
This pod specification has one container which runs a bash script when the container is born. This script simply writes out the value of a counter and the date once per second and runs indefinitely. Let’s create the pod in the default
8080
namespace.
@@ -192,7 +192,7 @@ spec:
192192
```
193193
194194
[Download example](../../cluster/saltbase/salt/fluentd-gcp/fluentd-gcp.yaml)
195-
<!-- END MUNGE: EXAMPLE -->
195+
<!-- END MUNGE: EXAMPLE ../../cluster/saltbase/salt/fluentd-gcp/fluentd-gcp.yaml -->
196196
197197
This pod specification maps the directory on the host containing the Docker log files, `/var/lib/docker/containers`, to a directory inside the container which has the same path. The pod runs one image, `gcr.io/google_containers/fluentd-gcp:1.6`, which is configured to collect the Docker log files from the logs directory and ingest them into Google Cloud Logging. One instance of this pod runs on each node of the cluster. Kubernetes will notice if this pod fails and automatically restart it.
198198

docs/user-guide/downward-api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ spec:
105105
```
106106
107107
[Download example](downward-api/dapi-pod.yaml)
108-
<!-- END MUNGE: EXAMPLE -->
108+
<!-- END MUNGE: EXAMPLE downward-api/dapi-pod.yaml -->
109109
110110
Some more thorough examples:
111111
* [environment variables](environment-guide/)

docs/user-guide/logging.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ spec:
5959
```
6060

6161
[Download example](../../examples/blog-logging/counter-pod.yaml)
62-
<!-- END MUNGE: EXAMPLE -->
62+
<!-- END MUNGE: EXAMPLE ../../examples/blog-logging/counter-pod.yaml -->
6363

6464
we can run the pod:
6565

docs/user-guide/simple-yaml.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ spec:
6565
```
6666
6767
[Download example](pod.yaml)
68-
<!-- END MUNGE: EXAMPLE -->
68+
<!-- END MUNGE: EXAMPLE pod.yaml -->
6969
7070
You can see your cluster's pods:
7171
@@ -117,7 +117,7 @@ spec:
117117
```
118118
119119
[Download example](replication.yaml)
120-
<!-- END MUNGE: EXAMPLE -->
120+
<!-- END MUNGE: EXAMPLE replication.yaml -->
121121
122122
To delete the replication controller (and the pods it created):
123123

docs/user-guide/walkthrough/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ spec:
166166
```
167167
168168
[Download example](pod-redis.yaml)
169-
<!-- END MUNGE: EXAMPLE -->
169+
<!-- END MUNGE: EXAMPLE pod-redis.yaml -->
170170
171171
Notes:
172172
- The volume mount name is a reference to a specific empty dir volume.

docs/user-guide/walkthrough/k8s201.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ spec:
8787
```
8888
8989
[Download example](pod-nginx-with-label.yaml)
90-
<!-- END MUNGE: EXAMPLE -->
90+
<!-- END MUNGE: EXAMPLE pod-nginx-with-label.yaml -->
9191
9292
Create the labeled pod ([pod-nginx-with-label.yaml](pod-nginx-with-label.yaml)):
9393
@@ -143,7 +143,7 @@ spec:
143143
```
144144
145145
[Download example](replication-controller.yaml)
146-
<!-- END MUNGE: EXAMPLE -->
146+
<!-- END MUNGE: EXAMPLE replication-controller.yaml -->
147147
148148
#### Replication Controller Management
149149
@@ -196,7 +196,7 @@ spec:
196196
```
197197
198198
[Download example](service.yaml)
199-
<!-- END MUNGE: EXAMPLE -->
199+
<!-- END MUNGE: EXAMPLE service.yaml -->
200200
201201
#### Service Management
202202
@@ -312,7 +312,7 @@ spec:
312312
```
313313
314314
[Download example](pod-with-http-healthcheck.yaml)
315-
<!-- END MUNGE: EXAMPLE -->
315+
<!-- END MUNGE: EXAMPLE pod-with-http-healthcheck.yaml -->
316316
317317
For more information about health checking, see [Container Probes](../pod-states.md#container-probes).
318318

examples/cassandra/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ spec:
101101
```
102102
103103
[Download example](cassandra-controller.yaml)
104-
<!-- END MUNGE: EXAMPLE -->
104+
<!-- END MUNGE: EXAMPLE cassandra-controller.yaml -->
105105
106106
There are a few things to note in this description. First is that we are running the ```kubernetes/cassandra``` image. This is a standard Cassandra installation on top of Debian. However it also adds a custom [```SeedProvider```](https://svn.apache.org/repos/asf/cassandra/trunk/src/java/org/apache/cassandra/locator/SeedProvider.java) to Cassandra. In Cassandra, a ```SeedProvider``` bootstraps the gossip protocol that Cassandra uses to find other nodes. The ```KubernetesSeedProvider``` discovers the Kubernetes API Server using the built in Kubernetes discovery service, and then uses the Kubernetes API to find new nodes (more on this later)
107107

@@ -132,7 +132,7 @@ spec:
132132
```
133133

134134
[Download example](cassandra-service.yaml)
135-
<!-- END MUNGE: EXAMPLE -->
135+
<!-- END MUNGE: EXAMPLE cassandra-service.yaml -->
136136

137137
The important thing to note here is the ```selector```. It is a query over labels, that identifies the set of _Pods_ contained by the _Service_. In this case the selector is ```name=cassandra```. If you look back at the Pod specification above, you'll see that the pod has the corresponding label, so it will be selected for membership in this Service.
138138

@@ -242,7 +242,7 @@ spec:
242242
```
243243

244244
[Download example](cassandra-controller.yaml)
245-
<!-- END MUNGE: EXAMPLE -->
245+
<!-- END MUNGE: EXAMPLE cassandra-controller.yaml -->
246246

247247
Most of this replication controller definition is identical to the Cassandra pod definition above, it simply gives the resplication controller a recipe to use when it creates new Cassandra pods. The other differentiating parts are the ```selector``` attribute which contains the controller's selector query, and the ```replicas``` attribute which specifies the desired number of replicas, in this case 1.
248248

examples/celery-rabbitmq/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ spec:
8282
```
8383
8484
[Download example](rabbitmq-service.yaml)
85-
<!-- END MUNGE: EXAMPLE -->
85+
<!-- END MUNGE: EXAMPLE rabbitmq-service.yaml -->
8686
8787
To start the service, run:
8888
@@ -127,7 +127,7 @@ spec:
127127
```
128128
129129
[Download example](rabbitmq-controller.yaml)
130-
<!-- END MUNGE: EXAMPLE -->
130+
<!-- END MUNGE: EXAMPLE rabbitmq-controller.yaml -->
131131
132132
Running `$ kubectl create -f examples/celery-rabbitmq/rabbitmq-controller.yaml` brings up a replication controller that ensures one pod exists which is running a RabbitMQ instance.
133133

@@ -168,7 +168,7 @@ spec:
168168
```
169169

170170
[Download example](celery-controller.yaml)
171-
<!-- END MUNGE: EXAMPLE -->
171+
<!-- END MUNGE: EXAMPLE celery-controller.yaml -->
172172

173173
There are several things to point out here...
174174

@@ -239,7 +239,7 @@ spec:
239239
```
240240

241241
[Download example](flower-service.yaml)
242-
<!-- END MUNGE: EXAMPLE -->
242+
<!-- END MUNGE: EXAMPLE flower-service.yaml -->
243243

244244
It is marked as external (LoadBalanced). However on many platforms you will have to add an explicit firewall rule to open port 5555.
245245
On GCE this can be done with:
@@ -280,7 +280,7 @@ spec:
280280
```
281281

282282
[Download example](flower-controller.yaml)
283-
<!-- END MUNGE: EXAMPLE -->
283+
<!-- END MUNGE: EXAMPLE flower-controller.yaml -->
284284

285285
This will bring up a new pod with Flower installed and port 5555 (Flower's default port) exposed through the service endpoint. This image uses the following command to start Flower:
286286

0 commit comments

Comments
 (0)