如何使用 kubectl patch 命令更新此配置映射,而不使用 kubectl edit 命令

发布于 2025-01-10 06:33:07 字数 1935 浏览 0 评论 0原文

下面是一个k8s configmap配置,我需要使用kubectl patch命令来更新它,但不知道该怎么做

# 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"
                }
            ]
        }
    }

我在 google 网站上搜索找到类似的方法来处理 ,但是不起作用

我尝试了这个命令,但不起作用:

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 -

我将命令减少到这里:

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"


所以,我认为我当前的问题是如何在 escaped 符号之后继续工作jq

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2025-01-17 06:33:07

以下是更新问题中的 ConfigMap 的方法:

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

现在执行 kubectl get configmap debug-config -o jsonpath='{.data.config\.json}' | jq 将向您显示 ConfigMap 中更新后的 config.json

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文