Skip to content

Set the default namespace with OPENFAAS_NS - #1028

Merged
alexellis merged 2 commits into
openfaas:masterfrom
welteki:feature/openfaas-ns
Sep 4, 2026
Merged

Set the default namespace with OPENFAAS_NS#1028
alexellis merged 2 commits into
openfaas:masterfrom
welteki:feature/openfaas-ns

Conversation

@welteki

@welteki welteki commented Sep 4, 2026

Copy link
Copy Markdown
Member

Description

Add support for the OPENFAAS_NS environment variable as the default
namespace for namespace-aware commands (up, deploy, describe,
diff, invoke, list, logs, ready, remove and the secret
commands).

Namespace resolution precedence is unchanged: the --namespace flag
wins, then the stack.yaml namespace, then OPENFAAS_NS, then the
cluster default namespace.

diff previously listed deployed functions from a single namespace
only: the --namespace flag value, or the cluster default if the flag
was not set. For a stack that spans multiple namespaces this meant
deployed functions in other namespaces were invisible, so diff would
report them as not deployed. It now collects the set of namespaces
that the stack's functions resolve to (applying the same precedence)
and lists deployed functions from each of them, so every deployed
function that a stack function could match is found.

Also fixes invoke to use the resolved namespace on the 401 retry
path, which previously used the raw --namespace flag and dropped the
stack.yaml and OPENFAAS_NS values.

Motivation and Context

Users with access to a non-default namespace, e.g. a per-user
namespace on a shared cluster, currently have to pass --namespace
to every command. Setting OPENFAAS_NS in the environment lets their
commands avoid implicitly targeting the cluster default namespace.

  • I have raised an issue to propose this change (required)

How Has This Been Tested?

  • Added unit tests for namespace precedence, env-substituted stack
    namespaces, logs resolution, and multi-namespace diff behavior.
  • Covered same-named functions in different namespaces and API responses
    that omit the function namespace.
  • Tested OPENFAAS_NS end to end with deploy, store deploy, list,
    describe, ready, invoke, logs, remove, and the secret lifecycle.
  • Verified that --namespace overrides the environment and that existing
    stack namespace precedence is preserved.
  • Tested diff with a stack spanning multiple namespaces. The old CLI
    missed namespaced functions, while the updated CLI matched each function
    to its queried namespace and reported only genuinely missing functions.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@reviewfn

This comment has been minimized.

Use OPENFAAS_NS when a namespace-aware command has no explicit
namespace. This avoids implicitly targeting the cluster default for
users who work in another namespace while preserving existing flag and
stack precedence.

Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
Query every namespace resolved by the functions in a stack instead of
listing functions from only one namespace.

Key each deployed function by the namespace used for its query so a
same-named function in another namespace cannot satisfy the wrong stack
entry.

Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
@welteki
welteki force-pushed the feature/openfaas-ns branch from 143b5d3 to 1259b6d Compare September 4, 2026 12:08
@reviewfn

reviewfn Bot commented Sep 4, 2026

Copy link
Copy Markdown

AI Pull Request Overview

Disclaimer: This review was generated by automated AI and may contain errors. Do not trust its outputs without human verification.

Summary

  • Adds OPENFAAS_NS as the default namespace fallback after CLI flags and stack namespaces.
  • Applies the new namespace fallback across deploy, store deploy, list, describe, invoke, logs, ready, remove, and secret commands.
  • Updates diff to query every namespace resolved by the stack instead of only one namespace.
  • Fixes invoke retry behavior to reuse the resolved namespace on authenticated retry.
  • Adds focused unit coverage for namespace precedence, env-substituted stack namespaces, logs, and multi-namespace diff behavior.
  • Documents OPENFAAS_NS in the README environment variable list.

Approval rating (1-10)

9/10. The change is cohesive and well-covered; I found no blocking correctness issues in the scoped diff.

Summary per file

Summary per file
File path Summary
README.md Documents OPENFAAS_NS as the default function namespace environment variable.
commands/deploy.go Resolves function namespaces from flag, stack, and environment for stack and image deploys.
commands/describe.go Uses resolved namespace for function info, list lookup, and generated URLs.
commands/diff.go Resolves stack function namespaces and lists deployments from each relevant namespace.
commands/diff_test.go Adds coverage for namespace precedence and multi-namespace diff matching.
commands/invoke.go Uses OPENFAAS_NS in namespace resolution and authenticated retry path.
commands/list.go Applies resolved namespace when listing functions.
commands/logs.go Applies resolved namespace when constructing logs requests.
commands/logs_test.go Adds logs namespace precedence coverage and isolates existing parsing test from environment state.
commands/priority.go Adds OPENFAAS_NS constant and environment fallback to getNamespace.
commands/priority_test.go Adds namespace precedence and env-substituted stack namespace tests.
commands/ready.go Uses resolved namespace for status polling and waiting output.
commands/remove.go Resolves namespace for stack-based and direct function removal.
commands/secret_apply.go Applies resolved namespace to secret listing, deletion, creation, and output.
commands/secret_create.go Applies resolved namespace to secret creation and output.
commands/secret_list.go Applies resolved namespace when listing secrets.
commands/secret_remove.go Applies resolved namespace when removing secrets.
commands/secret_update.go Applies resolved namespace when updating secrets.
commands/store_deploy.go Applies resolved namespace to store deployments.

Overall Assessment

The PR implements the intended precedence consistently: explicit namespace flags remain strongest, stack namespaces still override defaults, and OPENFAAS_NS fills the previous implicit cluster-default gap. The diff change is the riskiest part because it changes how deployed functions are matched across namespaces, but the final implementation scopes deployed entries to namespaces the stack can actually resolve to and includes tests for same-named functions and responses that omit namespaces. I did not find a concrete blocking issue in the reviewed diff.

Detailed Review

Detailed Review

No blocking findings.

The central getNamespace precedence in commands/priority.go matches the PR description: flag, then stack namespace, then OPENFAAS_NS, then the existing empty default. Call sites introduced in the scoped command files pass stack namespaces only where stack function definitions are available, which preserves prior stack.yaml behavior while extending default namespace handling for direct commands.

The diff implementation now builds YAML keys with the same namespace resolution used by deploy and queries each unique resolved namespace. Keying deployed functions by the queried namespace is appropriate for providers that omit FunctionStatus.Namespace, and the code avoids matching deployments that do not correspond to a YAML key. The added tests cover the main regression cases for namespace-aware diff behavior.

The invoke retry path now uses the already-resolved namespace instead of the raw invoke flag, which avoids dropping stack or environment namespace defaults on the 401 authentication retry path.

The README addition is accurate for the feature. It is brief, but it fits the existing environment variable list format and does not introduce a rendering or content issue.

AI agent details.

Agent processing time: 1m53.156s
Environment preparation time: 3.571s
Total time from webhook: 2m2.24s

@welteki welteki changed the title Use OPENFAAS_NS as the default function namespace Support the OPENFAAS_NS environment variable as the default function namespace Sep 4, 2026
@welteki welteki changed the title Support the OPENFAAS_NS environment variable as the default function namespace Set the default namespace with OPENFAAS_NS Sep 4, 2026
@welteki
welteki marked this pull request as ready for review September 4, 2026 12:24

@alexellis alexellis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure on approach, but if it works, we'll ship it.

Comment thread commands/deploy.go
// Check if there is a functionNamespace flag passed, if so, override the namespace value
// defined in the stack.yaml
function.Namespace = getNamespace(functionNamespace, function.Namespace)
function.Namespace = getNamespace(functionNamespace, function.Namespace, os.Getenv(openFaaSNamespaceEnvironment))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I would have added os.Getenv(openFaaSNamespaceEnvironment) to every callsight - given it's consistent I would have perhaps not even have changed the signature at all and have had it self-contained.

Go unit tests can setenv dynamically when needed.

@alexellis
alexellis merged commit 4797310 into openfaas:master Sep 4, 2026
2 checks passed
@welteki welteki mentioned this pull request Sep 7, 2026
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants