garden-io / garden

Automation for Kubernetes development and testing. Spin up production-like environments for development, testing, and CI on demand. Use the same configuration and workflows at every step of the process. Speed up your builds and test runs via shared result caching
https://garden.io
Mozilla Public License 2.0
3.38k stars 273 forks source link

[Bug]: Namespace name not updated when passing variables via CLI in Garden environment configuration #6528

Open Daphaz opened 3 weeks ago

Daphaz commented 3 weeks ago

Bug

Current behavior

When dynamically defining a namespace name in the project.garden.yml using a variable passed through the CLI, the namespace name reverts to the default value instead of the one passed via the command line. For example, in the following configuration:

variables:
  my-variable: ${git.branch}
environments:
  - name: preview
    defaultNamespace: ns-${var.my-variable}

When running the command garden deploy --env preview --var my-variable=test, the expected namespace should be ns-test, but it instead uses the default value based on the git branch.

Expected behavior

The namespace name should be dynamically assigned based on the variable passed via the CLI (--var my-variable=test), not reverting to the default.

Reproducible example

  1. Define the following configuration in your project.garden.yml:
    variables:
    my-variable: ${git.branch}
    environments:
    - name: preview
    defaultNamespace: ns-${var.my-variable}
  2. Run the command:
    garden deploy --env preview --var my-variable=test
  3. Observe the namespace generated is not ns-test, but based on the default git branch instead.

Workaround

As a workaround, use environment variables to override the value:

variables:
  source-branch: ${local.env.SOURCE_BRANCH_OVERRIDE || git.branch}
environments:
  - name: preview
    defaultNamespace: ns-${var.source-branch}

And run the command with the following:

SOURCE_BRANCH_OVERRIDE=test garden deploy --env preview

Suggested solution(s)

N/A

Additional context

N/A

Your environment

garden version: 0.13.41

eysi09 commented 3 weeks ago

This looks like a legit bug but just FYI and to add to the list of workarounds, you can also set the namespace via the --env flag with the format <namespace>.<environment>.

For example:

garden deploy --env my-namespace.my-env

(From this guide: https://docs.garden.io/guides/namespaces)