本文已参与「新人创作礼」活动,一起开启掘金创作之路。
# 依赖库
kubernetes==12.0.1
ruamel.yaml==0.17.20
获取指定namespace下指定service的内容,以yaml格式返回
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import yaml
from datetime import datetime
from kubernetes import client, config
# 1.递归对数据进行简化
# 2.把一些value为None的key去掉,把datetime的转为字符串
# 3.把api_version改为apiVersion
def simple_data(data):
if isinstance(data, dict):
tmp_dict = {}
for key, value in data.items():
if key == 'api_version':
key = 'apiVersion'
if value is None:
continue
tmp_dict[key] = simple_data(value)
return tmp_dict
elif isinstance(data, datetime):
return data.strftime("%Y-%m-%dT%H:%M:%SZ")
else:
return data
# 获取service内容
# 参数说明:
# config_path:k8s集群token文件路径
# namespace:k8s namespace
# name:service名称
def get_service_content(config_path, namespace, name):
# load k8s集群token信息
config.load_kube_config(config_path)
v1 = client.CoreV1Api()
# 读取service
ret = v1.read_namespaced_service(namespace=namespace, name=name)
# 把一些value为None的key去掉
data = simple_data(ret.to_dict())
# 转为yaml格式
service_yaml = yaml.dump(data)
return service_yaml
获取指定namespace下的service列表
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from kubernetes import client, config
# 获取指定namespace下面的service列表
# 参数说明:
# config_path:k8s集群token文件路径
# namespace:k8s namespace
def list_service(config_path, namespace):
# load k8s集群token信息
config.load_kube_config(config_path)
v1 = client.CoreV1Api()
# 读取service列表
service_list = []
ret = v1.list_namespaced_service(namespace=namespace)
for item in ret.items:
# print(type(item.metadata))
service_list.append(item.metadata.name)
return service_list
更新service
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import yaml
from kubernetes import client, config
# 更新service
# 参数说明:
# config_path:k8s集群token文件路径
# namespace:k8s namespace
# name:service名称
# content:service内容
def replace_service(config_path, namespace, name, content):
# load k8s集群token信息
config.load_kube_config(config_path)
v1 = client.CoreV1Api()
# 读取service内容
service_content = v1.read_namespaced_service(namespace=namespace, name=name)
# 将内容转换为参数要求的格式
body = yaml.safe_load(content) # dict
# 如果现在的content里面没有配metadata.resourceVersion、 spec.clusterIP的值,就把当前的service内容的值填充上
if 'metadata' in body.keys() and 'resourceVersion' in body['metadata'].keys():
if body['metadata']['resourceVersion'] is None: # 有声明但是没有赋值
body['metadata']['resourceVersion'] = service_content.metadata.resource_version
else: # 没有声明
body['metadata']['resourceVersion'] = service_content.metadata.resource_version
if 'spec' in body.keys() and 'clusterIP' in body['spec'].keys():
if body['spec']['clusterIP'] is None: # 有声明但是没有赋值
body['spec']['clusterIP'] = service_content.spec.cluster_ip
else: # 没有声明
body['spec']['clusterIP'] = service_content.spec.cluster_ip
# 变更service
ret = v1.replace_namespaced_service(namespace=namespace, name=name, body=body)
return True
创建service
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import yaml
from kubernetes import client, config
# 创建service
# 参数说明:
# config_path:k8s集群token文件路径
# namespace:k8s namespace
# content:service内容
def create_service(config_path, namespace, content):
# load k8s集群token信息
config.load_kube_config(config_path)
v1 = client.CoreV1Api()
# 将内容转换为参数要求的格式
body = yaml.safe_load(content)
# 创建service
ret = v1.create_namespaced_service(namespace=namespace, body=body)
return True
创建/更新service
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 参数说明:
# config_path:k8s集群token文件路径
# namespace:k8s namespace
# name:service名称
# content:service内容
def deploy_service(config_path, namespace, name, content):
# 获取现有的service列表
service_list = list_service(config_path, namespace)
if name in service_list: # 更新
return replace_service(config_path, namespace, name, content)
else: # 新建
return create_service(config_path, namespace, content)