Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider running pods as possible victims #84

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
}
}