Skip to content

Commit d5a34a9

Browse files
authored
Merge pull request #84 from linki/pod-phase
Only consider running pods as possible victims
2 parents bdaff21 + 47be61b commit d5a34a9

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

chaoskube/chaoskube.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,8 @@ func (c *Chaoskube) Candidates() ([]v1.Pod, error) {
151151
return nil, err
152152
}
153153

154-
pods, err = filterByAnnotations(pods, c.Annotations)
155-
if err != nil {
156-
return nil, err
157-
}
154+
pods = filterByAnnotations(pods, c.Annotations)
155+
pods = filterByPhase(pods, v1.PodRunning)
158156

159157
return pods, nil
160158
}
@@ -231,10 +229,10 @@ func filterByNamespaces(pods []v1.Pod, namespaces labels.Selector) ([]v1.Pod, er
231229
}
232230

233231
// filterByAnnotations filters a list of pods by a given annotation selector.
234-
func filterByAnnotations(pods []v1.Pod, annotations labels.Selector) ([]v1.Pod, error) {
232+
func filterByAnnotations(pods []v1.Pod, annotations labels.Selector) []v1.Pod {
235233
// empty filter returns original list
236234
if annotations.Empty() {
237-
return pods, nil
235+
return pods
238236
}
239237

240238
filteredList := []v1.Pod{}
@@ -249,5 +247,18 @@ func filterByAnnotations(pods []v1.Pod, annotations labels.Selector) ([]v1.Pod,
249247
}
250248
}
251249

252-
return filteredList, nil
250+
return filteredList
251+
}
252+
253+
// filterByPhase filters a list of pods by a given PodPhase, e.g. Running.
254+
func filterByPhase(pods []v1.Pod, phase v1.PodPhase) []v1.Pod {
255+
filteredList := []v1.Pod{}
256+
257+
for _, pod := range pods {
258+
if pod.Status.Phase == phase {
259+
filteredList = append(filteredList, pod)
260+
}
261+
}
262+
263+
return filteredList
253264
}

chaoskube/chaoskube_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (suite *Suite) TestDeletePod() {
187187
tt.dryRun,
188188
)
189189

190-
victim := util.NewPod("default", "foo")
190+
victim := util.NewPod("default", "foo", v1.PodRunning)
191191

192192
err := chaoskube.DeletePod(victim)
193193
suite.Require().NoError(err)
@@ -499,8 +499,9 @@ func (suite *Suite) setupWithPods(labelSelector labels.Selector, annotations lab
499499
)
500500

501501
pods := []v1.Pod{
502-
util.NewPod("default", "foo"),
503-
util.NewPod("testing", "bar"),
502+
util.NewPod("default", "foo", v1.PodRunning),
503+
util.NewPod("testing", "bar", v1.PodRunning),
504+
util.NewPod("testing", "baz", v1.PodPending), // Non-running pods are ignored
504505
}
505506

506507
for _, pod := range pods {

util/util.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TimeOfDay(pointInTime time.Time) time.Time {
125125
}
126126

127127
// NewPod returns a new pod instance for testing purposes.
128-
func NewPod(namespace, name string) v1.Pod {
128+
func NewPod(namespace, name string, phase v1.PodPhase) v1.Pod {
129129
return v1.Pod{
130130
ObjectMeta: metav1.ObjectMeta{
131131
Namespace: namespace,
@@ -137,5 +137,8 @@ func NewPod(namespace, name string) v1.Pod {
137137
"chaos": name,
138138
},
139139
},
140+
Status: v1.PodStatus{
141+
Phase: phase,
142+
},
140143
}
141144
}

0 commit comments

Comments
 (0)