...
1
16
17 package topologymanager
18
19 import (
20 "sync"
21
22 "k8s.io/api/core/v1"
23 "k8s.io/klog/v2"
24 "k8s.io/kubernetes/pkg/kubelet/cm/admission"
25 "k8s.io/kubernetes/pkg/kubelet/cm/containermap"
26 "k8s.io/kubernetes/pkg/kubelet/lifecycle"
27 )
28
29 const (
30
31 containerTopologyScope = "container"
32
33 podTopologyScope = "pod"
34
35 noneTopologyScope = "none"
36 )
37
38 type podTopologyHints map[string]map[string]TopologyHint
39
40
41 type Scope interface {
42 Name() string
43 GetPolicy() Policy
44 Admit(pod *v1.Pod) lifecycle.PodAdmitResult
45
46
47 AddHintProvider(h HintProvider)
48
49 AddContainer(pod *v1.Pod, container *v1.Container, containerID string)
50
51 RemoveContainer(containerID string) error
52
53 Store
54 }
55
56 type scope struct {
57 mutex sync.Mutex
58 name string
59
60
61 podTopologyHints podTopologyHints
62
63 hintProviders []HintProvider
64
65 policy Policy
66
67 podMap containermap.ContainerMap
68 }
69
70 func (s *scope) Name() string {
71 return s.name
72 }
73
74 func (s *scope) getTopologyHints(podUID string, containerName string) TopologyHint {
75 s.mutex.Lock()
76 defer s.mutex.Unlock()
77 return s.podTopologyHints[podUID][containerName]
78 }
79
80 func (s *scope) setTopologyHints(podUID string, containerName string, th TopologyHint) {
81 s.mutex.Lock()
82 defer s.mutex.Unlock()
83
84 if s.podTopologyHints[podUID] == nil {
85 s.podTopologyHints[podUID] = make(map[string]TopologyHint)
86 }
87 s.podTopologyHints[podUID][containerName] = th
88 }
89
90 func (s *scope) GetAffinity(podUID string, containerName string) TopologyHint {
91 return s.getTopologyHints(podUID, containerName)
92 }
93
94 func (s *scope) GetPolicy() Policy {
95 return s.policy
96 }
97
98 func (s *scope) AddHintProvider(h HintProvider) {
99 s.hintProviders = append(s.hintProviders, h)
100 }
101
102
103
104 func (s *scope) AddContainer(pod *v1.Pod, container *v1.Container, containerID string) {
105 s.mutex.Lock()
106 defer s.mutex.Unlock()
107
108 s.podMap.Add(string(pod.UID), container.Name, containerID)
109 }
110
111
112
113 func (s *scope) RemoveContainer(containerID string) error {
114 s.mutex.Lock()
115 defer s.mutex.Unlock()
116
117 klog.InfoS("RemoveContainer", "containerID", containerID)
118
119 podUIDString, containerName, err := s.podMap.GetContainerRef(containerID)
120 if err != nil {
121 return nil
122 }
123 s.podMap.RemoveByContainerID(containerID)
124
125
126
127
128 if _, err := s.podMap.GetContainerID(podUIDString, containerName); err != nil {
129 delete(s.podTopologyHints[podUIDString], containerName)
130 if len(s.podTopologyHints[podUIDString]) == 0 {
131 delete(s.podTopologyHints, podUIDString)
132 }
133 }
134
135 return nil
136 }
137
138 func (s *scope) admitPolicyNone(pod *v1.Pod) lifecycle.PodAdmitResult {
139 for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
140 err := s.allocateAlignedResources(pod, &container)
141 if err != nil {
142 return admission.GetPodAdmitResult(err)
143 }
144 }
145 return admission.GetPodAdmitResult(nil)
146 }
147
148
149
150 func (s *scope) allocateAlignedResources(pod *v1.Pod, container *v1.Container) error {
151 for _, provider := range s.hintProviders {
152 err := provider.Allocate(pod, container)
153 if err != nil {
154 return err
155 }
156 }
157 return nil
158 }
159
View as plain text