1

我有以下 yml 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: queue
spec:
  selector:
    matchLabels:
      app: queue
  replicas: 1
  template: # template for the pods
    metadata:
      labels:
        app: queue
    spec:
      containers:
      - name: 
        image: 
        resources:
          requests:
            memory: 300Mi

我需要在部署部分中指定名称和图像种类:部署元数据:名称:队列

我需要在此文件中输入以下字符串以进行部署

 spec:
      containers:

部分。

go:dev
go:latest

期望的输出

apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: queue
    spec:
      selector:
        matchLabels:
          app: queue
      replicas: 1
      template: # template for the pods
        metadata:
          labels:
            app: queue
        spec:
          containers:
          - name: go:dev
            image: go:latest
            resources:
              requests:
                memory: 300Mi

是否可以使用一些正则表达式或类似的 shell 脚本执行上述操作?

4

1 回答 1

2

@RavinderSingh13 关于不使用 或其他一些字符串实用程序是正确awksed。使用yq是执行此操作的正确方法。

https://github.com/mikefarah/yq

https://mikefarah.gitbook.io/yq/

我用过yq version 4.6.3

要单独更新每个属性,请运行以下命令。-i就地修改文件。

yq -i eval '.spec.template.spec.containers[0].name = "go:dev"' file.yml
yq -i eval '.spec.template.spec.containers[0].image = "go:latest"' file.yml

yq查询结果也可以通过管道传输。这可以在这里利用在单个命令中更新两个字段。

yq -i eval '.spec.template.spec.containers[0].name = "go:dev" | 
            .spec.template.spec.containers[0].image = "go:latest"' file.yml
于 2021-04-13T00:00:41.250 回答