Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include --builder option on func run command #1614

Merged
merged 1 commit into from
Mar 14, 2023
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
40 changes: 38 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/spf13/cobra"
"knative.dev/client/pkg/util"

"knative.dev/func/pkg/builders"
"knative.dev/func/pkg/builders/buildpacks"
"knative.dev/func/pkg/builders/s2i"
"knative.dev/func/pkg/config"
fn "knative.dev/func/pkg/functions"
)
Expand All @@ -26,25 +29,33 @@ specified by --path flag.
Building
By default the function will be built if never built, or if changes are detected
to the function's source. Use --build to override this behavior.
Also a builder strategy (pack, s2i) can be chosen using the --builder option.
Default builder is pack.

`,
Example: `
# Run the function locally, building if necessary
{{rootCmdUse}} run

# Run the function locally, building if necessary, with --builder option
{{rootCmdUse}} run --builder s2i

# Run the function, forcing a rebuild of the image.
# This is useful when the function's image was manually deleted, necessitating
# A rebuild even when no changes have been made the function's source.
{{rootCmdUse}} run --build

# Run the function, forcing a rebuild of the image with --builder option.
{{rootCmdUse}} run --build --builder s2i

# Run the function's existing image, disabling auto-build.
# This is useful when filesystem changes have been made, but one wishes to
# run the previously built image without rebuilding.
{{rootCmdUse}} run --build=false

`,
SuggestFor: []string{"rnu"},
PreRunE: bindEnv("build", "path", "registry", "verbose"),
PreRunE: bindEnv("build", "path", "builder", "registry", "verbose"),
RunE: func(cmd *cobra.Command, args []string) error {
return runRun(cmd, args, newClient)
},
Expand All @@ -61,10 +72,17 @@ to the function's source. Use --build to override this behavior.
"To unset, specify the environment variable name followed by a \"-\" (e.g., NAME-).")
cmd.Flags().StringP("build", "b", "auto", "Build the function. [auto|true|false].")
cmd.Flags().Lookup("build").NoOptDefVal = "true" // --build is equivalient to --build=true
cmd.Flags().StringP("builder", "", cfg.Builder,
fmt.Sprintf("Builder to use when creating the function's container. Currently supported builders are %s. (Env: $FUNC_BUILDER)", KnownBuilders()))
cmd.Flags().StringP("registry", "r", "", "Registry + namespace part of the image if building, ex 'quay.io/myuser' (Env: $FUNC_REGISTRY)")
addPathFlag(cmd)
addVerboseFlag(cmd, cfg.Verbose)

// Tab Completion
if err := cmd.RegisterFlagCompletionFunc("builder", CompleteBuilderList); err != nil {
fmt.Println("internal: error while calling RegisterFlagCompletionFunc: ", err)
}

return cmd
}

Expand Down Expand Up @@ -93,10 +111,24 @@ func runRun(cmd *cobra.Command, args []string, newClient ClientFactory) (err err
}
}

// Concrete implementations (ex builder) vary based on final effective config
var builder fn.Builder
if cfg.Builder == builders.Pack {
builder = buildpacks.NewBuilder(
buildpacks.WithName(builders.Pack),
buildpacks.WithVerbose(cfg.Verbose))
} else if cfg.Builder == builders.S2I {
builder = s2i.NewBuilder(
s2i.WithName(builders.S2I),
s2i.WithVerbose(cfg.Verbose))
} else {
return builders.ErrUnknownBuilder{Name: cfg.Builder, Known: KnownBuilders()}
}

// Client for use running (and potentially building), using the config
// gathered plus any additional option overrieds (such as for providing
// mocks when testing for builder and runner)
client, done := newClient(ClientConfig{Verbose: cfg.Verbose}, fn.WithRegistry(cfg.Registry))
client, done := newClient(ClientConfig{Verbose: cfg.Verbose}, fn.WithRegistry(cfg.Registry), fn.WithBuilder(builder))
defer done()

// Build?
Expand Down Expand Up @@ -165,6 +197,9 @@ type runConfig struct {
// value such as 'true', 'false, '1' or '0'.
Build string

// Builder strategy if building
Builder string

// Registry for the build tag if building
Registry string
}
Expand All @@ -178,6 +213,7 @@ func newRunConfig(cmd *cobra.Command) (cfg runConfig, err error) {
Build: viper.GetString("build"),
Path: viper.GetString("path"),
Verbose: viper.GetBool("verbose"), // defined on root
Builder: viper.GetString("builder"),
Registry: viper.GetString("registry"),
EnvToUpdate: envToUpdate,
EnvToRemove: envToRemove,
Expand Down
31 changes: 31 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ created: 2009-11-10 23:00:00`,
buildInvoked: true,
runInvoked: true,
},
{
name: "run and build with builder pack",
desc: "Should run and build when build is specifically requested with builder pack",
funcState: `name: test-func
runtime: go
created: 2023-03-12 15:00:00`,
args: []string{"--build=true", "--builder=pack"},
buildInvoked: true,
runInvoked: true,
},
{
name: "run and build with builder s2i",
desc: "Should run and build when build is specifically requested with builder s2i",
funcState: `name: test-func
runtime: go
created: 2023-03-12 15:00:00`,
args: []string{"--build=true", "--builder=s2i"},
buildInvoked: true,
runInvoked: true,
},
{
name: "run and build with builder invalid",
desc: "Should run and build when build is specifically requested with builder invalid",
funcState: `name: test-func
runtime: go
created: 2023-03-12 15:00:00`,
args: []string{"--build=true", "--builder=invalid"},
buildError: fmt.Errorf("\"invalid\" is not a known builder. Available builders are \"pack\" and \"s2i\""),
buildInvoked: true,
runInvoked: true,
},
{
name: "run without build when disabled",
desc: "Should run but not build when build is expressly disabled",
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/func_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ specified by --path flag.
Building
By default the function will be built if never built, or if changes are detected
to the function's source. Use --build to override this behavior.
Also a builder strategy (pack, s2i) can be chosen using the --builder option.
Default builder is pack.



Expand All @@ -26,11 +28,17 @@ func run
# Run the function locally, building if necessary
func run

# Run the function locally, building if necessary, with --builder option
func run --builder s2i

# Run the function, forcing a rebuild of the image.
# This is useful when the function's image was manually deleted, necessitating
# A rebuild even when no changes have been made the function's source.
func run --build

# Run the function, forcing a rebuild of the image with --builder option.
func run --build --builder s2i

# Run the function's existing image, disabling auto-build.
# This is useful when filesystem changes have been made, but one wishes to
# run the previously built image without rebuilding.
Expand All @@ -43,6 +51,7 @@ func run --build=false

```
-b, --build string[="true"] Build the function. [auto|true|false]. (default "auto")
--builder string Builder to use when creating the function's container. Currently supported builders are "pack" and "s2i". (Env: $FUNC_BUILDER) (default "pack")
-e, --env stringArray Environment variable to set in the form NAME=VALUE. You may provide this flag multiple times for setting multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
-h, --help help for run
-p, --path string Path to the function. Default is current directory (Env: $FUNC_PATH)
Expand Down