简体   繁体   English

kubectl 补丁:是否可以在单个补丁执行中向数组添加多个值

[英]kubectl patch: Is it possible to add multiple values to an array within a sinlge patch execution

I tried to use kubectl patch to add two more values to the args list of a kube.netes deployment.我尝试使用kubectl patch将两个值添加到 kube.netes 部署的 args 列表中。 I've gone over the officially documented ( https://kube.netes.io/docs/tasks/manage-kube.netes-objects/update-api-object-kubectl-patch/ ) variants but did not manage to append more than one value at a time.我已经查看了官方记录的 ( https://kube.netes.io/docs/tasks/manage-kube.netes-objects/update-api-object-kubectl-patch/ ) 变体,但没有管理到 append 更多一次多于一个值。

Assume this simple deployment:假设这个简单的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      name: test
  template:
    metadata:
      labels:
        name: test
      name: test
    spec:
      containers:
      - image: alpine
        name: test
        command:
        - echo
        args:
        - my
        - text

I now want to append additional values to the args section.我现在想要 append 附加值到args部分。 This works for a single value at a time:这一次适用于单个值:

Adding a single additional value添加单个附加值

kubectl patch deployments.apps test --type=json -p='[{"op": "add", "path": "/spec/t
emplate/spec/containers/0/args/-", "value": "additional" }]'

This works and leaves me with the following:这有效并给我留下了以下内容:

...
        args:
        - my
        - text
        - additional

But running the patch with an array of values gives me an error:但是运行带有值数组的补丁会给我一个错误:

# running:
k patch deployments.apps test --type=json -p='[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": ["additional","text"] }]'

# results in:
The request is invalid: patch: Invalid value: "...": v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.Args: []string: ReadString: expects " or n, but found [, error found in #10 byte of ...|itional",["additiona|..., bigger context ...|{"containers":[{"args":["my","text","additional",["additional","text"]],"command":["echo"],"image":"|...

Does anyone know a way to add mutliple values to an array within a single patch command without overwriting the whole args array?有谁知道在不覆盖整个 args 数组的情况下在单个补丁命令中将多个值添加到数组的方法? Thanks for your help.谢谢你的帮助。

Below uses a single patch but it's not very DRY:下面使用一个补丁,但它不是很干燥:

kubectl patch deployment <deployment-name> -n <namespace> --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-1"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-2"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-3"}]'

I've been doing something similar for cert-manager to allow fully automated TLS:我一直在为cert-manager做类似的事情以允许全自动 TLS:

kubectl patch deployment cert-manager -n cert-manager --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-name=letsencrypt-prod"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-kind=ClusterIssuer"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-group=cert-manager.io"}]'

You can use kubectl edit command to edit the resource.您可以使用kubectl edit命令来编辑资源。
Example usage:示例用法:
kubectl edit deploy <deployment_name>

For more info, refer: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#edit .有关更多信息,请参阅: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#edit

Edit: There is a nasty way of doing it programmatically.编辑:有一种令人讨厌的方式以编程方式进行。 You can pipe the yaml to python, alter the values you want to change and apply the new yaml.您可以 pipe yaml 到 python,更改您要更改的值并应用新的 Z6EEDC03A68A6993C42F73 In your case, it would be something like,在你的情况下,它会是这样的,

kubectl get deploy <deploy_name> -o yaml | python -c 'import sys,yaml; yml = yaml.safe_load(sys.stdin); yml["spec"]["template"]["spec"]["containers"][0]["args"].extend(["newValue1", "newValue2"]); print(yaml.dump(yml));' | kubectl apply -f -

Obviously you want to do this only if there isn't any easier way to do it.显然,只有在没有更简单的方法可以做到这一点时,您才想这样做。

The simplest way I've found is to use jq to edit the json, so instead of:我发现最简单的方法是使用jq来编辑 json,而不是:

kubectl patch deployment <deployment-name> -n <namespace> --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-1"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-2"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-3"}]'

You can use:您可以使用:

kubectl get deployment <deployment-name> -n <namespace> -o json \
  | jq '.spec.template.spec.containers[0].args += ["arg-1", "arg-2", "arg-3"]'  \
  | kubectl apply -f -

This has an advantage: it allows to inject even objects such as patching permissions.这有一个优势:它甚至允许注入对象,例如修补权限。 Example ( taken from the requirements for upgrading coredns to 1.8.3 ):示例( 取自将 coredns 升级到 1.8.3 的要求):

kubectl get clusterrole system:coredns -n kube-system -o json \
  | jq '.rules += [{"apiGroups":["discovery.k8s.io"],"resources":["endpointslices"],"verbs":["list","watch"]}]' \
  | kubectl apply -f -

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

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