def _update_or_create(self, request, project_id): release_data = self._release_data(request, project_id) release_manager = AppReleaseManager( dynamic_client=get_dynamic_client(request.use.token.access_token, project_id, release_data.cluster_id) ) release_manager.update_or_create(request.user.username, release_data=release_data) return Response()
def __init__(self, access_token: str, project_id: str, cluster_id: str): self.access_token = access_token self.project_id = project_id self.cluster_id = cluster_id self.dynamic_client = get_dynamic_client(access_token, project_id, cluster_id) self.api = self.dynamic_client.get_preferred_resource('ResourceQuota')
def delete(self, request, project_id, release_id): app_release = models.AppRelease.objects.get(id=release_id, project_id=project_id) release_manager = AppReleaseManager( dynamic_client=get_dynamic_client(request.use.token.access_token, project_id, app_release.cluster_id) ) release_manager.delete(request.user.username, release_id) return Response()
def __init__(self, ctx_cluster: CtxCluster, api_version: Optional[str] = None): self.dynamic_client = get_dynamic_client( ctx_cluster.context.auth.access_token, ctx_cluster.project_id, ctx_cluster.id ) if api_version: self.api = self.dynamic_client.resources.get(kind=self.kind, api_version=api_version) else: self.api = self.dynamic_client.get_preferred_resource(self.kind)
def __init__(self, access_token: str, project_id: str, cluster_id: str, namespace: str): self.access_token = access_token self.project_id = project_id self.cluster_id = cluster_id # TODO: 去掉初始化方法里的 namespace 参数,与其他类保持统一 self.namespace = namespace self.dynamic_client = get_dynamic_client(access_token, project_id, cluster_id) self.api = self.dynamic_client.get_preferred_resource('Deployment')
def __init__(self, ctx_cluster: CtxCluster, namespace: str, pod_name: str): self.ctx_cluster = ctx_cluster self.dynamic_client = get_dynamic_client( ctx_cluster.context.auth.access_token, ctx_cluster.project_id, ctx_cluster.id) pod_resource = self.dynamic_client.get_preferred_resource("Pod") self.resource = pod_resource.subresources['log'] self.namespace = namespace self.pod_name = pod_name
def __init__(self, cluster_auth: ClusterAuth, api_version: Optional[str] = None): self.dynamic_client = get_dynamic_client(cluster_auth.access_token, cluster_auth.project_id, cluster_auth.cluster_id) if api_version: self.api = self.dynamic_client.resources.get( kind=self.kind, api_version=api_version) else: self.api = self.dynamic_client.get_preferred_resource(self.kind)
def get_helm_dashboard_path(access_token: str, project_id: str, cluster_id: str) -> str: """获取dashboard的路径""" client = get_dynamic_client(access_token, project_id, cluster_id) # 获取版本 version = get_bcs_component_version( client.version["kubernetes"]["gitVersion"], DASHBOARD_CTL_VERSION, DEFAULT_DASHBOARD_CTL_VERSION) bin_path_map = getattr(settings, "DASHBOARD_CTL_VERSION_MAP", {}) return bin_path_map.get(version, settings.DASHBOARD_CTL_BIN)
def query_cluster_version(self, access_token: str, project_id: str, cluster_id: str) -> str: """查询集群版本 NOTE: 调用接口出现异常时,返回为空字符串,其它信息可以通过集群快照中获取 """ try: client = get_dynamic_client(access_token, project_id, cluster_id) return client.version["kubernetes"]["gitVersion"] except Exception as e: logger.error("query cluster version error, %s", e) # N/A 表示集群不可用 return "N/A"
def __init__(self, ctx_cluster: CtxCluster, api_version: Optional[str] = None, cache_client: Optional[bool] = True): self.dynamic_client = get_dynamic_client( ctx_cluster.context.auth.access_token, ctx_cluster.project_id, ctx_cluster.id, use_cache=cache_client) self.api = get_resource_api(self.dynamic_client, self.kind, api_version) # 保存 ctx_cluster 作为类对象,部分方法会使用 self.ctx_cluster = ctx_cluster
def __init__(self, ctx_cluster: CtxCluster, api_version: Optional[str] = None, cache_client: Optional[bool] = True): self.dynamic_client = get_dynamic_client( ctx_cluster.context.auth.access_token, ctx_cluster.project_id, ctx_cluster.id, use_cache=cache_client) if api_version: self.api = self.dynamic_client.resources.get( kind=self.kind, api_version=api_version) else: self.api = self.dynamic_client.get_preferred_resource(self.kind) # 保存 ctx_cluster 作为类对象,部分方法会使用 self.ctx_cluster = ctx_cluster
def test_filter_by_owners(kind, name, expected_names, project_id, cluster_id, random_name): client_obj = get_dynamic_client('token', project_id, cluster_id) rs_body = construct_replica_set('foo-rs-demo') deployment_body = construct_deployment('foo-deployment') rs_with_owner_body = construct_replica_set('foo-rs-owned-by-deployment', deployment_body) rs_api = client_obj.get_preferred_resource('ReplicaSet') rs_api.create(namespace=random_name, body=rs_body) rs_api.create(namespace=random_name, body=rs_with_owner_body) results = rs_api.get(namespace=random_name) filtered_results = filter_by_owners(results.items, kind, [name]) names = [InstanceAccessor(res).name for res in filtered_results] assert names == expected_names
def get_cluster_proper_kubectl(access_token, project_id, cluster_id): client = get_dynamic_client(access_token, project_id, cluster_id) kubectl_version = get_kubectl_version( client.version["kubernetes"]["gitVersion"], constants.KUBECTL_VERSION, constants.DEFAULT_KUBECTL_VERSION) try: kubectl_bin = settings.KUBECTL_BIN_MAP[kubectl_version] except Exception as err: logger.error( "get kubectl error, kubectl version: %s, error message: %s", kubectl_version, err) kubectl_bin = settings.KUBECTL_BIN_MAP[ constants.DEFAULT_KUBECTL_VERSION] return kubectl_bin, kubectl_version
def __init__(self, access_token: str, project_id: str, cluster_id: str, namespace: str): self.namespace = namespace self.dynamic_client = get_dynamic_client(access_token, project_id, cluster_id) self.api = self.dynamic_client.get_preferred_resource('Pod')