For yq - how do we get the data parameter in kubernetes configmap

4/26/2021

I am working on a way to manipulate the ConfigMap for kubernetes with Mike Farah's yq.

apiVersion: v1
kind: ConfigMap
metadata:
  name: game-config
  namespace: default
data:
  game.properties: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30    
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice  

I want to update the game.properties value - lives to 999.

However when I try below commands i get the error respectively.

$ yq e '.data.[game.properties]="enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30    \n"' test-configmap.yaml
Error: Parsing expression: Lexer error: could not match text starting at 1:8 failing at 1:9.
        unmatched text: "g"

I think the problem is in accessing the data.

$ yq e ".data[0]" test-configmap.yaml
null
$ yq e ".data.[0]" test-configmap.yaml
null
$ yq e ".data.[game.properties]" test-configmap.yaml
Error: Parsing expression: Lexer error: could not match text starting at 1:8 failing at 1:9.
        unmatched text: "g"

But when I try below I get the values of the data:

yq e ".data.[]" test-configmap.yaml
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

It is strange that it doesnt let me access the data name, i.e. game.properties and ui.properties.

-- Chinmaya Biswal
kubernetes
yq

1 Answer

4/26/2021

Looks like I found out how to do it. So we have to use double quotes for accessing the data field parameters.

Adding my command for reference.

yq e '.data."game.properties"="enemies=aliens\nlives=999\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase
=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30    \n"' test-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T18:52:05Z
  name: game-config
  namespace: default
  resourceVersion: "516"
  uid: b4952dc3-d670-11e5-8cd0-68f728db1985
data:
  game.properties: |-
    enemies=aliens\nlives=999\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30    \n
  ui.properties: "color.good=purple\ncolor.bad=yellow\nallow.textmode=true\nhow.nice.to.look=fairlyNice  \n"
-- Chinmaya Biswal
Source: StackOverflow