A Go tool to generate Markdown documentation from Go structs, designed for documenting Kubernetes operator APIs.
Inspired by crd-ref-docs, this tool analyzes your Go files containing Kubernetes API type definitions and generates complete, structured Markdown documentation.
- ✅ Automatic analysis of Go structs in a directory
- ✅ Extraction of documentation comments
- ✅ Support for kubebuilder markers (
+kubebuilder:validation:Pattern,+kubebuilder:default, etc.) - ✅ Generation of links to Kubernetes documentation
- ✅ Support for Kubernetes resource types (CRDs)
- ✅ Flexible configuration via YAML file
- ✅ Single output or per-API-group output mode
go get -u github.com/golgoth31/api2mdOr clone the repository and build:
git clone https://github.com/golgoth31/api2md.git
cd api2md
go build -o api2mdapi2md \
--source-path=./api \
--config=config.yaml \
--output-path=api.mdapi2md \
--source-path=./api \
--config=config.yaml \
--output-path=./docs \
--output-mode=group--source-path: Path to the directory containing Go files with API definitions (required)--config: Path to the YAML configuration file (default:config.yaml)--output-path: Path to the output Markdown file or directory (default:api.md)--output-mode: Output mode:single(one file) orgroup(one file per group) (default:single)
Create a config.yaml file to configure the tool behavior:
processor:
# Group versions to ignore
ignoreGroupVersions:
- "kubecloudscaler.cloud/v1alpha1"
- "kubecloudscaler.cloud/v1alpha2"
# Types to ignore (RE2 regular expressions)
ignoreTypes:
- "(K8s|Gcp)List$"
# Fields to ignore (RE2 regular expressions)
ignoreFields:
- "status$"
- "TypeMeta$"
render:
# Kubernetes version for generating documentation links
kubernetesVersion: 1.33ignoreGroupVersions: List of group versions to exclude from documentationignoreTypes: List of regular expressions (RE2) to exclude certain typesignoreFields: List of regular expressions (RE2) to exclude certain fields
kubernetesVersion: Kubernetes version used to generate links to the official documentation
The tool generates Markdown documentation with:
- Package index: List of all analyzed packages
- Package documentation: Description and types for each package
- Resource types: Identified Kubernetes types (with
+kubebuilder:object:root=true) - Data types: All other types used in the APIs
- Field tables: For each type, a table with:
- Field name (JSON tag)
- Type
- Description (from comments)
- Default value (from
+kubebuilder:default) - Validation rules (Pattern, Enum, etc.)
- "Appears in" links: Types that reference this type
# API Reference
## Packages
- [kubecloudscaler.cloud/common](#kubecloudscalercloudcommon)
- [kubecloudscaler.cloud/v1alpha3](#kubecloudscalercloudv1alpha3)
## kubecloudscaler.cloud/common
Package common contains shared API Schema definitions...
#### ScalerPeriod
ScalerPeriod defines a scaling period with time constraints...
| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `type` _string_ | | | Enum: [down up] <br /> |
| `time` _[TimePeriod](#timeperiod)_ | | | |
| `minReplicas` _integer_ | Minimum replicas | | |api2md/
├── main.go # Entry point
├── go.mod # Go dependencies
├── config.yaml # Default configuration
├── internal/
│ ├── config/ # Configuration handling
│ ├── parser/ # Go file parsing
│ └── generator/ # Markdown generation
└── tests_data/ # Test data
├── input/ # Sample Go files
└── output/ # Expected output samples
- Parsing: The tool uses
go/parserandgo/astto analyze Go files - Extraction: It extracts structs, their fields, comments, and kubebuilder markers
- Filtering: Ignored types and fields are filtered according to the configuration
- Generation: Markdown documentation is generated from the collected information
+kubebuilder:object:root=true: Identifies a type as a Kubernetes resource+kubebuilder:validation:Pattern=...: Pattern validation rule+kubebuilder:validation:Enum=...: Enumeration validation rule+kubebuilder:default:=...: Default value+optional: Optional field+required: Required field
- Go 1.21 or higher
go test ./...Contributions are welcome! Feel free to open an issue or a pull request.
Apache License 2.0
This tool is inspired by crd-ref-docs by Elastic, but is an independent implementation tailored to the specific needs of Kubernetes API documentation.