如何使用"kubectl补丁--type ='json'''更新机密 [英] How to update secret with "kubectl patch --type='json'"

查看:79
本文介绍了如何使用"kubectl补丁--type ='json'''更新机密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这样一个秘密:

I created a secret like this:

kubectl create secret generic test --from-literal=username=testuser --from-literal=password=12345

我想将用户名更新为 testuser2 ,但是我只想使用 kubectl patch --type ='json'来执行此操作.

I want to update the username to testuser2 but I want to do it only with kubectl patch --type='json'.

这是我尝试执行的操作:

This is how I tried to do it:

kubectl patch secret test --type='json' -p='[{"data":{"username": "testuser 2"}}]' -v=1  

但是我收到了:

"无效

请记住,我想使用-type ='json'选项,没有其他解决方法.

Remember, I want to do it with the option of --type='json', no other workarounds.

推荐答案

阅读

I found how to do it after I read here that referred me to this great article.
This is the JSON secret:

{
    "apiVersion": "v1",
    "data": {
        "password": "aWx1dnRlc3Rz",
        "username": "dGVzdHVzZXI="
    },
    "kind": "Secret",
    "metadata": {
        "creationTimestamp": "2019-04-18T11:37:09Z",
        "name": "test",
        "namespace": "default",
        "resourceVersion": "3017",
        "selfLink": "/api/v1/namespaces/default/secrets/test",
        "uid": "4d0a763e-61ce-11e9-92b6-0242ac110015"
    },
    "type": "Opaque"
}

因此,要更新用户字段,我需要创建JSON补丁格式:

Therefore, to update the user's field I needed to create the JSON Patch format:

[
    {
        "op" : "replace" ,
        "path" : "/data/username" ,
        "value" : "dGVzdHVzZXIy" # testuser2 in base64
    }
]

请注意,该值应位于base64中.

Notice that the value should be in base64.

结果是:

kubectl patch secret test --type='json' -p='[{"op" : "replace" ,"path" : "/data/username" ,"value" : "dGVzdHVzZXIy"}]'

这篇关于如何使用"kubectl补丁--type ='json'''更新机密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆