栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 更新k8s api

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 更新k8s api

# !/usr/bin/python3
# -*- coding: utf-8 -*-
'''replace_namespace_deployment,快速替换deployment需要升级或回滚的镜像'''

import os
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import requests
import time


print(time.time())
r = requests.get("https://zhangjiakou.aliyuncs.com/common/test/config")
#print(r.text)

def read_deployment(name, namespace, update_image):
    body = api_instance.read_namespaced_deployment(name, namespace)
    # 修改镜像版本为指定
    body.spec.template.spec.containers[0].image = update_image
    return body


def replace_deployment(name, namespace, body):
    try:
        api_response = api_instance.replace_namespaced_deployment(name, namespace, body)
        print("33[0;32m{} 中deployment : {} {}更新完成33[0m".format(namespace, name, new_image))
    except ApiException as e:
        print("Exception when calling AppsV1Api->replace_namespaced_deployment: %sn" % e)


if __name__ == "__main__":
    # kubernetes生成时/root目录下.kube目录下的认证配置文件
    kube_conf = os.path.join('/Users/hong/.kube/config')
    print(kube_conf)
    config.load_kube_config(kube_conf)
    # config.kube_config.load_kube_config_from_dict(r.text)
    api_instance = client.AppsV1Api()

    # 传入参数配置
    # name = 'bsopconsole-group-1-12'
    # namespace = 'serucity-dirac'
    #update_image = 'registry-vpc.cn-north-2-gov-1.aliyuncs.com/lippi-base-test/bsopconsole:test.20210514195010'
    #body = read_deployment(name, namespace, update_image)
    # print(body)
    # replace_deployment(name, namespace, body)
    # deployments=api_instance.list_namespaced_deployment(namespace='serucity-dirac', watch=False)


    v1 = client.CoreV1Api()
    app_name = 'security-dirac'

    # while 1:
    ret = api_instance.list_namespaced_deployment(namespace='serucity-dirac', watch=False)
    # ret = v1.list_namespaced_pod(namespace='serucity-dirac', watch=False)
    for i in ret.items:
        if i.metadata.labels['edas.oam.acname'] == app_name and i.status.replicas:
            new_image_id = 20210527100520
            deployment_name = i.metadata.name
            namespace = i.metadata.namespace
            old_image = i.spec.template.spec.containers[0].image
            new_image = old_image[:-14] + str(new_image_id)
            # print(i.status)
            #print("%st%st%st%s" % (deployment_name, namespace, old_image, new_image))
            # body = api_instance.read_namespaced_deployment(deployment_name, namespace)
            # available_replicas = body.status.replicas
            # ready_replicas = body.status.ready_replicas
            # replicas = body.status.replicas
            # unavailable_replicas = body.status.unavailable_replicas
            # if unavailable_replicas is None and available_replicas == replicas and replicas == ready_replicas:
            #     # print("{},available_replicas:{}, ready_replicas:{}, replicas:{}".format(deployment_name,
            #     #                                                                         available_replicas,
            #     #                                                                         ready_replicas, replicas))
            #     #print(body.status)
            #     pass
            # else:
            #     print("{},available_replicas:{}, ready_replicas:{}, replicas:{}".format(deployment_name,
            #                                                                             available_replicas,
            #                                                                             ready_replicas, replicas))
            #     #print(body.status)
            # pass
            body = read_deployment(deployment_name, namespace, new_image)
            print(body)
            replace_deployment(deployment_name, namespace, body)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/990069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号