openfaas / faas-cli

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

Add --namespace/-n flag to function commands #693

Closed alexellis closed 5 years ago

alexellis commented 5 years ago

Description

Add --namespace/-n flag to function commands so that users can access functions in any valid namespace.

Possible Solution

Start with a number of small PRs, or one larger PR covering:

Then add support to:

The namespace flag must be optional and will be added to the HTTP path as a query string i.e. GET /system/functions?namespace=staging

Tracking issue: https://github.com/openfaas/faas-netes/issues/511

martindekov commented 5 years ago

Derek assign: me

martindekov commented 5 years ago

I will directly post one large PR with the changes in all the commands

alexellis commented 5 years ago

Happy for you to run with this, thank you.

ghostbody commented 4 years ago

Hi, it seems that this option, i.e. --namespace or -n is not working when using a yaml file in command deploy.

I use this command:

faas-cli deploy -f some-config.yaml -n some-ns

With no namespace declearing in some-config.yaml, the option -n is not sent to the gateway.

but when i use this command, every thing works well:

faas-cli deploy --name some-func-name --image some-func-image -n some-ns

Extracting the two parameters for function name and function image from some-config.yaml to command line, it works. 🤔

I am wondering there is a bug. I am trying to read the source code for more info.

alexellis commented 4 years ago

For YAML files I think we need to extend the schema. Kubernetes AFAIK won't allow this either using its CLI.

The YAML file should allow for a namespace to be specified. @LucasRoesler @martindekov

martindekov commented 4 years ago

Yes this can be easily added to the schema I believe 👍 I can add the change in the weekend that comes. This issue covered the CLI, not the schema, I can open another one which can address this problem?

sperolsUNIV commented 4 years ago

Hi, it seems that this option, i.e. --namespace or -n is not working when using a yaml file in command deploy.

I use this command:

faas-cli deploy -f some-config.yaml -n some-ns

With no namespace declearing in some-config.yaml, the option -n is not sent to the gateway.

I tried also and today its still not working

martindekov commented 4 years ago

Hey @sperolsUNIV, thanks for the feedback on this, what is the version of the CLI which you use?

LucasRoesler commented 4 years ago

@martindekov i remember bringing up this same issue several months back (I can't find the thread where I raised it).

Ultimately, nothing was done because the reasoning is that this behave almost the same as how kubectl apply -f behaves. If you template already has a namespace, then it is not overridden. I think our behavior is slightly different though because the flag sets the variable functionNamespace and this seems to only be used when defining the function via flags, ie not using a stack file.

I don't think this behavior is obvious for most users, I would be glad to see this change to behave like the other flags: merge/replace the flag value into the stack file. I think it would be a relatively small change here

https://github.com/openfaas/faas-cli/blob/f7c29ea19b5df9d7aa87e9c70aacf4d9315da2cd/commands/deploy.go#L294

Just add this before line 276

if functionNamespace != "" {
    function.Namespace = functionNamespace
}

and the flag will then override the value from the yaml file.

nickgerace commented 4 years ago

Trying my hand at this and submitting a PR. I think your idea is sound @LucasRoesler.

LucasRoesler commented 4 years ago

@nickgerace you are always welcome to submit a PR, buy I think we need some input from @alexellis before we know if it will be accepted and merged.

alexellis commented 4 years ago

Can we come up with some specific scenarios? Like examples of the stack yml and flags passed along with the desired outcome?

LucasRoesler commented 4 years ago

The use case my data science team has is that they want to deploy their models to a DEV, STG, and default namespace as part of the development flow.

The desired workflow is that

  1. all merges to master would run faas-cli up --namespace DEV --env db=...
  2. all tags on master would run faas-cli up --namespace STG --env db=...
  3. finally, there is a custom Jenkins task that they can run (via slack slash command) after verifying the deployment is stable on stg, this task would faas-cli up --namespace PROD --env db=...

We worked around this by using env substitution, but it caused and still causes a lot of confusion to the ops and data science team. Every other flag seems to override the stack file except this one flag. It honestly wasted a day of effort because the team thought they were messing something up and spent a day trying to debug before asking me about it.

Our workaround looks like this

  1. all merges to master run namespace=DEV faas-cli up --env db=...
  2. all tags on master run namespace=STG faas-cli up --env db=...
  3. the custom Jenkins task runs namespace=PROD faas-cli up --env db=...

Where we added namespace: ${namespace:-DEV} into the stack files. But this was only implemented because I told them that it was possible.