diff --git a/chaoskube/chaoskube.go b/chaoskube/chaoskube.go index 1f0803c7..82015388 100644 --- a/chaoskube/chaoskube.go +++ b/chaoskube/chaoskube.go @@ -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 @@ -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 } diff --git a/chaoskube/chaoskube_test.go b/chaoskube/chaoskube_test.go index 6fd20887..5f86c80f 100644 --- a/chaoskube/chaoskube_test.go +++ b/chaoskube/chaoskube_test.go @@ -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) }