我有一个json结构,它包含了kubectl - jsonpath='{.spec.template.metadata.annotations}的注释列表。
{
"kubectl.kubernetes.io/restartedAt": "2022-03-09T16:09:47-08:00",
"sidecar.istio.io/proxyCPU": "100m",
"sidecar.istio.io/proxyCPULimit": "2",
"sidecar.istio.io/proxyMemory": "128Mi",
"sidecar.istio.io/proxyMemoryLimit": "1Gi",
"traffic.sidecar.istio.io/excludeOutboundPorts": "8200",
"vault.hashicorp.com/agent-cache-enable": "true",
"vault.hashicorp.com/agent-cache-use-auto-auth-token": "force",
"vault.hashicorp.com/agent-init-first": "true",
"vault.hashicorp.com/agent-inject": "true",
"vault.hashicorp.com/ca-cert": "/run/secrets/kubernetes.io/serviceaccount/ca.crt",
"vault.hashicorp.com/preserve-secret-case": "true",
"vault.hashicorp.com/role": "my-role",
"vault.hashicorp.com/secret-volume-path-a": "/var/path",
"vault.hashicorp.com/secret-volume-path-b": "/var/path",
"vault.hashicorp.com/secret-volume-path-c": "/var/path",
"vault.hashicorp.com/secret-volume-path-d": "/var/path",
"vault.hashicorp.com/secret-volume-path-e": "/var/path"
}我希望使用jq只获取包含“储藏室”的键,因此输出应该是:
{
"vault.hashicorp.com/agent-cache-enable": "true",
"vault.hashicorp.com/agent-cache-use-auto-auth-token": "force",
"vault.hashicorp.com/agent-init-first": "true",
"vault.hashicorp.com/agent-inject": "true",
"vault.hashicorp.com/ca-cert": "/run/secrets/kubernetes.io/serviceaccount/ca.crt",
"vault.hashicorp.com/preserve-secret-case": "true",
"vault.hashicorp.com/role": "my-role",
"vault.hashicorp.com/secret-volume-path-a": "/var/path",
"vault.hashicorp.com/secret-volume-path-b": "/var/path",
"vault.hashicorp.com/secret-volume-path-c": "/var/path",
"vault.hashicorp.com/secret-volume-path-d": "/var/path",
"vault.hashicorp.com/secret-volume-path-e": "/var/path"
}我试过用这样的方法
$ kubectl get deployment test -n test -o jsonpath='{.spec.template.metadata.annotations}' |jq '. | to_entries[] | select(.key | startswith("vault"))'
{
"key": "vault.hashicorp.com/agent-cache-enable",
"value": "true"
}
{
"key": "vault.hashicorp.com/agent-cache-use-auto-auth-token",
"value": "force"
}但我不知道如何将输出转换回原来的json格式。
发布于 2022-03-10 23:22:30
to_entries将对象解构为键值对数组.使用map(…)可以在不分解数组的情况下修改其项,并使用from_entries从修改后的数组重构原始对象:
… | jq 'to_entries | map(select(.key | startswith("vault"))) | from_entries'或者使用with_entries(…),这是to_entries | map(…) | from_entries的内置快捷方式。
… | jq 'with_entries(select(.key | startswith("vault")))'{
"vault.hashicorp.com/agent-cache-enable": "true",
"vault.hashicorp.com/agent-cache-use-auto-auth-token": "force",
"vault.hashicorp.com/agent-init-first": "true",
"vault.hashicorp.com/agent-inject": "true",
"vault.hashicorp.com/ca-cert": "/run/secrets/kubernetes.io/serviceaccount/ca.crt",
"vault.hashicorp.com/preserve-secret-case": "true",
"vault.hashicorp.com/role": "my-role",
"vault.hashicorp.com/secret-volume-path-a": "/var/path",
"vault.hashicorp.com/secret-volume-path-b": "/var/path",
"vault.hashicorp.com/secret-volume-path-c": "/var/path",
"vault.hashicorp.com/secret-volume-path-d": "/var/path",
"vault.hashicorp.com/secret-volume-path-e": "/var/path"
}https://stackoverflow.com/questions/71431880
复制相似问题