1、生成 servie.yaml

1.1、yaml转json

service模板yaml

apiVersion: v1

kind: Service

metadata:

name: ${jarName}

labels:

name: ${jarName}

version: v1

spec:

ports:

- port: ${port}

targetPort: ${port}

selector:

name: ${jarName}

转成json的结构

{

"apiVersion": "v1",

"kind": "Service",

"metadata": {

"name": "${jarName}",

"labels": {

"name": "${jarName}",

"version": "v1"

}

},

"spec": {

"ports": [

{

"port": "${port}",

"targetPort": "${port}"

}

],

"selector": {

"name": "${jarName}"

}

}

}

1.2、关键代码

# 通过传入service_name及ports列表

def create_service_yaml(service_name, ports):

# 将yaml读取为json,然后修改所有需要修改的${jarName}

service_data['metadata']['name'] = service_name

service_data['metadata']['labels']['name'] = service_name

service_data['spec']['selector']['name'] = service_name

# .spec.ports 比较特殊,是一个字典列表,由于传入的ports难以确定数量,难以直接修改

# 新建一个列表,遍历传入的ports列表,将传入的每个port都生成为一个字典,添加入新列表中

new_spec_ports = []

for port in ports:

port = int(port)

new_port = {'port': port, 'targetPort': port}

new_spec_ports.append(new_port)

# 修改.spec.ports为新列表

service_data['spec']['ports'] = new_spec_ports

2、生成 deployment.yaml

2.1、yaml转json

deployment模板yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: ${jarName}

labels:

name: ${jarName}

spec:

selector:

matchLabels:

name: ${jarName}

replicas: 1

template:

metadata:

labels:

name: ${jarName}

spec:

containers:

- name: ${jarName}

image: reg.test.local/library/${jarName}:${tag}

imagePullSecrets:

- name: registry-secret

转成的json结构

{

"apiVersion": "apps/v1",

"kind": "Deployment",

"metadata": {

"name": "${jarName}",

"labels": {

"name": "${jarName}"

}

},

"spec": {

"selector": {

"matchLabels": {

"name": "${jarName}"

}

},

"replicas": 1,

"template": {

"metadata": {

"labels": {

"name": "${jarName}"

}

},

"spec": {

"containers": [

{

"name": "${jarName}",

"image": "reg.test.local/library/${jarName}:${tag}"

}

],

"imagePullSecrets": [

{

"name": "registry-secret"

}

]

}

}

}

}

2.2、关键代码

# 传入service_name及image tag

def create_deploy_yaml(service_name, tag):

# 首先修改所有的${jarName}

deploy_data['metadata']['name'] = service_name

deploy_data['metadata']['labels']['name'] = service_name

deploy_data['spec']['selector']['matchLabels']['name'] = service_name

deploy_data['spec']['template']['metadata']['labels']['name'] = service_name

# 由于.spec.template.spec.containers的特殊性,我们采用直接修改的方式

# 首先拼接image字段

image = "reg.test.local/library/" + service_name + ":" + tag

# 创建new_containers字典列表

new_containers = [{'name': service_name, 'image': image}]

deploy_data['spec']['template']['spec']['containers'] = new_containers

3、完整脚本

#!/usr/bin/python

# encoding: utf-8

"""

The Script for Auto Create Deployment Yaml.

File: auto_create_deploy_yaml

User: miaocunfa

Create Date: 2020-06-10

Create Time: 17:06

"""

import os

from ruamel.yaml import YAML

yaml = YAML()

def create_service_yaml(service_name, ports):

service_mould_file = "mould/info-service-mould.yaml"

isServiceMould = os.path.isfile(service_mould_file)

if isServiceMould:

# read Service-mould yaml convert json

with open(service_mould_file, encoding='utf-8') as yaml_obj:

service_data = yaml.load(yaml_obj)

# Update jarName

service_data['metadata']['name'] = service_name

service_data['metadata']['labels']['name'] = service_name

service_data['spec']['selector']['name'] = service_name

# Update port

new_spec_ports = []

for port in ports:

port = int(port)

portname = 'port' + str(port)

new_port = {'name': portname, 'port': port, 'targetPort': port}

new_spec_ports.append(new_port)

service_data['spec']['ports'] = new_spec_ports

# json To service yaml

save_file = tag + '/' + service_name + '_svc.yaml'

with open(save_file, mode='w', encoding='utf-8') as yaml_obj:

yaml.dump(service_data, yaml_obj)

print(save_file + ": Success!")

else:

print("Service Mould File is Not Exist!")

def create_deploy_yaml(service_name, tag):

deploy_mould_file = "mould/info-deploy-mould.yaml"

isDeployMould = os.path.isfile(deploy_mould_file)

if isDeployMould:

with open(deploy_mould_file, encoding='utf-8') as yaml_obj:

deploy_data = yaml.load(yaml_obj)

# Update jarName

deploy_data['metadata']['name'] = service_name

deploy_data['metadata']['labels']['name'] = service_name

deploy_data['spec']['selector']['matchLabels']['name'] = service_name

deploy_data['spec']['template']['metadata']['labels']['name'] = service_name

# Update containers

image = "reg.test.local/library/" + service_name + ":" + tag

new_containers = [{'name': service_name, 'image': image}]

deploy_data['spec']['template']['spec']['containers'] = new_containers

# json To service yaml

save_file = tag + '/' + service_name + '_deploy.yaml'

with open(save_file, mode='w', encoding='utf-8') as yaml_obj:

yaml.dump(deploy_data, yaml_obj)

print(save_file + ": Success!")

else:

print("Deploy Mould File is Not Exist!")

services = {

'info-gateway': ['9999'],

'info-admin': ['7777'],

'info-config': ['8888'],

'info-message-service': ['8555', '9666'],

'info-auth-service': ['8666'],

'info-scheduler-service': ['8777'],

'info-uc-service': ['8800'],

'info-ad-service': ['8801'],

'info-community-service': ['8802'],

'info-groupon-service': ['8803'],

'info-hotel-service': ['8804'],

'info-nearby-service': ['8805'],

'info-news-service': ['8806'],

'info-store-service': ['8807'],

'info-payment-service': ['8808'],

'info-agent-service': ['8809'],

'info-consumer-service': ['8090'],

}

prompt = "\n请输入要生成的tag: "

answer = input(prompt)

print("")

if os.path.isdir(answer):

raise SystemExit(answer + ': is Already exists!')

else:

tag = answer

os.makedirs(tag)

for service_name, service_ports in services.items():

create_service_yaml(service_name, service_ports)

create_deploy_yaml(service_name, tag)

4、执行效果

➜ python3 Auto_Create_K8S_YAML.py

请输入要生成的tag: 0.0.1

0.0.1/info-gateway_svc.yaml: Success!

0.0.1/info-gateway_deploy.yaml: Success!

0.0.1/info-admin_svc.yaml: Success!

0.0.1/info-admin_deploy.yaml: Success!

0.0.1/info-config_svc.yaml: Success!

0.0.1/info-config_deploy.yaml: Success!

0.0.1/info-message-service_svc.yaml: Success!

0.0.1/info-message-service_deploy.yaml: Success!

0.0.1/info-auth-service_svc.yaml: Success!

0.0.1/info-auth-service_deploy.yaml: Success!

0.0.1/info-scheduler-service_svc.yaml: Success!

0.0.1/info-scheduler-service_deploy.yaml: Success!

0.0.1/info-uc-service_svc.yaml: Success!

0.0.1/info-uc-service_deploy.yaml: Success!

0.0.1/info-ad-service_svc.yaml: Success!

0.0.1/info-ad-service_deploy.yaml: Success!

0.0.1/info-community-service_svc.yaml: Success!

0.0.1/info-community-service_deploy.yaml: Success!

0.0.1/info-groupon-service_svc.yaml: Success!

0.0.1/info-groupon-service_deploy.yaml: Success!

0.0.1/info-hotel-service_svc.yaml: Success!

0.0.1/info-hotel-service_deploy.yaml: Success!

0.0.1/info-nearby-service_svc.yaml: Success!

0.0.1/info-nearby-service_deploy.yaml: Success!

0.0.1/info-news-service_svc.yaml: Success!

0.0.1/info-news-service_deploy.yaml: Success!

0.0.1/info-store-service_svc.yaml: Success!

0.0.1/info-store-service_deploy.yaml: Success!

0.0.1/info-payment-service_svc.yaml: Success!

0.0.1/info-payment-service_deploy.yaml: Success!

0.0.1/info-agent-service_svc.yaml: Success!

0.0.1/info-agent-service_deploy.yaml: Success!

0.0.1/info-consumer-service_svc.yaml: Success!

0.0.1/info-consumer-service_deploy.yaml: Success!

➜ ll

total 12

drwxr-xr-x. 2 root root 4096 Jun 29 18:24 0.0.1

# 生成的 service yaml

➜ cat info-message-service_svc.yaml

apiVersion: v1

kind: Service

metadata:

name: info-message-service

labels:

name: info-message-service

version: v1

spec:

ports:

- name: port8555

port: 8555

targetPort: 8555

- name: port9666

port: 9666

targetPort: 9666

selector:

name: info-message-service

# 生成的 deployment yaml

➜ cat info-message-service_deploy.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: info-message-service

labels:

name: info-message-service

spec:

selector:

matchLabels:

name: info-message-service

replicas: 2

template:

metadata:

labels:

name: info-message-service

spec:

containers:

- name: info-message-service

image: reg.test.local/library/info-message-service:0.0.1

imagePullSecrets:

- name: registry-secret

到此这篇关于使用python脚本自动生成K8S-YAML的方法示例的文章就介绍到这了,更多相关python自动生成K8S-YAML内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

本文标题: 使用python脚本自动生成K8S-YAML的方法示例

本文地址: http://www.cppcns.com/jiaoben/python/325029.html

python生成yaml_使用python脚本自动生成K8S-YAML的方法示例相关推荐

  1. python生成yaml_使用python脚本自动生成K8S-YAML

    使用python脚本自动生成K8S-YAML 1.生成 servie.yaml 1.1.yaml转json service模板yaml apiVersion: v1 kind: Service met ...

  2. NSG2-一个很好用的ns2的tcl脚本自动生成软件

    NSG2-一个很好用的ns2的tcl脚本自动生成软件 来源:Linux社区 作者:fzxy002763 NSG2-一个很好用的ns2的tcl脚本自动生成软件,NSG2.rar,一个很好的java写的t ...

  3. Polyworks脚本开发学习笔记(二二)-调取视角用脚本自动生成报告

    Polyworks脚本开发学习笔记(二二)-调取视角用脚本自动生成报告 Polyworks中,3D场景的视图可用标准视角及等轴侧视角.项目视角等方式调用,也可以用txt格式保存下来调用,如果以脚本的形 ...

  4. Orcad下使用TCL脚本自动生成导线、网络标号和off page

    Orcad是一个很优秀的原理图工具,但是手工画批量导线和网络标号的时候(比如:RGB的信号线,DDR/FLASH的地址线),比较麻烦,修改也麻烦.所以这里介绍了使用TCL脚本自动生成导线.网络标号和o ...

  5. matlab之用m脚本自动生成exe文件

    matlab之用m脚本自动生成exe文件 文章目录 matlab之用m脚本自动生成exe文件 0.前言 1.生成exe文件分两种情况 2.生成文件在自己电脑上运行 3.生成文件在其他电脑上运行 0.前 ...

  6. python过去日期_利用python获取当前日期前后N天或N月日期的方法示例

    前言 最近因为工作原因,发现一个Python的时间组件,很好用分享出来!(忘记作者名字了,在这里先感谢了),下面话不多说,来一起看看详细的介绍吧. 示例代码: # -*- coding: utf-8 ...

  7. python生成api文档_Django 自动生成api接口文档教程

    最近在写测试平台,需要实现一个节点服务器的api,正好在用django,准备使用djangorestframework插件实现. 需求 实现一个接口,在调用时,通过传递的参数,直接运行对应项目的自动化 ...

  8. 这个插件竟打通了Python和Excel,还能自动生成代码!

    作者 | 云朵君 来源丨数据STUDIO 加载一个Jupyter插件后,无需写代码就能做数据分析,还帮你生成相应代码? 没错,只需要加载这个名为Mito的小工具包,用Python做数据分析,变得和用E ...

  9. 【Python】这个插件竟打通了Python和Excel,还能自动生成代码!

    加载一个Jupyter插件后,无需写代码就能做数据分析,还帮你生成相应代码? 没错,只需要加载这个名为Mito的小工具包,用Python做数据分析,变得和用Excel一样简单: 介绍 以 Excel ...

最新文章

  1. Linux之文件权限命令
  2. 不要对对象进行粗暴的等号赋值
  3. Qt 断言的使用 Q_ASSERT
  4. vs2005常用调试快捷键 (转载)
  5. 摩根斯坦利面试题库_经验 | 金融公司摩根士丹利从笔试到实习的全程经验
  6. Burp Collaborator 使用总结
  7. megento 获取url参数
  8. sf | 创建空间矢量对象及其投影设置
  9. 【状压dp】【POJ2288】Islands and Bridges【Hamilton路】
  10. 微软原版Windows 8 原版镜像
  11. 大小端详解(判断+转换)
  12. GoldVideo-基于Web Assembly的H265播放器实现
  13. 精讲设计模式-Builder模式
  14. 注册和登录Github
  15. 地图标识符号大全_起名字大全男孩 男孩名字,起名字大全男孩
  16. 新一代云上基础技术和架构分论坛
  17. android 阅读 翻页,极速PDF安卓版如何翻页、阅读模式修改等操作详解
  18. VS2015中“项目无法加载,因为它缺少安装组件”的解决方法
  19. 杨辉三角与二项式定理
  20. 计算机打印机提示无法打印,打印机无法打印_打印机无法打印怎么办【步骤|图文教程】-太平洋IT百科...

热门文章

  1. 【Elasticsearch】如何使用 Elasticsearch 6.2 搜索中文、日文和韩文文本 - 第 2 部分: 多字段
  2. 80-450-020-原理-索引-索引失效与优化
  3. 【Zookeeper】查看ZK连接总数
  4. kerberos安装配置与使用
  5. 95-290-340-源码-内存管理-Buffer-ByteBuffer简介
  6. 60-100-340-使用-DataSource-hive相关-Flink加载hive数据源
  7. drools 7.11 复杂事件处理详解
  8. servlet技术是否过时
  9. 数字U家,即刻出发!2022联合利华黑客马拉松报名倒计时!
  10. IntelliJ IDEA 开启很慢,运行不流畅,大项目卡顿?一招配置解决!