Ejemplo n.º 1
0
def rollback(deployment_description, rollback_to=0):
    '''
    Undo upgrade by rolling back to a previous revision of the deployment.
    By default, go back one revision.
    rollback_to can be used to supply a specific revision number.
    Returns the image for the app container and the replica count from the rolled-back deployment
    '''
    '''
    2018-07-13
    Currently this does not work due to a bug in the create_namespaced_deployment_rollback() method.
    The k8s python client code throws an exception while processing the response from the API.
    See:
       - https://github.com/kubernetes-client/python/issues/491
       - https://github.com/kubernetes/kubernetes/pull/63837
    The fix has been merged into the master branch but is not in the latest release.
    '''
    _configure_api(deployment_description["location"])
    deployment = deployment_description["deployment"]
    namespace = deployment_description["namespace"]

    # Initiate the rollback
    client.ExtensionsV1beta1Api().create_namespaced_deployment_rollback(
        deployment, namespace,
        client.AppsV1beta1DeploymentRollback(
            name=deployment,
            rollback_to=client.AppsV1beta1RollbackConfig(
                revision=rollback_to)))

    # Read back the spec for the rolled-back deployment
    spec = client.ExtensionsV1beta1Api().read_namespaced_deployment(
        deployment, namespace)
    return spec.spec.template.spec.containers[0].image, spec.spec.replicas
Ejemplo n.º 2
0
def rollbackDeploymentPolicy(obj, api):
    #check if alert has been duplicated
    if getLabels(obj, api) == "v1":
        print("Duplicate alert")
        return

    api_instance = client.AppsV1beta1Api(api)
    #Parse name string and assign required values
    n = getName(obj)
    namespace = n[2]
    deployment = n[4]

    #Parse deployment name to remove suffix
    n = deployment.split("-")
    deployment = n[0]+"-"+n[1]+"-"+n[2]
    body = client.AppsV1beta1DeploymentRollback(None,None,deployment, client.AppsV1beta1RollbackConfig())
    print("Target: ", deployment)
    resp = api_instance.create_namespaced_deployment_rollback(deployment, namespace, body)
    print(resp)
Ejemplo n.º 3
0
 def _get_rollback_config(self, rollback_to=None):
     if rollback_to:
         return client.AppsV1beta1RollbackConfig(rollback_to)
Ejemplo n.º 4
0
 def post(self):
     arg = self.request.arguments
     print(arg)
     #获取namespace,keyword,回滚版本字段不为空的deploy和version
     namespace = arg['namespace'][0].decode('utf-8')
     keyword = arg['keyword'][0].decode('utf-8')
     v2 = client.AppsV1Api()
     v3 = client.AppsV1beta1Api()
     rollback_dict = {}
     for deploy in arg:
         if deploy != 'namespace' and deploy != 'keyword':
             version = arg[deploy][0].decode('utf-8')
             if version.strip():
                 rollback_dict[deploy]=version
     #如果rollback_dict不为空,则区分上一个版本和具体版本分别执行更新
     if rollback_dict:
         for deploy_name in rollback_dict:
             if rollback_dict[deploy_name] == '上一个版本':
                 # v3 = client.AppsV1beta1Api()
                 rollback = client.AppsV1beta1DeploymentRollback(name=deploy_name,rollback_to=client.AppsV1beta1RollbackConfig(revision=0))
                 v3.create_namespaced_deployment_rollback(name=deploy_name, namespace=namespace, body=rollback)
             else:
                 # v2 = client.AppsV1Api()
                 api_response = v2.read_namespaced_deployment(name=deploy_name, namespace=namespace)
                 image = api_response.spec.template.spec.containers[0].image
                 # print(image[:-12]+rollback_dict[deploy_name])
                 image_roll = image[:-12]+rollback_dict[deploy_name]
                 api_response.spec.template.spec.containers[0].image = image_roll
                 v2.patch_namespaced_deployment(name=deploy_name, namespace=namespace, body=api_response)
     else:
         print('no deploy need rollback')
     #返回更新后的deploy信息
     data = {'status': True, 'error': "", 'message': '','data': ''}
     deploy_list=[]
     #根据namespace和keyword获取对应的deploy,如keyword为空,则获取当前namespace下的所有deploy,
     # v2 = client.AppsV1Api()
     api_response = v2.list_namespaced_deployment(namespace)
     for deploy in api_response.items:
         if keyword in deploy.metadata.name:
             deploy_dict = {}
             deploy_dict['deploy'] = deploy.metadata.name
             deploy_dict['image'] = deploy.spec.template.spec.containers[0].image
             deploy_dict['replicas'] = deploy.status.replicas
             deploy_dict['available'] = deploy.status.available_replicas
             if not deploy_dict['available']:
                 deploy_dict['available'] = 0
             #根据deploy_dict['deploy']获取当前deploy前5的version号
             # sql='''
             #     select k8s_project.project_name,k8s_project_version.version_id from
             #     k8s_project,k8s_project_version
             #     where k8s_project.project_id = k8s_project_version.project_id
             #     and k8s_project.project_name=%s order by k8s_project_version.gmt_create desc limit 3
             # '''
             # cursor.execute(sql,[deploy_dict['deploy']])
             # result=cursor.fetchall()
             #通过harborapi获取版本信息,通过正则,截取image中的harbor项目名称,如果正则取到值,获取最近5个版本的版本号
             repository_name_re = re.findall('/.+:',deploy_dict['image'])
             if repository_name_re:
                 repository_name = repository_name_re[0][1:-1]
                 harbor_api = HarborApi(settings.harbor_dict["host"],settings.harbor_dict["user"],settings.harbor_dict["passwd"])
                 harbor_api.login_get_session_id()
                 result = harbor_api.tags_info_list(repository_name)[-5:]
                 result.reverse()
             else:
                 result=[]
             deploy_dict['version'] = result
             deploy_list.append(deploy_dict)
     print(deploy_list)
     data['data'] = deploy_list
     self.write(json.dumps(data))