Skip to content

Commit 0d45881

Browse files
committed
[chore] refactor k8s client
1 parent 456d1dd commit 0d45881

File tree

9 files changed

+221
-233
lines changed

9 files changed

+221
-233
lines changed

cmd/cmd.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,14 @@ func getKubeConfigPath(cmd *cobra.Command) string {
289289
return filepath.Join(homeDir(), ".kube", "config")
290290
}
291291

292-
func getKubeContexts(cmd *cobra.Command) string {
293-
return cmd.Flags().Lookup("context").Value.String()
292+
func getKubeContexts(cmd *cobra.Command) []string {
293+
contextsString := cmd.Flags().Lookup("context").Value.String()
294+
trimmed := strings.Trim(strings.TrimSpace(contextsString), ",")
295+
var contexts []string
296+
if len(trimmed) > 0 {
297+
contexts = strings.Split(trimmed, ",")
298+
}
299+
return contexts
294300
}
295301

296302
func getDescending(cmd *cobra.Command) bool {
@@ -353,8 +359,14 @@ func getLogFilter(cmd *cobra.Command) model.LogFilter {
353359
return model.LogFilter{}
354360
}
355361

356-
func getNamespaces(cmd *cobra.Command) string {
357-
return cmd.Flags().Lookup("namespace").Value.String()
362+
func getNamespaces(cmd *cobra.Command) []string {
363+
namespacesString := cmd.Flags().Lookup("namespace").Value.String()
364+
trimmed := strings.Trim(strings.TrimSpace(namespacesString), ",")
365+
var namespaces []string
366+
if len(trimmed) > 0 {
367+
namespaces = strings.Split(trimmed, ",")
368+
}
369+
return namespaces
358370
}
359371

360372
func getSelector(cmd *cobra.Command) labels.Selector {

internal/app.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/robinovitch61/kl/internal/constants"
1313
"github.com/robinovitch61/kl/internal/dev"
1414
"github.com/robinovitch61/kl/internal/fileio"
15-
"github.com/robinovitch61/kl/internal/k8s"
15+
"github.com/robinovitch61/kl/internal/k8s/client"
1616
"github.com/robinovitch61/kl/internal/keymap"
1717
"github.com/robinovitch61/kl/internal/message"
1818
"github.com/robinovitch61/kl/internal/model"
@@ -30,7 +30,6 @@ import (
3030
type Model struct {
3131
config Config
3232
keyMap keymap.KeyMap
33-
allClusterNamespaces []model.ClusterNamespaces
3433
width, height int
3534
initialized bool
3635
stylesLoaded bool
@@ -44,10 +43,10 @@ type Model struct {
4443
containerToShortName func(model.Container) (model.PageLogContainerName, error)
4544
containerIdToColors map[string]model.ContainerColors
4645
pageLogBuffer []model.PageLog
47-
client k8s.Client
46+
client client.Client
4847
cancel context.CancelFunc
4948
pages map[page.Type]page.GenericPage
50-
containerListeners []model.ContainerListener
49+
containerListeners []client.ContainerListener
5150
focusedPageType page.Type
5251
rightPageType page.Type
5352
fullScreen bool
@@ -381,8 +380,8 @@ func (m Model) changeFocusedPage(newPage page.Type) (Model, tea.Cmd) {
381380
func (m Model) cleanupCmd() tea.Cmd {
382381
return func() tea.Msg {
383382
for _, cl := range m.containerListeners {
384-
if cl.CleanupFunc != nil {
385-
cl.CleanupFunc()
383+
if cl.Stop != nil {
384+
cl.Stop()
386385
}
387386
}
388387

@@ -619,7 +618,7 @@ func (m Model) doSelectionActions(selectionActions map[model.Entity]bool) (Model
619618
return m, tea.Batch(cmds...)
620619
}
621620

622-
func (m Model) getStartLogScannerCmd(client k8s.Client, entity model.Entity, sinceTime time.Time) (Model, tea.Cmd) {
621+
func (m Model) getStartLogScannerCmd(client client.Client, entity model.Entity, sinceTime time.Time) (Model, tea.Cmd) {
623622
// ensure the entity is a container
624623
err := entity.AssertIsContainer()
625624
if err != nil {

internal/command/container.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package command
33
import (
44
"fmt"
55
tea "github.com/charmbracelet/bubbletea/v2"
6-
"github.com/robinovitch61/kl/internal/k8s"
6+
"github.com/robinovitch61/kl/internal/k8s/client"
77
"github.com/robinovitch61/kl/internal/model"
88
"k8s.io/apimachinery/pkg/labels"
99
"time"
1010
)
1111

1212
type GetContainerListenerMsg struct {
13-
Listener model.ContainerListener
13+
Listener client.ContainerListener
1414
Err error
1515
}
1616

1717
func GetContainerListenerCmd(
18-
client k8s.Client,
18+
client client.Client,
1919
cluster, namespace string,
2020
matchers model.Matchers,
2121
selector labels.Selector,
@@ -35,14 +35,14 @@ func GetContainerListenerCmd(
3535
}
3636

3737
type GetContainerDeltasMsg struct {
38-
Listener model.ContainerListener
38+
Listener client.ContainerListener
3939
DeltaSet model.ContainerDeltaSet
4040
Err error
4141
}
4242

4343
func GetNextContainerDeltasCmd(
44-
client k8s.Client,
45-
listener model.ContainerListener,
44+
client client.Client,
45+
listener client.ContainerListener,
4646
duration time.Duration,
4747
) tea.Cmd {
4848
return func() tea.Msg {

internal/command/log.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
tea "github.com/charmbracelet/bubbletea/v2"
66
"github.com/robinovitch61/kl/internal/dev"
7-
"github.com/robinovitch61/kl/internal/k8s"
7+
"github.com/robinovitch61/kl/internal/k8s/client"
88
"github.com/robinovitch61/kl/internal/model"
99
"time"
1010
)
@@ -15,7 +15,7 @@ type StartedLogScannerMsg struct {
1515
}
1616

1717
func StartLogScannerCmd(
18-
client k8s.Client,
18+
client client.Client,
1919
container model.Container,
2020
sinceTime time.Time,
2121
) tea.Cmd {

internal/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
type Config struct {
99
AllNamespaces bool
1010
ContainerLimit int
11-
Contexts string
11+
Contexts []string
1212
Descending bool
1313
IgnoreOwnerTypes []string
1414
KubeConfigPath string
1515
LogsView bool
1616
LogFilter model.LogFilter
1717
Matchers model.Matchers
18-
Namespaces string
18+
Namespaces []string
1919
Selector labels.Selector
2020
SinceTime model.SinceTime
2121
Version string

0 commit comments

Comments
 (0)