Skip to content

Commit 0f7d957

Browse files
committed
refactor: improve target selector
1 parent f8b2bd6 commit 0f7d957

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

pkg/devspace/kubectl/selector/selector.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ func initContainerPos(container string, pod *corev1.Pod) int {
4545
}
4646

4747
var FilterTerminatingContainers = func(p *corev1.Pod, c *corev1.Container) bool {
48-
if IsPodTerminating(p) {
49-
return true
50-
}
51-
return false
48+
return IsPodTerminating(p)
5249
}
5350

5451
var FilterNonRunningContainers = func(p *corev1.Pod, c *corev1.Container) bool {
@@ -108,19 +105,15 @@ func (s Selector) String() string {
108105
}
109106

110107
type FilterContainer func(p *corev1.Pod, c *corev1.Container) bool
111-
112-
type SortPods func(pods []*corev1.Pod, i, j int) bool
113108
type SortContainers func(containers []*SelectedPodContainer, i, j int) bool
114109

115110
type Filter interface {
116111
SelectContainers(ctx context.Context, selectors ...Selector) ([]*SelectedPodContainer, error)
117-
SelectPods(ctx context.Context, selectors ...Selector) ([]*corev1.Pod, error)
118112
}
119113

120114
type filter struct {
121115
client kubectl.Client
122116

123-
sortPods SortPods
124117
sortContainers SortContainers
125118
}
126119

@@ -130,30 +123,13 @@ func NewFilter(client kubectl.Client) Filter {
130123
}
131124
}
132125

133-
func NewFilterWithSort(client kubectl.Client, sortPods SortPods, sortContainers SortContainers) Filter {
126+
func NewFilterWithSort(client kubectl.Client, sortContainers SortContainers) Filter {
134127
return &filter{
135128
client: client,
136-
sortPods: sortPods,
137129
sortContainers: sortContainers,
138130
}
139131
}
140132

141-
func (f *filter) SelectPods(ctx context.Context, selectors ...Selector) ([]*corev1.Pod, error) {
142-
retList, err := f.SelectContainers(ctx, selectors...)
143-
if err != nil {
144-
return nil, err
145-
}
146-
147-
pods := podsFromPodContainer(retList)
148-
if f.sortPods != nil {
149-
sort.Slice(pods, func(i, j int) bool {
150-
return f.sortPods(pods, i, j)
151-
})
152-
}
153-
154-
return pods, nil
155-
}
156-
157133
func (f *filter) SelectContainers(ctx context.Context, selectors ...Selector) ([]*SelectedPodContainer, error) {
158134
retList := []*SelectedPodContainer{}
159135
for _, s := range selectors {
@@ -205,13 +181,17 @@ func deduplicate(stack []*SelectedPodContainer) []*SelectedPodContainer {
205181
return retStack
206182
}
207183

208-
func podsFromPodContainer(stack []*SelectedPodContainer) []*corev1.Pod {
184+
func PodsFromPodContainer(stack []*SelectedPodContainer) []*corev1.Pod {
209185
retPods := []*corev1.Pod{}
210186
for _, s := range stack {
211187
if !containsPod(retPods, key(s.Pod.Namespace, s.Pod.Name, "")) {
212188
retPods = append(retPods, s.Pod)
213189
}
214190
}
191+
192+
sort.Slice(retPods, func(i, j int) bool {
193+
return SortPodsByNewest(retPods, i, j)
194+
})
215195
return retPods
216196
}
217197

pkg/devspace/services/targetselector/target_selector.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ type Options struct {
3434
timeout int64
3535

3636
failIfMultiple bool
37-
sortPods selector.SortPods
3837
sortContainers selector.SortContainers
3938

4039
waitingStrategy WaitingStrategy
@@ -45,7 +44,6 @@ func NewEmptyOptions() Options {
4544
selector: selector.Selector{
4645
FilterContainer: selector.FilterTerminatingContainers,
4746
},
48-
sortPods: selector.SortPodsByNewest,
4947
sortContainers: selector.SortContainersByNewest,
5048
}
5149
}
@@ -60,7 +58,6 @@ func NewOptionsFromFlags(containerName string, labelSelector string, imageSelect
6058
Namespace: namespace,
6159
FilterContainer: selector.FilterNonRunningContainers,
6260
},
63-
sortPods: selector.SortPodsByNewest,
6461
sortContainers: selector.SortContainersByNewest,
6562
}
6663
}
@@ -239,7 +236,7 @@ func (t *targetSelector) selectSingle(ctx context.Context, options Options, log
239236
}
240237

241238
func (t *targetSelector) selectSingleContainer(ctx context.Context, options Options, log log.Logger) (bool, interface{}, error) {
242-
containers, err := selector.NewFilterWithSort(t.client, options.sortPods, options.sortContainers).SelectContainers(ctx, options.selector)
239+
containers, err := selector.NewFilterWithSort(t.client, options.sortContainers).SelectContainers(ctx, options.selector)
243240
if err != nil {
244241
return false, nil, err
245242
} else if options.waitingStrategy != nil {
@@ -289,10 +286,14 @@ func (t *targetSelector) selectSingleContainer(ctx context.Context, options Opti
289286
}
290287

291288
func (t *targetSelector) selectSinglePod(ctx context.Context, options Options, log log.Logger) (bool, interface{}, error) {
292-
pods, err := selector.NewFilterWithSort(t.client, options.sortPods, options.sortContainers).SelectPods(ctx, options.selector)
289+
stack, err := selector.NewFilterWithSort(t.client, options.sortContainers).SelectContainers(ctx, options.selector)
293290
if err != nil {
294291
return false, nil, err
295-
} else if options.waitingStrategy != nil {
292+
}
293+
294+
// transform stack
295+
pods := selector.PodsFromPodContainer(stack)
296+
if options.waitingStrategy != nil {
296297
return options.waitingStrategy.SelectPod(ctx, t.client, options.selector.Namespace, pods, log)
297298
}
298299

0 commit comments

Comments
 (0)