简体   繁体   English

如何使用“kubectl patch --type='json'”更新机密

[英]How to update secret with "kubectl patch --type='json'"

I created a secret like this:我创建了一个这样的秘密:

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

I want to update the username to testuser2 but I want to do it only with kubectl patch --type='json' .我想将用户名更新为testuser2 ,但我只想使用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  

But I received:但我收到了:

The "" is invalid “”无效

Remember, I want to do it with the option of --type='json' , no other workarounds.请记住,我想使用--type='json'选项来完成它,没有其他解决方法。

I found how to do it after I read here that referred me to this great article.在我阅读这篇文章后,我发现了如何做到这一点。
This is the JSON secret:这是 JSON 的秘密:

{
    "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"
}

Therefore, to update the user's field I needed to create the JSON Patch format:因此,要更新用户的字段,我需要创建 JSON Patch 格式:

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

Notice that the value should be in base64.请注意,该值应为 base64。

The result is:结果是:

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

这是我为了替换秘密所做的,希望它有所帮助

kubectl patch secret my-secret  --patch="{\"data\": { \"NEW_PASSWORD\": \"$(echo -n mypassword |base64)\" }}" -oyaml

This command solved my issue on version 1.24.x:此命令解决了我在 1.24.x 版上的问题:

kubectl patch secret app-sec  --patch="{\"data\": { \"license-id\": \"TEST\" }}" -oyaml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM