Skip to content

Commit d396a3e

Browse files
authored
Add get-started code as go SDK user guide sample code (#574)
1 parent 812496e commit d396a3e

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

go/get-started/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM golang:1.17-alpine3.15
2+
ENV CGO_ENABLED=0
3+
WORKDIR /go/src/
4+
COPY go.mod go.sum ./
5+
RUN go mod download
6+
COPY . .
7+
RUN go build -o /usr/local/bin/function ./
8+
FROM alpine:3.15
9+
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
10+
ENTRYPOINT ["function"]

go/get-started/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# some-function-name
2+
3+
Note: Please ensure you follow the [kpt doc style guide].
4+
5+
## Overview
6+
7+
<!--mdtogo:Short-->
8+
9+
Explain what this function does in one or two sentences.
10+
11+
<!--mdtogo-->
12+
13+
Describe why the user should care about this function.
14+
15+
What problem does it solve?
16+
17+
Provide some context (e.g. In the `gatekeeper` function, explain what's
18+
is `Gatekeeper` project)
19+
20+
[//]: <> (Note: The content between `<!--mdtogo:Short-->` and the following
21+
`<!--mdtogo-->` will be used as the short description for the command.)
22+
23+
<!--mdtogo:Long-->
24+
25+
## Usage
26+
27+
How do I use this function?
28+
29+
Explain what does it do in details.
30+
31+
Is this function meant to be used declaratively, imperatively or both?
32+
33+
### FunctionConfig
34+
35+
Omit this section, if the function doesn't support any `functionConfigs`.
36+
Otherwise, explain the function config and behavior for this function in detail.
37+
For each field in the function config, specify:
38+
39+
- An example value
40+
- Whether it is optional, and if so, the default value
41+
42+
If showing the function orchestrator (e.g. kpt) can make it clear about how to
43+
use the function, it's recommended to use it.
44+
45+
[//]: <> (Note: The content between `<!--mdtogo:Long-->` and the following
46+
`<!--mdtogo-->` will be used as the long description for the command.)
47+
48+
<!--mdtogo-->
49+
50+
## Examples
51+
52+
<!--mdtogo:Examples-->
53+
54+
Omit this section if you are providing complete example kpt packages which are
55+
linked from the catalog site.
56+
57+
Otherwise, provide inline examples in this section.
58+
59+
[//]: <> (Note: The content between `<!--mdtogo:Examples-->` and the following
60+
`<!--mdtogo-->` will be used as the examples for the command.)
61+
62+
<!--mdtogo-->
63+
64+
[kpt doc style guide]: https://github.com/GoogleContainerTools/kpt/blob/main/docs/style-guides/docs.md

go/get-started/data/resources.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: my-service
5+
spec:
6+
selector:
7+
app: MyApp
8+
ports:
9+
- protocol: TCP
10+
port: 80
11+
targetPort: 9376
12+
---
13+
apiVersion: apps/v1
14+
kind: Deployment
15+
metadata:
16+
name: nginx-deployment
17+
labels:
18+
app: nginx
19+
spec:
20+
replicas: 3
21+
selector:
22+
matchLabels:
23+
app: nginx
24+
template:
25+
metadata:
26+
labels:
27+
app: nginx
28+
spec:
29+
containers:
30+
- name: nginx
31+
image: nginx:1.14.2
32+
ports:
33+
- containerPort: 80

go/get-started/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
8+
)
9+
10+
// EDIT THIS FUNCTION!
11+
// This is the main logic. rl is the input `ResourceList` which has the `FunctionConfig` and `Items` fields.
12+
// You can modify the `Items` and add result information to `rl.Result`.
13+
func Run(rl *fn.ResourceList) (bool, error) {
14+
// Your code
15+
}
16+
17+
func main() {
18+
// CUSTOMIZE IF NEEDED
19+
// `AsMain` accepts a `ResourceListProcessor` interface.
20+
// You can explore other `ResourceListProcessor` structs in the SDK or define your own.
21+
if err := fn.AsMain(fn.ResourceListProcessorFunc(Run)); err != nil {
22+
os.Exit(1)
23+
}
24+
}

0 commit comments

Comments
 (0)