Skip to content

✨ Generate reference documentation automatically from godocs in the API #1703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/v1alpha5/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Package v1alpha5 contains API Schema definitions for the v1alpha5 API group
+kubebuilder:object:generate=true
+groupName=infrastructure.cluster.x-k8s.io
*/

// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7
package v1alpha5
5 changes: 4 additions & 1 deletion api/v1alpha6/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Package v1alpha6 contains API Schema definitions for the v1alpha6 API group
+kubebuilder:object:generate=true
+groupName=infrastructure.cluster.x-k8s.io
*/

// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7
package v1alpha6
4 changes: 4 additions & 0 deletions api/v1alpha7/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Package v1alpha7 contains API Schema definitions for the v1alpha7 API group
+kubebuilder:object:generate=true
+groupName=infrastructure.cluster.x-k8s.io
*/

package v1alpha7
5 changes: 4 additions & 1 deletion docs/book/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ verify: generate ## Verify that dependent artifacts are up to date
fi

.PHONY: build
build: $(BOOK_SRCS) $(MDBOOK) $(MDBOOK_EMBED) $(MDBOOK_RELEASELINK) $(MDBOOK_TABULATE) ## Build the book
build: $(BOOK_SRCS) $(MDBOOK) $(MDBOOK_EMBED) $(MDBOOK_RELEASELINK) $(MDBOOK_TABULATE)
../../hack/generate-api-docs.sh
$(MDBOOK) build
rm -rf src/api src/SUMMARY.md
mv src/SUMMARY_ORIGINAL.md src/SUMMARY.md

.PHONY: serve
serve: $(MDBOOK) $(MDBOOK_EMBED) $(MDBOOK_RELEASELINK) $(MDBOOK_TABULATE) ## Run a local webserver with the compiled book
Expand Down
46 changes: 46 additions & 0 deletions hack/api/template/members.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{ define "members" }}
{{ range .Members }}
{{ if not (hiddenMember .)}}
<tr>
<td>
<code>{{ fieldName . }}</code><br>
<em>
{{ if linkForType .Type }}
<a href="{{ linkForType .Type }}">
{{ typeDisplayName .Type }}
</a>
{{ else }}
{{ typeDisplayName .Type }}
{{ end }}
</em>
</td>
<td>
{{ if fieldEmbedded . }}
<p>
(Members of <code>{{ fieldName . }}</code> are embedded into this type.)
</p>
{{ end}}

{{ if isOptionalMember .}}
<em>(Optional)</em>
{{ end }}

{{ safe (renderComments .CommentLines) }}

{{ if and (eq (.Type.Name.Name) "ObjectMeta") }}
Refer to the Kubernetes API documentation for the fields of the
<code>metadata</code> field.
{{ end }}

{{ if or (eq (fieldName .) "spec") }}
<br/>
<br/>
<table>
{{ template "members" .Type }}
</table>
{{ end }}
</td>
</tr>
{{ end }}
{{ end }}
{{ end }}
31 changes: 31 additions & 0 deletions hack/api/template/pkg.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{ define "packages" }}
<h1>API reference</h1>

{{ range .packages }}
{{ with (index .GoPackages 0 )}}
{{ with .DocComments }}
{{ safe (renderComments .) }}
{{ end }}
{{ end }}

Resource Types:

<ul class="simple">
{{- range (visibleTypes (sortedTypes .Types)) -}}
{{ if isExportedType . -}}
<li>
<a href="{{ linkForType . }}">{{ typeDisplayName . }}</a>
</li>
{{- end }}
{{- end -}}
</ul>

{{ range (visibleTypes (sortedTypes .Types))}}
{{ template "type" . }}
{{ end }}
{{ end }}

<div class="admonition note">
<p class="last">This page was automatically generated with <code>gen-crd-api-reference-docs</code></p>
</div>
{{ end }}
60 changes: 60 additions & 0 deletions hack/api/template/type.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{{ define "type" }}
<h3 id="{{ anchorIDForType . }}">
{{- .Name.Name }}
{{ if eq .Kind "Alias" }}(<code>{{.Underlying}}</code> alias){{ end -}}
</h3>

{{ with (typeReferences .) }}
<p>
(<em>Appears on:</em>
{{- $prev := "" -}}
{{- range . -}}
{{- if $prev -}}, {{ end -}}
{{ $prev = . }}
<a href="{{ linkForType . }}">{{ typeDisplayName . }}</a>
{{- end -}}
)
</p>
{{ end }}

{{ with .CommentLines }}
{{ safe (renderComments .) }}
{{ end }}

{{ if .Members }}
<div class="md-typeset__scrollwrap">
<div class="md-typeset__table">
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{{ if isExportedType . }}
<tr>
<td>
<code>apiVersion</code><br>
string</td>
<td>
<code>{{ apiGroup . }}</code>
</td>
</tr>
<tr>
<td>
<code>kind</code><br>
string
</td>
<td>
<code>{{ .Name.Name }}</code>
</td>
</tr>
{{ end }}
{{ template "members" . }}
</tbody>
</table>
</div>
</div>
{{ end }}
{{ end }}
51 changes: 51 additions & 0 deletions hack/generate-api-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# Copyright 2023 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

__dir__=$(realpath $(dirname $0))
source "${__dir__}/ensure-go.sh"
verify_go_version

go install github.com/ahmetb/gen-crd-api-reference-docs@latest

__root__=$(realpath "${__dir__}/..")
API_VERSIONS=($(ls "${__root__}/api"))

BOOK_DIR="${__root__}/docs/book"

SUMMARY_FILE="${BOOK_DIR}/src/SUMMARY.md"
API_DOC_DIR="${BOOK_DIR}/src/api"

ORIGINAL_SUMMARY_FILE="${BOOK_DIR}/src/SUMMARY_ORIGINAL.md"

cp "${SUMMARY_FILE}" "${ORIGINAL_SUMMARY_FILE}"

mkdir -p "${API_DOC_DIR}"

cd "${__root__}"
echo "- [Reference](./api/index.md)" >> "${SUMMARY_FILE}"
touch "${API_DOC_DIR}/index.md"

for api_version in "${API_VERSIONS[@]}"; do
gen-crd-api-reference-docs --api-dir="./api/${api_version}" --config=./hack/api/config.json --template-dir=./hack/api/template -out-file="${API_DOC_DIR}/${api_version}.md"
echo " - [${api_version}](./api/${api_version}.md)" >> "${SUMMARY_FILE}"
done

# rm -f "${SUMMARY_FILE}"
# mv "${ORIGINAL_SUMMARY_FILE}" "${SUMMARY_FILE}"