/
pod_kill.go
172 lines (154 loc) · 5.55 KB
/
pod_kill.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
168
169
170
171
172
// Copyright 2021 Chaos Mesh 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 podchaos
import (
"context"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/util"
"github.com/chaos-mesh/chaos-mesh/e2e-test/pkg/fixture"
)
func TestcasePodKillOnceThenDelete(ns string, kubeCli kubernetes.Interface, cli client.Client) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pod := fixture.NewCommonNginxPod("nginx", ns)
_, err := kubeCli.CoreV1().Pods(ns).Create(context.TODO(), pod, metav1.CreateOptions{})
framework.ExpectNoError(err, "create nginx pod error")
err = waitPodRunning("nginx", ns, kubeCli)
framework.ExpectNoError(err, "wait nginx running error")
podKillChaos := &v1alpha1.PodChaos{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx-kill",
Namespace: ns,
},
Spec: v1alpha1.PodChaosSpec{
Action: v1alpha1.PodKillAction,
ContainerSelector: v1alpha1.ContainerSelector{
PodSelector: v1alpha1.PodSelector{
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{
ns,
},
LabelSelectors: map[string]string{
"app": "nginx",
},
},
},
Mode: v1alpha1.OneMode,
},
},
},
}
err = cli.Create(ctx, podKillChaos)
framework.ExpectNoError(err, "create pod chaos error")
err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
_, err = kubeCli.CoreV1().Pods(ns).Get(context.TODO(), "nginx", metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
return true, nil
}
return false, nil
})
framework.ExpectNoError(err, "Pod kill chaos perform failed")
}
func TestcasePodKillPauseThenUnPause(ns string, kubeCli kubernetes.Interface, cli client.Client) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nd := fixture.NewCommonNginxDeployment("nginx", ns, 3)
_, err := kubeCli.AppsV1().Deployments(ns).Create(context.TODO(), nd, metav1.CreateOptions{})
framework.ExpectNoError(err, "create nginx deployment error")
err = util.WaitDeploymentReady("nginx", ns, kubeCli)
framework.ExpectNoError(err, "wait nginx deployment ready error")
var pods *corev1.PodList
var newPods *corev1.PodList
listOption := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
"app": "nginx",
}).String(),
}
pods, err = kubeCli.CoreV1().Pods(ns).List(context.TODO(), listOption)
framework.ExpectNoError(err, "get nginx pods error")
podKillChaos := &v1alpha1.PodChaos{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx-kill",
Namespace: ns,
},
Spec: v1alpha1.PodChaosSpec{
Action: v1alpha1.PodKillAction,
Duration: pointer.StringPtr("9m"),
ContainerSelector: v1alpha1.ContainerSelector{
PodSelector: v1alpha1.PodSelector{
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{
ns,
},
LabelSelectors: map[string]string{
"app": "nginx",
},
},
},
Mode: v1alpha1.OneMode,
},
},
},
}
err = cli.Create(ctx, podKillChaos)
framework.ExpectNoError(err, "create pod chaos error")
chaosKey := types.NamespacedName{
Namespace: ns,
Name: "nginx-kill",
}
// some pod is killed as expected
err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
newPods, err = kubeCli.CoreV1().Pods(ns).List(context.TODO(), listOption)
framework.ExpectNoError(err, "get nginx pods error")
return !fixture.HaveSameUIDs(pods.Items, newPods.Items), nil
})
framework.ExpectNoError(err, "wait pod killed failed")
// pause experiment
err = util.PauseChaos(ctx, cli, podKillChaos)
framework.ExpectNoError(err, "pause chaos error")
err = wait.Poll(1*time.Second, 5*time.Second, func() (done bool, err error) {
chaos := &v1alpha1.PodChaos{}
err = cli.Get(ctx, chaosKey, chaos)
framework.ExpectNoError(err, "get pod chaos error")
if chaos.Status.Experiment.DesiredPhase == v1alpha1.StoppedPhase {
return true, nil
}
return false, err
})
framework.ExpectError(err, "chaos shouldn't enter stopped phase")
// wait for 1 minutes and no pod is killed
pods, err = kubeCli.CoreV1().Pods(ns).List(context.TODO(), listOption)
framework.ExpectNoError(err, "get nginx pods error")
err = wait.Poll(5*time.Second, 1*time.Minute, func() (done bool, err error) {
newPods, err = kubeCli.CoreV1().Pods(ns).List(context.TODO(), listOption)
framework.ExpectNoError(err, "get nginx pods error")
return !fixture.HaveSameUIDs(pods.Items, newPods.Items), nil
})
framework.ExpectError(err, "wait pod not killed failed")
framework.ExpectEqual(err.Error(), wait.ErrWaitTimeout.Error())
}