Skip to content

Commit

Permalink
Add get-started code as go SDK user guide sample code (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed May 14, 2022
1 parent 812496e commit d396a3e
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go/get-started/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:1.17-alpine3.15
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o /usr/local/bin/function ./
FROM alpine:3.15
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
ENTRYPOINT ["function"]
64 changes: 64 additions & 0 deletions go/get-started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# some-function-name

Note: Please ensure you follow the [kpt doc style guide].

## Overview

<!--mdtogo:Short-->

Explain what this function does in one or two sentences.

<!--mdtogo-->

Describe why the user should care about this function.

What problem does it solve?

Provide some context (e.g. In the `gatekeeper` function, explain what's
is `Gatekeeper` project)

[//]: <> (Note: The content between `<!--mdtogo:Short-->` and the following
`<!--mdtogo-->` will be used as the short description for the command.)

<!--mdtogo:Long-->

## Usage

How do I use this function?

Explain what does it do in details.

Is this function meant to be used declaratively, imperatively or both?

### FunctionConfig

Omit this section, if the function doesn't support any `functionConfigs`.
Otherwise, explain the function config and behavior for this function in detail.
For each field in the function config, specify:

- An example value
- Whether it is optional, and if so, the default value

If showing the function orchestrator (e.g. kpt) can make it clear about how to
use the function, it's recommended to use it.

[//]: <> (Note: The content between `<!--mdtogo:Long-->` and the following
`<!--mdtogo-->` will be used as the long description for the command.)

<!--mdtogo-->

## Examples

<!--mdtogo:Examples-->

Omit this section if you are providing complete example kpt packages which are
linked from the catalog site.

Otherwise, provide inline examples in this section.

[//]: <> (Note: The content between `<!--mdtogo:Examples-->` and the following
`<!--mdtogo-->` will be used as the examples for the command.)

<!--mdtogo-->

[kpt doc style guide]: https://github.com/GoogleContainerTools/kpt/blob/main/docs/style-guides/docs.md
33 changes: 33 additions & 0 deletions go/get-started/data/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
24 changes: 24 additions & 0 deletions go/get-started/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"os"

"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
)

// EDIT THIS FUNCTION!
// This is the main logic. rl is the input `ResourceList` which has the `FunctionConfig` and `Items` fields.
// You can modify the `Items` and add result information to `rl.Result`.
func Run(rl *fn.ResourceList) (bool, error) {
// Your code
}

func main() {
// CUSTOMIZE IF NEEDED
// `AsMain` accepts a `ResourceListProcessor` interface.
// You can explore other `ResourceListProcessor` structs in the SDK or define your own.
if err := fn.AsMain(fn.ResourceListProcessorFunc(Run)); err != nil {
os.Exit(1)
}
}

0 comments on commit d396a3e

Please sign in to comment.