def update_a_resource_quota( self, namespace: str, name: str, spec: Union[Dict, V1ResourceQuotaSpec]) -> ResourceInstance: """ Method to change the resource quota for a project. Note when patching a ResourceQuota make sure to include everything that you don't want changed either. :param namespace: The namespace containing the targeted resource quota :param name: The targeted resource quota :param spec: a V1ResourceQuotaSpec or a ResourceQuotaSpec dictionary :return: A ResourceInstace object """ """ Can't update/patch a quota with different scope values once a quota has been created with scopes values. It always throws a 422 error from OpenShift. If you really want to patch a quota so that it does not have scopes define. You're better off running the delete_a_resource_quota and create_a_resource_quota using the new scope values """ if isinstance(spec, dict): spec = V1ResourceQuotaSpec(**spec) body = {"spec": spec.to_dict()} api_response = None try: api_response = self.ocp_resource_quota.patch(name=name, namespace=namespace, body=body) except ApiException as e: logger.error("Exception while updating ResourceQuota: %s\n", e) return api_response
def build(self) -> V1ResourceQuota: """ This returns the final product that a client can use to create ResourceQuota. :return: V1ResourceQuotaSpec object """ lri = self._model self._model = V1ResourceQuotaSpec() return lri
def create_resource_quota_namespaced(cluster_mgmt,namespace,noOfgpu): v1_api=cluster_mgmt.kube_api quota = V1ResourceQuota() quota.api_version = "v1" quota.kind = "ResourceQuota" meta = V1ObjectMeta() meta.generate_name = "quota" quota.metadata = meta quota_spec = V1ResourceQuotaSpec() hard = {"requests.nvidia.com/gpu": str(int(noOfgpu))} quota_spec.hard = hard quota.spec = quota_spec api_response = v1_api.create_namespaced_resource_quota(namespace, quota) return api_response.metadata.name
def get_resource_quota_spec(self): '''We're going to return a resource quota spec that checks whether we have a custom resource map and uses that information. If we do not then our default quota allows a maximum of MAX_DASK_WORKERS or 25 (chosen arbitrarily) of the largest-size machines available to the user. Note that you could get a lot fancier, and check the user group memberships to determine what class a user belonged to, or some other more-sophisticated-than-one-size-fits-all quota mechanism. ''' self.log.info("Entering get_resource_quota_spec()") from kubernetes.client import V1ResourceQuotaSpec self.log.info("Calculating default resource quotas.") sizes = self.sizelist max_dask_workers = os.environ.get('MAX_DASK_WORKERS') or 25 max_machines = int(max_dask_workers) + 1 # (the 1 is the Lab) big_multiplier = 2**(len(sizes) - 1) tiny_cpu = os.environ.get('TINY_MAX_CPU') or 0.5 if type(tiny_cpu) is str: tiny_cpu = float(tiny_cpu) mem_per_cpu = os.environ.get('MB_PER_CPU') or 2048 if type(mem_per_cpu) is str: mem_per_cpu = int(mem_per_cpu) total_cpu = max_machines * big_multiplier * tiny_cpu total_mem = str(int(total_cpu * mem_per_cpu + 0.5)) + "Mi" total_cpu = str(int(total_cpu + 0.5)) self.log.debug("Default quota sizes: CPU %r, mem %r" % (total_cpu, total_mem)) if self._custom_resources: self.log.debug("Have custom resources.") cpuq = self._custom_resources.get("cpu_quota") if cpuq: self.log.debug("Overriding CPU quota.") total_cpu = str(cpuq) memq = self._custom_resources.get("mem_quota") if memq: self.log.debug("Overriding memory quota.") total_mem = str(memq) + "Mi" self.log.info("Determined quota sizes: CPU %r, mem %r" % (total_cpu, total_mem)) qs = V1ResourceQuotaSpec(hard={ "limits.cpu": total_cpu, "limits.memory": total_mem }) self.log.info("Resource quota spec: %r" % qs) self._quota = qs.hard return qs
def define_resource_quota_spec(self): '''We're going to return a resource quota spec that checks whether we have a custom resource map and uses that information. If we do not then we use the quota from our parent's config object. Note that you could get a lot fancier, and check the user group memberships to determine what class a user belonged to, or some other more-sophisticated-than-one-size-fits-all quota mechanism. ''' with start_action(action_type="define_resource_quota_spec"): self.log.debug("Calculating default resource quotas.") om = self.parent.optionsform_mgr sizemap = om.sizemap # sizemap is an ordered dict, and we want the last-inserted one, # which is the biggest big = list(sizemap.keys())[-1] cpu = sizemap[big]['cpu'] cfg = self.parent.config max_dask_workers = 0 if cfg.allow_dask_spawn: max_dask_workers = cfg.max_dask_workers mem_per_cpu = cfg.mb_per_cpu total_cpu = (1 + max_dask_workers) * cpu total_mem = str(int(total_cpu * mem_per_cpu + 0.5)) + "Mi" total_cpu = str(int(total_cpu + 0.5)) self.log.debug("Default quota sizes: CPU %r, mem %r" % (total_cpu, total_mem)) self._set_custom_user_resources() if self.custom_resources: self.log.debug("Have custom resources.") cpuq = self.custom_resources.get("cpu_quota") if cpuq: self.log.debug("Overriding CPU quota.") total_cpu = str(cpuq) memq = self.custom_resources.get("mem_quota") if memq: self.log.debug("Overriding memory quota.") total_mem = str(memq) + "Mi" self.log.debug("Determined quota sizes: CPU %r, mem %r" % (total_cpu, total_mem)) qs = V1ResourceQuotaSpec(hard={ "limits.cpu": total_cpu, "limits.memory": total_mem }) self.quota = qs.hard
def make_resource_quota( name, namespace, limits_cpu, limits_memory, requests_cpu, requests_memory, ): hard = {"limits.cpu": limits_cpu, "limits.memory": str(limits_memory) + "Mi", "requests.cpu": requests_cpu, "requests.memory": str(requests_memory) + "Mi", } spec = V1ResourceQuotaSpec(hard=hard) meta = V1ObjectMeta(namespace=namespace, name=name) quota = V1ResourceQuota(metadata=meta, spec=spec) return quota
def get_resource_quota_spec(self): '''We're going to return a resource quota spec that allows a max of 25 (chosen arbitrarily) of the largest-size machines available to the user. Note that you could get a lot fancier, and check the user group memberships to determine what class a user belonged to, or some other more-sophisticated-than-one-size-fits-all quota mechanism. ''' self.log.info("Entering get_resource_quota_spec()") from kubernetes.client import V1ResourceQuotaSpec sizes = self.sizelist max_machines = 25 + 1 # Arbitrary (the 25 is intended to represent # the number of dask machines, and the 1 is the lab ) big_multiplier = 2**(len(sizes) - 1) self.log.info("Max machines %r, multiplier %r" % (max_machines, big_multiplier)) tiny_cpu = os.environ.get('TINY_MAX_CPU') or 0.5 if type(tiny_cpu) is str: tiny_cpu = float(tiny_cpu) mem_per_cpu = os.environ.get('MB_PER_CPU') or 2048 if type(mem_per_cpu) is str: mem_per_cpu = int(mem_per_cpu) self.log.info("Tiny CPU: %r, Mem per cpu: %r" % (tiny_cpu, mem_per_cpu)) total_cpu = max_machines * big_multiplier * tiny_cpu total_mem = str(int(total_cpu * mem_per_cpu + 0.5)) + "Mi" total_cpu = str(int(total_cpu + 0.5)) self.log.info("Determined quota sizes: CPU %r, mem %r" % (total_cpu, total_mem)) qs = V1ResourceQuotaSpec(hard={ "limits.cpu": total_cpu, "limits.memory": total_mem }) self.log.info("Resource quota spec: %r" % qs) self._quota = qs.hard return qs
def __init__(self): self._model = V1ResourceQuotaSpec()