gpt4 book ai didi

yaml - yq:如何读取兄弟元素的值?

转载 作者:行者123 更新时间:2023-12-05 04:57:56 25 4
gpt4 key购买 nike

我正在探索 yq 以修改我的 YAML,我想在 name == openshift45 的 ImageStream 的 spec 下添加一个新元素

apiVersion: v1
items:
- apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
creationTimestamp: null
labels:
app: openshift45
app.kubernetes.io/component: openshift45
app.kubernetes.io/instance: openshift45
name: nodejs-10
spec:
lookupPolicy:
local: false
tags:
- annotations:
openshift.io/imported-from: registry.access.redhat.com/ubi8/nodejs-10
from:
kind: DockerImage
name: registry.access.redhat.com/ubi8/nodejs-10
generation: null
importPolicy: {}
name: latest
referencePolicy:
type: ""
status:
dockerImageRepository: ""
- apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
creationTimestamp: null
labels:
app: openshift45
app.kubernetes.io/component: openshift45
app.kubernetes.io/instance: openshift45
name: openshift45
spec:
lookupPolicy:
local: false
status:
dockerImageRepository: ""

下面的命令返回有效的 metadata 元素。现在,我想移动到父级,然后选择 specyq 是否可行 - https://github.com/mikefarah/yq

yq r openshift45.yaml --printMode pv "items(kind==ImageStream).(name==openshift45)"

返回

items.[1].metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
creationTimestamp: null
labels:
app: openshift45
app.kubernetes.io/component: openshift45
app.kubernetes.io/instance: openshift45
name: openshift45

预期输出:

apiVersion: v1
items:
- apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
creationTimestamp: null
labels:
app: openshift45
app.kubernetes.io/component: openshift45
app.kubernetes.io/instance: openshift45
name: nodejs-10
spec:
lookupPolicy:
local: false
tags:
- annotations:
openshift.io/imported-from: registry.access.redhat.com/ubi8/nodejs-10
from:
kind: DockerImage
name: registry.access.redhat.com/ubi8/nodejs-10
generation: null
importPolicy: {}
name: latest
referencePolicy:
type: ""
status:
dockerImageRepository: ""
- apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
annotations:
openshift.io/generated-by: OpenShiftNewApp
creationTimestamp: null
labels:
app: openshift45
app.kubernetes.io/component: openshift45
app.kubernetes.io/instance: openshift45
name: openshift45
spec:
*dockerImageRepository: <$MYREGISTRY>/<$MYNAMESPACE>/<$MYPROJECT>*
lookupPolicy:
local: false
status:
dockerImageRepository: ""

最佳答案

Path Expressions in mikefarah/yq没有完整的文档来展示如何使用多个条件来获得所需对象的真实示例。因此,对于有问题的 YAML,使用一个独特的条件,您可以执行如下操作。在 yq 版本 3.3.2

中验证
yq w openshift45.yaml 'items.(metadata.name == openshift45).spec.dockerImageRepository' '<$MYREGISTRY>/<$MYNAMESPACE>/<$MYPROJECT>'

您可以使用 -i 标志和 write 来就地修改 YAML。参见 Updating files in-place

如果这不是您想要的,并且您需要多个条件选择才能获得所需的对象,建议在 GitHub 页面上提出问题 - https://github.com/mikefarah/yq/issues请求正确的语法。

关于yaml - yq:如何读取兄弟元素的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64213503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com