def list_pods_by_deployment(self, request, project_id_or_code, cluster_id, namespace, deploy_name): # TODO 增加用户对层级资源project/cluster/namespace的权限校验(由于粒度没有细化到Deployment) deployment = Deployment(request.ctx_cluster).get(namespace=namespace, name=deploy_name, is_format=False) labels_string = make_labels_string(getitems(deployment.data.to_dict(), 'spec.selector.matchLabels', {})) pods = Pod(request.ctx_cluster).list( namespace=namespace, label_selector=labels_string, is_format=False, owner_kind=K8sResourceKind.Deployment.value, owner_name=deploy_name, )['items'] return Response(ResourceDefaultFormatter().format_list(pods))
def get_pods_by_deployment(self, deploy_name: str) -> List[Dict]: """根据 Deployment 查询所有 Pod 列表""" pod_api = self.dynamic_client.get_preferred_resource('Pod') labels = self.get_selector_labels(deploy_name) # Query all pods matchs labels pods = pod_api.get(namespace=self.namespace, label_selector=make_labels_string(labels)) rs_name_list = self.get_rs_name_list(deploy_name) # Filter by replicaset names results = filter_by_owners(pods.items, 'ReplicaSet', rs_name_list) return ResourceDefaultFormatter().format_list(results)
def get_pod_by_labels(self, selector_labels: Dict) -> List[Dict]: """通过 labels 字典过滤 Pods""" pods = self.api.get(namespace=self.namespace, label_selector=make_labels_string(selector_labels)) return ResourceDefaultFormatter().format_list(pods)