k8s.io/kubernetes@v1.29.3/pkg/kubelet/apis/config/validation/validation_reserved_memory.go (about)

     1  /*
     2  Copyright 2018 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 validation
    18  
    19  import (
    20  	"fmt"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	corev1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
    24  	kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
    25  )
    26  
    27  // validateReservedMemory validates the reserved memory configuration and returns an error if it is invalid.
    28  func validateReservedMemoryConfiguration(kc *kubeletconfig.KubeletConfiguration) []error {
    29  	if len(kc.ReservedMemory) == 0 {
    30  		return nil
    31  	}
    32  
    33  	var errors []error
    34  
    35  	numaTypeDuplicates := map[int32]map[v1.ResourceName]bool{}
    36  	for _, reservedMemory := range kc.ReservedMemory {
    37  		numaNode := reservedMemory.NumaNode
    38  		if _, ok := numaTypeDuplicates[numaNode]; !ok {
    39  			numaTypeDuplicates[numaNode] = map[v1.ResourceName]bool{}
    40  		}
    41  
    42  		for resourceName, q := range reservedMemory.Limits {
    43  			if !reservedMemorySupportedLimit(resourceName) {
    44  				errors = append(errors, fmt.Errorf("invalid configuration: the limit type %q for NUMA node %d is not supported, only %v is accepted", resourceName, numaNode, []v1.ResourceName{v1.ResourceMemory, v1.ResourceHugePagesPrefix + "<HugePageSize>"}))
    45  			}
    46  
    47  			// validates that the limit has non-zero value
    48  			if q.IsZero() {
    49  				errors = append(errors, fmt.Errorf("invalid configuration: reserved memory may not be zero for NUMA node %d and resource %q", numaNode, resourceName))
    50  			}
    51  
    52  			// validates that no duplication for NUMA node and limit type occurred
    53  			if _, ok := numaTypeDuplicates[numaNode][resourceName]; ok {
    54  				errors = append(errors, fmt.Errorf("invalid configuration: the reserved memory has a duplicate value for NUMA node %d and resource %q", numaNode, resourceName))
    55  			}
    56  			numaTypeDuplicates[numaNode][resourceName] = true
    57  		}
    58  	}
    59  	return errors
    60  }
    61  
    62  func reservedMemorySupportedLimit(resourceName v1.ResourceName) bool {
    63  	return corev1helper.IsHugePageResourceName(resourceName) || resourceName == v1.ResourceMemory
    64  }