...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/scope_container.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cm/topologymanager

     1  /*
     2  Copyright 2020 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 topologymanager
    18  
    19  import (
    20  	"k8s.io/api/core/v1"
    21  	"k8s.io/klog/v2"
    22  	"k8s.io/kubernetes/pkg/kubelet/cm/admission"
    23  	"k8s.io/kubernetes/pkg/kubelet/cm/containermap"
    24  	"k8s.io/kubernetes/pkg/kubelet/lifecycle"
    25  	"k8s.io/kubernetes/pkg/kubelet/metrics"
    26  )
    27  
    28  type containerScope struct {
    29  	scope
    30  }
    31  
    32  // Ensure containerScope implements Scope interface
    33  var _ Scope = &containerScope{}
    34  
    35  // NewContainerScope returns a container scope.
    36  func NewContainerScope(policy Policy) Scope {
    37  	return &containerScope{
    38  		scope{
    39  			name:             containerTopologyScope,
    40  			podTopologyHints: podTopologyHints{},
    41  			policy:           policy,
    42  			podMap:           containermap.NewContainerMap(),
    43  		},
    44  	}
    45  }
    46  
    47  func (s *containerScope) Admit(pod *v1.Pod) lifecycle.PodAdmitResult {
    48  	for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
    49  		bestHint, admit := s.calculateAffinity(pod, &container)
    50  		klog.InfoS("Best TopologyHint", "bestHint", bestHint, "pod", klog.KObj(pod), "containerName", container.Name)
    51  
    52  		if !admit {
    53  			metrics.TopologyManagerAdmissionErrorsTotal.Inc()
    54  			return admission.GetPodAdmitResult(&TopologyAffinityError{})
    55  		}
    56  		klog.InfoS("Topology Affinity", "bestHint", bestHint, "pod", klog.KObj(pod), "containerName", container.Name)
    57  		s.setTopologyHints(string(pod.UID), container.Name, bestHint)
    58  
    59  		err := s.allocateAlignedResources(pod, &container)
    60  		if err != nil {
    61  			metrics.TopologyManagerAdmissionErrorsTotal.Inc()
    62  			return admission.GetPodAdmitResult(err)
    63  		}
    64  	}
    65  	return admission.GetPodAdmitResult(nil)
    66  }
    67  
    68  func (s *containerScope) accumulateProvidersHints(pod *v1.Pod, container *v1.Container) []map[string][]TopologyHint {
    69  	var providersHints []map[string][]TopologyHint
    70  
    71  	for _, provider := range s.hintProviders {
    72  		// Get the TopologyHints for a Container from a provider.
    73  		hints := provider.GetTopologyHints(pod, container)
    74  		providersHints = append(providersHints, hints)
    75  		klog.InfoS("TopologyHints", "hints", hints, "pod", klog.KObj(pod), "containerName", container.Name)
    76  	}
    77  	return providersHints
    78  }
    79  
    80  func (s *containerScope) calculateAffinity(pod *v1.Pod, container *v1.Container) (TopologyHint, bool) {
    81  	providersHints := s.accumulateProvidersHints(pod, container)
    82  	bestHint, admit := s.policy.Merge(providersHints)
    83  	klog.InfoS("ContainerTopologyHint", "bestHint", bestHint)
    84  	return bestHint, admit
    85  }
    86  

View as plain text