...

Source file src/k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/scope_pod.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 podScope struct {
    29  	scope
    30  }
    31  
    32  // Ensure podScope implements Scope interface
    33  var _ Scope = &podScope{}
    34  
    35  // NewPodScope returns a pod scope.
    36  func NewPodScope(policy Policy) Scope {
    37  	return &podScope{
    38  		scope{
    39  			name:             podTopologyScope,
    40  			podTopologyHints: podTopologyHints{},
    41  			policy:           policy,
    42  			podMap:           containermap.NewContainerMap(),
    43  		},
    44  	}
    45  }
    46  
    47  func (s *podScope) Admit(pod *v1.Pod) lifecycle.PodAdmitResult {
    48  	bestHint, admit := s.calculateAffinity(pod)
    49  	klog.InfoS("Best TopologyHint", "bestHint", bestHint, "pod", klog.KObj(pod))
    50  	if !admit {
    51  		metrics.TopologyManagerAdmissionErrorsTotal.Inc()
    52  		return admission.GetPodAdmitResult(&TopologyAffinityError{})
    53  	}
    54  
    55  	for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
    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 *podScope) accumulateProvidersHints(pod *v1.Pod) []map[string][]TopologyHint {
    69  	var providersHints []map[string][]TopologyHint
    70  
    71  	for _, provider := range s.hintProviders {
    72  		// Get the TopologyHints for a Pod from a provider.
    73  		hints := provider.GetPodTopologyHints(pod)
    74  		providersHints = append(providersHints, hints)
    75  		klog.InfoS("TopologyHints", "hints", hints, "pod", klog.KObj(pod))
    76  	}
    77  	return providersHints
    78  }
    79  
    80  func (s *podScope) calculateAffinity(pod *v1.Pod) (TopologyHint, bool) {
    81  	providersHints := s.accumulateProvidersHints(pod)
    82  	bestHint, admit := s.policy.Merge(providersHints)
    83  	klog.InfoS("PodTopologyHint", "bestHint", bestHint)
    84  	return bestHint, admit
    85  }
    86  

View as plain text