Skip to content

Commit

Permalink
Update faas-cli new to use stack.yaml
Browse files Browse the repository at this point in the history
Updates faas-cli new to create a stack.yaml file instead of
a YAML file named after the function. This is to help users
discover that stack.yaml does not require a --yaml or -f
flag which can become cumbersome.

When a stack.yaml or the file specified via -f already exists
the new command will now prompt users to use --append

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jan 23, 2025
1 parent 13a967a commit fac428d
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 527 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: docker/setup-buildx-action@v3
-
name: Local docker build (non-root image)
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
Expand All @@ -42,7 +42,7 @@ jobs:
tags: openfaas/faas-cli:${{ github.sha }}
-
name: Test for multi-arch build
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
Expand Down
434 changes: 0 additions & 434 deletions CHANGELOG.md

This file was deleted.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The main commands supported by the CLI are:
The default gateway URL of `127.0.0.1:8080` can be overridden in three places including an environmental variable.

* 1st priority `--gateway` flag
* 2nd priority `--yaml` / `-f` flag or `stack.yml` if in current directory
* 2nd priority `--yaml` / `-f` flag or `stack.yaml` if in current directory
* 3rd priority `OPENFAAS_URL` environmental variable

For Kubernetes users you may want to set this in your `.bash_rc` file:
Expand Down Expand Up @@ -293,7 +293,7 @@ A YAML stack file groups functions together and also saves on typing.

You can define individual functions or a set of them within a YAML file. This makes the CLI easier to use and means you can use this file to deploy to your OpenFaaS instance. By default the faas-cli will attempt to load `stack.yaml` from the current directory.

Here is an example file using the `stack.yml` file included in the repository.
Here is an example file using the `stack.yaml` file included in the repository.

```yaml
provider:
Expand All @@ -317,10 +317,10 @@ $ faas-cli build

> `-f` specifies the file or URL to download your YAML file from. The long version of the `-f` flag is: `--yaml`.

You can also download over HTTP(s):
You can also download over HTTP(s), for example:

```sh
$ faas-cli build -f https://raw.githubusercontent.com/openfaas/faas-cli/master/stack.yml
$ faas-cli build -f https://raw.githubusercontent.com/openfaas/store-functions/master/stack.yml
```

Docker along with a Python template will be used to build an image named alexellis2/faas-urlping.
Expand Down
22 changes: 11 additions & 11 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func init() {
buildCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'digest', 'sha', 'branch', or 'describe', or 'latest'")
buildCmd.Flags().StringArrayVar(&buildLabels, "build-label", []string{}, "Add a label for Docker image (LABEL=VALUE)")
buildCmd.Flags().StringArrayVar(&copyExtra, "copy-extra", []string{}, "Extra paths that will be copied into the function build context")
buildCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
buildCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
buildCmd.Flags().BoolVar(&quietBuild, "quiet", false, "Perform a quiet build, without showing output from Docker")
buildCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yml")
buildCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yaml")
buildCmd.Flags().BoolVar(&forcePull, "pull", false, "Force a re-pull of base images in template during build, useful for publishing images")

// Set bash-completion.
Expand Down Expand Up @@ -88,16 +88,16 @@ var buildCmd = &cobra.Command{
the "--yaml" flag (which may contain multiple function definitions), or directly
via flags.`,
Example: ` faas-cli build -f https://domain/path/myfunctions.yml
faas-cli build -f ./stack.yml --no-cache --build-arg NPM_VERSION=0.2.2
faas-cli build -f ./stack.yml --build-option dev
faas-cli build -f ./stack.yml --tag sha
faas-cli build -f ./stack.yml --tag branch
faas-cli build -f ./stack.yml --tag describe
faas-cli build -f ./stack.yml --filter "*gif*"
faas-cli build -f ./stack.yml --regex "fn[0-9]_.*"
faas-cli build --image=my_image --lang=python --handler=/path/to/fn/
faas-cli build -f stack.yaml --no-cache --build-arg NPM_VERSION=0.2.2
faas-cli build -f stack.yaml --build-option dev
faas-cli build -f stack.yaml --tag sha
faas-cli build -f stack.yaml --tag branch
faas-cli build -f stack.yaml --tag describe
faas-cli build -f stack.yaml --filter "*gif*"
faas-cli build -f stack.yaml --regex "fn[0-9]_.*"
faas-cli build --image=my_image --lang=python --handler=/path/to/fn/ \
--name=my_fn --squash
faas-cli build -f ./stack.yml --build-label org.label-schema.label-name="value"`,
faas-cli build -f stack.yaml --build-label org.label-schema.label-name="value"`,
PreRunE: preRunBuild,
RunE: runBuild,
}
Expand Down
22 changes: 11 additions & 11 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func init() {
deployCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'latest', 'sha', 'branch', or 'describe'")

deployCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
deployCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
deployCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
deployCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth")
// Set bash-completion.
_ = deployCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{})
Expand Down Expand Up @@ -113,16 +113,16 @@ var deployCmd = &cobra.Command{
the "--yaml" flag (which may contain multiple function definitions), or directly
via flags. Note: --replace and --update are mutually exclusive.`,
Example: ` faas-cli deploy -f https://domain/path/myfunctions.yml
faas-cli deploy -f ./stack.yml
faas-cli deploy -f ./stack.yml --label canary=true
faas-cli deploy -f ./stack.yml --annotation user=true
faas-cli deploy -f ./stack.yml --filter "*gif*" --secret dockerhuborg
faas-cli deploy -f ./stack.yml --regex "fn[0-9]_.*"
faas-cli deploy -f ./stack.yml --replace=false --update=true
faas-cli deploy -f ./stack.yml --replace=true --update=false
faas-cli deploy -f ./stack.yml --tag sha
faas-cli deploy -f ./stack.yml --tag branch
faas-cli deploy -f ./stack.yml --tag describe
faas-cli deploy -f stack.yaml
faas-cli deploy -f stack.yaml --label canary=true
faas-cli deploy -f stack.yaml --annotation user=true
faas-cli deploy -f stack.yaml --filter "*gif*" --secret dockerhuborg
faas-cli deploy -f stack.yaml --regex "fn[0-9]_.*"
faas-cli deploy -f stack.yaml --replace=false --update=true
faas-cli deploy -f stack.yaml --replace=true --update=false
faas-cli deploy -f stack.yaml --tag sha
faas-cli deploy -f stack.yaml --tag branch
faas-cli deploy -f stack.yaml --tag describe
faas-cli deploy --image=alexellis/faas-url-ping --name=url-ping
faas-cli deploy --image=my_image --name=my_fn --handler=/path/to/fn/
--gateway=http://remote-site.com:8080 --lang=python
Expand Down
2 changes: 1 addition & 1 deletion commands/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
describeCmd.Flags().StringVar(&functionName, "name", "", "Name of the function")
describeCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
describeCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
describeCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
describeCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
describeCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth")
describeCmd.Flags().StringVarP(&functionNamespace, "namespace", "n", "", "Namespace of the function")
describeCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")
Expand Down
20 changes: 11 additions & 9 deletions commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func init() {
generateCmd.Flags().StringVar(&api, "api", defaultAPIVersion, "CRD API version e.g openfaas.com/v1, serving.knative.dev/v1")
generateCmd.Flags().StringVarP(&crdFunctionNamespace, "namespace", "n", "openfaas-fn", "Kubernetes namespace for functions")
generateCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'digest', 'latest', 'sha', 'branch', 'describe'")
generateCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
generateCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
generateCmd.Flags().StringVar(&desiredArch, "arch", "x86_64", "Desired image arch. (Default x86_64)")
generateCmd.Flags().StringArrayVar(&annotationArgs, "annotation", []string{}, "Any annotations you want to add (to store functions only)")
generateCmd.Flags().StringArrayVar(&labelArgs, "label", []string{}, "Any labels you want to add (to store functions only)")
Expand All @@ -57,14 +57,14 @@ func init() {
}

var generateCmd = &cobra.Command{
Use: "generate --api=openfaas.com/v1 --yaml stack.yml --tag sha --namespace=openfaas-fn",
Use: "generate --api=openfaas.com/v1 --yaml stack.yaml --tag sha --namespace=openfaas-fn",
Short: "Generate Kubernetes CRD YAML file",
Long: `The generate command creates kubernetes CRD YAML file for functions`,
Example: `faas-cli generate --api=openfaas.com/v1 --yaml stack.yml | kubectl apply -f -
faas-cli generate --api=openfaas.com/v1 -f stack.yml
faas-cli generate --api=serving.knative.dev/v1 -f stack.yml
faas-cli generate --api=openfaas.com/v1 --namespace openfaas-fn -f stack.yml
faas-cli generate --api=openfaas.com/v1 -f stack.yml --tag branch -n openfaas-fn`,
Example: `faas-cli generate --api=openfaas.com/v1 --yaml stack.yaml | kubectl apply -f -
faas-cli generate --api=openfaas.com/v1 -f stack.yaml
faas-cli generate --api=serving.knative.dev/v1 -f stack.yaml
faas-cli generate --api=openfaas.com/v1 --namespace openfaas-fn -f stack.yaml
faas-cli generate --api=openfaas.com/v1 -f stack.yaml --tag branch -n openfaas-fn`,
PreRunE: preRunGenerate,
RunE: runGenerate,
}
Expand Down Expand Up @@ -183,8 +183,10 @@ func runGenerate(cmd *cobra.Command, args []string) error {
}
} else {
fmt.Println(
`"stack.yml" file not found in the current directory.
Use "--yaml" to pass a file or "--from-store" to generate using function store.`)
`No "stack.yaml" or "stack.yml" file was found in the current directory.
Use "--yaml" / "-f" to specify a custom filename.
Alternatively, to generate a definition for store functions, use "--from-store"`)
os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion commands/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
invokeCmd.Flags().StringVar(&sigHeader, "sign", "", "name of HTTP request header to hold the signature")
invokeCmd.Flags().StringVar(&key, "key", "", "key to be used to sign the request (must be used with --sign)")

invokeCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
invokeCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")

faasCmd.AddCommand(invokeCmd)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {

listCmd.Flags().BoolVarP(&verboseList, "verbose", "v", false, "Verbose output for the function list")
listCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
listCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
listCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
listCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth")
listCmd.Flags().StringVar(&sortOrder, "sort", "name", "Sort the functions by \"name\" or \"invocations\"")

Expand Down
6 changes: 3 additions & 3 deletions commands/local_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ services deployed within your OpenFaaS cluster.`,
# Run on a random port
faas-cli local-run -p 0
# Use a custom YAML file other than stack.yml
faas-cli local-run stronghash -f ./stronghash.yml
# Use a custom YAML file other than stack.yaml
faas-cli local-run stronghash -f ./stronghash.yaml
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
Expand Down Expand Up @@ -133,7 +133,7 @@ func localRunExec(cmd *cobra.Command, args []string, ctx context.Context) error
}

// AE: I found that the `localrun` command will do a build of all functions in
// the stack.yml if no argument is given and there is > 1 function in
// the stack.yaml if no argument is given and there is > 1 function in
// the file, then it will exit with an error when it comes to the run step
func localBuild(cmd *cobra.Command, args []string) error {
if err := preRunBuild(cmd, args); err != nil {
Expand Down
23 changes: 12 additions & 11 deletions commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var newFunctionCmd = &cobra.Command{
Long: `The new command creates a new function based upon hello-world in the given
language or type in --list for a list of languages available.`,
Example: ` faas-cli new chatbot --lang node
faas-cli new chatbot --lang node --append stack.yml
faas-cli new chatbot --lang node --append bots.yaml
faas-cli new text-parser --lang python --quiet
faas-cli new text-parser --lang python --gateway http://mydomain:8080
faas-cli new --list`,
Expand Down Expand Up @@ -97,6 +97,10 @@ func preRunNewFunction(cmd *cobra.Command, args []string) error {
return err
}

if len(yamlFile) == 0 && len(appendFile) == 0 {
yamlFile = defaultYAML
}

return nil
}

Expand Down Expand Up @@ -160,27 +164,24 @@ Download templates:
appendMode := len(appendFile) > 0

if appendMode {
if (strings.HasSuffix(appendFile, ".yml") || strings.HasSuffix(appendFile, ".yaml")) == false {
if !(strings.HasSuffix(appendFile, ".yml") || strings.HasSuffix(appendFile, ".yaml")) {
return fmt.Errorf("when appending to a stack the suffix should be .yml or .yaml")
}

if _, statErr := os.Stat(appendFile); statErr != nil {
return fmt.Errorf("unable to find file: %s - %s", appendFile, statErr.Error())
}

var duplicateError error
duplicateError = duplicateFunctionName(functionName, appendFile)

if duplicateError != nil {
return duplicateError
if err := duplicateFunctionName(functionName, appendFile); err != nil {
return err
}

fileName = appendFile
outputMsg = fmt.Sprintf("Stack file updated: %s\n", fileName)

} else {
gateway = getGatewayURL(gateway, defaultGateway, gateway, os.Getenv(openFaaSURLEnvironment))
fileName = functionName + ".yml"
fileName = yamlFile
outputMsg = fmt.Sprintf("Stack file written: %s\n", fileName)
}

Expand All @@ -192,14 +193,14 @@ Download templates:
return fmt.Errorf("folder: %s already exists", handlerDir)
}

_, err := os.Stat(fileName)
if err == nil && appendMode == false {
return fmt.Errorf("file: %s already exists", fileName)
if _, err := os.Stat(fileName); err == nil && !appendMode {
return fmt.Errorf("file: %s already exists. Try \"faas-cli new --append %s\" instead", fileName, fileName)
}

if err := os.Mkdir(handlerDir, 0700); err != nil {
return fmt.Errorf("folder: could not create %s : %s", handlerDir, err)
}

fmt.Printf("Folder: %s created.\n", handlerDir)

if err := updateGitignore(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func init() {
publishCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'latest', 'sha', 'branch', or 'describe'")
publishCmd.Flags().StringArrayVar(&buildLabels, "build-label", []string{}, "Add a label for Docker image (LABEL=VALUE)")
publishCmd.Flags().StringArrayVar(&copyExtra, "copy-extra", []string{}, "Extra paths that will be copied into the function build context")
publishCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
publishCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
publishCmd.Flags().BoolVar(&quietBuild, "quiet", false, "Perform a quiet build, without showing output from Docker")
publishCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yml")
publishCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yaml")
publishCmd.Flags().StringVar(&platforms, "platforms", "linux/amd64", "A set of platforms to publish")
publishCmd.Flags().StringArrayVar(&extraTags, "extra-tag", []string{}, "Additional extra image tag")
publishCmd.Flags().BoolVar(&resetQemu, "reset-qemu", false, "Runs \"docker run multiarch/qemu-user-static --reset -p yes\" to enable multi-arch builds. Compatible with AMD64 machines only.")
Expand Down
16 changes: 8 additions & 8 deletions commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {

pushCmd.Flags().IntVar(&parallel, "parallel", 1, "Push images in parallel to depth specified.")
pushCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'digest', 'latest', 'sha', 'branch', 'describe'")
pushCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
pushCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
pushCmd.Flags().BoolVar(&quietBuild, "quiet", false, "Perform a quiet build, without showing output from Docker")
}

Expand All @@ -37,13 +37,13 @@ config to a remote repository.
These container images must already be present in your local image cache.`,

Example: ` faas-cli push -f https://domain/path/myfunctions.yml
faas-cli push -f ./stack.yml
faas-cli push -f ./stack.yml --parallel 4
faas-cli push -f ./stack.yml --filter "*gif*"
faas-cli push -f ./stack.yml --regex "fn[0-9]_.*"
faas-cli push -f ./stack.yml --tag sha
faas-cli push -f ./stack.yml --tag branch
faas-cli push -f ./stack.yml --tag describe`,
faas-cli push -f stack.yaml
faas-cli push -f stack.yaml --parallel 4
faas-cli push -f stack.yaml --filter "*gif*"
faas-cli push -f stack.yaml --regex "fn[0-9]_.*"
faas-cli push -f stack.yaml --tag sha
faas-cli push -f stack.yaml --tag branch
faas-cli push -f stack.yaml --tag describe`,
RunE: runPush,
}

Expand Down
8 changes: 4 additions & 4 deletions commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func init() {
// Setup flags that are used by multiple commands (variables defined in faas.go)
removeCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
removeCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
removeCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
removeCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")
removeCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth")
removeCmd.Flags().StringVarP(&functionNamespace, "namespace", "n", "", "Namespace of the function")

Expand All @@ -34,9 +34,9 @@ var removeCmd = &cobra.Command{
using the "--yaml" flag (which may contain multiple function definitions), or by
explicitly specifying a function name.`,
Example: ` faas-cli remove -f https://domain/path/myfunctions.yml
faas-cli remove -f ./stack.yml
faas-cli remove -f ./stack.yml --filter "*gif*"
faas-cli remove -f ./stack.yml --regex "fn[0-9]_.*"
faas-cli remove -f stack.yaml
faas-cli remove -f stack.yaml --filter "*gif*"
faas-cli remove -f stack.yaml --regex "fn[0-9]_.*"
faas-cli remove url-ping
faas-cli remove img2ansi --gateway==http://remote-site.com:8080`,
RunE: runDelete,
Expand Down
2 changes: 1 addition & 1 deletion commands/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ functions:
# limits:
# memory: 64Mi

# skip_build is useful for deploying pre-existing images from stack.yml
# skip_build is useful for deploying pre-existing images from stack.yaml
stronghash:
skip_build: true
image: ghcr.io/openfaas/alpine:latest
Expand Down
2 changes: 1 addition & 1 deletion commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
versionCmd.Flags().BoolVar(&shortVersion, "short-version", false, "Just print Git SHA")
versionCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
versionCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
versionCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file")
versionCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yaml file")

versionCmd.Flags().BoolVar(&warnUpdate, "warn-update", true, "Check for new version and warn about updating")

Expand Down
Loading

0 comments on commit fac428d

Please sign in to comment.