我尝试过多次不同的组合,但我无法使它工作。这是我的yq命令
yq -i e '(.spec.template.spec.containers[]|select(.name == "od-fe").image) = "abcd"
它应该替换成功的部署映像,但也会将template.spec.containers添加到服务中。下面是部署+服务yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: od
name: od-fe
spec:
replicas: 2
selector:
matchLabels:
app: od-fe
template:
metadata:
labels:
app: od-fe
spec:
containers:
- name: od-fe
image: od-frontend:latest. <<<replace here only
imagePullPolicy: Always
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
namespace: od
name: od-fe-service
labels:
run: od-fe-service
spec:
ports:
- port: 3000
targetPort: 3000
protocol: TCP
type: NodePort
selector:
app: od-fe现在的问题是,服务也被改变成
apiVersion: v1
kind: Service
metadata:
namespace: od
name: od-fe-service
labels:
run: od-fe-service
spec:
ports:
- port: 3000
targetPort: 3000
protocol: TCP
type: NodePort
selector:
app: od-fe
template:
spec:
containers: []发布于 2022-02-19 17:48:46
解决这一问题的一种方法是在顶层包含一个select语句,以便只对Deployment类型执行操作。
yq e '(select(.kind == "Deployment").spec.template.spec.containers[]|select(.name == "od-fe").image) |= "abcd"' yaml注意:如果您使用的是yq版本4.18.1或更高版本,则将the eval flag e is no longer needed作为默认操作。
https://stackoverflow.com/questions/71187168
复制相似问题