Exemplo n.º 1
0
func GetResourceRequest(pod *v1.Pod) *schedulercache.Resource {
	result := schedulercache.Resource{}
	for _, container := range pod.Spec.Containers {
		for rName, rQuantity := range container.Resources.Requests {
			switch rName {
			case v1.ResourceMemory:
				result.Memory += rQuantity.Value()
			case v1.ResourceCPU:
				result.MilliCPU += rQuantity.MilliValue()
			case v1.ResourceNvidiaGPU:
				result.NvidiaGPU += rQuantity.Value()
			default:
				if v1.IsOpaqueIntResourceName(rName) {
					// Lazily allocate this map only if required.
					if result.OpaqueIntResources == nil {
						result.OpaqueIntResources = map[v1.ResourceName]int64{}
					}
					result.OpaqueIntResources[rName] += rQuantity.Value()
				}
			}
		}
	}
	// take max_resource(sum_pod, any_init_container)
	for _, container := range pod.Spec.InitContainers {
		for rName, rQuantity := range container.Resources.Requests {
			switch rName {
			case v1.ResourceMemory:
				if mem := rQuantity.Value(); mem > result.Memory {
					result.Memory = mem
				}
			case v1.ResourceCPU:
				if cpu := rQuantity.MilliValue(); cpu > result.MilliCPU {
					result.MilliCPU = cpu
				}
			case v1.ResourceNvidiaGPU:
				if gpu := rQuantity.Value(); gpu > result.NvidiaGPU {
					result.NvidiaGPU = gpu
				}
			default:
				if v1.IsOpaqueIntResourceName(rName) {
					// Lazily allocate this map only if required.
					if result.OpaqueIntResources == nil {
						result.OpaqueIntResources = map[v1.ResourceName]int64{}
					}
					value := rQuantity.Value()
					if value > result.OpaqueIntResources[rName] {
						result.OpaqueIntResources[rName] = value
					}
				}
			}
		}
	}
	return &result
}
Exemplo n.º 2
0
// Sets the overall node information.
func (n *NodeInfo) SetNode(node *v1.Node) error {
	n.node = node
	for rName, rQuant := range node.Status.Allocatable {
		switch rName {
		case v1.ResourceCPU:
			n.allocatableResource.MilliCPU = rQuant.MilliValue()
		case v1.ResourceMemory:
			n.allocatableResource.Memory = rQuant.Value()
		case v1.ResourceNvidiaGPU:
			n.allocatableResource.NvidiaGPU = rQuant.Value()
		case v1.ResourcePods:
			n.allowedPodNumber = int(rQuant.Value())
		default:
			if v1.IsOpaqueIntResourceName(rName) {
				// Lazily allocate opaque resource map.
				if n.allocatableResource.OpaqueIntResources == nil {
					n.allocatableResource.OpaqueIntResources = map[v1.ResourceName]int64{}
				}
				n.allocatableResource.OpaqueIntResources[rName] = rQuant.Value()
			}
		}
	}
	n.generation++
	return nil
}
Exemplo n.º 3
0
func calculateResource(pod *v1.Pod) (res Resource, non0_cpu int64, non0_mem int64) {
	for _, c := range pod.Spec.Containers {
		for rName, rQuant := range c.Resources.Requests {
			switch rName {
			case v1.ResourceCPU:
				res.MilliCPU += rQuant.MilliValue()
			case v1.ResourceMemory:
				res.Memory += rQuant.Value()
			case v1.ResourceNvidiaGPU:
				res.NvidiaGPU += rQuant.Value()
			default:
				if v1.IsOpaqueIntResourceName(rName) {
					// Lazily allocate opaque resource map.
					if res.OpaqueIntResources == nil {
						res.OpaqueIntResources = map[v1.ResourceName]int64{}
					}
					res.OpaqueIntResources[rName] += rQuant.Value()
				}
			}
		}

		non0_cpu_req, non0_mem_req := priorityutil.GetNonzeroRequests(&c.Resources.Requests)
		non0_cpu += non0_cpu_req
		non0_mem += non0_mem_req
		// No non-zero resources for GPUs or opaque resources.
	}
	return
}
Exemplo n.º 4
0
// Sets the overall node information.
func (n *NodeInfo) SetNode(node *v1.Node) error {
	n.node = node
	for rName, rQuant := range node.Status.Allocatable {
		switch rName {
		case v1.ResourceCPU:
			n.allocatableResource.MilliCPU = rQuant.MilliValue()
		case v1.ResourceMemory:
			n.allocatableResource.Memory = rQuant.Value()
		case v1.ResourceNvidiaGPU:
			n.allocatableResource.NvidiaGPU = rQuant.Value()
		case v1.ResourcePods:
			n.allowedPodNumber = int(rQuant.Value())
		default:
			if v1.IsOpaqueIntResourceName(rName) {
				// Lazily allocate opaque resource map.
				if n.allocatableResource.OpaqueIntResources == nil {
					n.allocatableResource.OpaqueIntResources = map[v1.ResourceName]int64{}
				}
				n.allocatableResource.OpaqueIntResources[rName] = rQuant.Value()
			}
		}
	}
	n.taints, n.taintsErr = v1.GetTaintsFromNodeAnnotations(node.Annotations)
	for i := range node.Status.Conditions {
		cond := &node.Status.Conditions[i]
		switch cond.Type {
		case v1.NodeMemoryPressure:
			n.memoryPressureCondition = cond.Status
		case v1.NodeDiskPressure:
			n.diskPressureCondition = cond.Status
		default:
			// We ignore other conditions.
		}
	}
	n.generation++
	return nil
}