使用ruamel.yaml库,解析yaml文件

在实现的需求如下:

同事提供了一个文本文件,内含200多个host与ip的对应关系,希望能在k8s生成pod时,将这些对应关系注入到/etc/hosts中。

网上看文档,这可以通过扩充pod中的hostAliases来实现。

实现的思路如下:

一,hosts文件内容示例

192.168.0.24       bi-server-3391
192.168.0.25       bi-server-3392
192.168.0.26       bi-server-3393
192.168.0.27       bi-server-3394
192.168.0.28       bi-server-3395
192.168.0.29       bi-server-3396
192.168.0.30       bi-server-3397
192.168.0.31       bi-server-3398
192.168.0.32       bi-server-3399
192.168.0.33       bi-server-3400
192.168.0.34       bi-server-3401
192.168.0.35       bi-server-3402
192.168.0.36       bi-server-3403
192.168.0.37       bi-server-3404
192.168.0.38       bi-server-3405
192.168.0.39       bi-server-3406
192.168.0.40       bi-server-3407

二,org_dep.yaml文件内容

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxx-ai-jupyter-v2
spec:
  replicas: 1
  selector:
    matchLabels:
      name: xxx-ai-jupyter-v2
  template:
    metadata:
      labels:
        name: xxx-ai-jupyter-v2
    spec:
      imagePullSecrets:
      - name: xxx
      nodeSelector:
        accelerator: nvidia-tesla-k80
      containers:
      - name: xxx-ai-jupyter-v2
        image: harbor.xxx.com.cn/3rd_part/tensorflow:xxx
        imagePullPolicy: IfNotPresent
        command: ["bash", "-c", "jupyter notebook --notebook-dir=/tf --ip 0.0.0.0  --no-browser --allow-root --NotebookApp.allow_remote_access=True --NotebookApp.disable_check_xsrf=True --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.allow_origin='*' --NotebookApp.allow_origin='*'"]
        resources:
          limits:
            nvidia.com/gpu: 4
        volumeMounts:
        - mountPath: /tf
          name: jupyter-data
      volumes:
      - name: jupyter-data
        hostPath:
          # directory location on host
          path: /docker/jupyter_data
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "bar.local"
      - ip: "10.1.2.3"
        hostnames:
        - "bar.remote"

三,解析yaml并新增hostsAliases段的python脚本

# coding:utf-8

from ruamel import yaml as ruamel_yaml
import yaml

import os

cur_path = os.path.dirname(os.path.realpath(__file__))
org_dep_yaml = os.path.join(cur_path, "org_dep.yaml")
hosts_file = os.path.join(cur_path, "hosts")

f1 = open(org_dep_yaml)
d1 = yaml.load(f1)

yaml_host = d1['spec']['template']['spec']['hostAliases']
with open("hosts", 'r') as f:
    for i in f:
        if len(i.strip()) > 0:
            temp_list = i.split()
            temp_dict = dict()
            temp_dict['ip'] = temp_list[0]
            temp_dict['hostnames'] = [temp_list[1]]
            yaml_host.append(temp_dict)

d1['spec']['template']['spec']['hostAliases'] = yaml_host

# 如果用原生的yaml功能,yaml文件一些列表项会有引号,所以要用ruamel的yaml库。
# with open("dst_dep.yaml", "w", encoding="utf-8") as f:
#    yaml.dump(d1, f)

# 写入到yaml文件
with open("dst_dep.yaml", "w", encoding="utf-8") as f:
    ruamel_yaml.dump(d1, f, Dumper=ruamel_yaml.RoundTripDumper)

四,最后扩展后的Yaml.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxx-ai-jupyter-v2
spec:
  replicas: 1
  selector:
    matchLabels:
      name: xxx-ai-jupyter-v2
  template:
    metadata:
      labels:
        name: xxx-ai-jupyter-v2
    spec:
      imagePullSecrets:
      - name: xxx
      nodeSelector:
        accelerator: nvidia-tesla-k80
      containers:
      - name: xxx-ai-jupyter-v2
        image: harbor.xxx.com.cn/3rd_part/tensorflow:xxx
        imagePullPolicy: IfNotPresent
        command: ["bash", "-c", "jupyter notebook --notebook-dir=/tf --ip 0.0.0.0  --no-browser --allow-root --NotebookApp.allow_remote_access=True --NotebookApp.disable_check_xsrf=True --NotebookApp.token='' --NotebookApp.password='' --NotebookApp.allow_origin='*' --NotebookApp.allow_origin='*'"]
        resources:
          limits:
            nvidia.com/gpu: 4
        volumeMounts:
        - mountPath: /tf
          name: jupyter-data
      volumes:
      - name: jupyter-data
        hostPath:
          # directory location on host
          path: /docker/jupyter_data
      hostAliases:
      - ip: 127.0.0.1
        hostnames:
        - bar.local
      - ip: 10.1.2.3
        hostnames:
        - bar.remote
      - ip: 192.16.0.24
        hostnames:
        - bi-server-33391
      - ip: 192.16.0.25
        hostnames:
        - bi-server-33392
      - ip: 192.16.0.26
        hostnames:
        - bi-server-33393
      ......
      

五。END.最后,将这些yaml合进其它yaml文件即可,这时,脚本就需要进一步加功能了。

转载于:https://www.cnblogs.com/aguncn/p/11337992.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值