Skip to content

Commit 8831621

Browse files
committed
cleanup context
1 parent 45f90e1 commit 8831621

File tree

2 files changed

+40
-58
lines changed

2 files changed

+40
-58
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
}

0 commit comments

Comments
 (0)