...

Source file src/k8s.io/kubernetes/pkg/scheduler/util/pod_resources.go

Documentation: k8s.io/kubernetes/pkg/scheduler/util

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/api/resource"
    22  )
    23  
    24  // For each of these resources, a container that doesn't request the resource explicitly
    25  // will be treated as having requested the amount indicated below, for the purpose
    26  // of computing priority only. This ensures that when scheduling zero-request pods, such
    27  // pods will not all be scheduled to the node with the smallest in-use request,
    28  // and that when scheduling regular pods, such pods will not see zero-request pods as
    29  // consuming no resources whatsoever. We chose these values to be similar to the
    30  // resources that we give to cluster addon pods (#10653). But they are pretty arbitrary.
    31  // As described in #11713, we use request instead of limit to deal with resource requirements.
    32  const (
    33  	// DefaultMilliCPURequest defines default milli cpu request number.
    34  	DefaultMilliCPURequest int64 = 100 // 0.1 core
    35  	// DefaultMemoryRequest defines default memory request size.
    36  	DefaultMemoryRequest int64 = 200 * 1024 * 1024 // 200 MB
    37  )
    38  
    39  // GetNonzeroRequests returns the default cpu in milli-cpu and memory in bytes resource requests if none is found or
    40  // what is provided on the request.
    41  func GetNonzeroRequests(requests *v1.ResourceList) (int64, int64) {
    42  	cpu := GetRequestForResource(v1.ResourceCPU, requests, true)
    43  	mem := GetRequestForResource(v1.ResourceMemory, requests, true)
    44  	return cpu.MilliValue(), mem.Value()
    45  
    46  }
    47  
    48  // GetRequestForResource returns the requested values unless nonZero is true and there is no defined request
    49  // for CPU and memory.
    50  // If nonZero is true and the resource has no defined request for CPU or memory, it returns a default value.
    51  func GetRequestForResource(resourceName v1.ResourceName, requests *v1.ResourceList, nonZero bool) resource.Quantity {
    52  	if requests == nil {
    53  		return resource.Quantity{}
    54  	}
    55  	switch resourceName {
    56  	case v1.ResourceCPU:
    57  		// Override if un-set, but not if explicitly set to zero
    58  		if _, found := (*requests)[v1.ResourceCPU]; !found && nonZero {
    59  			return *resource.NewMilliQuantity(DefaultMilliCPURequest, resource.DecimalSI)
    60  		}
    61  		return requests.Cpu().DeepCopy()
    62  	case v1.ResourceMemory:
    63  		// Override if un-set, but not if explicitly set to zero
    64  		if _, found := (*requests)[v1.ResourceMemory]; !found && nonZero {
    65  			return *resource.NewQuantity(DefaultMemoryRequest, resource.DecimalSI)
    66  		}
    67  		return requests.Memory().DeepCopy()
    68  	default:
    69  		quantity, found := (*requests)[resourceName]
    70  		if !found {
    71  			return resource.Quantity{}
    72  		}
    73  		return quantity.DeepCopy()
    74  	}
    75  }
    76  

View as plain text