使用bash脚本在Kubernetes YAML中删除和添加特定键值对

huangapple go评论62阅读模式
英文:

Remove and add specific key value pair using bash script into Kubernetes YAML

问题

我想要将特定键值对添加和移除到Kubernetes的YAML文件中

例如:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: null
    version: latest
  name: null
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: null
      version: latest
  template:
    metadata:
      labels:
        app: null
        version: latest
    spec:
      containers:
      - image: aude-service
        name: aude-api
        ports:
        - containerPort: 3003

要在选择器后删除spec中的内容:

hostPort: 3003
protocol: TCP

要添加的内容:

strategy:
  type: RollingUpdate 
  rollingUpdate:
    maxUnavailable: 25% 

这将非常有帮助,如果有人能提供解决方案。

所以,我尝试使用bash来删除hostport键,但它不起作用

awk '/^[[:alpha:]]+: {level1=$0; next} level1 ~ "hostPort" && /aaa/ {next} 1' aude-convert.yml
英文:

I want to add and remove specific key value pair into kubernetes yamls

For eg:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: null
    version: latest
  name: null
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: null
      version: latest
  template:
    metadata:
      labels:
        app: null
        version: latest
    spec:
      containers:
      - image: aude-service
        name: aude-api
        ports:
        - containerPort: 3003
          hostPort: 3003
          protocol: TCP

Contents to be removed in spec after selector:

hostPort: 3003
protocol: TCP

Contents to be added:

strategy:
  type: RollingUpdate 
  rollingUpdate:
    maxUnavailable: 25% 

It would be of great help, if anyone suggest solution

So, I tried using bash to remove hostport key but it didn't work

awk '/^[[:alpha:]]+:/ {level1=$0; next} level1 ~ "hostPort" && /aaa/ {next} 1' aude-convert.yml

答案1

得分: 1

如果你想尝试一些不同的东西,你可以使用 yq 来实现相同的效果。yq 是一个轻量级且便携的命令行 YAML 处理器。yq 使用类似 jq 的语法,但可以处理 yaml 文件以及 json。在 这里 了解更多信息。

所以在你的情况下,

从 yaml 中删除内容

yq -i 'del(.spec.template.spec.containers[0].ports[0].hostPort) | del(.spec.template.spec.containers[0].ports[0].protocol)' deployment.yaml

我们在 ports 数组的第一个元素中删除了 hostPortprotocol 条目。

这为你提供了很多灵活性,例如,如果你有多个 containerPorthostPort 条目,你可以轻松选择 ports 数组的第二个元素:

yq -i 'del(.spec.template.spec.containers[0].ports[1].hostPort) | del(.spec.template.spec.containers[0].ports[1].protocol)' deployment.yaml

或者删除所有元素:

yq -i 'del(.spec.template.spec.containers[0].ports[].hostPort) | del(.spec.template.spec.containers[0].ports[].protocol)' deployment.yaml

同样的方式也适用于 containers[0],不提供示例,但我希望你明白了。非常灵活。


向 yaml 中添加所需内容

yq -i '.spec.strategy = {"type": "RollingUpdate", "rollingUpdate": {"maxUnavailable": "25%"}}' deployment3.yaml

这在文件末尾的 spec 中添加了所需的内容(如在 cmd 中使用 .spec.strategy 定义)。

要将其添加到 replicas 正下方:

yq -i '.spec |= {"replicas": .replicas, "strategy": {"type": "RollingUpdate", "rollingUpdate": {"maxUnavailable": "25%"}}, "selector": .selector, "template": .template}' deployment.yaml

将新的 strategy 键值对添加到 .spec 对象中,同时保留现有的 .replicas.selectortemplate 键。

这有助于避免使用 sed 时担心空格或制表符。

希望对你有所帮助。

英文:

If you wanna try something different, you could work with yq to achieve the same. yq is a lightweight and portable command-line YAML processor. yq uses jq like syntax but works with yaml files as well as json. Read more about it here

So, in your case

To remove the content from the yaml:

yq -i 'del(.spec.template.spec.containers[0].ports[0].hostPort) | del(.spec.template.spec.containers[0].ports[0].protocol)' deployment.yaml

Here we are removing the hostPort and protocol entries from the first element of the ports array.

This gives you a lot of flexibility, for eg if you have multiple containerPort & hostPort entries you could easily select the second element of the ports array:

yq -i 'del(.spec.template.spec.containers[0].ports[1].hostPort) | del(.spec.template.spec.containers[0].ports[1].protocol)' deployment.yaml

or both elements:

yq -i 'del(.spec.template.spec.containers[0].ports[].hostPort) | del(.spec.template.spec.containers[0].ports[].protocol)' deployment.yaml

and the same can be applied to containers[0] - not providing examples for this but I hope you got the point. Really flexible.


To add the required content to the yaml:

yq -i '.spec.strategy = {"type": "RollingUpdate", "rollingUpdate": {"maxUnavailable": "25%"}}' deployment3.yaml

This adds the required content at the end of the file within spec (as is defined using .spec.strategy in the cmd).

To add it right below replicas:

yq -i '.spec |= {"replicas": .replicas, "strategy": {"type": "RollingUpdate", "rollingUpdate": {"maxUnavailable": "25%"}}, "selector": .selector, "template": .template}' deployment.yaml

adding the new strategy key-value pair to the .spec object along with the existing .replicas, .selector & template key.

This helps with not worrying about spaces or tabs that would come with sed.

Hope it helps.

答案2

得分: 0

我假设您已将此YAML保存在deployment.yaml中。

  1. 从YAML中删除您给定的内容。

    sed -i '/hostPort/,/protocol/d' deployment.yaml
    
  2. 将您给定的内容添加到YAML中。

    sed -i '/replicas/a \  strategy:\n    type: RollingUpdate\n    rollingUpdate:\n      maxUnavailable: 25%' deployment.yaml
    
英文:

I assume you have saved this yaml in deployment.yaml.

  1. To delete your given content from the yaml.

    sed -i '/hostPort/,/protocol/d' deployment.yaml
    

    Here, /hostPort/,/protocol/ is the range pattern that specifies the lines we want to delete between the pattern hostPort and protocol (inclusive). d is for deleting the lines.

  2. To add your given content to the yaml.

    sed -i '/replicas/a \  strategy:\n    type: RollingUpdate\n    rollingUpdate:\n      maxUnavailable: 25%' deployment.yaml
    

    Here, /replicas/a is the pattern after which we want to append the content. a command is for appending content after the given pattern.

    Take care of the spaces here as they should have a proper indentation. Here I have considered 1 tab = 2 spaces. Please modify them according to your indentation.

huangapple
  • 本文由 发表于 2023年4月17日 15:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定