docs: SDK documentation cleanup and examples fix#753
Conversation
Extend fn.AsMain with functional options to support: - --help: renders human-readable docs from embedded README markers - --doc: outputs machine-readable JSON for catalog tooling - Standalone file mode: accepts positional file args for local debugging New fn.WithDocs(readme, meta) option registers embedded content. Existing callers with no options are unaffected (backward-compatible). Adds internal/docs package with: - ParseMarkers: extracts mdtogo Short/Long/Examples sections - ParseMetadata: parses metadata.yaml content - RenderHelp/RenderDoc: formats output for --help and --doc Includes property-based tests (rapid) and unit tests for all new code. Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Mechanical changes from 'go fix ./...':
- interface{} → any (Go 1.18+ type alias)
- reflect.Ptr → reflect.Pointer (deprecated constant)
- strings.Index+slice → strings.Cut (Go 1.18+ idiom)
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
- Replace manual loops with slices.Contains for --help/--doc checks (matches what 'go fix' produces) - Change test file permissions from 0644 to 0600 (gosec G306) Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
…tches catalog READMEs) The catalog functions use <- All existing tests continue tomdtogo--> (bare) as the section end marker, not <- All existing tests continue tomdtogo:End-->. Updated the parser and all tests to match the real-world README format. Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
d67a0af to
8f9b6a2
Compare
Go test runner passes -test.* flags via os.Args. The file mode arg parser must skip all flag-like arguments (starting with -), not just those starting with --. Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
- Delete legacy docs/ directory (TypeScript API docs, Jekyll artifacts) - Add docs/tutorial.md — end-to-end function development workflow - Add docs/interfaces.md — Runner vs ResourceListProcessor guide - Add docs/testing.md — golden test patterns with testhelpers - Add docs/containerizing.md — Dockerfile patterns and local testing - Add CONTRIBUTING.md aligned with kpt/catalog conventions - Rewrite README.md with better flow and accurate descriptions - Fix go/get-started/ to use //go:embed + fn.WithDocs - Fix go/fn/examples/ go.mod - Rename testdata/test1/ to testdata/noop-passthrough/ - Update gcr.io/kpt-fn/ references to ghcr.io/kptdev/krm-functions-catalog/ - Add replace directive comments in go.mod files - Update .gitignore for compiled binary Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
8f9b6a2 to
293bc1f
Compare
| functions use. It accepts `BUILDER_IMAGE` and `BASE_IMAGE` as build args. | ||
|
|
||
| For standalone functions or local development, use a multi-stage build with a | ||
| minimal base image. The function binary should be statically linked (no CGO) so |
There was a problem hiding this comment.
statically linked (no CGO) so -> statically linked (no CGO), so
| ``` | ||
|
|
||
| Note: the image must be published and accessible from the machine running | ||
| `kpt fn render`. For local development, build the image locally first and it |
There was a problem hiding this comment.
build the image locally first and it will be used
->
build the image locally first. It will be used
| # Interfaces | ||
|
|
||
| The SDK provides two interfaces for implementing KRM functions. Choose based on | ||
| what your function needs to do. |
There was a problem hiding this comment.
Choose based on what your function needs to do.
->
Choose according to your function requirements.
|
|
||
| Characteristics: | ||
| - The SDK automatically parses `functionConfig` into your struct's exported fields (via JSON tags). | ||
| - You can **modify** existing items but cannot add or remove items from the slice. |
There was a problem hiding this comment.
You can modify existing items but cannot add or remove
->
You can modify existing items, but you cannot add or remove
| // runner implements ResourceListProcessor and can be passed to fn.AsMain | ||
| ``` | ||
|
|
||
| This wrapper handles: |
There was a problem hiding this comment.
This wrapper handles: -> This wrapper handles the following:
|
|
||
| ## Golden Test Pattern | ||
|
|
||
| Golden tests compare function output against expected baseline files. This |
There was a problem hiding this comment.
Golden tests compare function output against expected baseline files.
->
Golden tests compare the function output against the expected baseline files.
| go test ./... | ||
| ``` | ||
|
|
||
| If the function output doesn't match `_expected.yaml`, the test fails with a |
There was a problem hiding this comment.
If the function output doesn't match -> If the function output does not match
| diffs in version control before committing. | ||
|
|
||
| **Caution:** `WRITE_GOLDEN_OUTPUT` accepts whatever the function currently | ||
| produces as "correct." If the function has a bug, you've just blessed buggy |
There was a problem hiding this comment.
you've just blessed -> you have just blessed
| - Keep test cases focused — one behavior per test directory. | ||
| - Use descriptive directory names (e.g., `empty-input`, `missing-namespace`, `multiple-resources`). | ||
| - The `_fnconfig.yaml` can be empty if your function doesn't require configuration. | ||
| - Golden tests catch unintentional formatting changes too, which helps maintain stable output. |
There was a problem hiding this comment.
Golden tests catch unintentional formatting changes too, which helps maintain stable output.
->
Golden tests also catch unintentional formatting changes. This helps to maintain a stable output.
| The e2e runner uses a different test structure (`.expected/` directories with | ||
| `config.yaml`, `diff.patch`, `results.yaml`) and is used by the | ||
| [krm-functions-catalog](https://github.com/kptdev/krm-functions-catalog) `tests/` | ||
| directory to validate functions running inside containers against `kpt fn render`. |
There was a problem hiding this comment.
to validate functions running inside containers -> to validate the functions running inside the containers
| # Tutorial: Developing a KRM Function | ||
|
|
||
| This tutorial walks through the end-to-end workflow for building a KRM function | ||
| using the Go SDK. By the end, you'll have a working function with embedded |
There was a problem hiding this comment.
By the end, you'll have -> By the end, you will have
| } | ||
| ``` | ||
|
|
||
| Here's a minimal function that sets labels on all resources: |
There was a problem hiding this comment.
Here's a minimal function that sets labels on all resources:
->
Here is a minimal function that sets labels on all the resources:
Summary
Documentation overhaul and examples fix for the SDK repository.
Changes
New documentation:
docs/tutorial.md— end-to-end function development workflowdocs/interfaces.md— Runner vs ResourceListProcessor with code examplesdocs/testing.md— golden test patterns, WRITE_GOLDEN_OUTPUT, e2e testing pointersdocs/containerizing.md— Dockerfile patterns, building, running locallyCONTRIBUTING.md— DCO, copyright, code review, AI disclosure (aligned with kpt/catalog)Removed:
docs/directory (TypeScript API docs, Jekyll artifacts)Fixed:
go/get-started/— uses//go:embed+fn.WithDocs, addedmetadata.yamlgo/fn/examples/— updated go.mod to compile against current SDKREADME.md— rewritten with better flow, accurate descriptions, pkg.go.dev linkOther:
testdata/test1/→testdata/noop-passthrough/gcr.io/kpt-fn/→ghcr.io/kptdev/krm-functions-catalog/across all filesDependencies
This PR is based on #752 (feat/asmain-help-doc-filemode) for the
fn.WithDocsAPI.AI Disclosure
I have used AI in the creation of this PR.
I have used the following AI tools: