def k8s_project_offline(context=None,dm_name=None,offline=None): try: tools.Async_log(g.user, request.url) busi_data = defaultdict() mounts = None labels = None healthcheck = None sidecar = None tables = ('项目', '业务','集群', '代码包', '最近上线日期', '最近上线时间', '操作') # offline project if dm_name and context and offline: values = db_k8s.query.with_entities(db_k8s.project, db_k8s.image, db_k8s.container_port, db_k8s.replicas, db_k8s.re_requests,db_k8s.re_limits).filter(db_k8s.deployment==dm_name).order_by(desc(db_k8s.id)).limit(1).all() if values: id,project, image, container_port, replicas,re_requests,re_limits = values[0] k8s = k8s_resource.k8s_object(context,dm_name, image, container_port, replicas,mounts,labels,healthcheck,sidecar, re_requests, re_limits) if k8s.delete_hpa() and k8s.delete_ingress() and k8s.delete_service() and k8s.delete_deployment(): db_k8s.query.filter(db_k8s.deployment==dm_name).update({db_k8s.action:'delete'}) db_op.DB.session.commit() return jsonify({'result': 'offline project %s success!' % dm_name}) return jsonify({'result':'offline project %s fail!' %dm_name}) db_k8s_deploy = db_op.k8s_deploy db_project = db_op.project_list db_business = db_op.business projects = db_k8s_deploy.query.with_entities(distinct(db_k8s_deploy.deployment)).all() projects = [project[0] for project in projects] dm_names = [] for context in contexts: config.load_kube_config(config_file,context) v1 = client.AppsV1Api() ret = v1.list_deployment_for_all_namespaces() names = [[i.metadata.name] for i in ret.items if i.metadata.name in projects] dm_names.extend(names) for dm_name in names: vals = db_k8s_deploy.query.with_entities(db_k8s_deploy.project,db_k8s_deploy.context,db_k8s_deploy.war,db_k8s_deploy.update_date, db_k8s_deploy.update_time).filter(and_(db_k8s_deploy.deployment==dm_name[0], db_k8s_deploy.context==context) ).order_by(desc(db_k8s_deploy.id)).all() if vals: dm_name.extend(vals[0]) vals = db_project.query.with_entities(db_project.business_id).filter(db_project.project == vals[0][0]).limit(1).all() if vals: business = db_business.query.with_entities(db_business.business).filter(db_business.id==vals[0][0]).all() if business: dm_name[1] = business[0][0] busi_data[business[0][0]] = vals[0][0] return render_template('k8s_offline.html',dm_names=dm_names,tables=tables,busi_data=busi_data) except Exception as e: logging.error(e) return redirect(url_for('error'))
def k8s_project_offline(dm_name=None): try: tools.Async_log(g.user, request.url) busi_data = defaultdict() # Delete deployment if dm_name: values = db_k8s.query.with_entities(db_k8s.project, db_k8s.image, db_k8s.container_port, db_k8s.replicas, db_k8s.re_requests,db_k8s.re_limits).filter(db_k8s.deployment==dm_name).order_by(desc(db_k8s.id)).limit(1).all() if values: project, image, container_port, replicas,re_requests,re_limits = values[0] k8s = k8s_resource.k8s_object(dm_name, image, container_port, replicas, re_requests, re_limits) if k8s.delete_hpa() and k8s.delete_ingress() and k8s.delete_service() and k8s.delete_deployment(): v = db_k8s(project=project, image=image, container_port=container_port, replicas=replicas, re_requests=re_requests, re_limits=re_limits, action='delete', update_date=time.strftime('%Y-%m-%d', time.localtime()), update_time=time.strftime('%H:%M:%S', time.localtime())) db_op.DB.session.add(v) db_op.DB.session.commit() else: tables = ('项目','业务','代码包','最近上线日期','最近上线时间','操作') db_k8s_deploy = db_op.k8s_deploy db_project = db_op.project_list db_business = db_op.business projects = db_k8s_deploy.query.with_entities(distinct(db_k8s_deploy.deployment)).all() projects = [project[0] for project in projects] v1 = client.AppsV1Api() ret = v1.list_deployment_for_all_namespaces() dm_names = [[i.metadata.name] for i in ret.items if i.metadata.name in projects] for dm_name in dm_names: vals = db_k8s_deploy.query.with_entities(db_k8s_deploy.project,db_k8s_deploy.war,db_k8s_deploy.update_date, db_k8s_deploy.update_time).filter(db_k8s_deploy.deployment==dm_name[0]).order_by(desc(db_k8s_deploy.id)).all() if vals: dm_name.extend(vals[0]) vals = db_project.query.with_entities(db_project.business_id).filter(db_project.project == vals[0][0]).limit(1).all() if vals: business = db_business.query.with_entities(db_business.business).filter(db_business.id==vals[0][0]).all() if business: dm_name[1] = business[0][0] busi_data[business[0][0]] = vals[0][0] return render_template('k8s_offline.html',dm_names=dm_names,tables=tables,busi_data=busi_data) except Exception as e: logging.error(e) return redirect(url_for('error'))