forked from kubernetes/perf-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
112 lines (99 loc) · 3.31 KB
/
util.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
/*
Copyright 2017 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 framework
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/test/e2e/framework"
)
// CreateNSIfNotExists creates a namespace if it is new, otherwise it will return the existing namespace pointer
func CreateNSIfNotExists(f *framework.Framework, namespaceName string) (*v1.Namespace, error) {
var ns *v1.Namespace
var err error
fullNamespace := getNamespace(f, namespaceName)
if fullNamespace == "" {
ns, err = f.CreateNamespace(namespaceName, nil)
if err != nil {
return nil, err
}
framework.Logf("Created new namespace: %s", namespaceName)
} else {
ns, err = f.ClientSet.CoreV1().Namespaces().Get(fullNamespace, metav1.GetOptions{})
if err != nil {
return nil, err
}
framework.Logf("Namespace exists %s ", namespaceName)
}
return ns, err
}
// getNamespace takes the basename from the config and returns the full generated namespace name
func getNamespace(f *framework.Framework, baseName string) string {
existingNamespaces, _ := f.ClientSet.Core().Namespaces().List(metav1.ListOptions{})
for _, value := range existingNamespaces.Items {
if value.GenerateName == fmt.Sprintf("e2e-tests-%v-", baseName) {
return value.Name
}
}
return ""
}
// ParseConfig unmarshalls the json file defined in the CL config into a struct
func (cl *ClusterLoaderObject) ParseConfig() (*v1.Pod, error) {
pod := &v1.Pod{}
// If the file is defined used that as the config
if cl.File != "" {
configFile, err := ioutil.ReadFile(MakePath(cl.File))
if err != nil {
return pod, err
}
if err = json.Unmarshal(configFile, &pod); err != nil {
return pod, err
}
} else if cl.Image != "" && cl.Basename != "" {
// Otherwise if we have the image name use that instead
zero := int64(0)
pod.Spec = v1.PodSpec{TerminationGracePeriodSeconds: &zero, Containers: []v1.Container{
{
Name: cl.Basename,
Image: cl.Image,
},
},
}
} else {
return pod, errors.New("Missing both config file and imagename")
}
return pod, nil
}
// MakePath returns fully qualified file location as a string
func MakePath(file string) string {
// Handle an empty filename.
if file == "" {
framework.Failf("No template file defined!")
}
// TODO: We should enable passing this as a flag instead of hardcoding.
return filepath.Join(os.Getenv("GOPATH"), "src/k8s.io/perf-tests/clusterloader/content/", file)
}
// ConvertToLabelSet will convert the string label to a set, while also setting a default value
func (cl *ClusterLoaderObject) ConvertToLabelSet() (labels.Set, error) {
if cl.Label == "" {
cl.Label = "purpose=test"
}
label, err := labels.ConvertSelectorToLabelsMap(cl.Label)
return label, err
}