def log(self): if self._spec.kind == "Pod": return self._spec.logs() else: pod_selector = "job-name=%s" % self._spec.name pods = kubernetes.list_cluster_pods(raw_selector=pod_selector) for pod in pods: if pod.obj['status']['phase'] == "Failed": continue return pod.logs()\
def _get_pods_status(service, svc_map): pods = kubernetes.list_cluster_pods(service=service) total = running = 0 for pod in pods: total += 1 if pod.ready: running += 1 return State(name=service, total=total, running=running, urls=svc_map.get(service, []))
def get_pod_states(components=None): ext_ip = CONF.configs.get("k8s_external_ip", "") states = {} selector = "app in (%s)" % ','.join(components) if components else None for dp in kubernetes.list_cluster_deployments(selector): states.setdefault(dp.name, copy.deepcopy(STATE_TEMPLATE)) dp_st = dp.obj["status"] states[dp.name]["pod_total"] = dp.obj["spec"]["replicas"] states[dp.name]["pod_running"] = min(dp_st.get("availableReplicas", 0), dp_st.get("updatedReplicas", 0)) for job in kubernetes.list_cluster_jobs(selector): app_name = job.obj["metadata"]["labels"].get("app") states.setdefault(app_name, copy.deepcopy(STATE_TEMPLATE)) states[app_name]["job_total"] += job.obj["spec"]["completions"] states[app_name]["job_completed"] += (job.obj["status"].get( "succeeded", 0)) for ss in kubernetes.list_cluster_statefulsets(selector): states.setdefault(ss.name, copy.deepcopy(STATE_TEMPLATE)) states[ss.name]["pod_total"] = ss.obj["spec"]["replicas"] for pod in kubernetes.list_cluster_pods(ss.name): if not pod.obj.get("status", {}).get("containerStatuses"): continue if all((cont.get("ready", False) for cont in pod.obj["status"]["containerStatuses"])): states[ss.name]["pod_running"] += 1 if CONF.configs.ingress.enabled: url_template = "https://%s" if CONF.configs.ingress.get("port"): url_template += ":%d" % CONF.configs.ingress.port for ing in kubernetes.list_cluster_ingress(): if components and ing.name not in components: continue states.setdefault(ing.name, copy.deepcopy(STATE_TEMPLATE)) for rule in ing.obj['spec']['rules']: states[ing.name]['links'].append(url_template % rule['host']) else: for svc in kubernetes.list_cluster_services(): svc_name = svc.obj["metadata"]["name"] if components and svc_name not in components: continue states.setdefault(svc_name, copy.deepcopy(STATE_TEMPLATE)) for port in svc.obj["spec"]["ports"]: if port.get('nodePort'): states[svc_name]["links"].append( EXT_LINK_TEMPLATE.format(ext_ip=ext_ip, port=port["nodePort"])) return states
def get_pod_states(components=None): ext_ip = CONF.configs.get("k8s_external_ip", "") states = {} selector = "app in (%s)" % ','.join(components) if components else None for dp in kubernetes.list_cluster_deployments(selector): states.setdefault(dp.name, copy.deepcopy(STATE_TEMPLATE)) dp_st = dp.obj["status"] states[dp.name]["pod_total"] = dp.obj["spec"]["replicas"] states[dp.name]["pod_running"] = min( dp_st.get("availableReplicas", 0), dp_st.get("updatedReplicas", 0)) for job in kubernetes.list_cluster_jobs(selector): app_name = job.obj["metadata"]["labels"].get("app") states.setdefault(app_name, copy.deepcopy(STATE_TEMPLATE)) states[app_name]["job_total"] += job.obj["spec"]["completions"] states[app_name]["job_completed"] += ( job.obj["status"].get("succeeded", 0)) for ss in kubernetes.list_cluster_statefulsets(selector): states.setdefault(ss.name, copy.deepcopy(STATE_TEMPLATE)) states[ss.name]["pod_total"] = ss.obj["spec"]["replicas"] for pod in kubernetes.list_cluster_pods(ss.name): if not pod.obj.get("status", {}).get("containerStatuses"): continue if all((cont.get("ready", False) for cont in pod.obj["status"]["containerStatuses"])): states[ss.name]["pod_running"] += 1 if CONF.configs.ingress.enabled: url_template = "https://%s" if CONF.configs.ingress.get("port"): url_template += ":%d" % CONF.configs.ingress.port for ing in kubernetes.list_cluster_ingress(): if components and ing.name not in components: continue states.setdefault(ing.name, copy.deepcopy(STATE_TEMPLATE)) for rule in ing.obj['spec']['rules']: states[ing.name]['links'].append(url_template % rule['host']) else: for svc in kubernetes.list_cluster_services(): svc_name = svc.obj["metadata"]["name"] if components and svc_name not in components: continue states.setdefault(svc_name, copy.deepcopy(STATE_TEMPLATE)) for port in svc.obj["spec"]["ports"]: if port.get('nodePort'): states[svc_name]["links"].append(EXT_LINK_TEMPLATE.format( ext_ip=ext_ip, port=port["nodePort"])) return states
def delete_action(action_name): try: action = kubernetes.list_cluster_jobs(name=action_name) except pykube_exc.ObjectDoesNotExist: try: action = kubernetes.list_cluster_pods(name=action_name) except pykube_exc.ObjectDoesNotExist: LOG.error('Action with name %s not found', action_name) return False try: action.delete() except pykube_exc.HTTPError as ex: LOG.error(ex.message) return False LOG.info('Action %s is terminating', action_name) return True
def get_actions(cls, action_name=None): selector = "ccp-action=true" if action_name: selector += "," + "app=%s" % action_name actions = [] job_names = [] for job in kubernetes.list_cluster_jobs(selector=selector): actions.append(cls(job)) job_names.append(job.name) for pod in kubernetes.list_cluster_pods(selector=selector): job_name = pod.labels.get("job-name") if job_name and job_name not in job_names: actions.append(cls(pod, job_name)) elif not job_name: actions.append(cls(pod)) return actions