def export_ingress(self,domains,ingress_port): try: db_ingress = db_op.k8s_ingress # ingress信息写入数据库 for ingress_domain in domains: path = None if '/' in ingress_domain: ingress_domain = ingress_domain.split('/')[0] path = '/{}'.format('/'.join(ingress_domain.split('/')[1:])) v = db_ingress(name='nginx-ingress',context=self.context, namespace=self.namespace, domain=ingress_domain, path=path, serviceName=self.dm_name, servicePort=int(ingress_port)) db_op.DB.session.add(v) db_op.DB.session.commit() except Exception as e: logging.error(e) else: # 从数据库读取ingress信息 Rules = [] domain_infos = db_ingress.query.with_entities(distinct(db_ingress.domain)).all() for domain in domain_infos: paths = [] Rules_infos = db_ingress.query.with_entities(db_ingress.path, db_ingress.serviceName, db_ingress.servicePort ).filter(and_(db_ingress.domain == domain[0], db_ingress.context==self.context)).all() Rules_infos = [domain[0] for domain in Rules_infos] for infos in Rules_infos: path, serviceName, servicePort = infos if path: paths.append(client.V1beta1HTTPIngressPath(client.V1beta1IngressBackend( service_name=serviceName, service_port=int(servicePort) ), path=path)) if paths: Rules.append(client.V1beta1IngressRule(host=domain, http=client.V1beta1HTTPIngressRuleValue( paths=paths))) else: path, serviceName, servicePort = Rules_infos[0] Rules.append(client.V1beta1IngressRule(host=domain, http=client.V1beta1HTTPIngressRuleValue( paths=[client.V1beta1HTTPIngressPath( client.V1beta1IngressBackend( service_name=serviceName, service_port=int(servicePort) ))]) )) spec = client.V1beta1IngressSpec(rules=Rules) ingress = client.V1beta1Ingress( api_version='extensions/v1beta1', kind='Ingress', metadata=client.V1ObjectMeta(name='nginx-ingress', namespace=self.namespace, annotations={'kubernetes.io/ingress.class': 'nginx'}),spec=spec) return ingress finally: db_op.DB.session.remove()
def ingress_apply(): form = MyForm.Form_k8s_ingress() namespace = "default" msg = None if form.submit.data: try: domains = form.domains.data ingress_port = form.service_port.data service_name = form.service_name.data db_ingress = db_op.k8s_ingress api_instance = client.ExtensionsV1beta1Api() domains = [domain.strip() for domain in domains.split(',') if domain] try: Rules = [] Rules_infos = db_ingress.query.with_entities(db_ingress.domain,db_ingress.serviceName,db_ingress.servicePort).all() if Rules_infos: for infos in Rules_infos: domain,serviceName,servicePort = infos Rules.append(client.V1beta1IngressRule(host=domain, http=client.V1beta1HTTPIngressRuleValue( paths=[client.V1beta1HTTPIngressPath(client.V1beta1IngressBackend( service_name=serviceName, service_port=int(servicePort) ))]) )) for ingress_domain in domains: Rules.append(client.V1beta1IngressRule(host=ingress_domain, http=client.V1beta1HTTPIngressRuleValue( paths=[client.V1beta1HTTPIngressPath(client.V1beta1IngressBackend( service_name=service_name, service_port=int(ingress_port) ))]) )) except Exception as e: logging.error(e) else: spec = client.V1beta1IngressSpec(rules=Rules) ingress = client.V1beta1Ingress( api_version='extensions/v1beta1', kind='Ingress', metadata=client.V1ObjectMeta(name='nginx-ingress', namespace=namespace, annotations={'kubernetes.io/ingress.class': 'nginx'}),spec=spec) api_instance.patch_namespaced_ingress(body=ingress, namespace=namespace,name='nginx-ingress') except Exception as e: logging.error(e) msg = 'ingress配置失败!' else: # ingress信息写入数据库 for domain in domains: v = db_ingress(name='nginx-ingress', namespace=namespace, domain=domain, serviceName=service_name, servicePort=int(ingress_port)) db_op.DB.session.add(v) db_op.DB.session.commit() msg = 'ingress配置完成!' finally: db_op.DB.session.remove() return render_template('k8s_ingress.html',form=form,msg=msg)
def template(context): return client.V1beta1Ingress( api_version="extensions/v1beta1", kind="Ingress", metadata=client.V1ObjectMeta(name=context["name"]), spec=client.V1beta1IngressSpec(backend=client.V1beta1IngressBackend( service_name=context["name"], service_port=context["externalPort"])), )
def export_ingress(self, domains, ingress_port): try: Rules = [] db_ingress = db_op.k8s_ingress Rules_infos = db_ingress.query.with_entities( db_ingress.domain, db_ingress.serviceName, db_ingress.servicePort).all() if Rules_infos: for infos in Rules_infos: domain, serviceName, servicePort = infos Rules.append( client.V1beta1IngressRule( host=domain, http=client.V1beta1HTTPIngressRuleValue(paths=[ client.V1beta1HTTPIngressPath( client.V1beta1IngressBackend( service_name=serviceName, service_port=int(servicePort))) ]))) except Exception as e: logging.error(e) else: for ingress_domain in domains: Rules.append( client.V1beta1IngressRule( host=ingress_domain, http=client.V1beta1HTTPIngressRuleValue(paths=[ client.V1beta1HTTPIngressPath( client.V1beta1IngressBackend( service_name=self.dm_name, service_port=int(ingress_port))) ]))) spec = client.V1beta1IngressSpec(rules=Rules) ingress = client.V1beta1Ingress( api_version='extensions/v1beta1', kind='Ingress', metadata=client.V1ObjectMeta( name='nginx-ingress', namespace=self.namespace, annotations={'kubernetes.io/ingress.class': 'nginx'}), spec=spec) return ingress finally: db_op.DB.session.remove()
def ingress_definition(self): return client.V1beta1Ingress( metadata=client.V1ObjectMeta( name=self.kube_name ), spec=client.V1beta1IngressSpec( backend=client.V1beta1IngressBackend( service_name=self.kube_name, service_port=5000, ) ), )
def create_ingress( self, namespace=None, name=None, service_name=None, ingress_paths=None, annotations=None, ): if ingress_paths is None: ingress_paths = [] if annotations is None: annotations = {} api_instance = client.ExtensionsV1beta1Api() metadata = client.V1ObjectMeta(name=name, annotations=annotations) path_list = [] for ing_path in ingress_paths: ing_backend = client.V1beta1IngressBackend( service_name=service_name, service_port=ing_path.get("port", 0)) path_list.append( client.V1beta1HTTPIngressPath(path=ing_path.get("path", ""), backend=ing_backend)) http_dict = client.V1beta1HTTPIngressRuleValue(paths=path_list) rule_list = [client.V1beta1IngressRule(http=http_dict, host="")] ingress_spec = client.V1beta1IngressSpec(rules=rule_list) body = client.V1beta1Ingress( api_version="extensions/v1beta1", metadata=metadata, spec=ingress_spec, kind="Ingress", ) try: api_instance.create_namespaced_ingress(namespace=namespace, body=body, pretty="true") except ApiException as e: LOG.error("Create ingress failed %s", e) raise e return True
def _build_ingress(self, metadata): """Build ingress Kubernetes object. :param metadata: Common Kubernetes metadata for the interactive deployment. """ ingress_backend = client.V1beta1IngressBackend( service_name=self.deployment_name, service_port=InteractiveDeploymentK8sBuilder.internal_service_port ) ingress_rule_value = client.V1beta1HTTPIngressRuleValue([ client.V1beta1HTTPIngressPath( path=self.path, backend=ingress_backend)]) spec = client.V1beta1IngressSpec( rules=[client.V1beta1IngressRule(http=ingress_rule_value)]) ingress = client.V1beta1Ingress( api_version="extensions/v1beta1", kind="Ingress", spec=spec, metadata=metadata, ) return ingress
def specifications(self): """ Create the ingress specifications. :returns: The ingress specifications. :rtype: client.V1beta1IngressSpec """ spec = client.V1beta1IngressSpec( rules=[client.V1beta1IngressRule( host=self.definition['domain_name'], http=client.V1beta1HTTPIngressRuleValue( paths=[client.V1beta1HTTPIngressPath( path='/', backend=client.V1beta1IngressBackend(service_name=self.definition['repo'], service_port=port['number'])) for port in self.definition['ports'] ] ) )] ) return client.V1beta1Ingress( metadata=client.V1ObjectMeta(name=self.definition['repo']), spec=spec )
def build_ingress(beta_api_instance, service_name, namespace, ingress_host=None): path = client.V1beta1HTTPIngressPath( path=f'/{service_name}', backend=client.V1beta1IngressBackend( service_name=service_name, service_port=9999, ) ) ingress_body = client.V1beta1Ingress( metadata=client.V1ObjectMeta( name=f'{service_name}-external', annotations={'nginx.ingress.kubernetes.io/rewrite-target': '/'} ), spec=client.V1beta1IngressSpec( rules=[client.V1beta1IngressRule( http=client.V1beta1HTTPIngressRuleValue( paths=[path], ), )] ) ) if ingress_host: ingress_body = client.V1beta1Ingress( metadata=client.V1ObjectMeta( name=f'{service_name}-external', ), spec=client.V1beta1IngressSpec( rules=[client.V1beta1IngressRule( host=ingress_host, http=client.V1beta1HTTPIngressRuleValue( paths=[path], ), )] ) ) beta_api_instance.create_namespaced_ingress( namespace, ingress_body )
def create_ingress_object(tag_name, domain): rule = client.V1beta1IngressRule( host=tag_name + "." + domain, http=client.V1beta1HTTPIngressRuleValue( paths=[client.V1beta1HTTPIngressPath( backend=client.V1beta1IngressBackend( service_name=tag_name, service_port=80 ) )]) ) spec = client.V1beta1IngressSpec( rules=[rule] ) ingress = client.V1beta1Ingress( api_version="extensions/v1beta1", kind="Ingress", metadata=client.V1ObjectMeta(name=tag_name), spec=spec ) return ingress
def _get_ingress_backend(self, backend_dict={}): return client.V1beta1IngressBackend( backend_dict.get('service_name'), backend_dict.get('service_port', 80))
"app": "canary", "version": "v2.0.0" }, ports=[client.V1ServicePort(port=5000, target_port=5000)])) extension_svc.create_namespaced_service(namespace="ratanb", body=body) extension_v1_beta1 = client.ExtensionsV1beta1Api() body = client.V1beta1Ingress( api_version="extensions/v1beta1", kind="Ingress", metadata=client.V1ObjectMeta( name="dep2-ingress-canary", labels={"app": "canary"}, annotations={ "kubernetes.io/ingress.class": "nginx", "nginx.ingress.kubernetes.io/canary": "true", "nginx.ingress.kubernetes.io/canary-by-header": "ratan", #"nginx.ingress.kubernetes.io/canary-weight": "10" }), spec=client.V1beta1IngressSpec(rules=[ client.V1beta1IngressRule( host="canary.k8.lti-mosaic.com", http=client.V1beta1HTTPIngressRuleValue(paths=[ client.V1beta1HTTPIngressPath( backend=client.V1beta1IngressBackend( service_port=5000, service_name="dep2-service")) ])) ])) extension_v1_beta1.create_namespaced_ingress(namespace="ratanb", body=body)
def ingress_apply(): form = MyForm.FormK8sIngress() namespace = "default" msg = None if form.submit.data: try: domains = form.domains.data ingress_port = form.service_port.data service_name = form.service_name.data context = form.contexts.data db_ingress = db_op.k8s_ingress config.load_kube_config(config_file, context) api_instance = client.ExtensionsV1beta1Api() domains = [ domain.strip() for domain in domains.splitlines() if domain ] try: # ingress信息写入数据库 for ingress_domain in domains: if '/' in ingress_domain: domain = ingress_domain.split('/')[0] path = '/{}'.format('/'.join( ingress_domain.split('/')[1:])) v = db_ingress(name='nginx-ingress', context=context, namespace=namespace, domain=domain, path=path, serviceName=service_name, servicePort=int(ingress_port)) db_op.DB.session.add(v) db_op.DB.session.commit() except Exception as e: logging.error(e) else: # 从数据库读取ingress信息 Rules = [] domain_infos = db_ingress.query.with_entities( distinct(db_ingress.domain)).all() for domain in domain_infos: paths = [] Rules_infos = db_ingress.query.with_entities( db_ingress.path, db_ingress.serviceName, db_ingress.servicePort).filter( and_(db_ingress.domain == domain[0], db_ingress.context == context)).all() path, serviceName, servicePort = Rules_infos[0] for infos in Rules_infos: path, serviceName, servicePort = infos if path: paths.append( client.V1beta1HTTPIngressPath( client.V1beta1IngressBackend( service_name=serviceName, service_port=int(servicePort)), path=path)) if paths: Rules.append( client.V1beta1IngressRule( host=domain, http=client.V1beta1HTTPIngressRuleValue( paths=paths))) else: Rules.append( client.V1beta1IngressRule( host=domain, http=client.V1beta1HTTPIngressRuleValue(paths=[ client.V1beta1HTTPIngressPath( client.V1beta1IngressBackend( service_name=serviceName, service_port=int(servicePort))) ]))) spec = client.V1beta1IngressSpec(rules=Rules) ingress = client.V1beta1Ingress( api_version='extensions/v1beta1', kind='Ingress', metadata=client.V1ObjectMeta( name='nginx-ingress', namespace=namespace, annotations={'kubernetes.io/ingress.class': 'nginx'}), spec=spec) api_instance.patch_namespaced_ingress(body=ingress, namespace=namespace, name='nginx-ingress') except Exception as e: logging.error(e) msg = 'ingress配置失败!' else: msg = 'ingress配置完成!' finally: db_op.DB.session.remove() return render_template('k8s_ingress.html', form=form, msg=msg)
def post(self, request, types): if types == "add": ret = {'code': 0} ns = request.POST.get('ns') svc_name = request.POST.get('selector') svc_port = request.POST.get('target_port', 80) ing_name = request.POST.get('name', svc_name) config.load_kube_config() body = client.V1beta1Ingress() body.api_version = 'extensions/v1beta1' body.kind = 'Ingress' bakend = client.V1beta1IngressBackend(service_name=svc_name, service_port=int(svc_port)) paths = [client.V1beta1HTTPIngressPath(backend=bakend)] htp = client.V1beta1HTTPIngressRuleValue(paths=paths) rules = [client.V1beta1IngressRule(host=ing_name + '.test.cbble.com', http=htp)] body.metadata = client.V1ObjectMeta(name=ing_name) body.spec = client.V1beta1IngressSpec(rules=rules) api = client.ExtensionsV1beta1Api() try: api.create_namespaced_ingress(namespace=ns, body=body) except ApiException as e: tmp = eval(str(e.body)) ret['code'] = tmp.get('code') ret['msg'] = tmp.get('message') return JsonResponse(ret) except ValueError: return JsonResponse({"code": 477, "msg": "Params Err"}) return JsonResponse(ret, safe=True) if types == 'delete': ret = {'code': 0} ing_name = request.POST.get('name') ns = request.POST.get('ns') config.load_kube_config() try: ad = client.V1DeleteOptions(api_version='extensions/v1beta1') client.ExtensionsV1beta1Api().delete_namespaced_ingress(namespace=ns, name=ing_name, body=ad) except ApiException as e: print("Exception when calling ExtensionsV1beta1Api->create_namespaced_ingress: %s\n" % e) ret['code'] = 12 ret['msg'] = e return JsonResponse(ret, safe=True) return JsonResponse(ret, safe=True) if types == 'update': ret = {'code': 0} try: ing_name = request.POST.get('name', None) m_ns = request.POST.get('ns', None) m_port = request.POST.get('target_port', None) m_host = request.POST.get('url', None) + "test.cbble.com" m_label = request.POST.get('selector', None) except TypeError: return JsonResponse({'code': 500, "msg": "parameter ERR!"}) config.load_kube_config() api = client.ExtensionsV1beta1Api() try: ing_obj = api.read_namespaced_ingress(ing_name, m_ns) ing_obj.spec.rules[0].http.paths[0].backend.service_name = m_label ing_obj.spec.rules[0].http.paths[0].backend.service_port = int(m_port) ing_obj.spec.rules[0].host = m_host api.patch_namespaced_ingress(ing_name, m_ns, ing_obj) ret['msg'] = 'succeed' except ApiException as e: tmp = eval(str(e.body)) ret['code'] = tmp.get('code') ret['msg'] = tmp.get('message') return JsonResponse(ret, safe=True) return JsonResponse({"code": 404})
def from_runs(cls, id: str, runs: List[Run]): k8s_name = 'tensorboard-' + id run_names_hash = K8STensorboardInstance.generate_run_names_hash(runs) volume_mounts = [] for run in runs: mount = k8s.V1VolumeMount( name=cls.EXPERIMENTS_OUTPUT_VOLUME_NAME, mount_path=os.path.join( cls.TENSORBOARD_CONTAINER_MOUNT_PATH_PREFIX, run.owner, run.name), sub_path=os.path.join(run.owner, run.name)) volume_mounts.append(mount) deployment_labels = { 'name': k8s_name, 'type': 'nauta-tensorboard', 'nauta_app_name': 'tensorboard', 'id': id, 'runs-hash': run_names_hash } tensorboard_command = [ "tensorboard", "--logdir", cls.TENSORBOARD_CONTAINER_MOUNT_PATH_PREFIX, "--port", "6006", "--host", "127.0.0.1" ] nauta_config = NautaPlatformConfig.incluster_init() tensorboard_image = nauta_config.get_tensorboard_image() tensorboard_proxy_image = nauta_config.get_activity_proxy_image() deployment = k8s.V1Deployment( api_version='apps/v1', kind='Deployment', metadata=k8s.V1ObjectMeta(name=k8s_name, labels=deployment_labels), spec=k8s.V1DeploymentSpec( replicas=1, selector=k8s.V1LabelSelector(match_labels=deployment_labels), template=k8s.V1PodTemplateSpec( metadata=k8s.V1ObjectMeta(labels=deployment_labels), spec=k8s.V1PodSpec( tolerations=[ k8s.V1Toleration(key='master', operator='Exists', effect='NoSchedule') ], affinity=k8s. V1Affinity(node_affinity=k8s.V1NodeAffinity( required_during_scheduling_ignored_during_execution =k8s.V1NodeSelector(node_selector_terms=[ k8s.V1NodeSelectorTerm(match_expressions=[ k8s.V1NodeSelectorRequirement( key="master", operator="In", values=["True"]) ]) ]))), containers=[ k8s.V1Container(name='app', image=tensorboard_image, command=tensorboard_command, volume_mounts=volume_mounts), k8s.V1Container( name='proxy', image=tensorboard_proxy_image, ports=[k8s.V1ContainerPort(container_port=80)], readiness_probe=k8s.V1Probe( period_seconds=5, http_get=k8s.V1HTTPGetAction( path='/healthz', port=80))) ], volumes=[ k8s.V1Volume( name=cls.EXPERIMENTS_OUTPUT_VOLUME_NAME, persistent_volume_claim= # noqa k8s.V1PersistentVolumeClaimVolumeSource( claim_name=cls. EXPERIMENTS_OUTPUT_VOLUME_NAME, read_only=True)) ])))) service = k8s.V1Service( api_version='v1', kind='Service', metadata=k8s.V1ObjectMeta(name=k8s_name, labels={ 'name': k8s_name, 'type': 'nauta-tensorboard', 'nauta_app_name': 'tensorboard', 'id': id }), spec=k8s.V1ServiceSpec( type='ClusterIP', ports=[k8s.V1ServicePort(name='web', port=80, target_port=80)], selector={ 'name': k8s_name, 'type': 'nauta-tensorboard', 'nauta_app_name': 'tensorboard', 'id': id })) ingress = k8s.V1beta1Ingress( api_version='extensions/v1beta1', kind='Ingress', metadata=k8s.V1ObjectMeta( name=k8s_name, labels={ 'name': k8s_name, 'type': 'nauta-tensorboard', 'nauta_app_name': 'tensorboard', 'id': id }, annotations={ 'nauta.ingress.kubernetes.io/rewrite-target': '/', 'kubernetes.io/ingress.class': 'nauta-ingress' }), spec=k8s.V1beta1IngressSpec(rules=[ k8s.V1beta1IngressRule( host='localhost', http=k8s.V1beta1HTTPIngressRuleValue(paths=[ k8s.V1beta1HTTPIngressPath( path='/tb/' + id + "/", backend=k8s.V1beta1IngressBackend( service_name=k8s_name, service_port=80)) ])) ])) return cls(deployment=deployment, service=service, ingress=ingress)