Skip to content

Commit

Permalink
Merge pull request #84 from linki/pod-phase
Browse files Browse the repository at this point in the history
Only consider running pods as possible victims
  • Loading branch information
linki authored Jul 23, 2018
2 parents bdaff21 + 47be61b commit d5a34a9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
25 changes: 18 additions & 7 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ func (c *Chaoskube) Candidates() ([]v1.Pod, error) {
return nil, err
}

pods, err = filterByAnnotations(pods, c.Annotations)
if err != nil {
return nil, err
}
pods = filterByAnnotations(pods, c.Annotations)
pods = filterByPhase(pods, v1.PodRunning)

return pods, nil
}
Expand Down Expand Up @@ -231,10 +229,10 @@ func filterByNamespaces(pods []v1.Pod, namespaces labels.Selector) ([]v1.Pod, er
}

// filterByAnnotations filters a list of pods by a given annotation selector.
func filterByAnnotations(pods []v1.Pod, annotations labels.Selector) ([]v1.Pod, error) {
func filterByAnnotations(pods []v1.Pod, annotations labels.Selector) []v1.Pod {
// empty filter returns original list
if annotations.Empty() {
return pods, nil
return pods
}

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

return filteredList, nil
return filteredList
}

// filterByPhase filters a list of pods by a given PodPhase, e.g. Running.
func filterByPhase(pods []v1.Pod, phase v1.PodPhase) []v1.Pod {
filteredList := []v1.Pod{}

for _, pod := range pods {
if pod.Status.Phase == phase {
filteredList = append(filteredList, pod)
}
}

return filteredList
}
7 changes: 4 additions & 3 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (suite *Suite) TestDeletePod() {
tt.dryRun,
)

victim := util.NewPod("default", "foo")
victim := util.NewPod("default", "foo", v1.PodRunning)

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

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

for _, pod := range pods {
Expand Down
5 changes: 4 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TimeOfDay(pointInTime time.Time) time.Time {
}

// NewPod returns a new pod instance for testing purposes.
func NewPod(namespace, name string) v1.Pod {
func NewPod(namespace, name string, phase v1.PodPhase) v1.Pod {
return v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Expand All @@ -137,5 +137,8 @@ func NewPod(namespace, name string) v1.Pod {
"chaos": name,
},
},
Status: v1.PodStatus{
Phase: phase,
},
}
}

0 comments on commit d5a34a9

Please sign in to comment.