can we change this to an if/else conditional?```ya...
# 🤖|ask-kapa
m
Please stand by @quaint-dress-831 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- Yes, you can change the ternary operator to an if/else conditional in Garden. Here's how you can do it:
Copy code
yaml
spec:
  image: 
    $if: "${local.env.GARDEN_DEV_SERVICE == 'gateway'}"
    $then: "${var.registryRoot + '/api-gateway:latest'}"
    $else: "${actions.build.gateway.outputs.deploymentImageId}"
This sets
spec.image
to
${var.registryRoot + '/api-gateway:latest'}
when the
GARDEN_DEV_SERVICE
environment variable is set to 'gateway', otherwise it sets
spec.image
to
${actions.build.gateway.outputs.deploymentImageId}
. However, if
GARDEN_DEV_SERVICE
is not provided at all, Garden will still throw an error. To avoid this, you can use the
local.env.<env-var-name>
syntax to check if the environment variable exists before using it in a condition. Unfortunately, the extracts provided do not contain information on how to do this. For more information on using if/else conditionals and environment variables in Garden, you can refer to the Variables and Templating () section of the Garden documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
Let me check the knowledge base for you. One moment! :books:
q
@microscopic-river-74022 please produce an example that sets
${local.env.GARDEN_DEV_SERVICE == 'gateway'}
as an optional value e.g.
Copy code
${var.kubeconfig}?
m
---------------------- Sure, you can set
GARDEN_DEV_SERVICE
as an optional value using the
?
operator. Here's how you can do it:
Copy code
yaml
spec:
  image: 
    $if: "${{local.env.GARDEN_DEV_SERVICE? == 'gateway'}}"
    $then: "${{var.registryRoot + '/api-gateway:latest'}}"
    $else: "${{actions.build.gateway.outputs.deploymentImageId}}"
In this case, if
GARDEN_DEV_SERVICE
is not provided, Garden will not throw an error and the condition will evaluate to false, setting
spec.image
to
${{actions.build.gateway.outputs.deploymentImageId}}
. For more information on using optional values in Garden, you can refer to the Optional values () section of the Garden documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
f
Yeah, Kapa seems to hallucinate a fair bit.
But I'll try all of these.
q
We hadn't noticed hallucinations before but this is good feedback for @few-fish-16423
@flat-state-47578 if you can 👎 and mention @few-fish-16423 in any answers where you've noticed hallucinations that would be helpful. I can also take a look
In this case, I'm just using kapa to expand on what I know. Optional values and if/else conditionals are both in the docs at https://docs.garden.io/using-garden/variables-and-templating#optional-values if you wanted to cross-reference
f
Yes, I've been trying to do that 🙂
@few-fish-16423 I don't believe this
?
syntax is valid:
Copy code
Invalid template string (${{local.env.GARDEN_DEV_SERVICE? =…): Unable to parse as valid template string.