Beispiel #1
0
 def get_pod(self, pod_name: str) -> List[Dict]:
     """通过名称获取 Pod"""
     pod = self.api.get_or_none(namespace=self.namespace, name=pod_name)
     if not pod:
         return []
     # TODO: 格式化为 Response 格式的逻辑应该从此处往外移动
     return [ResourceDefaultFormatter().format(pod)]
    def test_format_diff_cluster_id(self, labels, cluster_id, pod_resource):
        pod_resource['metadata']['labels'] = labels
        formatted_data = ResourceDefaultFormatter().format_dict(pod_resource)

        assert formatted_data['clusterId'] == cluster_id
        assert formatted_data['resourceType'] == 'Pod'
        assert formatted_data['resourceName'] == 'demo-pod'
        assert formatted_data['namespace'] == 'demo-pod-ns'
        assert formatted_data['data']['metadata']['annotations'] == {}
Beispiel #3
0
 def get_pod(self, request, project_id_or_code, cluster_id, namespace,
             pod_name):
     """ 获取指定 Pod 信息,以列表格式返回 """
     pod = Pod(request.ctx_cluster).get(
         namespace=namespace,
         name=pod_name,
         formatter=ResourceDefaultFormatter())
     # 保持接口格式不变,如果查询不到则返回空列表
     response_data = [pod] if pod else []
     return Response(response_data)
Beispiel #4
0
 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)
Beispiel #6
0
 def get_pods_by_rs(self, rs_name: str) -> List[Dict]:
     """根据 ReplicaSet 名称查询 Pod 列表"""
     pods = self.api.get(namespace=self.namespace)
     results = filter_by_owners(pods.items, 'ReplicaSet', [rs_name])
     return ResourceDefaultFormatter().format_list(results)
Beispiel #7
0
 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)
Beispiel #8
0
class MyPod(ResourceClient):
    kind = 'Pod'
    formatter = ResourceDefaultFormatter()
    result_type = PodResourceObj