Skip to content
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

Add hack script to generare reference docs for the Antrea API #1507

Merged
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
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.io 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this text looks like something that is K8s API doc related.. im not sure how and where this is used though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the delay, what's your question?

I didn't write this file, I copied it from https://github.com/ahmetb/gen-crd-api-reference-docs as it seems to be how it's meant to be used. My understanding is that this specific part generates a generic description for the ObjectMeta fields in API resources. See https://antrea.io/docs/master/api-reference/.

<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 }}