0

Below is a k8s configmap configuration, I need to use the kubectl patch command to update it, but don't know how to do it

# kubectl get configmap myconfig -o yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: debug-config
data:
  config.json: |-
    {
        "portServiceDMS": 500,
        "Buggdse": {
            "Enable": false
        },
        "GHInterval": {
            "Start": 5062,
            "End": 6000
        },
        "LOPFdFhd": false,
        "CHF": {
            "DriverName": "mysql"
        },
        "Paralbac": {
            "LoginURL": "https://127.0.0.1:7788",
            "Sources": [
                {
                    "ServiceName": "Hopyyu",
                    "Status": false,
                    "ServiceURL": "https://127.0.0.1:9090/ft/test"
                },
                {
                    "SourceName": "Bgudreg",
                    "Status": false, # need to patch here to true
                    "ServiceURL": "https://127.0.0.1:9090"  # need to patch here to  "https://192.168.123.177:45663"
                }
            ]
        }
    }

I searched on google site to find a similar way to deal with it, but it doesn't work

I tried this command and it doesn't work:

kubectl get cm myconfig -o json | jq -r '.data."config.json".Paralbac.Sources[1]={"SourceName": "Bgudreg", "Status": true, "ServiceURL": "https://192.168.123.177:45663"}' | kubectl apply -f -

I reduced the command to here:

kubectl get cm myconfig -o json | jq -r '.data."config.json" # it works (The double quotes are for escaping the dot)

kubectl get cm myconfig -o json | jq -r '.data."config.json".Paralbac # it can't work:   jq: error (at <stdin>:18): Cannot index string with string "Paralbac"


So, I think my current problem is in how to keep working after escaped symbols in jq

cydia
  • 103
  • 12
  • For the `patch` problem, what have you tried and what outcome is it producing for you? Please read the [how to ask](https://stackoverflow.com/help/how-to-ask) page, and pay especial attention to the [MCVE](https://stackoverflow.com/help/mcve) section – mdaniel Feb 25 '22 at 04:12
  • @mdaniel Thanks for the tip, I've updated what I've tried to above – cydia Feb 25 '22 at 05:11

1 Answers1

1

Here's how you can update the ConfigMap in the question:

myconfig=$(mktemp) \
  && kubectl get configmap debug-config -o jsonpath='{.data.config\.json}' \
  | jq '.Paralbac.Sources[1].Status = true' \
  | jq '.Paralbac.Sources[1].ServiceURL = "https://192.168.123.177:45663"' > myconfig \
  && kubectl create configmap debug-config --from-file=config.json=myconfig --dry-run=client -o yaml | kubectl replace -f - \
  && rm myconfig

Now do kubectl get configmap debug-config -o jsonpath='{.data.config\.json}' | jq will show you the updated config.json in the ConfigMap.

gohm'c
  • 13,492
  • 1
  • 9
  • 16