Zadig はサービス yaml を gitlab に同期します

要件: zadgi サービスの yaml ファイルを gitlab ウェアハウスに同期する
ここに画像の説明を挿入します

Python3コード:

import requests
import sys
import os
import subprocess
from loguru import logger
logger.add("create_jenkins_job.log", rotation="100 MB")


def yaml_create(name_proect,xinagmu_name):
    HEADERS = {
    
    'Content-Type': 'application/x-www-form-urlencoded',
               "Authorization": "Bearer iOiJsdW96aXh1In0sImF1ZCI6lMcOTSdm0ETsdVLecdr6GdoSpu1pY1KoPIwU",
               }

    rul = "http://zadig.buydance.com/api/aslan/service/services/{}/k8s?projectName=ceshihehuisechuangjianxinxiangmu".format(
        xinagmu_name)

    conten = requests.get(url=rul, headers=HEADERS)
    if conten.status_code != 200:
        logger.error("项目请求失败,状态码非200")
    else:
        logger.debug("状态码200,开始流程")
        podra_yaml = conten.json()
        zhuan_huan = podra_yaml["yaml"]
        ksdf = zhuan_huan.split('---')
        if not os.path.exists(name_proect):
            os.makedirs(name_proect)
            logger.debug("创建路径--{}".format(name_proect))
            for i in ksdf:
                if "kind: Deployment" in i:
                    xiangmu_path = name_proect + "/deployment.yaml"
                    with open(xiangmu_path, 'w', encoding='utf-8') as f:
                        f.write(i)
                    logger.debug("创建deployment.yaml文件")
                if "kind: Service" in i:
                    xiangmu_path = name_proect + "/service.yaml"
                    with open(xiangmu_path, 'w', encoding='utf-8') as f:
                        f.write(i)
                    logger.debug("创建service.yaml文件")
                if "kind: ConfigMap" in i:
                    xiangmu_path = name_proect + "/configmap.yaml"
                    with open(xiangmu_path, 'w', encoding='utf-8') as f:
                        f.write(i)
                    logger.debug("创建configmap.yaml文件")
            git_push(xianmug_name)

def git_push(xiangmu_name):
    cmd_copy = "cp -r /home/dtk-kubernetes-test/app/{}  /home/dtk-kubernetes-test/z-huise/app; cd /home/dtk-kubernetes-test/;git pull ;git add . ;git commit -m 'xinzeng';git push  ".format(xiangmu_name)
    subprocess.run(f"{
      
      cmd_copy}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode("utf-8")
    logger.debug("执行命令{},推送到gitlab仓库".format(cmd_copy))

if __name__ == '__main__':
    xianmug_name = sys.argv[1]
    yaml_create("/home/dtk-kubernetes-test/app/{}".format(xianmug_name),xianmug_name)

ここに画像の説明を挿入します

ここに画像の説明を挿入します
gitlab ウェアハウスから取得:
ここに画像の説明を挿入します
Q: なぜ gitlab ウェアハウスと同期する必要があるのですか? zadig上で直接管理することはできないのでしょうか?
回答: 個人的なビジネス上の問題については、gitlab を使用して configmap ファイルを変更する必要があるため、この機能が利用可能です。

おすすめ

転載: blog.csdn.net/weixin_43606975/article/details/127690183