Skip to content

Commit 4783749

Browse files
Display resources selected for function run (#2537)
* Display resources selected for function run * Update test * Update test
1 parent 13d5ad4 commit 4783749

File tree

7 files changed

+58
-13
lines changed

7 files changed

+58
-13
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
stdErr: |
2+
[RUNNING] "gcr.io/kpt-fn/set-namespace:v0.1.3" on 1 resource(s)
3+
[PASS] "gcr.io/kpt-fn/set-namespace:v0.1.3" in 0s

e2e/testdata/fn-eval/selectors/out-of-place-fnchain-unwrap/.expected/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# limitations under the License.
1414

1515
stdErr: |
16-
[RUNNING] "gcr.io/kpt-fn/set-namespace:v0.1.3"
16+
[RUNNING] "gcr.io/kpt-fn/set-namespace:v0.1.3" on 1 resource(s)
1717
[PASS] "gcr.io/kpt-fn/set-namespace:v0.1.3" in 0s
18-
[RUNNING] "gcr.io/kpt-fn/set-annotations:v0.1.3"
18+
[RUNNING] "gcr.io/kpt-fn/set-annotations:v0.1.3" on 1 resource(s)
1919
[PASS] "gcr.io/kpt-fn/set-annotations:v0.1.3" in 0s
2020
[RUNNING] "gcr.io/kpt-fn/set-labels:v0.1.3"
2121
[PASS] "gcr.io/kpt-fn/set-labels:v0.1.3" in 0s
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
stdErr: |
2+
Package "basicpipeline":
3+
[RUNNING] "gcr.io/kpt-fn/set-namespace:v0.1.3" on 1 resource(s)
4+
[PASS] "gcr.io/kpt-fn/set-namespace:v0.1.3" in 0s
5+
[RUNNING] "gcr.io/kpt-fn/set-labels:v0.1.4"
6+
[PASS] "gcr.io/kpt-fn/set-labels:v0.1.4" in 0s
7+
8+
Successfully executed 2 function(s) in 1 package(s).
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
stdErr: |
2+
Package "generator/db":
3+
[RUNNING] "gcr.io/kpt-fn/starlark:v0.2.1"
4+
[PASS] "gcr.io/kpt-fn/starlark:v0.2.1" in 0s
5+
[RUNNING] "gcr.io/kpt-fn/set-namespace:v0.1.3" on 1 resource(s)
6+
[PASS] "gcr.io/kpt-fn/set-namespace:v0.1.3" in 0s
7+
[RUNNING] "gcr.io/kpt-fn/set-labels:v0.1.4" on 1 resource(s)
8+
[PASS] "gcr.io/kpt-fn/set-labels:v0.1.4" in 0s
9+
10+
Package "generator":
11+
[RUNNING] "gcr.io/kpt-fn/set-namespace:v0.1.3" on 1 resource(s)
12+
[PASS] "gcr.io/kpt-fn/set-namespace:v0.1.3" in 0s
13+
[RUNNING] "gcr.io/kpt-fn/set-labels:v0.1.4" on 1 resource(s)
14+
[PASS] "gcr.io/kpt-fn/set-labels:v0.1.4" in 0s
15+
16+
Successfully executed 5 function(s) in 2 package(s).

internal/cmdrender/executor.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ func (pn *pkgNode) runMutators(ctx context.Context, hctx *hydrationContext, inpu
363363
return nil, err
364364
}
365365
}
366-
367366
// select the resources on which function should be applied
368367
selectedInput, err := fnruntime.SelectInput(input, selectors, &fnruntime.SelectionContext{RootPackagePath: hctx.root.pkg.UniquePath})
369368
if err != nil {
@@ -415,16 +414,20 @@ func (pn *pkgNode) runValidators(ctx context.Context, hctx *hydrationContext, in
415414

416415
for i := range pl.Validators {
417416
fn := pl.Validators[i]
418-
validator, err := fnruntime.NewContainerRunner(ctx, &fn, pn.pkg.UniquePath, hctx.fnResults, hctx.imagePullPolicy)
419-
if err != nil {
420-
return err
421-
}
422417
// validators are run on a copy of mutated resources to ensure
423418
// resources are not mutated.
424419
selectedResources, err := fnruntime.SelectInput(input, fn.Selectors, &fnruntime.SelectionContext{RootPackagePath: hctx.root.pkg.UniquePath})
425420
if err != nil {
426421
return err
427422
}
423+
displayResourceCount := false
424+
if len(fn.Selectors) > 0 {
425+
displayResourceCount = true
426+
}
427+
validator, err := fnruntime.NewContainerRunner(ctx, &fn, pn.pkg.UniquePath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount)
428+
if err != nil {
429+
return err
430+
}
428431
if _, err = validator.Filter(cloneResources(selectedResources)); err != nil {
429432
return err
430433
}
@@ -515,7 +518,11 @@ func fnChain(ctx context.Context, hctx *hydrationContext, pkgPath types.UniquePa
515518
for i := range fns {
516519
fn := fns[i]
517520
fn.Image = fnruntime.AddDefaultImagePathPrefix(fn.Image)
518-
r, err := fnruntime.NewContainerRunner(ctx, &fn, pkgPath, hctx.fnResults, hctx.imagePullPolicy)
521+
displayResourceCount := false
522+
if len(fn.Selectors) > 0 {
523+
displayResourceCount = true
524+
}
525+
r, err := fnruntime.NewContainerRunner(ctx, &fn, pkgPath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount)
519526
if err != nil {
520527
return nil, err
521528
}

internal/fnruntime/runner.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343
func NewContainerRunner(
4444
ctx context.Context, f *kptfilev1.Function,
4545
pkgPath types.UniquePath, fnResults *fnresult.ResultList,
46-
imagePullPolicy ImagePullPolicy) (kio.Filter, error) {
46+
imagePullPolicy ImagePullPolicy, displayResourceCount bool) (kio.Filter, error) {
4747
config, err := newFnConfig(f, pkgPath)
4848
if err != nil {
4949
return nil, err
@@ -66,7 +66,7 @@ func NewContainerRunner(
6666
Run: cfn.Run,
6767
FunctionConfig: config,
6868
}
69-
return NewFunctionRunner(ctx, fltr, pkgPath, fnResult, fnResults, true)
69+
return NewFunctionRunner(ctx, fltr, pkgPath, fnResult, fnResults, true, displayResourceCount)
7070
}
7171

7272
// NewFunctionRunner returns a kio.Filter given a specification of a function
@@ -76,7 +76,8 @@ func NewFunctionRunner(ctx context.Context,
7676
pkgPath types.UniquePath,
7777
fnResult *fnresult.Result,
7878
fnResults *fnresult.ResultList,
79-
setPkgPathAnnotation bool) (kio.Filter, error) {
79+
setPkgPathAnnotation bool,
80+
displayResourceCount bool) (kio.Filter, error) {
8081
name := fnResult.Image
8182
if name == "" {
8283
name = fnResult.ExecPath
@@ -89,6 +90,7 @@ func NewFunctionRunner(ctx context.Context,
8990
fnResult: fnResult,
9091
fnResults: fnResults,
9192
setPkgPathAnnotation: setPkgPathAnnotation,
93+
displayResourceCount: displayResourceCount,
9294
}, nil
9395
}
9496

@@ -105,12 +107,17 @@ type FunctionRunner struct {
105107
// on resources that do not have it set. The resources generated by
106108
// functions do not have this annotation set.
107109
setPkgPathAnnotation bool
110+
displayResourceCount bool
108111
}
109112

110113
func (fr *FunctionRunner) Filter(input []*yaml.RNode) (output []*yaml.RNode, err error) {
111114
pr := printer.FromContextOrDie(fr.ctx)
112115
if !fr.disableCLIOutput {
113-
pr.Printf("[RUNNING] %q\n", fr.name)
116+
pr.Printf("[RUNNING] %q", fr.name)
117+
if fr.displayResourceCount {
118+
pr.Printf(" on %d resource(s)", len(input))
119+
}
120+
pr.Printf("\n")
114121
}
115122
t0 := time.Now()
116123
output, err = fr.do(input)

thirdparty/kyaml/runfn/runfn.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,11 @@ func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig
396396
DeferFailure: spec.DeferFailure,
397397
}
398398
fnResult.ExecPath = r.OriginalExec
399+
}
399400

401+
displayResourceCount := false
402+
if !r.Selector.IsEmpty() {
403+
displayResourceCount = true
400404
}
401-
return fnruntime.NewFunctionRunner(r.Ctx, fltr, "", fnResult, r.fnResults, false)
405+
return fnruntime.NewFunctionRunner(r.Ctx, fltr, "", fnResult, r.fnResults, false, displayResourceCount)
402406
}

0 commit comments

Comments
 (0)