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

Ensure a random pod is picked within an owner reference group #203

Merged
merged 1 commit into from
May 2, 2020
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
14 changes: 8 additions & 6 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func filterByPodName(pods []v1.Pod, includedPodNames, excludedPodNames *regexp.R
}

func filterByOwnerReference(pods []v1.Pod) []v1.Pod {
owners := make(map[types.UID]struct{})
owners := make(map[types.UID][]v1.Pod)
filteredList := []v1.Pod{}
for _, pod := range pods {
// Don't filter out pods with no owner reference
Expand All @@ -453,14 +453,16 @@ func filterByOwnerReference(pods []v1.Pod) []v1.Pod {
continue
}

// Group remaining pods by their owner reference
for _, ref := range pod.GetOwnerReferences() {
_, found := owners[ref.UID]
if !found {
filteredList = append(filteredList, pod)
owners[ref.UID] = struct{}{}
}
owners[ref.UID] = append(owners[ref.UID], pod)
}
}

// For each owner reference select a random pod from its group
for _, pods := range owners {
filteredList = append(filteredList, util.RandomPodSubSlice(pods, 1)...)
}

return filteredList
}
27 changes: 21 additions & 6 deletions chaoskube/chaoskube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,32 +949,47 @@ func (suite *Suite) TestFilterByOwnerReference() {
baz1 := util.NewPod("default", "baz-1", v1.PodRunning)

for _, tt := range []struct {
seed int64
name string
pods []v1.Pod
expected []v1.Pod
}{
{
name: "2 pods, same parent",
seed: 1000,
name: "2 pods, same parent, pick first",
pods: []v1.Pod{foo, foo1},
expected: []v1.Pod{foo},
},
{
name: "2 pods, different parents",
seed: 2000,
name: "2 pods, same parent, pick second",
pods: []v1.Pod{foo, foo1},
expected: []v1.Pod{foo1},
},
{
seed: 1000,
name: "2 pods, different parents, pick both",
pods: []v1.Pod{foo, bar},
expected: []v1.Pod{foo, bar},
},
{
name: "2 pods, one with/without parent",
pods: []v1.Pod{foo, baz},
expected: []v1.Pod{foo, baz},
seed: 1000,
name: "2 pods, one without and one with parent, pick both",
pods: []v1.Pod{baz, foo},
expected: []v1.Pod{baz, foo},
},
{
name: "2 pods, no parents",
seed: 1000,
name: "2 pods, no parents, pick both",
pods: []v1.Pod{baz, baz1},
expected: []v1.Pod{baz, baz1},
},
} {
rand.Seed(tt.seed)

results := filterByOwnerReference(tt.pods)
suite.Require().Len(results, len(tt.expected))

for i, result := range results {
suite.Assert().Equal(tt.expected[i], result, tt.name)
}
Expand Down