
正文
使用ruamel.yaml库,解析yaml文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在实现的需求如下:
同事提供了一个文本文件,内含200多个host与ip的对应关系,希望能在k8s生成pod时,将这些对应关系注入到/etc/hosts中。
网上看文档,这可以通过扩充pod中的hostAliases来实现。
实现的思路如下:
一,hosts文件内容示例
192.168.0.24 bi-server-3391192.168.0.25 bi-server-3392192.168.0.26 bi-server-3393192.168.0.27 bi-server-3394192.168.0.28 bi-server-3395192.168.0.29 bi-server-3396192.168.0.30 bi-server-3397192.168.0.31 bi-server-3398192.168.0.32 bi-server-3399192.168.0.33 bi-server-3400192.168.0.34 bi-server-3401192.168.0.35 bi-server-3402192.168.0.36 bi-server-3403192.168.0.37 bi-server-3404192.168.0.38 bi-server-3405192.168.0.39 bi-server-3406192.168.0.40 bi-server-3407
二,org_dep.yaml文件内容
---apiVersion: apps/v1kind: Deploymentmetadata: name: xxx-ai-jupyter-v2spec: 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-8from ruamel import yaml as ruamel_yamlimport yamlimport oscur_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/v1kind: Deploymentmetadata: name: xxx-ai-jupyter-v2spec: 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文件即可,这时,脚本就需要进一步加功能了。







