|
| 1 | +/* |
| 2 | +Copyright 2022 The Koordinator 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 statesinformer |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + |
| 23 | + "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha1" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/client-go/util/retry" |
| 28 | + "k8s.io/klog/v2" |
| 29 | + "k8s.io/kubernetes/pkg/kubelet/cm/cpuset" |
| 30 | + |
| 31 | + "github.com/koordinator-sh/koordinator/apis/extension" |
| 32 | + "github.com/koordinator-sh/koordinator/pkg/koordlet/metriccache" |
| 33 | +) |
| 34 | + |
| 35 | +func (s *statesInformer) syncNodeResourceTopology(node *corev1.Node) { |
| 36 | + topologyName := node.Name |
| 37 | + ctx := context.TODO() |
| 38 | + blocker := true |
| 39 | + _, err := s.topologyClient.TopologyV1alpha1().NodeResourceTopologies().Get(ctx, topologyName, metav1.GetOptions{ResourceVersion: "0"}) |
| 40 | + if err == nil { |
| 41 | + return |
| 42 | + } |
| 43 | + if !errors.IsNotFound(err) { |
| 44 | + klog.Errorf("failed to get NodeResourceTopology %s, err: %v", topologyName, err) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + topology := &v1alpha1.NodeResourceTopology{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: topologyName, |
| 51 | + Labels: map[string]string{ |
| 52 | + extension.LabelManagedBy: "Koordinator", |
| 53 | + }, |
| 54 | + OwnerReferences: []metav1.OwnerReference{ |
| 55 | + { |
| 56 | + APIVersion: "v1", |
| 57 | + Kind: "Node", |
| 58 | + Name: node.Name, |
| 59 | + UID: node.GetUID(), |
| 60 | + Controller: &blocker, |
| 61 | + BlockOwnerDeletion: &blocker, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + // fields are required |
| 66 | + TopologyPolicies: []string{string(v1alpha1.None)}, |
| 67 | + Zones: v1alpha1.ZoneList{v1alpha1.Zone{Name: "fake-name", Type: "fake-type"}}, |
| 68 | + } |
| 69 | + //TODO: add retry if create fail |
| 70 | + _, err = s.topologyClient.TopologyV1alpha1().NodeResourceTopologies().Create(ctx, topology, metav1.CreateOptions{}) |
| 71 | + if err != nil { |
| 72 | + klog.Errorf("failed to create NodeResourceTopology %s, err: %v", topologyName, err) |
| 73 | + return |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (s *statesInformer) reportNodeTopology() { |
| 78 | + s.nodeRWMutex.RLock() |
| 79 | + nodeName := s.node.Name |
| 80 | + s.nodeRWMutex.RUnlock() |
| 81 | + ctx := context.TODO() |
| 82 | + |
| 83 | + cpuTopology, usedCPUs, err := s.calCpuTopology() |
| 84 | + if err != nil { |
| 85 | + return |
| 86 | + } |
| 87 | + sharePools := s.calCPUSharePools(usedCPUs) |
| 88 | + |
| 89 | + cpuTopologyJson, err := json.Marshal(cpuTopology) |
| 90 | + if err != nil { |
| 91 | + klog.Errorf("failed to marshal cpu topology of node %s, err: %v", nodeName, err) |
| 92 | + return |
| 93 | + } |
| 94 | + cpuSharePoolsJson, err := json.Marshal(sharePools) |
| 95 | + if err != nil { |
| 96 | + klog.Errorf("failed to marshal cpushare pools of node %s, err: %v", nodeName, err) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + err = retry.OnError(retry.DefaultBackoff, errors.IsTooManyRequests, func() error { |
| 101 | + topology, err := s.topologyClient.TopologyV1alpha1().NodeResourceTopologies().Get(ctx, nodeName, metav1.GetOptions{ResourceVersion: "0"}) |
| 102 | + if err != nil { |
| 103 | + klog.Errorf("failed to get node resource topology %s, err: %v", nodeName, err) |
| 104 | + return err |
| 105 | + } |
| 106 | + if topology.Annotations == nil { |
| 107 | + topology.Annotations = make(map[string]string) |
| 108 | + } |
| 109 | + if topology.Annotations[extension.AnnotationNodeCPUTopology] == string(cpuTopologyJson) && topology.Annotations[extension.AnnotationNodeCPUSharedPools] == string(cpuSharePoolsJson) { |
| 110 | + return nil |
| 111 | + } |
| 112 | + topology.Annotations[extension.AnnotationNodeCPUTopology] = string(cpuTopologyJson) |
| 113 | + topology.Annotations[extension.AnnotationNodeCPUSharedPools] = string(cpuSharePoolsJson) |
| 114 | + _, err = s.topologyClient.TopologyV1alpha1().NodeResourceTopologies().Update(context.TODO(), topology, metav1.UpdateOptions{}) |
| 115 | + if err != nil { |
| 116 | + klog.Errorf("failed to update cpu info of node %s, err: %v", nodeName, err) |
| 117 | + return err |
| 118 | + } |
| 119 | + return nil |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + klog.Errorf("failed to update NodeResourceTopology, err: %v", err) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (s *statesInformer) calCPUSharePools(usedCPUs map[int32]*extension.CPUInfo) []extension.CPUSharedPool { |
| 127 | + podMetas := s.GetAllPods() |
| 128 | + for _, podMeta := range podMetas { |
| 129 | + status, err := extension.GetResourceStatus(podMeta.Pod.Annotations) |
| 130 | + if err != nil { |
| 131 | + klog.Errorf("failed to get resource status of pod %s, err: %v", podMeta.Pod.Name, err) |
| 132 | + continue |
| 133 | + } |
| 134 | + if status.CPUSet == "" { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + set, err := cpuset.Parse(status.CPUSet) |
| 139 | + if err != nil { |
| 140 | + klog.Errorf("failed to parse cpuset info of pod %s, err: %v", podMeta.Pod.Name, err) |
| 141 | + continue |
| 142 | + } |
| 143 | + for _, cpuID := range set.ToSliceNoSort() { |
| 144 | + delete(usedCPUs, int32(cpuID)) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // nodeID -> cpulist |
| 149 | + nodeIDToCpus := make(map[int32][]int) |
| 150 | + for cpuID, info := range usedCPUs { |
| 151 | + if info != nil { |
| 152 | + nodeIDToCpus[info.Node] = append(nodeIDToCpus[info.Node], int(cpuID)) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + sharePools := []extension.CPUSharedPool{} |
| 157 | + for nodeID, cpus := range nodeIDToCpus { |
| 158 | + if len(cpus) <= 0 { |
| 159 | + continue |
| 160 | + } |
| 161 | + set := cpuset.NewCPUSet(cpus...) |
| 162 | + sharePools = append(sharePools, extension.CPUSharedPool{ |
| 163 | + CPUSet: set.String(), |
| 164 | + Node: nodeID, |
| 165 | + Socket: usedCPUs[int32(cpus[0])].Socket, |
| 166 | + }) |
| 167 | + } |
| 168 | + return sharePools |
| 169 | +} |
| 170 | + |
| 171 | +func (s *statesInformer) calCpuTopology() (*extension.CPUTopology, map[int32]*extension.CPUInfo, error) { |
| 172 | + nodeCpuInfo, err := s.metricsCache.GetNodeCPUInfo(&metriccache.QueryParam{}) |
| 173 | + if err != nil { |
| 174 | + klog.Errorf("failed to get node cpu info") |
| 175 | + return nil, nil, err |
| 176 | + } |
| 177 | + |
| 178 | + usedCPUs := make(map[int32]*extension.CPUInfo) |
| 179 | + cpuTopology := &extension.CPUTopology{} |
| 180 | + for _, cpu := range nodeCpuInfo.ProcessorInfos { |
| 181 | + info := extension.CPUInfo{ |
| 182 | + ID: cpu.CPUID, |
| 183 | + Core: cpu.CoreID, |
| 184 | + Socket: cpu.SocketID, |
| 185 | + Node: cpu.NodeID, |
| 186 | + } |
| 187 | + cpuTopology.Detail = append(cpuTopology.Detail, info) |
| 188 | + usedCPUs[cpu.CPUID] = &info |
| 189 | + } |
| 190 | + return cpuTopology, usedCPUs, nil |
| 191 | +} |
0 commit comments