Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions commands/fn/doc/cmdfndoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,12 @@ type Runner struct {
Ctx context.Context
}

func (r *Runner) runE(c *cobra.Command, _ []string) error {
func (r *Runner) runE(_ *cobra.Command, _ []string) error {
if r.Image == "" {
return errors.New("image must be specified")
}
resolveFunc := (&runneroptions.RunnerOptions{}).ResolveToImageForCLIFunc(runneroptions.GHCRImagePrefix)
image, err := resolveFunc(c.Context(), r.Image)
if err != nil {
return err
}
resolveFunc := runneroptions.ResolveToImageForCLIFunc(runneroptions.GHCRImagePrefix)
image := resolveFunc(r.Image)
var out, errout bytes.Buffer
dockerRunArgs := []string{
"run",
Expand Down
5 changes: 1 addition & 4 deletions internal/fnruntime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ func NewRunner(
return nil, err
}
if f.Image != "" {
img, err := opts.ResolveToImage(ctx, f.Image)
if err != nil {
return nil, err
}
img := opts.ResolveToImage(f.Image)
f.Image = img
}

Expand Down
4 changes: 1 addition & 3 deletions internal/fnruntime/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,7 @@ func TestRunnerOptions_InitDefaults(t *testing.T) {
opts := &runneroptions.RunnerOptions{}
opts.InitDefaults(tc.prefix)

result, err := opts.ResolveToImage(context.TODO(), fnName)

assert.NoError(t, err)
result := opts.ResolveToImage(fnName)
assert.Equal(t, getExpectedPrefix(tc.prefix)+fnName, result)
})
}
Expand Down
19 changes: 9 additions & 10 deletions pkg/lib/runneroptions/runneroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package runneroptions

import (
"context"
"fmt"
"strings"
)
Expand All @@ -27,7 +26,7 @@ const (
)

// ImageResolveFunc is the type for a function that can resolve a partial image to a (more) fully-qualified name
type ImageResolveFunc func(ctx context.Context, image string) (string, error)
type ImageResolveFunc func(image string) string
Comment thread
CsatariGergely marked this conversation as resolved.

type RunnerOptions struct {
// ImagePullPolicy controls the image pulling behavior before running the container.
Expand Down Expand Up @@ -62,24 +61,24 @@ type RunnerOptions struct {

func (opts *RunnerOptions) InitDefaults(defaultImagePrefix string) {
opts.ImagePullPolicy = IfNotPresentPull
opts.ResolveToImage = opts.ResolveToImageForCLIFunc(defaultImagePrefix)
opts.ResolveToImage = ResolveToImageForCLIFunc(defaultImagePrefix)
}

// ResolveToImageForCLIFunc returns a func that converts the KRM function short path to the full image url.
// If the function is a catalog function, it prepends `prefix`, e.g. "set-namespace:v0.1" --> prefix + "set-namespace:v0.1".
// A "/" is appended to `prefix` if it is not an empty string and does not end with a "/".
func (opts *RunnerOptions) ResolveToImageForCLIFunc(prefix string) func(_ context.Context, image string) (string, error) {
prefix = strings.TrimSuffix(prefix, "/")
func ResolveToImageForCLIFunc(prefix string) ImageResolveFunc {
prefix = strings.TrimRight(prefix, "/")
if prefix == "" {
return func(_ context.Context, image string) (string, error) {
return image, nil
return func(image string) string {
return image
}
}
return func(_ context.Context, image string) (string, error) {
return func(image string) string {
if !strings.Contains(image, "/") {
return fmt.Sprintf("%s/%s", prefix, image), nil
return fmt.Sprintf("%s/%s", prefix, image)
}
return image, nil
return image
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/test/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func (r *Runner) runFnEval() error {
return fmt.Errorf("failed to prepare package: %w", err)
}


err = r.runSetupScript(pkgPath)
if err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions thirdparty/cmdconfig/commands/cmdeval/cmdeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,14 @@ func (r *EvalFnRunner) preserveCommentsAndFieldOrder(kf *kptfile.KptFile) (*yaml

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

// TODO: This probably doesn't belong here, but moving it changes the test output
if r.Image != "" {
img, err := r.RunnerOptions.ResolveToImage(ctx, r.Image)
if err != nil {
return nil, err
}
img := r.RunnerOptions.ResolveToImage(r.Image)
r.Image = img
}

Expand Down Expand Up @@ -479,7 +476,7 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error {
if len(dataItems) > 0 && r.FnConfigPath != "" {
return fmt.Errorf("function arguments can only be specified without function config file")
}
fnConfig, err := r.getCLIFunctionConfig(c.Context(), dataItems)
fnConfig, err := r.getCLIFunctionConfig(dataItems)
if err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions thirdparty/kyaml/runfn/runfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,7 @@ func (r *RunFns) defaultFnFilterProvider(spec runtimeutil.FunctionSpec, fnConfig
if spec.Container.Image != "" {
fnResult.Image = spec.Container.Image

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