Skip to content

Commit dd78da8

Browse files
authored
Merge pull request #1882 from pratikjagrut/exclude.evicted.pods
feat: check if pod is evicated before listing it
2 parents 98701cb + 1885c37 commit dd78da8

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

pkg/devspace/kubectl/selector/selector.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package selector
22

33
import (
44
"context"
5-
"github.com/loft-sh/devspace/pkg/devspace/imageselector"
65
"sort"
76
"strings"
87

8+
"github.com/loft-sh/devspace/pkg/devspace/imageselector"
9+
910
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1011
"github.com/loft-sh/devspace/pkg/util/hash"
1112
"github.com/pkg/errors"
12-
k8sv1 "k8s.io/api/core/v1"
13+
corev1 "k8s.io/api/core/v1"
1314
kerrors "k8s.io/apimachinery/pkg/api/errors"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
)
@@ -21,7 +22,7 @@ const (
2122
ReplacedLabel = "devspace.sh/replaced"
2223
)
2324

24-
var SortPodsByNewest = func(pods []*k8sv1.Pod, i, j int) bool {
25+
var SortPodsByNewest = func(pods []*corev1.Pod, i, j int) bool {
2526
return pods[i].CreationTimestamp.Unix() > pods[j].CreationTimestamp.Unix()
2627
}
2728

@@ -34,7 +35,7 @@ var SortContainersByNewest = func(pods []*SelectedPodContainer, i, j int) bool {
3435
return pods[i].Pod.CreationTimestamp.Unix() > pods[j].Pod.CreationTimestamp.Unix()
3536
}
3637

37-
func initContainerPos(container string, pod *k8sv1.Pod) int {
38+
func initContainerPos(container string, pod *corev1.Pod) int {
3839
for i, c := range pod.Spec.InitContainers {
3940
if c.Name == container {
4041
return i
@@ -43,11 +44,11 @@ func initContainerPos(container string, pod *k8sv1.Pod) int {
4344
return -1
4445
}
4546

46-
var FilterNonRunningPods = func(p *k8sv1.Pod) bool {
47+
var FilterNonRunningPods = func(p *corev1.Pod) bool {
4748
return kubectl.GetPodStatus(p) != "Running"
4849
}
4950

50-
var FilterNonRunningContainers = func(p *k8sv1.Pod, c *k8sv1.Container) bool {
51+
var FilterNonRunningContainers = func(p *corev1.Pod, c *corev1.Container) bool {
5152
if p.DeletionTimestamp != nil {
5253
return true
5354
}
@@ -65,8 +66,8 @@ var FilterNonRunningContainers = func(p *k8sv1.Pod, c *k8sv1.Container) bool {
6566
}
6667

6768
type SelectedPodContainer struct {
68-
Pod *k8sv1.Pod
69-
Container *k8sv1.Container
69+
Pod *corev1.Pod
70+
Container *corev1.Container
7071
}
7172

7273
type Selector struct {
@@ -109,15 +110,15 @@ func (s Selector) String() string {
109110
return strings.Join(strs, ", ")
110111
}
111112

112-
type FilterPod func(p *k8sv1.Pod) bool
113-
type FilterContainer func(p *k8sv1.Pod, c *k8sv1.Container) bool
113+
type FilterPod func(p *corev1.Pod) bool
114+
type FilterContainer func(p *corev1.Pod, c *corev1.Container) bool
114115

115-
type SortPods func(pods []*k8sv1.Pod, i, j int) bool
116+
type SortPods func(pods []*corev1.Pod, i, j int) bool
116117
type SortContainers func(containers []*SelectedPodContainer, i, j int) bool
117118

118119
type Filter interface {
119120
SelectContainers(ctx context.Context, selectors ...Selector) ([]*SelectedPodContainer, error)
120-
SelectPods(ctx context.Context, selectors ...Selector) ([]*k8sv1.Pod, error)
121+
SelectPods(ctx context.Context, selectors ...Selector) ([]*corev1.Pod, error)
121122
}
122123

123124
type filter struct {
@@ -141,7 +142,7 @@ func NewFilterWithSort(client kubectl.Client, sortPods SortPods, sortContainers
141142
}
142143
}
143144

144-
func (f *filter) SelectPods(ctx context.Context, selectors ...Selector) ([]*k8sv1.Pod, error) {
145+
func (f *filter) SelectPods(ctx context.Context, selectors ...Selector) ([]*corev1.Pod, error) {
145146
retList, err := f.SelectContainers(ctx, selectors...)
146147
if err != nil {
147148
return nil, err
@@ -208,8 +209,8 @@ func deduplicate(stack []*SelectedPodContainer) []*SelectedPodContainer {
208209
return retStack
209210
}
210211

211-
func podsFromPodContainer(stack []*SelectedPodContainer) []*k8sv1.Pod {
212-
retPods := []*k8sv1.Pod{}
212+
func podsFromPodContainer(stack []*SelectedPodContainer) []*corev1.Pod {
213+
retPods := []*corev1.Pod{}
213214
for _, s := range stack {
214215
if !containsPod(retPods, key(s.Pod.Namespace, s.Pod.Name, "")) {
215216
retPods = append(retPods, s.Pod)
@@ -218,7 +219,7 @@ func podsFromPodContainer(stack []*SelectedPodContainer) []*k8sv1.Pod {
218219
return retPods
219220
}
220221

221-
func containsPod(stack []*k8sv1.Pod, k string) bool {
222+
func containsPod(stack []*corev1.Pod, k string) bool {
222223
for _, s := range stack {
223224
if key(s.Namespace, s.Name, "") == k {
224225
return true
@@ -258,6 +259,8 @@ func byPodName(ctx context.Context, client kubectl.Client, namespace string, nam
258259
return nil, nil
259260
} else if pod.DeletionTimestamp != nil {
260261
return nil, nil
262+
} else if isPodEvicted(pod) {
263+
return nil, nil
261264
}
262265

263266
if !skipInit {
@@ -306,6 +309,8 @@ func byLabelSelector(ctx context.Context, client kubectl.Client, namespace strin
306309
continue
307310
} else if pod.DeletionTimestamp != nil {
308311
continue
312+
} else if isPodEvicted(&pod) {
313+
continue
309314
}
310315

311316
if !skipInit {
@@ -357,6 +362,8 @@ func byImageName(ctx context.Context, client kubectl.Client, namespace string, i
357362
continue
358363
} else if pod.DeletionTimestamp != nil {
359364
continue
365+
} else if isPodEvicted(&pod) {
366+
continue
360367
}
361368

362369
if !skipInit {
@@ -410,3 +417,7 @@ func byImageName(ctx context.Context, client kubectl.Client, namespace string, i
410417
}
411418
return retPods, nil
412419
}
420+
421+
func isPodEvicted(pod *corev1.Pod) bool {
422+
return strings.Contains(pod.Status.Reason, "Evicted")
423+
}

0 commit comments

Comments
 (0)