Skip to content

Commit a0c97fa

Browse files
committed
example for new fnrunner and test infra
1 parent e63010a commit a0c97fa

File tree

7 files changed

+912
-21
lines changed

7 files changed

+912
-21
lines changed

go/get-started/go.mod

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module github.com/GoogleContainerTools/kpt-functions-sdk/go/get-started
2+
3+
go 1.19
4+
5+
require (
6+
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20221018174030-e63010a12b00 // indirect
7+
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20221018174030-e63010a12b00 // indirect
8+
github.com/PuerkitoBio/purell v1.2.0 // indirect
9+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
10+
github.com/davecgh/go-spew v1.1.1 // indirect
11+
github.com/go-errors/errors v1.4.2 // indirect
12+
github.com/go-logr/logr v1.2.3 // indirect
13+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
14+
github.com/go-openapi/jsonreference v0.20.0 // indirect
15+
github.com/go-openapi/swag v0.22.3 // indirect
16+
github.com/gogo/protobuf v1.3.2 // indirect
17+
github.com/golang/protobuf v1.5.2 // indirect
18+
github.com/google/gnostic v0.6.9 // indirect
19+
github.com/google/go-cmp v0.5.9 // indirect
20+
github.com/google/gofuzz v1.2.0 // indirect
21+
github.com/josharian/intern v1.0.0 // indirect
22+
github.com/mailru/easyjson v0.7.7 // indirect
23+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
24+
github.com/pkg/errors v0.9.1 // indirect
25+
github.com/pmezard/go-difflib v1.0.0 // indirect
26+
github.com/stretchr/testify v1.8.1 // indirect
27+
github.com/xlab/treeprint v1.1.0 // indirect
28+
golang.org/x/net v0.1.0 // indirect
29+
golang.org/x/sys v0.1.0 // indirect
30+
golang.org/x/text v0.4.0 // indirect
31+
google.golang.org/protobuf v1.28.1 // indirect
32+
gopkg.in/yaml.v2 v2.4.0 // indirect
33+
gopkg.in/yaml.v3 v3.0.1 // indirect
34+
k8s.io/apimachinery v0.25.3 // indirect
35+
k8s.io/klog/v2 v2.80.1 // indirect
36+
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
37+
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
38+
)

go/get-started/go.sum

Lines changed: 764 additions & 0 deletions
Large diffs are not rendered by default.

go/get-started/golden_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package main
15+
16+
import (
17+
"context"
18+
"testing"
19+
20+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
21+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/testhelpers"
22+
)
23+
24+
const TestDataPath = "testdata"
25+
26+
func TestFunction(t *testing.T) {
27+
// Read the `testdata/source` YAML krm resources
28+
fnRunner := fn.WithContext(context.TODO(), &YourFunction{})
29+
testhelpers.RunGoldenTests(t, TestDataPath, fnRunner)
30+
}

go/get-started/main.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,33 @@
1515
package main
1616

1717
import (
18-
"fmt"
18+
"context"
1919
"os"
2020

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

24-
// EDIT THIS FUNCTION!
25-
// This is the main logic. rl is the input `ResourceList` which has the `FunctionConfig` and `Items` fields.
26-
// You can modify the `Items` and add result information to `rl.Result`.
27-
func Run(rl *fn.ResourceList) (bool, error) {
28-
// Your code
24+
var _ fn.Runner = &YourFunction{}
25+
26+
// TODO: Change to your functionConfig "Kind" name.
27+
type YourFunction struct {
28+
FnConfigBool bool
29+
FnConfigInt int
30+
FnConfigFoo string
31+
}
32+
33+
// Run is the main function logic.
34+
// `items` is parsed from the STDIN "ResourceList.Items".
35+
// `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r attributes
36+
// `results` is the "ResourceList.Results" that you can write result info to.
37+
func (r *YourFunction) Run(ctx *fn.Context, functionConfig *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool {
38+
// TODO: Write your code.
39+
return true
2940
}
3041

3142
func main() {
32-
// CUSTOMIZE IF NEEDED
33-
// `AsMain` accepts a `ResourceListProcessor` interface.
34-
// You can explore other `ResourceListProcessor` structs in the SDK or define your own.
35-
if err := fn.AsMain(fn.ResourceListProcessorFunc(Run)); err != nil {
43+
runner := fn.WithContext(context.Background(), &YourFunction{})
44+
if err := fn.AsMain(runner); err != nil {
3645
os.Exit(1)
3746
}
3847
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: config.kubernetes.io/v1
16+
kind: ResourceList
17+
items:
18+
- apiVersion: apps/v1
19+
kind: Deployment
20+
metadata:
21+
name: nginx-deployment
22+
labels:
23+
app: nginx
24+
spec:
25+
replicas: 3
26+
selector:
27+
matchLabels:
28+
app: nginx
29+
template:
30+
metadata:
31+
labels:
32+
app: nginx
33+
spec:
34+
containers:
35+
- name: nginx
36+
image: nginx:1.14.2
37+
ports:
38+
- containerPort: 80
39+
- apiVersion: v1
40+
kind: Service
41+
metadata:
42+
name: test
43+
spec:
44+
selector:
45+
app: MyApp
46+
ports:
47+
- protocol: TCP
48+
port: 80
49+
targetPort: 9376
50+
functionConfig:
51+
apiVersion: fn.kpt.dev/v1alpha1
52+
kind: YourFunction
53+
metadata:
54+
name: test
55+
fnConfigFoo: example

go/get-started/Dockerfile renamed to go/get-started/testdata/test1/_fnconfig.yaml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang:1.17-alpine3.15
16-
ENV CGO_ENABLED=0
17-
WORKDIR /go/src/
18-
COPY go.mod go.sum ./
19-
RUN go mod download
20-
COPY . .
21-
RUN go build -o /usr/local/bin/function ./
22-
FROM alpine:3.15
23-
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
24-
ENTRYPOINT ["function"]
15+
apiVersion: fn.kpt.dev/v1alpha1
16+
kind: YourFunction
17+
metadata:
18+
name: test
19+
fnConfigFoo: example

go/get-started/data/resources.yaml renamed to go/get-started/testdata/test1/resources.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
apiVersion: v1
1616
kind: Service
1717
metadata:
18-
name: my-service
18+
name: test
1919
spec:
2020
selector:
2121
app: MyApp

0 commit comments

Comments
 (0)