Skip to content

Commit

Permalink
Generate APIs Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jun 8, 2023
1 parent 1670b64 commit ca5bd5d
Show file tree
Hide file tree
Showing 28 changed files with 4,389 additions and 43 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module sigs.k8s.io/kwok
go 1.20

require (
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0
github.com/blang/semver/v4 v4.0.0
github.com/compose-spec/compose-go v1.8.2
github.com/containerd/go-cni v1.1.9
Expand Down Expand Up @@ -89,6 +90,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/component-base v0.27.1 // indirect
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d // indirect
k8s.io/klog v0.2.0 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0 h1:+XfOU14S4bGuwyvCijJwhhBIjYN+YXS18jrCY2EzJaY=
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0/go.mod h1:TdjdkYhlOifCQWPs1UdTma97kQQMozf5h26hTuG70u8=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -166,6 +168,7 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -181,8 +184,10 @@ github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJf
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down Expand Up @@ -378,8 +383,11 @@ k8s.io/component-base v0.27.1 h1:kEB8p8lzi4gCs5f2SPU242vOumHJ6EOsOnDM3tTuDTM=
k8s.io/component-base v0.27.1/go.mod h1:UGEd8+gxE4YWoigz5/lb3af3Q24w98pDseXcXZjw+E0=
k8s.io/cri-api v0.27.1 h1:KWO+U8MfI9drXB/P4oU9VchaWYOlwDglJZVHWMpTT3Q=
k8s.io/cri-api v0.27.1/go.mod h1:+Ts/AVYbIo04S86XbTD73UPp/DkTiYxtsFeOFEu32L0=
k8s.io/gengo v0.0.0-20201203183100-97869a43a9d9/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d h1:U9tB195lKdzwqicbJvyJeOXV7Klv+wNAWENRnXEGi08=
k8s.io/gengo v0.0.0-20220902162205-c0856e24416d/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog v0.2.0 h1:0ElL0OHzF3N+OhoJTL0uca20SxtYt4X4+bzHeqrB83c=
k8s.io/klog v0.2.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
Expand Down
25 changes: 25 additions & 0 deletions hack/api_docs/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"hideMemberFields": [
"TypeMeta"
],
"hideTypePatterns": [
"ParseError$",
"List$",
"ReferencePolicy$"
],
"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/(api|apimachinery/pkg/apis)/",
"docsURLTemplate": "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#{{ lower .TypeIdentifier }}-{{ arrIndex .PackageSegments -1 }}-{{ arrIndex .PackageSegments -2 }}"
}
],
"typeDisplayNamePrefixOverrides": {
"k8s.io/api/": "Kubernetes ",
"k8s.io/apimachinery/pkg/apis/": "Kubernetes "
},
"markdownDisabled": false
}
38 changes: 38 additions & 0 deletions hack/api_docs/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/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

ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")/../.."

function gendoc() {
local confdir="${ROOT_DIR}/hack/api_docs"

go run github.com/ahmetb/gen-crd-api-reference-docs \
-template-dir "${confdir}" \
-config "${confdir}/config.json" \
"$@"
}

if [[ "$#" != "1" ]]; then
echo "usage: generate.sh OUTFILE"
exit 2
fi

gendoc \
-api-dir "sigs.k8s.io/kwok/pkg/apis/" \
-out-file "${1}"
46 changes: 46 additions & 0 deletions hack/api_docs/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>
<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") }}
<table>
{{ template "members" .Type }}
</table>
{{ end }}
</td>
</tr>
{{ end }}
{{ end }}

{{ end }}
73 changes: 73 additions & 0 deletions hack/api_docs/pkg.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{{ define "packages" }}

---
title: API reference
bookToc: false
---

{{ "\n" }}

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

{{ end }}

{{ range .packages }}
<h2 id="{{ packageAnchorID . }}">
{{ packageDisplayName . }}
<a href="#{{ packageAnchorID . }}"> #</a>
</h2>

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

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

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

{{ end }}

<h2 id="references">
References
<a href="#references"> #</a>
</h2>

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

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

<h3 id="{{ anchorIDForType . }}">
{{ .Name.Name }}
{{ if eq .Kind "Alias" }}(<code>{{ .Underlying }}</code> alias){{ end }}
<a href="#{{ anchorIDForType . }}"> #</a>
</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>

{{ with (constantsOfType .) }}
<table>
<thead>
<tr>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{{ range . }}
<tr>
<td><code>{{ typeDisplayName . }}</code></td>
<td>{{ safe (renderComments .CommentLines) }}</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }}

{{ if .Members }}
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{{ if not (typeReferences .) }}
<tr>
<td>
<code>apiVersion</code>
string
</td>
<td>
<code>
{{ apiGroup . }}
</code>
</td>
</tr>
<tr>
<td>
<code>kind</code>
string
</td>
<td><code>{{ .Name.Name }}</code></td>
</tr>
{{ end }}
{{ template "members" . }}
</tbody>
</table>
{{ end }}

{{ end }}
3 changes: 3 additions & 0 deletions hack/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ package tools
import (
// code-generator
_ "k8s.io/code-generator"

// gen-crd-api-reference-docs
_ "github.com/ahmetb/gen-crd-api-reference-docs"
)
5 changes: 5 additions & 0 deletions hack/update-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ if [[ "${UPDATE_CMD_DOCS:-true}" == "true" ]]; then
"${ROOT_DIR}"/hack/update-cmd-docs.sh || failed+=(cmd-docs)
fi

if [[ "${UPDATE_API_DOCS:-true}" == "true" ]]; then
echo "[*] Update api docs..."
"${ROOT_DIR}"/hack/update-api-docs.sh || failed+=(api-docs)
fi

if [[ "${UPDATE_SHELL_FORMAT:-true}" == "true" ]]; then
echo "[*] Update shell format..."
"${ROOT_DIR}"/hack/update-shell-format.sh || failed+=(shell-format)
Expand Down
29 changes: 29 additions & 0 deletions hack/update-api-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/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

ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")/.."

function check() {
echo "Update api docs"
./hack/api_docs/generate.sh site/content/en/docs/generated/apis.md
}

cd "${ROOT_DIR}"

check || exit 1
5 changes: 5 additions & 0 deletions hack/verify-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ if [[ "${VERIFY_CMD_DOCS:-true}" == "true" ]]; then
"${ROOT_DIR}"/hack/verify-cmd-docs.sh || failed+=(cmd-docs)
fi

if [[ "${VERIFY_API_DOCS:-true}" == "true" ]]; then
echo "[*] Verifying api docs..."
"${ROOT_DIR}"/hack/verify-api-docs.sh || failed+=(api-docs)
fi

if [[ "${VERIFY_YAMLLINT:-true}" == "true" ]]; then
echo "[*] Verifying YAML lint..."
"${ROOT_DIR}"/hack/verify-yamllint.sh || failed+=(yamllint)
Expand Down
Loading

0 comments on commit ca5bd5d

Please sign in to comment.