Skip to content

Commit 115464c

Browse files
authored
Merge pull request #135 from pwittrock/docs-gen
Make docs gen ordering deterministic
2 parents ac35586 + 9836a2c commit 115464c

25 files changed

+706
-86
lines changed

cmd/kubebuilder/docs/docs.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ var generateConfig bool
6161
var cleanup, verbose bool
6262
var outputDir string
6363
var copyright string
64+
var brodocs bool
6465

6566
func AddDocs(cmd *cobra.Command) {
6667
docsCmd.Flags().BoolVar(&cleanup, "cleanup", true, "If true, cleanup intermediary files")
6768
docsCmd.Flags().BoolVar(&verbose, "verbose", true, "If true, use verbose output")
6869
docsCmd.Flags().BoolVar(&generateConfig, "generate-config", true, "If true, generate the docs/reference/config.yaml.")
6970
docsCmd.Flags().StringVar(&outputDir, "output-dir", filepath.Join("docs", "reference"), "Build docs into this directory")
7071
docsCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
72+
docsCmd.Flags().BoolVar(&brodocs, "brodocs", true, "Run brodocs to generate html.")
7173
docsCmd.Flags().StringVar(&generatecmd.Docscopyright, "docs-copyright", "<a href=\"https://github.com/kubernetes/kubernetes\">Copyright 2018 The Kubernetes Authors.</a>", "html for the copyright text on the docs")
7274
docsCmd.Flags().StringVar(&generatecmd.Docstitle, "title", "API Reference", "title of the docs page")
7375
cmd.AddCommand(docsCmd)
@@ -154,25 +156,26 @@ func RunDocs(cmd *cobra.Command, args []string) {
154156
generatecmd.Codegenerators = []string{"apidocs"}
155157
generatecmd.RunGenerate(cmd, args)
156158

157-
// Run the docker command to build the docs
158-
c = exec.Command("docker", "run",
159-
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "includes"), "/source"),
160-
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "build"), "/build"),
161-
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "build"), "/build"),
162-
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir), "/manifest"),
163-
"gcr.io/kubebuilder/brodocs",
164-
)
165-
if verbose {
166-
log.Println(strings.Join(c.Args, " "))
167-
c.Stderr = os.Stderr
168-
c.Stdout = os.Stdout
169-
}
170-
err = c.Run()
171-
if err != nil {
172-
log.Fatalf("error: %v\n", err)
159+
if brodocs {
160+
// Run the docker command to build the docs
161+
c = exec.Command("docker", "run",
162+
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "includes"), "/source"),
163+
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "build"), "/build"),
164+
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "build"), "/build"),
165+
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir), "/manifest"),
166+
"gcr.io/kubebuilder/brodocs",
167+
)
168+
if verbose {
169+
log.Println(strings.Join(c.Args, " "))
170+
c.Stderr = os.Stderr
171+
c.Stdout = os.Stdout
172+
}
173+
err = c.Run()
174+
if err != nil {
175+
log.Fatalf("error: %v\n", err)
176+
}
177+
fmt.Printf("Reference docs written to %s\n", filepath.Join(outputDir, "build", "index.html"))
173178
}
174-
175-
fmt.Printf("Reference docs written to %s\n", filepath.Join(outputDir, "build", "index.html"))
176179
}
177180

178181
// Scaffolding file for writing the openapi generated structs to a swagger.json file

cmd/kubebuilder/docs/gen.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package docs
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"sort"
78
"strings"
89

910
"github.com/kubernetes-sigs/kubebuilder/cmd/internal/codegen/parse"
1011
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
1112
"k8s.io/gengo/args"
12-
"os"
1313
)
1414

1515
// CodeGenerator generates code for Kubernetes resources and controllers
@@ -36,7 +36,13 @@ func (g CodeGenerator) Execute(dir string) error {
3636
path := filepath.Join(dir, outputDir, "config.yaml")
3737

3838
args := ConfigArgs{}
39-
for group, kindversion := range p.ByGroupKindVersion {
39+
groups := []string{}
40+
for group, _ := range p.ByGroupKindVersion {
41+
groups = append(groups, group)
42+
}
43+
sort.Strings(groups)
44+
for _, group := range groups {
45+
kindversion := p.ByGroupKindVersion[group]
4046
args.Groups = append(args.Groups, strings.Title(group))
4147
c := Category{
4248
Name: strings.Title(group),

test.sh

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cd "$base_dir" || {
4040
exit 1
4141
}
4242

43-
k8s_version=1.10
43+
k8s_version=1.10.1
4444
goarch=amd64
4545
goos="unknown"
4646

@@ -75,6 +75,7 @@ tmp_root=/tmp
7575

7676
kb_root_dir=$tmp_root/kubebuilder
7777
kb_vendor_dir=$tmp_root/vendor
78+
kb_orig=$(pwd)
7879

7980
# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable
8081
# in your environment to any value:
@@ -160,10 +161,12 @@ function generate_crd_resources {
160161
kubebuilder create resource --group insect --version v1beta1 --kind Bee
161162

162163
header_text "editing generated files to simulate a user"
163-
sed -i pkg/apis/insect/v1beta1/bee_types.go -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|"
164+
sed -i -e "s|type Bee struct|// +kubebuilder:categories=foo,bar\ntype Bee struct|" pkg/apis/insect/v1beta1/bee_types.go
165+
sed -i -e "s|type BeeController struct {|// +kubebuilder:rbac:groups="",resources=pods,verbs=get;watch;list\ntype BeeController struct {|" pkg/controller/bee/controller.go
164166

165167
header_text "generating and testing CRD definition"
166168
kubebuilder create config --crds --output crd.yaml
169+
kubebuilder create config --controller-image myimage:v1 --name myextensionname --output install.yaml
167170

168171
# Test for the expected generated CRD definition
169172
#
@@ -211,6 +214,166 @@ status:
211214
EOF
212215
diff crd.yaml expected.yaml
213216

217+
cat << EOF > expected.yaml
218+
apiVersion: v1
219+
kind: Namespace
220+
metadata:
221+
creationTimestamp: null
222+
labels:
223+
api: myextensionname
224+
kubebuilder.k8s.io: $INJECT_KB_VERSION
225+
name: myextensionname-system
226+
spec: {}
227+
status: {}
228+
---
229+
apiVersion: rbac.authorization.k8s.io/v1
230+
kind: ClusterRole
231+
metadata:
232+
creationTimestamp: null
233+
labels:
234+
api: myextensionname
235+
kubebuilder.k8s.io: $INJECT_KB_VERSION
236+
name: myextensionname-role
237+
rules:
238+
- apiGroups:
239+
- ""
240+
resources:
241+
- pods
242+
verbs:
243+
- get
244+
- watch
245+
- list
246+
- apiGroups:
247+
- insect.sample.kubernetes.io
248+
resources:
249+
- '*'
250+
verbs:
251+
- '*'
252+
---
253+
apiVersion: rbac.authorization.k8s.io/v1
254+
kind: ClusterRoleBinding
255+
metadata:
256+
creationTimestamp: null
257+
labels:
258+
api: myextensionname
259+
kubebuilder.k8s.io: $INJECT_KB_VERSION
260+
name: myextensionname-rolebinding
261+
namespace: myextensionname-system
262+
roleRef:
263+
apiGroup: rbac.authorization.k8s.io
264+
kind: ClusterRole
265+
name: myextensionname-role
266+
subjects:
267+
- kind: ServiceAccount
268+
name: default
269+
namespace: myextensionname-system
270+
---
271+
apiVersion: apiextensions.k8s.io/v1beta1
272+
kind: CustomResourceDefinition
273+
metadata:
274+
creationTimestamp: null
275+
labels:
276+
api: myextensionname
277+
kubebuilder.k8s.io: $INJECT_KB_VERSION
278+
name: bees.insect.sample.kubernetes.io
279+
spec:
280+
group: insect.sample.kubernetes.io
281+
names:
282+
categories:
283+
- foo
284+
- bar
285+
kind: Bee
286+
plural: bees
287+
scope: Namespaced
288+
validation:
289+
openAPIV3Schema:
290+
properties:
291+
apiVersion:
292+
type: string
293+
kind:
294+
type: string
295+
metadata:
296+
type: object
297+
spec:
298+
type: object
299+
status:
300+
type: object
301+
type: object
302+
version: v1beta1
303+
status:
304+
acceptedNames:
305+
kind: ""
306+
plural: ""
307+
conditions: null
308+
---
309+
apiVersion: v1
310+
kind: Service
311+
metadata:
312+
creationTimestamp: null
313+
labels:
314+
api: myextensionname
315+
control-plane: controller-manager
316+
kubebuilder.k8s.io: $INJECT_KB_VERSION
317+
name: myextensionname-controller-manager-service
318+
namespace: myextensionname-system
319+
spec:
320+
clusterIP: None
321+
selector:
322+
api: myextensionname
323+
control-plane: controller-manager
324+
kubebuilder.k8s.io: $INJECT_KB_VERSION
325+
status:
326+
loadBalancer: {}
327+
---
328+
apiVersion: apps/v1
329+
kind: StatefulSet
330+
metadata:
331+
creationTimestamp: null
332+
labels:
333+
api: myextensionname
334+
control-plane: controller-manager
335+
kubebuilder.k8s.io: $INJECT_KB_VERSION
336+
name: myextensionname-controller-manager
337+
namespace: myextensionname-system
338+
spec:
339+
replicas: 1
340+
selector:
341+
matchLabels:
342+
api: myextensionname
343+
control-plane: controller-manager
344+
kubebuilder.k8s.io: $INJECT_KB_VERSION
345+
serviceName: myextensionname-controller-manager-service
346+
template:
347+
metadata:
348+
creationTimestamp: null
349+
labels:
350+
api: myextensionname
351+
control-plane: controller-manager
352+
kubebuilder.k8s.io: $INJECT_KB_VERSION
353+
spec:
354+
containers:
355+
- args:
356+
- --install-crds=false
357+
command:
358+
- /root/controller-manager
359+
image: myimage:v1
360+
name: controller-manager
361+
resources:
362+
limits:
363+
cpu: 100m
364+
memory: 30Mi
365+
requests:
366+
cpu: 100m
367+
memory: 20Mi
368+
terminationGracePeriodSeconds: 10
369+
updateStrategy: {}
370+
status:
371+
replicas: 0
372+
373+
EOF
374+
diff -B install.yaml expected.yaml
375+
376+
214377
kubebuilder create resource --group insect --version v1beta1 --kind Wasp
215378
kubebuilder create resource --group ant --version v1beta1 --kind Ant
216379
kubebuilder create config --crds --output crd.yaml
@@ -345,13 +508,22 @@ function run_dep_ensure {
345508
dep ensure
346509
}
347510

511+
function test_docs {
512+
header_text "building docs"
513+
kubebuilder docs --docs-copyright "Hello" --title "World" --cleanup=false --brodocs=false
514+
diff docs/reference/includes $kb_orig/test/docs/expected/includes
515+
diff docs/reference/manifest.json $kb_orig/test/docs/expected/manifest.json
516+
diff docs/reference/config.yaml $kb_orig/test/docs/expected/config.yaml
517+
}
518+
348519
prepare_staging_dir
349520
fetch_tools
350521
build_kb
351522
prepare_vendor_deps
352523
prepare_testdir_under_gopath
353524

354525
generate_crd_resources
526+
test_docs
355527
test_generated_controller
356528
run_dep_ensure
357529
# Run controller tests after running dep ensure because we want ensure code

test/docs/expected/config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
example_location: "examples"
2+
api_groups:
3+
- "Ant"
4+
5+
- "Insect"
6+
resource_categories:
7+
- name: "Ant"
8+
include: "ant"
9+
resources:
10+
- name: "Ant"
11+
version: "v1beta1"
12+
group: "ant"
13+
14+
- name: "Insect"
15+
include: "insect"
16+
resources:
17+
- name: "Bee"
18+
version: "v1beta1"
19+
group: "insect"
20+
- name: "Wasp"
21+
version: "v1beta1"
22+
group: "insect"

test/docs/expected/includes/_ant.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# <strong>Ant</strong>
3+
4+
------------
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# <strong>Field Definitions</strong>
3+
4+
------------
5+

0 commit comments

Comments
 (0)