使用 yq 从 YAML 映射对象中删除某些字段
Remove certain fields from a YAML map object using yq
我需要为 yaml 文件做一些棘手的事情,这是它之前的样子
之前
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
creationTimestamp: '2022-02-097T19:511:11Z'
finalizers:
- cuv.ssf.com
generation: 1
name: bar
namespace: foo
resourceVersion: '12236'
uid: 0117657e8
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
status:
conditions:
- lastTransitionTime: '2022-02-09T19:50:12Z'
message: test
observedGeneration: 1
reason: Ready
status: 'True'
type: Ready
lastOperation:
description: test
state: Succeeded
type: Reconcile
https://codebeautify.org/yaml-validator/y22fe4943
我需要删除 metadata
部分下的所有字段,棘手的部分是只保留 name
和 namespace
除了完全删除 status
部分之外,它应该如下所示
之后
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
name: bar
namespace: foo
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
link之后
https://codebeautify.org/yaml-validator/y220531ef
使用 yq (https://github.com/mikefarah/yq/) 版本 4.19.1
这应该在 https://github.com/kislyuk/yq (not https://github.com/mikefarah/yq 的实现中使用 yq
和 -y
(或 -Y
)标志:
yq -y '.items[] |= (del(.status) | .metadata |= {name, namespace})'
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
name: bar
namespace: foo
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
使用 mikefarah/yq (aka Go yq), you could do something like below. Using del for deleting an entry and with_entries 选择已知字段
yq '.items[] |= (del(.status) | .metadata |= with_entries(select(.key == "name" or .key == "namespace")))' yaml
开始v4.18.1,eval
标志是默认动作,所以e
标志可以避免
我需要为 yaml 文件做一些棘手的事情,这是它之前的样子
之前
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
creationTimestamp: '2022-02-097T19:511:11Z'
finalizers:
- cuv.ssf.com
generation: 1
name: bar
namespace: foo
resourceVersion: '12236'
uid: 0117657e8
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
status:
conditions:
- lastTransitionTime: '2022-02-09T19:50:12Z'
message: test
observedGeneration: 1
reason: Ready
status: 'True'
type: Ready
lastOperation:
description: test
state: Succeeded
type: Reconcile
https://codebeautify.org/yaml-validator/y22fe4943
我需要删除 metadata
部分下的所有字段,棘手的部分是只保留 name
和 namespace
除了完全删除 status
部分之外,它应该如下所示
之后
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
name: bar
namespace: foo
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
link之后 https://codebeautify.org/yaml-validator/y220531ef
使用 yq (https://github.com/mikefarah/yq/) 版本 4.19.1
这应该在 https://github.com/kislyuk/yq (not https://github.com/mikefarah/yq 的实现中使用 yq
和 -y
(或 -Y
)标志:
yq -y '.items[] |= (del(.status) | .metadata |= {name, namespace})'
apiVersion: v1
items:
- apiVersion: core.k8s.com/v1alpha1
kind: Test
metadata:
name: bar
namespace: foo
spec:
certificateIssuer:
acme:
email: myemail
provider:
credentials: dst
type: foo
domain: vst
type: bar
使用 mikefarah/yq (aka Go yq), you could do something like below. Using del for deleting an entry and with_entries 选择已知字段
yq '.items[] |= (del(.status) | .metadata |= with_entries(select(.key == "name" or .key == "namespace")))' yaml
开始v4.18.1,eval
标志是默认动作,所以e
标志可以避免