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

在實現的需求以下:node

同事提供了一個文本文件,內含200多個host與ip的對應關係,但願能在k8s生成pod時,將這些對應關係注入到/etc/hosts中。python

網上看文檔,這能夠經過擴充pod中的hostAliases來實現。docker

實現的思路以下:api

一,hosts文件內容示例bash

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文件內容app

---
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腳本spa

# 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.code

---
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文件便可,這時,腳本就須要進一步加功能了。server

相關文章
相關標籤/搜索