Here is my deploy input
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"io.kompose.service": "item-api"
}
},
"strategy": {
"type": "Recreate"
},
"template": {
"spec": {
"containers": [
{
"env": [
{
"name": "APP_JWTSECRET",
"valueFrom": {
"configMapKeyRef": {
"key": "APP_JWTSECRET",
"name": "item-api-env"
}
}
},
{
"name": "SPRING_DATASOURCE_BASEXML_JDBCURL",
"valueFrom": {
"configMapKeyRef": {
"key": "SPRING_DATASOURCE_BASEXML_JDBCURL",
"name": "item-api-env"
}
}
}
...
For each item into env
, I’d like to transform the configMapKeyRef
into secretKeyRef
if name contains (SECRET|PASSWORD|KEY) pattern and then replace the secretKeyRef name by the key name into lowcase.
for example:
"name": "APP_JWTSECRET",
"valueFrom": {
"configMapKeyRef": {
"key": "APP_JWTSECRET",
"name": "item-api-env"
}
}
}
would be transformed into
"name": "APP_JWTSECRET",
"valueFrom": {
"secretKeyRef": {
"key": "APP_JWTSECRET",
"name": "app-jwtsecret"
}
}
}
I tried some kind of manipulation with with_entries
without any success:
jq -r '.spec.template.spec.containers[].env[]|with_entries(.key |test(PASSWORD|SECRET))'
3
Answers
One way to use
jq
would be to do below.jqplay demo – https://jqplay.org/s/UfYQYpJIa6o
.spec.template.spec.containers[].env[]?
select(.name | test("SECRET|PASSWORD|KEY"))
(…).valueFrom
.key
and.value
:|= with_entries(…)
select(.key == "configMapKeyRef") |= …
{key: "secretKeyRef", value: …}
.value | .name = (.key | ascii_downcase)
Demo
Note: I regarded the change from
app_jwtsecret
toapp-jwtsecret
a typo.Here’s one way you could do it. The
containers
andenv
array are rewritten with the desired modification, or kept "as is" if the condition isn’t met.Try it on jqplay.org.