forked from kubernetes-sigs/scheduler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginhelpers.go
167 lines (146 loc) · 5.23 KB
/
pluginhelpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Copyright 2021 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 noderesourcetopology
import (
"context"
"fmt"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
topologyv1alpha1 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha1"
topoclientset "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned"
topologyinformers "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/informers/externalversions"
listerv1alpha1 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/listers/topology/v1alpha1"
)
func findNodeTopology(nodeName string, nodeResTopoPlugin *nodeResTopologyPlugin) *topologyv1alpha1.NodeResourceTopology {
klog.V(5).Infof("namespaces: %s", nodeResTopoPlugin.namespaces)
for _, namespace := range nodeResTopoPlugin.namespaces {
klog.V(5).Infof("data.lister: %v", nodeResTopoPlugin.lister)
// NodeTopology couldn't be placed in several namespaces simultaneously
lister := nodeResTopoPlugin.lister
nodeTopology, err := (*lister).NodeResourceTopologies(namespace).Get(nodeName)
if err != nil {
klog.V(5).Infof("Cannot get NodeTopologies from NodeResourceTopologyNamespaceLister: %v", err)
continue
}
if nodeTopology != nil {
return nodeTopology
}
}
return nil
}
func initNodeTopologyInformer(masterOverride, kubeConfigPath *string) (*listerv1alpha1.NodeResourceTopologyLister, error) {
kubeConfig, err := clientcmd.BuildConfigFromFlags(*masterOverride, *kubeConfigPath)
if err != nil {
klog.Errorf("Cannot create kubeconfig based on: %s, %s, %v", *masterOverride, *kubeConfigPath, err)
return nil, err
}
topoClient, err := topoclientset.NewForConfig(kubeConfig)
if err != nil {
klog.Errorf("Cannot create clientset for NodeTopologyResource: %s, %s", kubeConfig, err)
return nil, err
}
topologyInformerFactory := topologyinformers.NewSharedInformerFactory(topoClient, 0)
nodeTopologyInformer := topologyInformerFactory.Topology().V1alpha1().NodeResourceTopologies()
nodeResourceTopologyLister := nodeTopologyInformer.Lister()
klog.V(5).Infof("start nodeTopologyInformer")
ctx := context.Background()
topologyInformerFactory.Start(ctx.Done())
topologyInformerFactory.WaitForCacheSync(ctx.Done())
return &nodeResourceTopologyLister, nil
}
func createNUMANodeList(zones topologyv1alpha1.ZoneList) NUMANodeList {
nodes := make(NUMANodeList, 0)
for _, zone := range zones {
if zone.Type == "Node" {
var numaID int
_, err := fmt.Sscanf(zone.Name, "node-%d", &numaID)
if err != nil {
klog.Errorf("Invalid format: %v", zone.Name)
continue
}
if numaID > 63 || numaID < 0 {
klog.Errorf("Invalid NUMA id range: %v", numaID)
continue
}
resources := extractResources(zone)
nodes = append(nodes, NUMANode{NUMAID: numaID, Resources: resources})
}
}
return nodes
}
func makePodByResourceList(resources *v1.ResourceList) *v1.Pod {
return &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: v1.ResourceRequirements{
Requests: *resources,
Limits: *resources,
},
},
},
},
}
}
func makeResourceListFromZones(zones topologyv1alpha1.ZoneList) v1.ResourceList {
result := make(v1.ResourceList)
for _, zone := range zones {
for _, resInfo := range zone.Resources {
resQuantity := resInfo.Available
if quantity, ok := result[v1.ResourceName(resInfo.Name)]; ok {
resQuantity.Add(quantity)
}
result[v1.ResourceName(resInfo.Name)] = resQuantity
}
}
return result
}
func MakeTopologyResInfo(name, capacity, available string) topologyv1alpha1.ResourceInfo {
return topologyv1alpha1.ResourceInfo{
Name: name,
Capacity: resource.MustParse(capacity),
Available: resource.MustParse(available),
}
}
func makePodByResourceListWithManyContainers(resources *v1.ResourceList, containerCount int) *v1.Pod {
var containers []v1.Container
for i := 0; i < containerCount; i++ {
containers = append(containers, v1.Container{
Resources: v1.ResourceRequirements{
Requests: *resources,
Limits: *resources,
},
})
}
return &v1.Pod{
Spec: v1.PodSpec{
Containers: containers,
},
}
}
func extractResources(zone topologyv1alpha1.Zone) v1.ResourceList {
res := make(v1.ResourceList)
for _, resInfo := range zone.Resources {
klog.V(5).Infof("extractResources: resInfo.FilterPluginName %v, resInfo quantity %d", resInfo.Name, resInfo.Available)
res[v1.ResourceName(resInfo.Name)] = resInfo.Available
}
return res
}
func newPolicyHandlerMap() PolicyHandlerMap {
return PolicyHandlerMap{
topologyv1alpha1.SingleNUMANodePodLevel: newPodScopedHandler(),
topologyv1alpha1.SingleNUMANodeContainerLevel: newContainerScopedHandler(),
}
}