Skip to content

Commit

Permalink
Add hack script to generare reference docs for the Antrea API
Browse files Browse the repository at this point in the history
Generated documentation is then included on the Antrea website.
The script relies on a slightly modified version of
https://github.com/ahmetb/gen-crd-api-reference-docs. It doesn't require
running an actual apiserver, instead it uses the Go source files
directly.
  • Loading branch information
antoninbas committed Nov 7, 2020
1 parent 96fc15c commit 1b9275a
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
32 changes: 32 additions & 0 deletions hack/api-reference/gen-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"hideMemberFields": [
"TypeMeta"
],
"hideTypePatterns": [
"ParseError$",
"List$"
],
"externalPackages": [
{
"typeMatchPrefix": "^k8s\\.io/apimachinery/pkg/apis/meta/v1\\.Duration$",
"docsURLTemplate": "https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#Duration"
},
{
"typeMatchPrefix": "^k8s\\.io/apimachinery/pkg/types\\.UID$",
"docsURLTemplate": "https://godoc.org/k8s.io/apimachinery/pkg/types#UID"
},
{
"typeMatchPrefix": "^k8s\\.io/apimachinery/pkg/util/intstr\\.IntOrString$",
"docsURLTemplate": "https://godoc.org/k8s.io/apimachinery/pkg/util/intstr#IntOrString"
},
{
"typeMatchPrefix": "^k8s\\.io/(api|apimachinery/pkg/apis)/",
"docsURLTemplate": "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#{{lower .TypeIdentifier}}-{{arrIndex .PackageSegments -1}}-{{arrIndex .PackageSegments -2}}"
}
],
"typeDisplayNamePrefixOverrides": {
"k8s.io/api/": "Kubernetes ",
"k8s.io/apimachinery/pkg/apis/": "Kubernetes "
},
"markdownDisabled": false
}
39 changes: 39 additions & 0 deletions hack/api-reference/generate-api-reference.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

# Copyright 2020 Antrea 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.

# This script is used to generate the reference docs for the Antrea API, which
# is included in the Antrea website.

set -eo pipefail

OUTPUT=api-reference.html

TMP_DIR=$(mktemp -d)

function exit_handler() {
rm -rf $TMP_DIR
}

trap exit_handler INT EXIT

git clone --branch antrea https://github.com/antoninbas/gen-crd-api-reference-docs.git $TMP_DIR/refdocs
cd $TMP_DIR/refdocs && go build -o gen && cd -

BIN=$TMP_DIR/refdocs/gen

$BIN -config gen-config.json -api-dir github.com/vmware-tanzu/antrea/pkg/apis -out-file $OUTPUT -template-dir template -skip-missing-api-version

echo "API reference doc generated as $OUTPUT"
48 changes: 48 additions & 0 deletions hack/api-reference/template/members.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{{ 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 }}
49 changes: 49 additions & 0 deletions hack/api-reference/template/pkg.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{ define "packages" }}

{{ with .packages}}
<p>Packages:</p>
<ul>
{{ range . }}
<li>
<a href="#{{- packageAnchorID . -}}">{{ packageDisplayName . }}</a>
</li>
{{ end }}
</ul>
{{ end}}

{{ range .packages }}
<h2 id="{{- packageAnchorID . -}}">
{{- packageDisplayName . -}}
</h2>

{{ with (index .GoPackages 0 )}}
{{ with .DocComments }}
<p>
{{ safe (renderComments .) }}
</p>
{{ end }}
{{ end }}

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

{{ range (visibleTypes (sortedTypes .Types))}}
{{ template "type" . }}
{{ end }}
<hr/>
{{ end }}

<p><em>
Generated with <code>gen-crd-api-reference-docs</code>
{{ with .gitCommit }} on git commit <code>{{ . }}</code>{{end}}.
</em></p>

{{ end }}
58 changes: 58 additions & 0 deletions hack/api-reference/template/type.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{ define "type" }}

<h3 id="{{ anchorIDForType . }}">
{{- .Name.Name }}
{{ if eq .Kind "Alias" }}(<code>{{.Underlying}}</code> alias)</p>{{ end -}}
</h3>
{{ with (typeReferences .) }}
<p>
(<em>Appears on:</em>
{{- $prev := "" -}}
{{- range . -}}
{{- if $prev -}}, {{ end -}}
{{ $prev = . }}
<a href="{{ linkForType . }}">{{ typeDisplayName . }}</a>
{{- end -}}
)
</p>
{{ end }}


<p>
{{ safe (renderComments .CommentLines) }}
</p>

{{ if .Members }}
<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>
{{ end }}

{{ end }}

0 comments on commit 1b9275a

Please sign in to comment.