2020-12-01 01:06:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package topologymanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
"k8s.io/klog/v2"
|
2021-06-18 20:46:09 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/cm/containermap"
|
2020-12-01 01:06:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// containerTopologyScope specifies the TopologyManagerScope per container.
|
|
|
|
containerTopologyScope = "container"
|
|
|
|
// podTopologyScope specifies the TopologyManagerScope per pod.
|
|
|
|
podTopologyScope = "pod"
|
|
|
|
)
|
|
|
|
|
|
|
|
type podTopologyHints map[string]map[string]TopologyHint
|
|
|
|
|
|
|
|
// Scope interface for Topology Manager
|
|
|
|
type Scope interface {
|
|
|
|
Name() string
|
|
|
|
Admit(pod *v1.Pod) lifecycle.PodAdmitResult
|
|
|
|
// AddHintProvider adds a hint provider to manager to indicate the hint provider
|
|
|
|
// wants to be consoluted with when making topology hints
|
|
|
|
AddHintProvider(h HintProvider)
|
|
|
|
// AddContainer adds pod to Manager for tracking
|
2021-06-18 20:46:09 +00:00
|
|
|
AddContainer(pod *v1.Pod, container *v1.Container, containerID string)
|
2020-12-01 01:06:26 +00:00
|
|
|
// RemoveContainer removes pod from Manager tracking
|
|
|
|
RemoveContainer(containerID string) error
|
|
|
|
// Store is the interface for storing pod topology hints
|
|
|
|
Store
|
|
|
|
}
|
|
|
|
|
|
|
|
type scope struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
name string
|
|
|
|
// Mapping of a Pods mapping of Containers and their TopologyHints
|
|
|
|
// Indexed by PodUID to ContainerName
|
|
|
|
podTopologyHints podTopologyHints
|
|
|
|
// The list of components registered with the Manager
|
|
|
|
hintProviders []HintProvider
|
|
|
|
// Topology Manager Policy
|
|
|
|
policy Policy
|
2021-06-18 20:46:09 +00:00
|
|
|
// Mapping of (PodUid, ContainerName) to ContainerID for Adding/Removing Pods from PodTopologyHints mapping
|
|
|
|
podMap containermap.ContainerMap
|
2020-12-01 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scope) Name() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
2021-02-22 20:08:19 +00:00
|
|
|
func (s *scope) getTopologyHints(podUID string, containerName string) TopologyHint {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2020-12-01 01:06:26 +00:00
|
|
|
return s.podTopologyHints[podUID][containerName]
|
|
|
|
}
|
|
|
|
|
2021-02-22 20:08:19 +00:00
|
|
|
func (s *scope) setTopologyHints(podUID string, containerName string, th TopologyHint) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
if s.podTopologyHints[podUID] == nil {
|
|
|
|
s.podTopologyHints[podUID] = make(map[string]TopologyHint)
|
|
|
|
}
|
|
|
|
s.podTopologyHints[podUID][containerName] = th
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scope) GetAffinity(podUID string, containerName string) TopologyHint {
|
|
|
|
return s.getTopologyHints(podUID, containerName)
|
|
|
|
}
|
|
|
|
|
2020-12-01 01:06:26 +00:00
|
|
|
func (s *scope) AddHintProvider(h HintProvider) {
|
|
|
|
s.hintProviders = append(s.hintProviders, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It would be better to implement this function in topologymanager instead of scope
|
|
|
|
// but topologymanager do not track mapping anymore
|
2021-06-18 20:46:09 +00:00
|
|
|
func (s *scope) AddContainer(pod *v1.Pod, container *v1.Container, containerID string) {
|
2020-12-01 01:06:26 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
s.podMap.Add(string(pod.UID), container.Name, containerID)
|
2020-12-01 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// It would be better to implement this function in topologymanager instead of scope
|
|
|
|
// but topologymanager do not track mapping anymore
|
|
|
|
func (s *scope) RemoveContainer(containerID string) error {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
klog.InfoS("RemoveContainer", "containerID", containerID)
|
2021-06-18 20:46:09 +00:00
|
|
|
// Get the podUID and containerName associated with the containerID to be removed and remove it
|
|
|
|
podUIDString, containerName, err := s.podMap.GetContainerRef(containerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.podMap.RemoveByContainerID(containerID)
|
|
|
|
|
|
|
|
// In cases where a container has been restarted, it's possible that the same podUID and
|
|
|
|
// containerName are already associated with a *different* containerID now. Only remove
|
|
|
|
// the TopologyHints associated with that podUID and containerName if this is not true
|
|
|
|
if _, err := s.podMap.GetContainerID(podUIDString, containerName); err != nil {
|
|
|
|
delete(s.podTopologyHints[podUIDString], containerName)
|
2020-12-01 01:06:26 +00:00
|
|
|
if len(s.podTopologyHints[podUIDString]) == 0 {
|
|
|
|
delete(s.podTopologyHints, podUIDString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scope) admitPolicyNone(pod *v1.Pod) lifecycle.PodAdmitResult {
|
|
|
|
for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
|
|
|
|
err := s.allocateAlignedResources(pod, &container)
|
|
|
|
if err != nil {
|
|
|
|
return unexpectedAdmissionError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return admitPod()
|
|
|
|
}
|
|
|
|
|
|
|
|
// It would be better to implement this function in topologymanager instead of scope
|
|
|
|
// but topologymanager do not track providers anymore
|
|
|
|
func (s *scope) allocateAlignedResources(pod *v1.Pod, container *v1.Container) error {
|
|
|
|
for _, provider := range s.hintProviders {
|
|
|
|
err := provider.Allocate(pod, container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func topologyAffinityError() lifecycle.PodAdmitResult {
|
|
|
|
return lifecycle.PodAdmitResult{
|
|
|
|
Message: "Resources cannot be allocated with Topology locality",
|
|
|
|
Reason: "TopologyAffinityError",
|
|
|
|
Admit: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unexpectedAdmissionError(err error) lifecycle.PodAdmitResult {
|
|
|
|
return lifecycle.PodAdmitResult{
|
|
|
|
Message: fmt.Sprintf("Allocate failed due to %v, which is unexpected", err),
|
|
|
|
Reason: "UnexpectedAdmissionError",
|
|
|
|
Admit: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func admitPod() lifecycle.PodAdmitResult {
|
|
|
|
return lifecycle.PodAdmitResult{Admit: true}
|
|
|
|
}
|