clever-policeman-58407
01/29/2024, 8:28 PMconfigurationValues
object when a certain environment is detected I would expect to be able to use Garden's $if; $then; $else
syntax to conditionally include or exclude that object.
i.e.
Starting from this:
SomeParentValue:
configurationValues:
debug: true
application_url: "localhost"
log_level: debug
I might then want to transition to a config like this:
SomeParentValue:
$if: ${local.env.DEBUG_MODE}
$then:
configurationValues:
debug: true
application_url: "localhost"
log_level: debug
This doesn't appear to function or validate. Is there a canonical way to achieve this?big-spring-14945
02/01/2024, 2:46 PMapiVersion: garden.io/v1
kind: Project
name: foo
environments:
- name: dev
providers: []
---
kind: Test
type: exec
name: test
spec:
command: [echo, "${yamlEncode(var.conditionalObject)}"]
variables:
conditionalObject:
$if: ${local.env.FOO == "1"}
$then:
foo: bar
$else:
bar: baz
When you run FOO=1 garden test -l3
it behaves as expected (printing foo: bar
)
I guess where things become interesting if SomeParentValue
also has other keys:
conditionalObject:
hello: world
$if: ${local.env.FOO == "1"}
$then:
foo: bar
$else:
bar: baz
This leads to the error Found one or more unexpected keys on $if object: "hello". Expected: $if, $then and $else
You can solve that by using `$merge`:
conditionalObject:
hello: world
$merge:
$if: ${local.env.FOO == "1"}
$then:
foo: bar
$else:
bar: baz
The command FOO=1 garden test -l3
with this config results in
hello: world
foo: bar
big-spring-14945
02/01/2024, 2:47 PMclever-policeman-58407
02/06/2024, 5:55 PMproviders.deploymentRegistry
based on which Kubernetes cluster tool was running and attempting to do something similar results in the following:
Found one or more unexpected keys on $if object: "name", "environments", "namespace", "defaultHostname", "context", "setupIngressController", "sync" and "dependencies". Expected: $if, $then and $else
clever-policeman-58407
02/06/2024, 5:56 PMapiVersion: garden.io/v1
kind: Project
name: foobar
defaultEnvironment: local
environments:
- name: local
defaultNamespace: default
providers:
- name: local-kubernetes
environments: [local]
namespace: foobar
defaultHostname: localhost
context: "k3d-default"
setupIngressController: nginx
$if: ${local.env.FOO == "1"}
$then:
deploymentRegistry:
hostname: k3d-registry
port: 5000
insecure: true
namespace: ${kebabCase(local.username)}
clever-policeman-58407
02/06/2024, 5:58 PMclever-policeman-58407
02/06/2024, 6:00 PM$if: ${environment.name} == local
rather than being forced to do something like $if: ${environment.name} in [ 'local-k3d', 'local-rancher', 'local-k3s' ]
and needing to maintain that list an undefined number of times in every place environment name based logic is usedclever-policeman-58407
02/06/2024, 6:03 PMis-local
or similarly named environment variable.
Functional, but messier and more maintenance than just being able to have them all function as literally the same environment. Every user and every tool now has to specify which environment it is using rather than being able to rely on the default local implementation.big-spring-14945
03/06/2024, 2:06 PMYAML
apiVersion: garden.io/v1
kind: Project
name: foobar
defaultEnvironment: local
environments:
- name: local
defaultNamespace: default
providers:
- name: local-kubernetes
environments: [local]
namespace: foobar
defaultHostname: localhost
context: "k3d-default"
setupIngressController: nginx
$merge:
$if: ${local.env.FOO == "1"}
$then:
deploymentRegistry:
hostname: k3d-registry
port: 5000
insecure: true
namespace: ${kebabCase(local.username)}
For differing configs across different environments, one can declare environment-specific variables like so; Maybe this is the way to go in your case:
YAML
apiVersion: garden.io/v1
kind: Project
name: foobar
defaultEnvironment: local
environments:
- name: local
defaultNamespace: default
variables:
deploymentRegistry:
$if: ${local.env.FOO == "1"}
$then:
hostname: k3d-registry
port: 5000
insecure: true
namespace: ${kebabCase(local.username)}
providers:
- name: local-kubernetes
environments: [local]
namespace: foobar
defaultHostname: localhost
context: "k3d-default"
setupIngressController: nginx
deploymentRegistry: ${var.deploymentRegistry}
clever-policeman-58407
03/08/2024, 8:16 PM