Skip to content

Commit 19f8004

Browse files
committed
simplify ImageResolveFunc
Signed-off-by: Mózes László Máté <laszlo.mozes@nokia.com>
1 parent 712ada1 commit 19f8004

7 files changed

Lines changed: 17 additions & 33 deletions

File tree

commands/fn/doc/cmdfndoc.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,12 @@ type Runner struct {
5858
Ctx context.Context
5959
}
6060

61-
func (r *Runner) runE(c *cobra.Command, _ []string) error {
61+
func (r *Runner) runE(_ *cobra.Command, _ []string) error {
6262
if r.Image == "" {
6363
return errors.New("image must be specified")
6464
}
65-
resolveFunc := (&runneroptions.RunnerOptions{}).ResolveToImageForCLIFunc(runneroptions.GHCRImagePrefix)
66-
image, err := resolveFunc(c.Context(), r.Image)
67-
if err != nil {
68-
return err
69-
}
65+
resolveFunc := runneroptions.ResolveToImageForCLIFunc(runneroptions.GHCRImagePrefix)
66+
image := resolveFunc(r.Image)
7067
var out, errout bytes.Buffer
7168
dockerRunArgs := []string{
7269
"run",

internal/fnruntime/runner.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ func NewRunner(
5858
return nil, err
5959
}
6060
if f.Image != "" {
61-
img, err := opts.ResolveToImage(ctx, f.Image)
62-
if err != nil {
63-
return nil, err
64-
}
61+
img := opts.ResolveToImage(f.Image)
6562
f.Image = img
6663
}
6764

internal/fnruntime/runner_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,7 @@ func TestRunnerOptions_InitDefaults(t *testing.T) {
646646
opts := &runneroptions.RunnerOptions{}
647647
opts.InitDefaults(tc.prefix)
648648

649-
result, err := opts.ResolveToImage(context.TODO(), fnName)
650-
651-
assert.NoError(t, err)
649+
result := opts.ResolveToImage(fnName)
652650
assert.Equal(t, getExpectedPrefix(tc.prefix)+fnName, result)
653651
})
654652
}

pkg/lib/runneroptions/runneroptions.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package runneroptions
1717

1818
import (
19-
"context"
2019
"fmt"
2120
"strings"
2221
)
@@ -27,7 +26,7 @@ const (
2726
)
2827

2928
// ImageResolveFunc is the type for a function that can resolve a partial image to a (more) fully-qualified name
30-
type ImageResolveFunc func(ctx context.Context, image string) (string, error)
29+
type ImageResolveFunc func(image string) string
3130

3231
type RunnerOptions struct {
3332
// ImagePullPolicy controls the image pulling behavior before running the container.
@@ -62,24 +61,24 @@ type RunnerOptions struct {
6261

6362
func (opts *RunnerOptions) InitDefaults(defaultImagePrefix string) {
6463
opts.ImagePullPolicy = IfNotPresentPull
65-
opts.ResolveToImage = opts.ResolveToImageForCLIFunc(defaultImagePrefix)
64+
opts.ResolveToImage = ResolveToImageForCLIFunc(defaultImagePrefix)
6665
}
6766

6867
// ResolveToImageForCLIFunc returns a func that converts the KRM function short path to the full image url.
6968
// If the function is a catalog function, it prepends `prefix`, e.g. "set-namespace:v0.1" --> prefix + "set-namespace:v0.1".
7069
// A "/" is appended to `prefix` if it is not an empty string and does not end with a "/".
71-
func (opts *RunnerOptions) ResolveToImageForCLIFunc(prefix string) func(_ context.Context, image string) (string, error) {
70+
func ResolveToImageForCLIFunc(prefix string) func(image string) string {
7271
prefix = strings.TrimSuffix(prefix, "/")
7372
if prefix == "" {
74-
return func(_ context.Context, image string) (string, error) {
75-
return image, nil
73+
return func(image string) string {
74+
return image
7675
}
7776
}
78-
return func(_ context.Context, image string) (string, error) {
77+
return func(image string) string {
7978
if !strings.Contains(image, "/") {
80-
return fmt.Sprintf("%s/%s", prefix, image), nil
79+
return fmt.Sprintf("%s/%s", prefix, image)
8180
}
82-
return image, nil
81+
return image
8382
}
8483
}
8584

pkg/test/runner/runner.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ func (r *Runner) runFnEval() error {
171171
return fmt.Errorf("failed to prepare package: %w", err)
172172
}
173173

174-
175174
err = r.runSetupScript(pkgPath)
176175
if err != nil {
177176
return err

thirdparty/cmdconfig/commands/cmdeval/cmdeval.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,14 @@ func (r *EvalFnRunner) preserveCommentsAndFieldOrder(kf *kptfile.KptFile) (*yaml
313313

314314
// getCLIFunctionConfig parses the commandline flags and arguments into explicit
315315
// function config
316-
func (r *EvalFnRunner) getCLIFunctionConfig(ctx context.Context, dataItems []string) (*yaml.RNode, error) {
316+
func (r *EvalFnRunner) getCLIFunctionConfig(dataItems []string) (*yaml.RNode, error) {
317317
if r.Image == "" && r.Exec == "" {
318318
return nil, nil
319319
}
320320

321321
// TODO: This probably doesn't belong here, but moving it changes the test output
322322
if r.Image != "" {
323-
img, err := r.RunnerOptions.ResolveToImage(ctx, r.Image)
324-
if err != nil {
325-
return nil, err
326-
}
323+
img := r.RunnerOptions.ResolveToImage(r.Image)
327324
r.Image = img
328325
}
329326

@@ -479,7 +476,7 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error {
479476
if len(dataItems) > 0 && r.FnConfigPath != "" {
480477
return fmt.Errorf("function arguments can only be specified without function config file")
481478
}
482-
fnConfig, err := r.getCLIFunctionConfig(c.Context(), dataItems)
479+
fnConfig, err := r.getCLIFunctionConfig(dataItems)
483480
if err != nil {
484481
return err
485482
}

thirdparty/kyaml/runfn/runfn.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,7 @@ func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig
355355
if spec.Container.Image != "" {
356356
fnResult.Image = spec.Container.Image
357357

358-
resolvedImage, err := r.RunnerOptions.ResolveToImage(context.TODO(), spec.Container.Image)
359-
if err != nil {
360-
return nil, err
361-
}
358+
resolvedImage := r.RunnerOptions.ResolveToImage(spec.Container.Image)
362359
// If AllowWasm is true, we try to use the image field as a wasm image.
363360
// TODO: we can be smarter here. If the image doesn't support wasm/js platform,
364361
// it should fallback to run it as container fn.

0 commit comments

Comments
 (0)