openfaas / faas-cli

Official CLI for OpenFaaS
https://www.openfaas.com/
Other
794 stars 226 forks source link

Override Function names when deploying with yaml file #994

Closed mrobinsonFS closed 5 months ago

mrobinsonFS commented 5 months ago

I'm looking to use the --name flag when deploying a function and specifying the yaml file with the -f flag. Currently, a deploy with the yaml file specified will ignore the --name flag. I'm looking to use both options and have the individual command line flags override the yaml file when they are specified. This would help our development & CI/CD pipelines.

Expected Behaviour

Say we have a function with a yaml file as follows:

version: 1.0
provider:
  name: openfaas
  gateway: http://127.0.0.1:8080
functions:
  example-ruby:
    lang: ruby
    handler: ./example-ruby
    image: ruby:latest

If I deploy the function via: faas-cli deploy -f ./example-ruby.yaml --name staging-example-ruby, the resulting function would be named staging-example-ruby in the openfaas engine

Current Behaviour

With the current behaviour, the name declared in the yaml is used for the deploy, so the function is ultimately deployed as just example-ruby

Why do you need this?

We'd like to prefix the function names in our deployment pipeline based on the source repo and/or environment, while allowing the use of the yaml file. Ideally, developers create a function, and could modify the yaml file to specify secrets or build options. However, the pipeline jobs we use can set prefixes to their function names at deploy time. I have considered using more namespaces, but the number of namespaces will get hard to manage with multiple desired prefixes based on the source repository and environment

Who is this for?

What company is this for? Are you listed in the ADOPTERS.md file?

This for Fullscript, we are a customer

Are you a GitHub Sponsor (Yes/No?)

Check at: https://github.com/sponsors/openfaas

List All Possible Solutions and Workarounds

Similar to this PR, I'd like to see a similar check to determine if the --name flag is being used, and if so, supersede the name value that is used from the yaml file. The other options we've considered are:

Which Solution Do You Recommend?

I would implement the --name override. I think this is a straightforward solutions and uses behavior similar to other tools like helm, where there is variable precedence.

Context

We are trying to deploy functions from multiple repositories and into multiple environments (i.e. QA, staging, production), with prefixes to easily identify the functions. The jobs that actually run the deployments are templated CI jobs that are shared across these repos, so the end result is something like:

faas-cli deploy -f <example-function.yaml> --name $REPO-<example-function>

Your Environment

version: 0.16.23

Kubernetes

Linux

rgee0 commented 5 months ago

Taking your example:

version: 1.0
provider:
  name: openfaas
  gateway: http://127.0.0.1:8080
functions:
  example-ruby:
    lang: ruby
    handler: ./example-ruby
    image: ruby:latest

You could try:

version: 1.0
provider:
  name: openfaas
  gateway: http://127.0.0.1:8080
functions:
  ${envFnName:-example-ruby}:
    lang: ruby
    handler: ./example-ruby
    image: ruby:latest

Then you can use the following to over-ride:

➜  /tmp envFnName=burt faas-cli deploy -f stack.yml
Deploying: burt.

Is OpenFaaS deployed? Do you need to specify the --gateway flag?
Put "http://127.0.0.1:8080/system/functions": dial tcp 127.0.0.1:8080: connect: connection refused

Function 'burt' failed to deploy with status code: 500

Similarly, if you don't set the var then:

➜  /tmp faas-cli deploy -f stack.yml 
Deploying: example-ruby.

Is OpenFaaS deployed? Do you need to specify the --gateway flag?
Put "http://127.0.0.1:8080/system/functions": dial tcp 127.0.0.1:8080: connect: connection refused

Function 'example-ruby' failed to deploy with status code: 500

The first over-rides the function name, the second uses the default.

(I don't have an instance set up currently, hence the http/500, but the point, really, is to show the function name is replaced as desired)

mrobinsonFS commented 5 months ago

@rgee0 Thanks for the suggestion, I did come across this approach and should have included that in my issue. It's too much overhead to ask the developers to go into the yaml file and change the function name to have that prefix. We are really trying to simplify the development process as much as possible, and leave these details to the pipeline. Also, in your example, it looks like the function is just deploying as burt, as opposed to burt-example-ruby

alexellis commented 5 months ago

Unfortunately setting a function's name via --name would do the wrong thing with > 1 function in the stack.yml file, which is the design for OpenFaaS.

On a private call I mentioned this can be achieved via envsubst (as per Richard's example).

${fnPrefix:-}:example-ruby`

This deploys as example-ruby, but with fnPrefix=burt- it would deploy as burt-example-ruby

But after hearing more on the call it seemed like the team were trying to create "namespaces" by prefixing function names.

With access to OpenFaaS for Enterprises you can create namespaces and manage them as required via faas-cli namespaces, so we would recommend that in this case.

I would strongly recommend using namespaces, what would be the reason not to?

alexellis commented 5 months ago

I'm going to get this closed for the time being.

I won't lock it, so if you have more context about why you feel namespaces don't fit, please add it below.