Skip to content

Commit a8c4ecf

Browse files
authored
docs: SDK documentation cleanup and examples fix (#753)
* feat(fn): add --help, --doc, and standalone file mode to AsMain 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> * chore: apply go fix modernization 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> * chore: gitignore rapid testdata failure files Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech> * fix: CI drift — use slices.Contains, fix file permissions - 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> * fix: use <- All existing tests continue tomdtogo--> as end marker (matches 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> * fix: skip single-dash flags in file mode arg parsing 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> * docs: SDK documentation cleanup and examples fix - 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> * docs: address PR review comments for text improvements Apply text suggestions from mpgreaves review: - Expand contractions (we'd, you'll, shouldn't, it's, you've, doesn't) - Add missing periods to sentence fragments - Improve sentence structure and clarity - Use more formal phrasing in documentation Assisted-by: Kiro:Auto [code-editing] Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech> --------- Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
1 parent c3fd4dc commit a8c4ecf

43 files changed

Lines changed: 1069 additions & 2840 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ ts/create-kpt-functions/bin
1212
# rapid property-based testing failure reproductions
1313
**/testdata/rapid/
1414

15+
# compiled binaries
16+
go/get-started/get-started
17+

CONTRIBUTING.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Contributing to krm-functions-sdk
2+
3+
We would love to accept your contributions to this project. There are just a few
4+
small guidelines you need to follow.
5+
6+
## Developer Certificate of Origin (DCO)
7+
8+
Contributors to this project should state that they agree with the terms published
9+
at https://developercertificate.org/ for their contribution. To do this when
10+
creating a commit with the Git CLI, a sign-off can be added with
11+
[the -s option](https://git-scm.com/docs/git-commit#git-commit--s). The sign-off
12+
is stored as part of the commit message itself.
13+
14+
## Copyright notices
15+
16+
All files should have the copyright notice.
17+
```
18+
// Copyright 2026 The kpt Authors
19+
//
20+
// Licensed under the Apache License, Version 2.0 (the "License");
21+
// you may not use this file except in compliance with the License.
22+
// You may obtain a copy of the License at
23+
//
24+
// http://www.apache.org/licenses/LICENSE-2.0
25+
//
26+
// Unless required by applicable law or agreed to in writing, software
27+
// distributed under the License is distributed on an "AS IS" BASIS,
28+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+
// See the License for the specific language governing permissions and
30+
// limitations under the License.
31+
```
32+
33+
If the file has never been modified: use the creation year only.
34+
35+
* Example: `Copyright 2026 The kpt Authors`
36+
37+
If the file has been modified: use a year range from creation to last modification.
38+
39+
* Example: `Copyright 2024-2026 The kpt Authors`
40+
41+
## Building and Testing
42+
43+
The SDK uses a Makefile-based workflow. From the repository root:
44+
45+
```bash
46+
# Run all checks (fix, vet, fmt, test, lint)
47+
make go
48+
49+
# Run only tests
50+
cd go && make test
51+
52+
# Run only linting
53+
cd go && make lint
54+
55+
# Tidy all go.mod files
56+
make tidy
57+
```
58+
59+
The CI script (`hack/ci-validate-go.sh`) runs `make go` and then checks that no
60+
files have been modified. If the CI script fails with "files are not to date", run `make go`
61+
locally and commit the changes.
62+
63+
## Code reviews
64+
65+
All submissions, including submissions by project members, require review. We
66+
use GitHub pull requests for this purpose. Consult [GitHub Help] for more
67+
information on using pull requests.
68+
69+
Process for code reviews. Before requesting human review, a PR must:
70+
71+
* All tests passing
72+
* All linting passing
73+
* Meeting project code quality requirements, including passing all configured
74+
static analysis and not reducing automated test coverage
75+
* The comments from the first run of automatically generated comments (AI
76+
generated comments, bot generated comments, etc.) of the PR are addressed
77+
(addressing further re-runs of AI are optional)
78+
* If it is not possible to resolve an automatic comment, please add a sub-comment
79+
indicating why the automated comment cannot be resolved or ask for help in
80+
resolving the comment
81+
* The PR description states whether AI was used to help create the PR; if so, it
82+
lists the AI tools used and the areas where they were used
83+
84+
## Declare any use of AI
85+
86+
> In addition to the above, the use of AI in the creation of PRs is allowed, but
87+
> you must declare any use of AI and you must be able to explain the PR code
88+
> independently of any AI tools.
89+
90+
Update the PR description to state whether you used AI to help you create this
91+
PR; if so, list the AI tools you have used and in what areas.
92+
93+
For example:
94+
```text
95+
I have used AI in the creation of this PR.
96+
97+
I have used the following AI tools:
98+
- GitHub Copilot to analyse the code
99+
- Kiro to generate the implementation and tests
100+
```
101+
102+
### Attribute AI in the Git commit messages
103+
104+
Following the [guidance of the Linux kernel](https://docs.kernel.org/process/coding-assistants.html#attribution)
105+
we recommend the attribution of AI tools in the commit messages using the following format:
106+
107+
```text
108+
Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]
109+
```
110+
111+
## Community Guidelines
112+
113+
This project follows a [Code of Conduct].
114+
115+
## Community Discussion Groups
116+
117+
1. Join our [Slack channel](https://kubernetes.slack.com/channels/kpt)
118+
1. Join our [Discussions](https://github.com/kptdev/kpt/discussions)
119+
120+
## Governance
121+
122+
The governance of the kpt project is described in the
123+
[governance repo](https://github.com/kptdev/governance).
124+
125+
[GitHub Help]: https://help.github.com/articles/about-pull-requests/
126+
[Code of Conduct]: code-of-conduct.md

README.md

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,111 @@
11
# KRM Functions SDK
2+
23
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkptdev%2Fkrm-functions-sdk.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkptdev%2Fkrm-functions-sdk?ref=badge_shield)
34
[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/10658/badge)](https://www.bestpractices.dev/projects/10658)
45

5-
An opinionated Go SDK for implementing KRM functions.
6+
An opinionated Go SDK for implementing [KRM functions](https://kpt.dev/book/05-developing-functions/).
67

7-
## Documentation
8+
## Quick Start
89

9-
[Documentation](https://kpt.dev/book/05-developing-functions/#developing-in-go)
10+
A KRM function is a program that reads Kubernetes resources from STDIN, transforms or validates them, and writes the result to STDOUT. The SDK handles the I/O — you write the logic.
1011

11-
## Issues
12+
```go
13+
package main
14+
15+
import (
16+
"context"
17+
_ "embed"
18+
"os"
19+
20+
"github.com/kptdev/krm-functions-sdk/go/fn"
21+
)
22+
23+
//go:embed README.md
24+
var readme []byte
25+
26+
//go:embed metadata.yaml
27+
var metadata []byte
28+
29+
type SetLabels struct {
30+
Labels map[string]string `json:"labels,omitempty"`
31+
}
32+
33+
func (r *SetLabels) Run(ctx *fn.Context, functionConfig *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool {
34+
for _, obj := range items {
35+
for k, v := range r.Labels {
36+
obj.SetLabel(k, v)
37+
}
38+
}
39+
return true
40+
}
41+
42+
func main() {
43+
runner := fn.WithContext(context.Background(), &SetLabels{})
44+
if err := fn.AsMain(runner, fn.WithDocs(readme, metadata)); err != nil {
45+
os.Exit(1)
46+
}
47+
}
48+
```
49+
50+
A starter template is available at [`go/get-started/`](go/get-started/main.go). For the full walkthrough, see the [Tutorial](docs/tutorial.md).
51+
52+
## How It Works
1253

13-
Please [Open Issues](https://github.com/kptdev/kpt/issues) for this repo at [kptdev/kpt](https://github.com/kptdev/kpt/).
54+
`fn.AsMain` is the single entrypoint. It handles:
1455

15-
## Pull requests
56+
- **STDIN/STDOUT** (default) — reads a ResourceList, processes it, writes the result
57+
- **File mode** — pass file paths as positional args for local debugging
58+
- **`--help`** — prints human-readable docs from embedded README markers
59+
- **`--doc`** — outputs machine-readable JSON (consumed by `kpt fn doc` and catalog pipelines)
1660

17-
Open pull requests [here](https://github.com/kptdev/krm-functions-sdk/pulls).
61+
Register embedded documentation with `fn.WithDocs`:
1862

19-
## Discussions
63+
```go
64+
fn.AsMain(runner, fn.WithDocs(readme, metadata))
65+
```
2066

21-
Discussions are [here](https://github.com/kptdev/kpt/discussions).
67+
The SDK provides two interfaces for implementing functions:
68+
69+
| Interface | Use for | Can add/remove items? | Auto-parses config? |
70+
|---|---|---|---|
71+
| `fn.Runner` | Transformers, validators | No | Yes |
72+
| `fn.ResourceListProcessor` | Generators, complex functions | Yes | No |
73+
74+
See [Interfaces](docs/interfaces.md) for details and code examples.
75+
76+
## Documentation
77+
78+
- [API Reference](https://pkg.go.dev/github.com/kptdev/krm-functions-sdk/go/fn) — Go API docs
79+
- [Tutorial](docs/tutorial.md) — end-to-end function development
80+
- [Interfaces](docs/interfaces.md) — Runner vs ResourceListProcessor
81+
- [Testing](docs/testing.md) — golden test patterns
82+
- [Containerizing](docs/containerizing.md) — building and running function images
83+
84+
## Contributing
85+
86+
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on DCO sign-off, copyright headers, and code review process.
87+
88+
## Issues
89+
90+
Please [open issues](https://github.com/kptdev/kpt/issues) at [kptdev/kpt](https://github.com/kptdev/kpt/).
2291

2392
## License
2493

2594
Code is under the [Apache License 2.0](LICENSE), documentation is [CC BY 4.0](LICENSE-documentation).
2695

27-
2896
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkptdev%2Fkrm-functions-sdk.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkptdev%2Fkrm-functions-sdk?ref=badge_large)
2997

3098
## Governance
3199

32-
The governance of the kpt project and KRM Functiona Catalog are described in the
100+
The governance of the kpt project is described in the
33101
[governance repo](https://github.com/kptdev/governance).
34102

35103
## Code of Conduct
36104

37-
The kpt project and the KRM Functions Catalog are following the
105+
The kpt project follows the
38106
[CNCF Code of Conduct](https://github.com/cncf/foundation/blob/main/code-of-conduct.md).
39-
More information and links about the CNCF Code of Conduct are [here](code-of-conduct.md).
107+
More information is [here](code-of-conduct.md).
40108

41109
## CNCF
42110

43-
The kpt project including the KRM Functions Catalog is a [CNCF Sandbox](https://www.cncf.io/sandbox-projects/) project.
111+
The kpt project is a [CNCF Sandbox](https://www.cncf.io/sandbox-projects/) project.

docs/.nojekyll

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/api/.nojekyll

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/api/assets/highlight.css

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)