Skip to content

Commit

Permalink
Add make target to automatically generate API docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MirahImage committed Mar 22, 2021
1 parent ee547a3 commit d7848a6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ deploy-rbac:
manifests: install-tools
controller-gen $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Generate API reference documentation
api-reference:
crd-ref-docs \
--source-path ./api/v1alpha1 \
--config ./docs/api/autogen/config.yaml \
--templates-dir ./docs/api/autogen/templates \
--output-path ./docs/api/rabbitmq.com.ref.asciidoc \
--max-depth 30

# Run go fmt against code
fmt:
go fmt ./...
Expand All @@ -69,8 +78,8 @@ fmt:
vet:
go vet ./...

# Generate code
generate: install-tools
# Generate code & docs
generate: install-tools api-reference
controller-gen object:headerFile="hack/NOTICE.go.txt" paths="./..."

check-env-docker-credentials: check-env-registry-server
Expand Down
2 changes: 2 additions & 0 deletions docs/api/autogen/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
render:
kubernetesVersion: "1.20"
20 changes: 20 additions & 0 deletions docs/api/autogen/templates/gv_details.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{- define "gvDetails" -}}
{{- $gv := . -}}
[id="{{ asciidocGroupVersionID $gv | asciidocRenderAnchorID }}"]
== {{ $gv.GroupVersionString }}

{{ $gv.Doc }}

{{- if $gv.Kinds }}
.Resource Types
{{- range $gv.SortedKinds }}
- {{ $gv.TypeForKind . | asciidocRenderTypeLink }}
{{- end }}
{{ end }}

=== Definitions
{{ range $gv.SortedTypes }}
{{ template "type" . }}
{{ end }}

{{- end -}}
19 changes: 19 additions & 0 deletions docs/api/autogen/templates/gv_list.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- define "gvList" -}}
{{- $groupVersions := . -}}

// Generated documentation. Please do not edit.
:anchor_prefix: k8s-api

[id="{p}-api-reference"]
= API Reference

.Packages
{{- range $groupVersions }}
- {{ asciidocRenderGVLink . }}
{{- end }}

{{ range $groupVersions }}
{{ template "gvDetails" . }}
{{ end }}

{{- end -}}
35 changes: 35 additions & 0 deletions docs/api/autogen/templates/type.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{- define "type" -}}
{{- $type := . -}}
{{- if asciidocShouldRenderType $type -}}

[id="{{ asciidocTypeID $type | asciidocRenderAnchorID }}"]
==== {{ $type.Name }} {{ if $type.IsAlias }}({{ asciidocRenderTypeLink $type.UnderlyingType }}) {{ end }}

{{ $type.Doc }}

{{ if $type.References -}}
.Appears In:
****
{{- range $type.SortedReferences }}
- {{ asciidocRenderTypeLink . }}
{{- end }}
****
{{- end }}

{{ if $type.Members -}}
[cols="25a,75a", options="header"]
|===
| Field | Description
{{ if $type.GVK -}}
| *`apiVersion`* __string__ | `{{ $type.GVK.Group }}/{{ $type.GVK.Version }}`
| *`kind`* __string__ | `{{ $type.GVK.Kind }}`
{{ end -}}

{{ range $type.Members -}}
| *`{{ .Name }}`* __{{ asciidocRenderType .Type }}__ | {{ template "type_members" . }}
{{ end -}}
|===
{{ end -}}

{{- end -}}
{{- end -}}
8 changes: 8 additions & 0 deletions docs/api/autogen/templates/type_members.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- define "type_members" -}}
{{- $field := . -}}
{{- if eq $field.Name "metadata" -}}
Refer to Kubernetes API documentation for fields of `metadata`.
{{ else -}}
{{ $field.Doc }}
{{- end -}}
{{- end -}}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/rabbitmq/messaging-topology-operator
go 1.16

require (
github.com/elastic/crd-ref-docs v0.0.7
github.com/go-logr/logr v0.4.0
github.com/michaelklishin/rabbit-hole/v2 v2.6.0
github.com/onsi/ginkgo v1.15.2
Expand Down
1 change: 1 addition & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package tools

import (
_ "github.com/elastic/crd-ref-docs"
_ "github.com/onsi/ginkgo/ginkgo"
_ "sigs.k8s.io/controller-tools/cmd/controller-gen"
_ "sigs.k8s.io/kustomize/kustomize/v3"
Expand Down

0 comments on commit d7848a6

Please sign in to comment.