def list(self, request, project_id, cluster_id): """获取指定资源某resource_version后变更记录""" params = self.params_validate(FetchResourceWatchResultSLZ, context={'ctx_cluster': request.ctx_cluster}) res_kind, res_version, namespace = params['kind'], params['resource_version'], params.get('namespace') watch_kwargs = { 'namespace': namespace, 'resource_version': res_version, 'timeout': DEFAULT_SUBSCRIBE_TIMEOUT, } if is_native_kind(res_kind): # 根据 Kind 获取对应的 K8S Resource Client 并初始化 resource_client = get_native_kind_resource_client(res_kind)(request.ctx_cluster) # 对于命名空间,watch_kwargs 需要补充 cluster_type,project_code 以支持共享集群的需求 if res_kind == K8sResourceKind.Namespace.value: watch_kwargs.update( {'cluster_type': get_cluster_type(cluster_id), 'project_code': request.project.english_name} ) else: # 自定义资源类型走特殊的获取 ResourceClient 逻辑 且 需要指定 Formatter resource_client = CustomObject(request.ctx_cluster, kind=res_kind, api_version=params['api_version']) watch_kwargs['formatter'] = CustomObjectCommonFormatter() try: events = resource_client.watch(**watch_kwargs) except ApiException as e: if e.status == K8S_API_GONE_STATUS_CODE: raise ResourceVersionExpired(_('ResourceVersion {} 已过期,请重新获取').format(res_version)) raise # events 默认按时间排序,取最后一个 ResourceVersion 即为最新值 latest_rv = getitems(events[-1], 'manifest.metadata.resourceVersion') if events else None response_data = {'events': events, 'latest_rv': latest_rv} return Response(response_data)
def get_custom_object(self, request, project_id, cluster_id, crd_name, name): cobj_client = CustomObject( access_token=request.user.token.access_token, project_id=project_id, cluster_id=cluster_id, crd_name=crd_name, ) query_ns = request.query_params.get("namespace") if query_ns: cobj = cobj_client.get_namespaced_custom_object(query_ns, name) else: cobj = cobj_client.get_cluster_custom_object(name) return Response(cobj)
def patch_custom_object_scale(self, request, project_id, cluster_id, crd_name, name): req_data = request.data.copy() req_data["crd_name"] = crd_name serializer = PatchCustomObjectScaleSLZ(data=req_data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data cobj_client = CustomObject( access_token=request.user.token.access_token, project_id=project_id, cluster_id=cluster_id, crd_name=crd_name, ) # TODO 支持 cluster scope 的 custom object 扩缩容 cobj_client.patch_namespaced_custom_object_scale(validated_data["namespace"], name, validated_data["body"]) return Response()
def list(self, request, project_id, cluster_id): """获取指定资源某resource_version后变更记录""" params = self.params_validate(FetchResourceWatchResultSLZ) res_version = params['resource_version'] watch_kwargs = { 'resource_version': res_version, 'timeout': DEFAULT_SUBSCRIBE_TIMEOUT, } if params['kind'] in KIND_RESOURCE_CLIENT_MAP: # 根据 Kind 获取对应的 K8S Resource Client 并初始化 Client = KIND_RESOURCE_CLIENT_MAP[params['kind']] resource_client = Client(request.ctx_cluster) else: # 自定义资源类型走特殊的获取 ResourceClient 逻辑 且 需要指定 Formatter resource_client = CustomObject(request.ctx_cluster, kind=params['kind'], api_version=params['api_version']) watch_kwargs['formatter'] = CustomObjectCommonFormatter() try: events = resource_client.watch(**watch_kwargs) except ApiException as e: if e.status == K8S_API_GONE_STATUS_CODE: raise ResourceVersionExpired( _('ResourceVersion {} 已过期,请重新获取').format(res_version)) raise # events 默认按时间排序,取最后一个 ResourceVersion 即为最新值 latest_rv = getitems( events[-1], 'manifest.metadata.resourceVersion') if events else None response_data = {'events': events, 'latest_rv': latest_rv} return Response(response_data)
def list_custom_objects(self, request, project_id, cluster_id, crd_name): crd_client = CustomResourceDefinition( access_token=request.user.token.access_token, project_id=project_id, cluster_id=cluster_id, ) crd = crd_client.get_custom_resource_definition(crd_name) cobj_client = CustomObject( access_token=request.user.token.access_token, project_id=project_id, cluster_id=cluster_id, crd_name=crd_name, ) query_ns = request.query_params.get("namespace") if query_ns: cobjs = cobj_client.list_namespaced_custom_object(query_ns) else: cobjs = cobj_client.list_cluster_custom_object() return Response(to_table_format(crd, cobjs, cluster_id=cluster_id))
def delete_custom_object(self, request, project_id, cluster_id, crd_name, name): cobj_client = CustomObject( access_token=request.user.token.access_token, project_id=project_id, cluster_id=cluster_id, crd_name=crd_name, ) namespace = request.query_params.get("namespace") if namespace: cobj_client.delete_namespaced_custom_object(namespace, name) else: cobj_client.delete_cluster_custom_object(name) return Response()
def patch_custom_object(self, request, project_id, cluster_id, crd_name, name): serializer = PatchCustomObjectSLZ(data=request.data) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data cobj_client = CustomObject( access_token=request.user.token.access_token, project_id=project_id, cluster_id=cluster_id, crd_name=crd_name, ) namespace = validated_data.get("namespace") if namespace: cobj_client.patch_namespaced_custom_object(namespace, name, validated_data["body"]) else: cobj_client.patch_cluster_custom_object(name, validated_data["body"]) return Response()