Skip to content

Commit d60b3c0

Browse files
committed
example for new fnrunner and test infra
1 parent 5fa523b commit d60b3c0

7 files changed

Lines changed: 899 additions & 12 deletions

File tree

go/get-started/go.mod

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module github.com/GoogleContainerTools/kpt-functions-sdk/go/get-started
2+
3+
go 1.19
4+
5+
require github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-00010101000000-000000000000
6+
7+
require (
8+
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
9+
github.com/PuerkitoBio/purell v1.1.1 // indirect
10+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/go-errors/errors v1.0.1 // indirect
13+
github.com/go-logr/logr v1.2.0 // indirect
14+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
15+
github.com/go-openapi/jsonreference v0.19.6 // indirect
16+
github.com/go-openapi/swag v0.21.1 // indirect
17+
github.com/gogo/protobuf v1.3.2 // indirect
18+
github.com/golang/protobuf v1.5.2 // indirect
19+
github.com/google/gnostic v0.5.7-v3refs // indirect
20+
github.com/google/go-cmp v0.5.9 // 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.0 // indirect
27+
github.com/xlab/treeprint v1.1.0 // indirect
28+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
29+
golang.org/x/text v0.3.7 // indirect
30+
google.golang.org/protobuf v1.28.0 // indirect
31+
gopkg.in/yaml.v2 v2.4.0 // indirect
32+
gopkg.in/yaml.v3 v3.0.1 // indirect
33+
k8s.io/apimachinery v0.24.0 // indirect
34+
k8s.io/klog/v2 v2.60.1 // indirect
35+
k8s.io/kube-openapi v0.0.0-20220401212409-b28bf2818661 // indirect
36+
sigs.k8s.io/kustomize/kyaml v0.13.7-0.20220418212550-9d5491c2e20c // indirect
37+
)

go/get-started/go.sum

Lines changed: 711 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: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,82 @@
11
package main
22

33
import (
4-
"fmt"
4+
"context"
55
"os"
66

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

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
10+
var _ fn.Runner = &YourFunction{}
11+
12+
type YourFunction struct {
13+
// TODO: Modify with your expected function config.
14+
// Note: The value will be parsed from the STDIN `ResourceList.FunctionConfig`. For example:
15+
/*
16+
apiVersion: config.kubernetes.io/v1
17+
kind: ResourceList
18+
functionConfig:
19+
apiVersion: fn.kpt.dev/v1alpha1
20+
kind: FunctionX
21+
metadata:
22+
...
23+
fnConfigBool: true
24+
fnConfigInt: 31
25+
fnConfigFoo: kpt
26+
items:
27+
- ...
28+
*/
29+
FnConfigBool bool
30+
FnConfigInt int
31+
FnConfigFoo string
32+
}
33+
34+
// Run is the main function logic.
35+
// `items` is parsed from the STDIN "ResourceList.Items".
36+
// `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r attributes
37+
// `results` is the "ResourceList.Results" that you can write result info to.
38+
func (r *YourFunction) Run(ctx *fn.Context, functionConfig *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool {
39+
// TODO: Write your code. Below is some sample code.
40+
for _, item := range items {
41+
// Example 1, update the `Deployment` name to the functionConfig's "FnConfigFoo" value.
42+
if item.GetKind() == "Deployment" {
43+
err := item.SetName(r.FnConfigFoo)
44+
if err != nil {
45+
results.ErrorE(err)
46+
return false
47+
}
48+
}
49+
// Example 2, update the `Service` name only if the functionConfig's namespace is example.
50+
// Note, the functionConfig.NestedString("fnConfigFoo") is the same as YourFunction.FnConfigFoo. One is more convenient to
51+
// use in different cases.
52+
if item.GetKind() == "Service" {
53+
spec := item.GetMap("spec")
54+
pods, found, err := spec.NestedSlice("ports")
55+
if err != nil {
56+
results.ErrorE(err)
57+
}
58+
if !found{
59+
continue
60+
}
61+
for _, pod := range pods {
62+
name, _, err := pod.NestedString("name")
63+
if err != nil {
64+
results.ErrorE(err)
65+
}
66+
if name != functionConfig.GetName() {
67+
pod.SetNestedString(r.FnConfigFoo, "name")
68+
}
69+
}
70+
}
71+
}
72+
results.Infof("function pass")
73+
return true
1574
}
1675

76+
1777
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 {
78+
runner := fn.WithContext(context.Background(), &YourFunction{})
79+
if err := fn.AsMain(runner); err != nil {
2280
os.Exit(1)
2381
}
24-
}
82+
}
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
@@ -1,7 +1,7 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
name: my-service
4+
name: test
55
spec:
66
selector:
77
app: MyApp

0 commit comments

Comments
 (0)