Trying to set the affinity for the keda operator installed by the keda helm chart.
And I can’t seem to get terraform to like how I am doing it…
locals {
affinity_json = {
"nodeaffinity" : {
"requiredDuringSchedulingIgnoredDuringExecution" : {
"nodeSelectorTerms" : [
{
"matchExpressions" : [
{
"key" : "karpenter.sh/capacity-type",
"operator" : "NotIn",
"values" : ["spot"],
}
],
}
],
},
}
}
affinity_yaml = <<-EOF
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: karpenter.sh/capacity-type
operator: NotIn
values:
- spot
EOF
}
resource "helm_release" "keda" {
name = "keda"
repository = "https://kedacore.github.io/charts"
chart = "keda"
namespace = kubernetes_namespace.keda.metadata.0.name
version = "2.14"
create_namespace = false
# uninstall: https://keda.sh/docs/2.14/deploy/#helm
set {
name = "serviceAccount.operator.name"
value = "keda"
}
set {
name = "serviceAccount.operator.create"
value = "false"
}
set {
name = "operator.affinity"
value = jsonencode(local.affinity_json)
}
depends_on = [kubernetes_service_account.keda_service_account, kubernetes_namespace.keda]
}
The issue is with the key setting for operator.affinity
I tried several ways to get the value into that key
value = jsonencode(local.affinity_json)
(error from terraform, no apply)
Error: failed parsing key “operator.affinity” with value {“nodeaffinity”:{“requiredDuringSchedulingIgnoredDuringExecution”:{“nodeSelectorTerms”:[{“matchExpressions”:[{“key”:”karpenter.sh/capacity-type”,”operator”:”NotIn”,”values”:[“spot”]}]}]}}}, key “]}]}}}” has no value
value = local.affinity_json
(error from terraform, no apply)
Inappropriate value for attribute “value”: string required.
value = local.affinity_yaml
(error from terraform attempt to apply)
Error: cannot patch “keda-operator” with kind Deployment: “” is invalid: patch: Invalid value: …lots of json…
json: cannot unmarshal string into Go struct field PodSpec.spec.template.spec.affinity of type v1.Affinity
value = yamlencode(local.affinity_yaml)
(error from terraform attempt to apply)
Error: cannot patch “keda-operator” with kind Deployment: “” is invalid: patch: Invalid value: …lots of json…
json: cannot unmarshal string into Go struct field PodSpec.spec.template.spec.affinity of type v1.Affinity
And I can’t seem to get anything to work.