Skip to content

Commit

Permalink
fix ownerref selection
Browse files Browse the repository at this point in the history
  • Loading branch information
linki committed Apr 20, 2020
1 parent 089bb2c commit 9a892d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
24 changes: 0 additions & 24 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
Expand Down Expand Up @@ -228,7 +227,6 @@ func (c *Chaoskube) Candidates(ctx context.Context) ([]v1.Pod, error) {
pods = filterTerminatingPods(pods)
pods = filterByMinimumAge(pods, c.MinimumAge, c.Now())
pods = filterByPodName(pods, c.IncludedPodNames, c.ExcludedPodNames)
pods = filterByOwnerReference(pods)

return pods, nil
}
Expand Down Expand Up @@ -442,25 +440,3 @@ func filterByPodName(pods []v1.Pod, includedPodNames, excludedPodNames *regexp.R

return filteredList
}

func filterByOwnerReference(pods []v1.Pod) []v1.Pod {
owners := make(map[types.UID]struct{})
filteredList := []v1.Pod{}
for _, pod := range pods {
// Don't filter out pods with no owner reference
if len(pod.GetOwnerReferences()) == 0 {
filteredList = append(filteredList, pod)
continue
}

for _, ref := range pod.GetOwnerReferences() {
_, found := owners[ref.UID]
if !found {
filteredList = append(filteredList, pod)
owners[ref.UID] = struct{}{}
}
}
}

return filteredList
}
26 changes: 24 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ func RandomPodSubSlice(pods []v1.Pod, count int) []v1.Pod {
}

rand.Shuffle(len(pods), func(i, j int) { pods[i], pods[j] = pods[j], pods[i] })
res := pods[0:count]
return res

owners := make(map[types.UID]struct{})
filteredList := []v1.Pod{}
for _, pod := range pods {
if len(filteredList) == count {
break
}

// Don't filter out pods with no owner reference
if len(pod.GetOwnerReferences()) == 0 {
filteredList = append(filteredList, pod)
continue
}

for _, ref := range pod.GetOwnerReferences() {
_, found := owners[ref.UID]
if !found {
filteredList = append(filteredList, pod)
owners[ref.UID] = struct{}{}
}
}
}

return filteredList
}

0 comments on commit 9a892d5

Please sign in to comment.