forked from kubernetes-sigs/scheduler-plugins
/
utils.go
156 lines (138 loc) · 4.96 KB
/
utils.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
/*
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 util
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
st "k8s.io/kubernetes/pkg/scheduler/testing"
imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/muffin-rice/scheduler-plugins/pkg/apis/scheduling/v1alpha1"
)
func PrintPods(t *testing.T, cs clientset.Interface, ns string) {
podList, err := cs.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Errorf("Failed to list Pods: %v", err)
return
}
for _, pod := range podList.Items {
t.Logf("Pod %q scheduled to %q", pod.Name, pod.Spec.NodeName)
}
}
func MakeNodesAndPods(labels map[string]string, existingPodsNum, allNodesNum int) (existingPods []*corev1.Pod, allNodes []*corev1.Node) {
type keyVal struct {
k string
v string
}
var labelPairs []keyVal
for k, v := range labels {
labelPairs = append(labelPairs, keyVal{k: k, v: v})
}
// build nodes
for i := 0; i < allNodesNum; i++ {
res := map[corev1.ResourceName]string{
corev1.ResourceCPU: "1",
corev1.ResourcePods: "20",
}
node := st.MakeNode().Name(fmt.Sprintf("node%d", i)).Capacity(res)
allNodes = append(allNodes, &node.Node)
}
// build pods
for i := 0; i < existingPodsNum; i++ {
podWrapper := st.MakePod().Name(fmt.Sprintf("pod%d", i)).Node(fmt.Sprintf("node%d", i%allNodesNum))
// apply labels[0], labels[0,1], ..., labels[all] to each pod in turn
for _, p := range labelPairs[:i%len(labelPairs)+1] {
podWrapper = podWrapper.Label(p.k, p.v)
}
existingPods = append(existingPods, podWrapper.Obj())
}
return
}
func MakePG(name, namespace string, min int32, creationTime *time.Time, minResource *corev1.ResourceList) *v1alpha1.PodGroup {
var ti int32 = 10
pg := &v1alpha1.PodGroup{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: v1alpha1.PodGroupSpec{MinMember: min, ScheduleTimeoutSeconds: &ti},
}
if creationTime != nil {
pg.CreationTimestamp = metav1.Time{Time: *creationTime}
}
if minResource != nil {
pg.Spec.MinResources = minResource
}
return pg
}
func createKubeConfig(clientCfg *restclient.Config) *clientcmdapi.Config {
clusterNick := "cluster"
userNick := "user"
contextNick := "context"
configCmd := clientcmdapi.NewConfig()
credentials := clientcmdapi.NewAuthInfo()
credentials.Token = clientCfg.BearerToken
credentials.TokenFile = clientCfg.BearerTokenFile
credentials.ClientCertificate = clientCfg.TLSClientConfig.CertFile
if len(credentials.ClientCertificate) == 0 {
credentials.ClientCertificateData = clientCfg.TLSClientConfig.CertData
}
credentials.ClientKey = clientCfg.TLSClientConfig.KeyFile
if len(credentials.ClientKey) == 0 {
credentials.ClientKeyData = clientCfg.TLSClientConfig.KeyData
}
configCmd.AuthInfos[userNick] = credentials
cluster := clientcmdapi.NewCluster()
cluster.Server = clientCfg.Host
cluster.CertificateAuthority = clientCfg.CAFile
if len(cluster.CertificateAuthority) == 0 {
cluster.CertificateAuthorityData = clientCfg.CAData
}
cluster.InsecureSkipTLSVerify = clientCfg.Insecure
configCmd.Clusters[clusterNick] = cluster
context := clientcmdapi.NewContext()
context.Cluster = clusterNick
context.AuthInfo = userNick
configCmd.Contexts[contextNick] = context
configCmd.CurrentContext = contextNick
return configCmd
}
func BuildKubeConfigFile(config *restclient.Config) string {
kubeConfigPath := ""
if tempFile, err := ioutil.TempFile(os.TempDir(), "kubeconfig-"); err == nil {
kubeConfig := createKubeConfig(config)
clientcmd.WriteToFile(*kubeConfig, tempFile.Name())
kubeConfigPath = tempFile.Name()
return kubeConfigPath
}
return ""
}
func MakePod(podName string, namespace string, memReq int64, cpuReq int64, priority int32, uid string, nodeName string) *corev1.Pod {
pause := imageutils.GetPauseImageName()
pod := st.MakePod().Namespace(namespace).Name(podName).Container(pause).
Priority(priority).Node(nodeName).UID(uid).ZeroTerminationGracePeriod().Obj()
pod.Spec.Containers[0].Resources = corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: *resource.NewQuantity(memReq, resource.DecimalSI),
corev1.ResourceCPU: *resource.NewMilliQuantity(cpuReq, resource.DecimalSI),
},
}
return pod
}