Skip to content

Commit a054588

Browse files
committed
cleanup context
1 parent 45f90e1 commit a054588

File tree

4 files changed

+45
-65
lines changed

4 files changed

+45
-65
lines changed

go/fn/context.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,11 @@ package fn
1616

1717
import (
1818
"context"
19-
"time"
2019
)
2120

2221
var _ context.Context = &Context{}
2322

2423
// TODO: Have Context implement `context.Context`.
2524
type Context struct {
26-
ctx context.Context
27-
}
28-
29-
func (c *Context) Deadline() (deadline time.Time, ok bool) {
30-
return c.ctx.Deadline()
31-
}
32-
33-
func (c *Context) Done() <-chan struct{} {
34-
return c.ctx.Done()
35-
}
36-
func (c *Context) Err() error {
37-
return c.ctx.Err()
38-
}
39-
40-
func (c *Context) Value(key any) any {
41-
return c.ctx.Value(key)
25+
context.Context
4226
}

go/fn/examples/example_asmain_runner_test.go

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,31 +14,30 @@
1414
package example
1515

1616
import (
17-
"os"
17+
"os"
1818

19-
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
19+
"github.com/GoogleContainerTools/kpt-functions-sdk/go/fn"
2020
)
2121

2222
var _ fn.Runner = &SetLabels{}
2323

2424
type SetLabels struct {
25-
Labels map[string]string `json:"labels,omitempty"`
25+
Labels map[string]string `json:"labels,omitempty"`
2626
}
2727

2828
// Run is the main function logic.
2929
// `ctx` provides easy methods to add info, error or warning result to `ResourceList.Results`.
3030
// `items` is parsed from the STDIN "ResourceList.Items".
3131
// `functionConfig` is from the STDIN "ResourceList.FunctionConfig". The value has been assigned to the r.Labels
32-
//
33-
// the functionConfig is validated to have kind "SetLabels" and apiVersion "fn.kpt.dev/v1alpha1"
34-
func (r *SetLabels) Run(_ *fn.Context, _ *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool {
35-
for _, o := range items {
36-
for k, newLabel := range r.Labels {
37-
o.SetLabel(k, newLabel)
38-
}
39-
}
40-
results.Infof("updated labels")
41-
return true
32+
// the functionConfig is validated to have kind "SetLabels" and apiVersion "fn.kpt.dev/v1alpha1"
33+
func (r *SetLabels) Run(ctx *fn.Context, functionConfig *fn.KubeObject, items fn.KubeObjects, results *fn.Results) bool {
34+
for _, o := range items {
35+
for k, newLabel := range r.Labels {
36+
o.SetLabel(k, newLabel)
37+
}
38+
}
39+
results.Infof("updated labels", nil)
40+
return true
4241
}
4342

4443
// This example uses a SetLabels object, which implements `Runner.Run` methods.
@@ -51,34 +50,33 @@ func (r *SetLabels) Run(_ *fn.Context, _ *fn.KubeObject, items fn.KubeObjects, r
5150
// kind: Service
5251
// metadata:
5352
// name: example
54-
//
5553
// functionConfig:
56-
// apiVersion: fn.kpt.dev/v1alpha1
57-
// kind: SetLabels
58-
// metadata:
59-
// name: setlabel-fn-config
54+
// apiVersion: fn.kpt.dev/v1alpha1
55+
// kind: SetLabels
56+
// metadata:
57+
// name: setlabel-fn-config
6058
func Example_asMain() {
61-
file, _ := os.Open("./data/setlabels-resourcelist.yaml")
62-
defer file.Close()
63-
os.Stdin = file
59+
file, _ := os.Open("./data/setlabels-resourcelist.yaml")
60+
defer file.Close()
61+
os.Stdin = file
6462

65-
if err := fn.AsMain(&SetLabels{}); err != nil {
66-
os.Exit(1)
67-
}
68-
// Output:
69-
// apiVersion: config.kubernetes.io/v1
70-
// kind: ResourceList
71-
// items:
72-
// - apiVersion: v1
73-
// kind: Service
74-
// metadata:
75-
// name: example
76-
// functionConfig:
77-
// apiVersion: fn.kpt.dev/v1alpha1
78-
// kind: SetLabels
79-
// metadata:
80-
// name: setlabel-fn-config
81-
// results:
82-
// - message: updated labels
83-
// severity: info
63+
if err := fn.AsMain(&SetLabels{}); err != nil {
64+
os.Exit(1)
65+
}
66+
// Output:
67+
// apiVersion: config.kubernetes.io/v1
68+
// kind: ResourceList
69+
// items:
70+
// - apiVersion: v1
71+
// kind: Service
72+
// metadata:
73+
// name: example
74+
// functionConfig:
75+
// apiVersion: fn.kpt.dev/v1alpha1
76+
// kind: SetLabels
77+
// metadata:
78+
// name: setlabel-fn-config
79+
// results:
80+
// - message: updated labels
81+
// severity: info
8482
}

go/fn/functionrunner.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
package fn
1616

1717
type Runner interface {
18-
// Run passes the `functionConfig` and `items` from input ResourceList to allow you to do operations.
18+
// Run provides the entrypoint to allow you make changes to input `resourcelist.Items`
1919
// Args:
20-
// items: We intentionally do not allow you to add or delete KubeObject from "items", you can only modify
21-
// the items, because we expect you to use Runner only for transformer or validator functions.
20+
// items: The KRM resources in the form of a slice of KubeObject.
21+
// Note: You can only modify the existing items but not add or delete items.
22+
// We intentionally design the method this way to make the Runner be used as a Transformer or Validator, but not a Generator.
2223
// results: You can use `ErrorE` `Errorf` `Infof` `Warningf` `WarningE` to add user message to `Results`.
2324
// Returns:
2425
// return a boolean to tell whether the execution should be considered as PASS or FAIL. CLI like kpt will

go/fn/result.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type Field struct {
130130

131131
type Results []*Result
132132

133-
// ErrorE writes an Error level `result` to the results slice. It accepts arguments according to a format specifier.
133+
// Errorf writes an Error level `result` to the results slice. It accepts arguments according to a format specifier.
134134
// e.g.
135135
// results.Errorf("bad kind %v", "invalid")
136136
func (r *Results) Errorf(format string, a ...any) {
@@ -140,7 +140,6 @@ func (r *Results) Errorf(format string, a ...any) {
140140

141141
// ErrorE writes the `error` as an Error level `result` to the results slice.
142142
// e.g.
143-
//
144143
// err := error.New("test)
145144
// results.ErrorE(err)
146145
func (r *Results) ErrorE(err error) {
@@ -150,7 +149,6 @@ func (r *Results) ErrorE(err error) {
150149

151150
// Infof writes an Info level `result` to the results slice. It accepts arguments according to a format specifier.
152151
// e.g.
153-
//
154152
// results.Infof("update %v %q ", "ConfigMap", "kptfile.kpt.dev")
155153
func (r *Results) Infof(format string, a ...any) {
156154
infoResult := &Result{Severity: Info, Message: fmt.Sprintf(format, a...)}
@@ -159,7 +157,6 @@ func (r *Results) Infof(format string, a ...any) {
159157

160158
// Warningf writes a Warning level `result` to the results slice. It accepts arguments according to a format specifier.
161159
// e.g.
162-
//
163160
// results.Warningf("bad kind %q", "invalid")
164161
func (r *Results) Warningf(format string, a ...any) {
165162
warnResult := &Result{Severity: Warning, Message: fmt.Sprintf(format, a...)}

0 commit comments

Comments
 (0)