Skip to content

Commit

Permalink
feat: add CRD docs auto generator tooling (keptn#884)
Browse files Browse the repository at this point in the history
Signed-off-by: Amishakumari544 <[email protected]>
  • Loading branch information
mowies authored and Amishakumari544 committed Feb 27, 2023
1 parent 751ce0f commit 2071bf1
Show file tree
Hide file tree
Showing 17 changed files with 1,899 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
processor:
useRawDocstring: true
ignoreTypes: [ ]
ignoreFields:
- "status$"
- "TypeMeta$"

render:
kubernetesVersion: 1.24
62 changes: 62 additions & 0 deletions .github/scripts/generate-crd-docs/generate-crd-docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

# CRD docs auto generation script
#
# This script goes through all API definitions in the operator/apis folder
# and generates docs from code for each API group and version
#
# Inputs: None

# renovate: datasource=github-releases depName=elastic/crd-ref-docs
GENERATOR_VERSION=master
API_DOMAIN="keptn.sh"
API_ROOT='operator/apis/'
TEMPLATE_DIR='.github/scripts/generate-crd-docs/templates'
RENDERER='markdown'
RENDERER_CONFIG_FILE='.github/scripts/generate-crd-docs/crd-docs-generator-config.yaml'

echo "Checking if code generator tool is installed..."
test -s crd-ref-docs || go install github.com/elastic/crd-ref-docs@${GENERATOR_VERSION}

echo "Running CRD docs auto-generator..."

for api_group in "$API_ROOT"*; do
sanitized_api_group="${api_group#$API_ROOT}"
INDEX_PATH="./docs/content/en/docs/crd-ref/$sanitized_api_group/_index.md"

if [ ! -f "$INDEX_PATH" ]; then
echo "API group index file doesn't exist for group $sanitized_api_group. Creating it now..."
# Use sanitized_api_group and make first char uppercase
API_GROUP="$(tr '[:lower:]' '[:upper:]' <<< "${sanitized_api_group:0:1}")${sanitized_api_group:1}"
export API_GROUP
envsubst < './.github/scripts/generate-crd-docs/templates/index-template.md' > "$INDEX_PATH"
unset API_GROUP
fi

for api_version in "$api_group"/*; do
sanitized_api_version="${api_version#$API_ROOT$sanitized_api_group/}"

OUTPUT_PATH="./docs/content/en/docs/crd-ref/$sanitized_api_group/$sanitized_api_version"

echo "Arguments:"
echo "TEMPLATE_DIR: $TEMPLATE_DIR"
echo "API_ROOT: $API_ROOT"
echo "API_GROUP: $sanitized_api_group"
echo "API_VERSION: $sanitized_api_version"
echo "RENDERER: $RENDERER"
echo "RENDERER_CONFIG_FILE: $RENDERER_CONFIG_FILE"
echo "OUTPUT_PATH: $OUTPUT_PATH/_index.md"

echo "Creating docs folder $OUTPUT_PATH..."
mkdir -p "$OUTPUT_PATH"

echo "Generating CRD docs for $sanitized_api_group.$API_DOMAIN/$sanitized_api_version..."
crd-ref-docs \
--templates-dir "$TEMPLATE_DIR" \
--source-path="./$api_version" \
--renderer="$RENDERER" \
--config "$RENDERER_CONFIG_FILE" \
--output-path "$OUTPUT_PATH/_index.md"
echo "---------------------"
done
done
19 changes: 19 additions & 0 deletions .github/scripts/generate-crd-docs/templates/gv_details.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- define "gvDetails" -}}
{{- $gv := . -}}

## {{ $gv.GroupVersionString }}

{{ $gv.Doc }}

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

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

{{- end -}}
21 changes: 21 additions & 0 deletions .github/scripts/generate-crd-docs/templates/gv_list.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{- define "gvList" -}}
{{- $groupVersions := . -}}
{{- $groupVersion := index $groupVersions 0 -}}

---
title: {{ $groupVersion.Version }}
description: Reference information for {{ $groupVersion.GroupVersionString }}
---
<!-- markdownlint-disable -->

## Packages
{{- range $groupVersions }}
- {{ markdownRenderGVLink . }}
{{- end }}

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

{{- end -}}
<!-- markdownlint-enable -->
4 changes: 4 additions & 0 deletions .github/scripts/generate-crd-docs/templates/index-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: $API_GROUP API
description: Reference information about the $API_GROUP API
---
33 changes: 33 additions & 0 deletions .github/scripts/generate-crd-docs/templates/type.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{- define "type" -}}
{{- $type := . -}}
{{- if markdownShouldRenderType $type -}}

#### {{ $type.Name }}

{{ if $type.IsAlias }}_Underlying type:_ `{{ markdownRenderTypeLink $type.UnderlyingType }}`{{ end }}

{{ $type.Doc }}

{{ if $type.References -}}
_Appears in:_
{{- range $type.SortedReferences }}
- {{ markdownRenderTypeLink . }}
{{- end }}
{{- end }}

{{ if $type.Members -}}
| Field | Description |
| --- | --- |
{{ if $type.GVK -}}
| `apiVersion` _string_ | `{{ $type.GVK.Group }}/{{ $type.GVK.Version }}`
| `kind` _string_ | `{{ $type.GVK.Kind }}`
{{ end -}}

{{ range $type.Members -}}
| `{{ .Name }}` _{{ markdownRenderType .Type }}_ | {{ template "type_members" . }} |
{{ end -}}

{{ end -}}

{{- end -}}
{{- end -}}
8 changes: 8 additions & 0 deletions .github/scripts/generate-crd-docs/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 -}}
35 changes: 35 additions & 0 deletions .github/workflows/markdown-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:
paths:
- '**.md'

env:
GO_VERSION: "~1.19"

defaults:
run:
shell: bash
Expand Down Expand Up @@ -55,3 +58,35 @@ jobs:
- name: Lint Markdown files
run: make markdownlint

check-crd-docs:
name: Check CRD auto-generated docs
runs-on: ubuntu-22.04
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

- name: Copy old docs
run: |
cp -R ./docs/content/en/docs/crd-ref ./docs/content/en/docs/crd-ref-old
- name: Run docs generator
run: ./.github/scripts/generate-crd-docs/generate-crd-docs.sh

- name: Check if docs are up to date
run: |
if ! diff -rq ./docs/content/en/docs/crd-ref ./docs/content/en/docs/crd-ref-old &>/dev/null; then
echo "The CRD docs have changes that are not reflected in the docs pages. Please use ./.github/scripts/generate-crd-docs/generate-crd-docs.sh to re-generate the docs."
echo ""
echo "=========== Diff ==========="
diff -ru ./docs/content/en/docs/crd-ref ./docs/content/en/docs/crd-ref-old
exit 1
else
echo ""
echo "CRD docs are up to date!"
fi
2 changes: 1 addition & 1 deletion docs/content/en/docs/crd-ref/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: CRD Reference pages
title: API Reference
description: Reference information about the KLT CRDs
weight: 100
hidechildren: false # this flag hides all sub-pages in the sidebar-multicard.html
Expand Down
32 changes: 0 additions & 32 deletions docs/content/en/docs/crd-ref/crd-template.md

This file was deleted.

4 changes: 4 additions & 0 deletions docs/content/en/docs/crd-ref/lifecycle/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Lifecycle API
description: Reference information about the Lifecycle API
---
Loading

0 comments on commit 2071bf1

Please sign in to comment.