Skip to content

Commit 692f094

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

File tree

8 files changed

+905
-36
lines changed

8 files changed

+905
-36
lines changed

go/get-started/Dockerfile

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

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
27+
func TestFunction(t *testing.T) {
28+
// Read the `testdata/source` YAML krm resources
29+
fnRunner := fn.WithContext(context.TODO(), &YourFunction{})
30+
testhelpers.RunGoldenTests(t, TestDataPath, fnRunner)
31+
}

go/get-started/main.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,34 @@
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

42+
3143
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 {
44+
runner := fn.WithContext(context.Background(), &YourFunction{})
45+
if err := fn.AsMain(runner); err != nil {
3646
os.Exit(1)
3747
}
38-
}
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apiVersion: config.kubernetes.io/v1
2+
kind: ResourceList
3+
items:
4+
- apiVersion: apps/v1
5+
kind: Deployment
6+
metadata:
7+
name: example
8+
labels:
9+
app: nginx
10+
spec:
11+
replicas: 3
12+
selector:
13+
matchLabels:
14+
app: nginx
15+
template:
16+
metadata:
17+
labels:
18+
app: nginx
19+
spec:
20+
containers:
21+
- name: nginx
22+
image: nginx:1.14.2
23+
ports:
24+
- containerPort: 80
25+
- apiVersion: v1
26+
kind: Service
27+
metadata:
28+
name: test
29+
spec:
30+
selector:
31+
app: MyApp
32+
ports:
33+
- protocol: TCP
34+
port: 80
35+
targetPort: 9376
36+
name: example
37+
functionConfig:
38+
apiVersion: fn.kpt.dev/v1alpha1
39+
kind: YourFunction
40+
metadata:
41+
name: test
42+
fnConfigFoo: example
43+
results:
44+
- message: function pass
45+
severity: info
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: fn.kpt.dev/v1alpha1
2+
kind: YourFunction
3+
metadata:
4+
name: test
5+
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)