forked from openkruise/kruise
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainerrecreate_util.go
112 lines (96 loc) · 3.71 KB
/
containerrecreate_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 2021 The Kruise 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 (
"time"
"github.com/onsi/gomega"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
kruiseclientset "github.com/openkruise/kruise/pkg/client/clientset/versioned"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
)
type ContainerRecreateTester struct {
c clientset.Interface
kc kruiseclientset.Interface
ns string
}
func NewContainerRecreateTester(c clientset.Interface, kc kruiseclientset.Interface, ns string) *ContainerRecreateTester {
return &ContainerRecreateTester{
c: c,
kc: kc,
ns: ns,
}
}
func (t *ContainerRecreateTester) CreateTestCloneSetAndGetPods(randStr string, replicas int32, containers []v1.Container) (pods []*v1.Pod) {
set := &appsv1alpha1.CloneSet{
ObjectMeta: metav1.ObjectMeta{Namespace: t.ns, Name: "clone-foo-" + randStr},
Spec: appsv1alpha1.CloneSetSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"rand": randStr}},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"rand": randStr},
},
Spec: v1.PodSpec{
Affinity: &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: v1.PodAffinityTerm{TopologyKey: v1.LabelHostname, LabelSelector: &metav1.LabelSelector{MatchLabels: map[string]string{"rand": randStr}}},
},
},
},
},
Containers: containers,
},
},
},
}
var err error
if _, err = t.kc.AppsV1alpha1().CloneSets(t.ns).Create(set); err != nil {
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
// Wait for 60s
gomega.Eventually(func() int32 {
set, err = t.kc.AppsV1alpha1().CloneSets(t.ns).Get(set.Name, metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return set.Status.ReadyReplicas
}, 60*time.Second, 3*time.Second).Should(gomega.Equal(replicas))
podList, err := t.c.CoreV1().Pods(t.ns).List(metav1.ListOptions{LabelSelector: "rand=" + randStr})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
for i := range podList.Items {
p := &podList.Items[i]
pods = append(pods, p)
}
return
}
func (t *ContainerRecreateTester) CleanAllTestResources() error {
if err := t.kc.AppsV1alpha1().ContainerRecreateRequests(t.ns).DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return err
}
if err := t.kc.AppsV1alpha1().CloneSets(t.ns).DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil {
return err
}
return nil
}
func (t *ContainerRecreateTester) CreateCRR(crr *appsv1alpha1.ContainerRecreateRequest) (*appsv1alpha1.ContainerRecreateRequest, error) {
return t.kc.AppsV1alpha1().ContainerRecreateRequests(crr.Namespace).Create(crr)
}
func (t *ContainerRecreateTester) GetCRR(name string) (*appsv1alpha1.ContainerRecreateRequest, error) {
return t.kc.AppsV1alpha1().ContainerRecreateRequests(t.ns).Get(name, metav1.GetOptions{})
}
func (t *ContainerRecreateTester) GetPod(name string) (*v1.Pod, error) {
return t.c.CoreV1().Pods(t.ns).Get(name, metav1.GetOptions{})
}