Skip to content

Commit d2ffda0

Browse files
committed
put more k8s stuff in k8s package
1 parent b5e9b01 commit d2ffda0

24 files changed

+278
-254
lines changed

internal/app.go

+65-61
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package internal
33
import (
44
"context"
55
"fmt"
6+
"github.com/robinovitch61/kl/internal/k8s/container"
7+
"github.com/robinovitch61/kl/internal/k8s/entity"
8+
"github.com/robinovitch61/kl/internal/k8s/k8s_log"
9+
"github.com/robinovitch61/kl/internal/k8s/k8s_model"
610
"math"
711
"strconv"
812
"strings"
@@ -56,10 +60,10 @@ type components struct {
5660
whenPromptConfirm func() (Model, tea.Cmd)
5761
toast toast.Model
5862
// TODO: move entitytree to own package
59-
entityTree model.EntityTree
63+
entityTree entity.EntityTree
6064
// TODO: put these in entity tree?
61-
containerToShortName func(model.Container) (model.PageLogContainerName, error)
62-
containerIdToColors map[string]model.ContainerColors
65+
containerToShortName func(container.Container) (k8s_model.ContainerNameAndPrefix, error)
66+
containerIdToColors map[string]container.ContainerColors
6367
}
6468

6569
type Model struct {
@@ -315,7 +319,7 @@ func (m Model) topBar() string {
315319
var numPending, numSelected int
316320
containerEntities := m.components.entityTree.GetContainerEntities()
317321
for _, e := range containerEntities {
318-
if e.State == model.ScannerStarting || e.State == model.WantScanning {
322+
if e.State == entity.ScannerStarting || e.State == entity.WantScanning {
319323
numPending++
320324
}
321325
if e.State.MayHaveLogs() {
@@ -581,7 +585,7 @@ func (m Model) handleEntitiesPageKeyMsg(msg tea.KeyMsg) (Model, tea.Cmd) {
581585

582586
// handle deselecting all containers
583587
if key.Matches(msg, m.keyMap.DeselectAll) {
584-
selectionActions := make(map[model.Entity]bool)
588+
selectionActions := make(map[entity.Entity]bool)
585589
containerEntities := m.components.entityTree.GetContainerEntities()
586590
for i := range containerEntities {
587591
if !containerEntities[i].State.ActivatesWhenSelected() {
@@ -608,24 +612,24 @@ func (m Model) handleEntitiesPageKeyMsg(msg tea.KeyMsg) (Model, tea.Cmd) {
608612
return m, nil
609613
}
610614

611-
func (m Model) promptToConfirmSelectionActions(text []string, selectionActions map[model.Entity]bool) (Model, tea.Cmd) {
615+
func (m Model) promptToConfirmSelectionActions(text []string, selectionActions map[entity.Entity]bool) (Model, tea.Cmd) {
612616
m.components.prompt = prompt.New(true, m.state.width, m.state.height-m.data.topBarHeight, text, m.data.styles.Inverse)
613617
m.components.whenPromptConfirm = func() (Model, tea.Cmd) { return m.doSelectionActions(selectionActions) }
614618
return m, nil
615619
}
616620

617-
func (m Model) doSelectionActions(selectionActions map[model.Entity]bool) (Model, tea.Cmd) {
621+
func (m Model) doSelectionActions(selectionActions map[entity.Entity]bool) (Model, tea.Cmd) {
618622
var cmd tea.Cmd
619623
var cmds []tea.Cmd
620624

621-
for entity, startLogScanner := range selectionActions {
625+
for ent, startLogScanner := range selectionActions {
622626
if startLogScanner {
623-
newEntity, newTree, actions := entity.Activate(m.components.entityTree)
627+
newEntity, newTree, actions := ent.Activate(m.components.entityTree)
624628
m.components.entityTree = newTree
625629
m, cmd = m.doActions(newEntity, actions)
626630
cmds = append(cmds, cmd)
627631
} else {
628-
newEntity, newTree, actions := entity.Deactivate(m.components.entityTree)
632+
newEntity, newTree, actions := ent.Deactivate(m.components.entityTree)
629633
m.components.entityTree = newTree
630634
m, cmd = m.doActions(newEntity, actions)
631635
cmds = append(cmds, cmd)
@@ -636,24 +640,24 @@ func (m Model) doSelectionActions(selectionActions map[model.Entity]bool) (Model
636640
return m, tea.Batch(cmds...)
637641
}
638642

639-
func (m Model) getStartLogScannerCmd(client client.K8sClient, entity model.Entity, sinceTime time.Time) (Model, tea.Cmd) {
643+
func (m Model) getStartLogScannerCmd(client client.K8sClient, ent entity.Entity, sinceTime time.Time) (Model, tea.Cmd) {
640644
// ensure the entity is a container
641-
err := entity.AssertIsContainer()
645+
err := ent.AssertIsContainer()
642646
if err != nil {
643647
m.state.err = err
644648
return m, nil
645649
}
646650

647651
// ensure the entity does not already have an active log scanner
648-
if entity.LogScanner != nil {
652+
if ent.LogScanner != nil {
649653
return m, nil
650654
}
651655

652656
// check the limit of active log scanners isn't reached
653657
numPendingOrActive := 0
654658
for _, ce := range m.components.entityTree.GetContainerEntities() {
655659
switch ce.State {
656-
case model.WantScanning, model.ScannerStarting, model.Scanning, model.ScannerStopping, model.Deleted:
660+
case entity.WantScanning, entity.ScannerStarting, entity.Scanning, entity.ScannerStopping, entity.Deleted:
657661
numPendingOrActive++
658662
default:
659663
}
@@ -664,7 +668,7 @@ func (m Model) getStartLogScannerCmd(client client.K8sClient, entity model.Entit
664668
return m, tea.Tick(time.Second*5, func(t time.Time) tea.Msg { return toast.TimeoutMsg{ID: newToast.ID} })
665669
}
666670

667-
return m, command.StartLogScannerCmd(client, entity.Container, sinceTime)
671+
return m, command.StartLogScannerCmd(client, ent.Container, sinceTime)
668672
}
669673

670674
func (m Model) handleLogsPageKeyMsg(msg tea.KeyMsg) (Model, tea.Cmd) {
@@ -822,7 +826,7 @@ func (m Model) handleContainerDeltasMsg(msg command.GetContainerDeltasMsg) (Mode
822826

823827
for _, delta := range msg.DeltaSet.OrderedDeltas() {
824828
// get the existing entity for the container, if it exists
825-
var existingContainerEntity *model.Entity
829+
var existingContainerEntity *entity.Entity
826830
for _, containerEntity := range existingContainerEntities {
827831
if containerEntity.Container.Equals(delta.Container) {
828832
existingContainerEntity = &containerEntity
@@ -832,24 +836,24 @@ func (m Model) handleContainerDeltasMsg(msg command.GetContainerDeltasMsg) (Mode
832836

833837
if delta.ToDelete {
834838
if existingContainerEntity != nil {
835-
entity, newTree, actions := existingContainerEntity.Delete(m.components.entityTree, delta)
839+
ent, newTree, actions := existingContainerEntity.Delete(m.components.entityTree, delta)
836840
m.components.entityTree = newTree
837-
m, cmd = m.doActions(entity, actions)
841+
m, cmd = m.doActions(ent, actions)
838842
cmds = append(cmds, cmd)
839843
}
840844
} else {
841845
if existingContainerEntity == nil {
842-
entity := model.Entity{
846+
ent := entity.Entity{
843847
Container: delta.Container,
844848
}
845-
newEntity, newTree, actions := entity.Create(m.components.entityTree, delta)
849+
newEntity, newTree, actions := ent.Create(m.components.entityTree, delta)
846850
m.components.entityTree = newTree
847851
m, cmd = m.doActions(newEntity, actions)
848852
cmds = append(cmds, cmd)
849853
} else {
850-
entity, newTree, actions := existingContainerEntity.Update(m.components.entityTree, delta)
854+
ent, newTree, actions := existingContainerEntity.Update(m.components.entityTree, delta)
851855
m.components.entityTree = newTree
852-
m, cmd = m.doActions(entity, actions)
856+
m, cmd = m.doActions(ent, actions)
853857
cmds = append(cmds, cmd)
854858
}
855859
}
@@ -864,7 +868,7 @@ func (m Model) handleStartedLogScannerMsg(msg command.StartedLogScannerMsg) (Mod
864868
var cmd tea.Cmd
865869
var cmds []tea.Cmd
866870
existingContainerEntities := m.components.entityTree.GetContainerEntities()
867-
var startedContainerEntity *model.Entity
871+
var startedContainerEntity *entity.Entity
868872
for _, containerEntity := range existingContainerEntities {
869873
if msg.LogScanner.Container.Equals(containerEntity.Container) {
870874
startedContainerEntity = &containerEntity
@@ -876,9 +880,9 @@ func (m Model) handleStartedLogScannerMsg(msg command.StartedLogScannerMsg) (Mod
876880
return m, nil
877881
}
878882

879-
entity, newTree, actions := startedContainerEntity.ScannerStarted(m.components.entityTree, msg.Err, msg.LogScanner)
883+
ent, newTree, actions := startedContainerEntity.ScannerStarted(m.components.entityTree, msg.Err, msg.LogScanner)
880884
m.components.entityTree = newTree
881-
m, cmd = m.doActions(entity, actions)
885+
m, cmd = m.doActions(ent, actions)
882886
cmds = append(cmds, cmd)
883887

884888
m.pages[page.EntitiesPageType] = m.pages[page.EntitiesPageType].(page.EntityPage).WithEntityTree(m.components.entityTree)
@@ -896,14 +900,14 @@ func (m Model) handleStoppedLogScannersMsg(msg command.StoppedLogScannersMsg) (M
896900
for _, existingEntity := range existingEntities {
897901
for _, stoppedContainer := range msg.Containers {
898902
if existingEntity.Container.Equals(stoppedContainer) {
899-
entity, newTree, actions := existingEntity.ScannerStopped(m.components.entityTree)
903+
ent, newTree, actions := existingEntity.ScannerStopped(m.components.entityTree)
900904
m.components.entityTree = newTree
901-
m, cmd = m.doActions(entity, actions)
905+
m, cmd = m.doActions(ent, actions)
902906
cmds = append(cmds, cmd)
903907
if msg.Restart {
904-
entity, newTree, actions = entity.Activate(m.components.entityTree)
908+
ent, newTree, actions = ent.Activate(m.components.entityTree)
905909
m.components.entityTree = newTree
906-
m, cmd = m.doActions(entity, actions)
910+
m, cmd = m.doActions(ent, actions)
907911
cmds = append(cmds, cmd)
908912
}
909913
}
@@ -928,32 +932,32 @@ func (m Model) handleNewLogsMsg(msg command.GetNewLogsMsg) (Model, tea.Cmd) {
928932
}
929933

930934
// ignore logs if logScanner has already been closed
931-
entity := m.components.entityTree.GetEntity(msg.LogScanner.Container)
932-
if entity == nil || entity.LogScanner == nil {
935+
ent := m.components.entityTree.GetEntity(msg.LogScanner.Container)
936+
if ent == nil || ent.LogScanner == nil {
933937
return m, nil
934938
}
935939

936940
// ignore logs if its from an old logScanner for a container that has been removed and reactivated
937-
if !entity.LogScanner.Equals(msg.LogScanner) {
941+
if !ent.LogScanner.Equals(msg.LogScanner) {
938942
return m, nil
939943
}
940944

941945
var err error
942946
var newLogs []model.PageLog
943947
for i := range msg.NewLogs {
944-
shortName := model.PageLogContainerName{}
948+
shortName := k8s_model.ContainerNameAndPrefix{}
945949
if m.components.containerToShortName != nil {
946950
shortName, err = m.components.containerToShortName(msg.NewLogs[i].Container)
947951
if err != nil {
948952
m.state.err = err
949953
return m, nil
950954
}
951955
}
952-
fullName := model.PageLogContainerName{
956+
fullName := k8s_model.ContainerNameAndPrefix{
953957
Prefix: msg.NewLogs[i].Container.IDWithoutContainerName(),
954958
ContainerName: msg.NewLogs[i].Container.Name,
955959
}
956-
var containerColors model.ContainerColors
960+
var containerColors container.ContainerColors
957961
if m.components.containerIdToColors != nil {
958962
containerColors = m.components.containerIdToColors[msg.NewLogs[i].Container.ID()]
959963
}
@@ -969,7 +973,7 @@ func (m Model) handleNewLogsMsg(msg command.GetNewLogsMsg) (Model, tea.Cmd) {
969973
Short: localTime.Format(time.TimeOnly),
970974
Full: localTime.Format("2006-01-02T15:04:05.000Z07:00"),
971975
},
972-
Terminated: entity.Container.Status.State == model.ContainerTerminated,
976+
Terminated: ent.Container.Status.State == container.ContainerTerminated,
973977
Styles: &m.data.styles,
974978
}
975979
newLogs = append(newLogs, newLog)
@@ -1008,27 +1012,27 @@ func (m Model) doUpdateSinceTime() (Model, tea.Cmd) {
10081012
m.state.pendingSinceTime = nil
10091013

10101014
// stop all scanning entities and signal to restart them with the new since time
1011-
var logScannersToStopAndRestart []model.LogScanner
1015+
var logScannersToStopAndRestart []k8s_log.LogScanner
10121016
for _, containerEntity := range m.components.entityTree.GetContainerEntities() {
1013-
if containerEntity.State == model.Scanning {
1014-
entity, newTree, actions := containerEntity.Restart(m.components.entityTree)
1017+
if containerEntity.State == entity.Scanning {
1018+
ent, newTree, actions := containerEntity.Restart(m.components.entityTree)
10151019
m.components.entityTree = newTree
1016-
m, cmd = m.doActions(entity, actions)
1020+
m, cmd = m.doActions(ent, actions)
10171021
cmds = append(cmds, cmd)
1018-
logScannersToStopAndRestart = append(logScannersToStopAndRestart, *entity.LogScanner)
1022+
logScannersToStopAndRestart = append(logScannersToStopAndRestart, *ent.LogScanner)
10191023
}
10201024
}
10211025
// bulk stop log scanners together so they begin restarting one by one only after all have stopped
10221026
cmds = append(cmds, command.StopLogScannersInPrepForNewSinceTimeCmd(logScannersToStopAndRestart))
10231027
return m, tea.Batch(cmds...)
10241028
}
10251029

1026-
func (m Model) doActions(entity model.Entity, actions []model.EntityAction) (Model, tea.Cmd) {
1030+
func (m Model) doActions(ent entity.Entity, actions []entity.EntityAction) (Model, tea.Cmd) {
10271031
var cmd tea.Cmd
10281032
var cmds []tea.Cmd
10291033

10301034
// ensure actions are unique to avoid duplicate processing
1031-
actionSet := make(map[model.EntityAction]bool)
1035+
actionSet := make(map[entity.EntityAction]bool)
10321036
for _, action := range actions {
10331037
if actionSet[action] {
10341038
dev.Debug(fmt.Sprintf("duplicate action detected: %s", action))
@@ -1038,19 +1042,19 @@ func (m Model) doActions(entity model.Entity, actions []model.EntityAction) (Mod
10381042

10391043
for action := range actionSet {
10401044
switch action {
1041-
case model.StartScanner:
1042-
m, cmd = m.getStartLogScannerCmd(m.k8sClient, entity, m.state.sinceTime.Time)
1045+
case entity.StartScanner:
1046+
m, cmd = m.getStartLogScannerCmd(m.k8sClient, ent, m.state.sinceTime.Time)
10431047
cmds = append(cmds, cmd)
1044-
case model.StopScanner:
1045-
cmds = append(cmds, command.StopLogScannerCmd(entity, false))
1046-
case model.StopScannerKeepLogs:
1047-
cmds = append(cmds, command.StopLogScannerCmd(entity, true))
1048-
case model.RemoveEntity:
1049-
m.components.entityTree.Remove(entity)
1050-
case model.RemoveLogs:
1051-
m.removeLogsForContainer(entity.Container)
1052-
case model.MarkLogsTerminated:
1053-
m.markLogsTerminatedForContainer(entity.Container)
1048+
case entity.StopScanner:
1049+
cmds = append(cmds, command.StopLogScannerCmd(ent, false))
1050+
case entity.StopScannerKeepLogs:
1051+
cmds = append(cmds, command.StopLogScannerCmd(ent, true))
1052+
case entity.RemoveEntity:
1053+
m.components.entityTree.Remove(ent)
1054+
case entity.RemoveLogs:
1055+
m.removeLogsForContainer(ent.Container)
1056+
case entity.MarkLogsTerminated:
1057+
m.markLogsTerminatedForContainer(ent.Container)
10541058
default:
10551059
panic(fmt.Sprintf("unknown entity action: %s", action))
10561060
}
@@ -1062,9 +1066,9 @@ func (m Model) doActions(entity model.Entity, actions []model.EntityAction) (Mod
10621066
// it should be called every time the set of active containers changes
10631067
func (m Model) withUpdatedContainerShortNames() Model {
10641068
containers := m.components.entityTree.GetContainerEntities()
1065-
m.components.containerIdToColors = make(map[string]model.ContainerColors)
1069+
m.components.containerIdToColors = make(map[string]container.ContainerColors)
10661070
for _, containerEntity := range containers {
1067-
m.components.containerIdToColors[containerEntity.Container.ID()] = model.ContainerColors{
1071+
m.components.containerIdToColors[containerEntity.Container.ID()] = container.ContainerColors{
10681072
ID: color.GetColor(containerEntity.Container.ID()),
10691073
Name: color.GetColor(containerEntity.Container.Name),
10701074
}
@@ -1102,12 +1106,12 @@ func (m *Model) updateShortNamesInBuffer() error {
11021106
return nil
11031107
}
11041108

1105-
func (m *Model) removeLogsForContainer(container model.Container) {
1109+
func (m *Model) removeLogsForContainer(container container.Container) {
11061110
m.pages[page.LogsPageType] = m.pages[page.LogsPageType].(page.LogsPage).WithLogsRemovedForContainer(container)
11071111
m.removeContainerLogsFromBuffer(container)
11081112
}
11091113

1110-
func (m *Model) removeContainerLogsFromBuffer(container model.Container) {
1114+
func (m *Model) removeContainerLogsFromBuffer(container container.Container) {
11111115
bufferedLogs := m.pageLogBuffer
11121116
m.pageLogBuffer = nil
11131117
for _, bufferedLog := range bufferedLogs {
@@ -1117,12 +1121,12 @@ func (m *Model) removeContainerLogsFromBuffer(container model.Container) {
11171121
}
11181122
}
11191123

1120-
func (m *Model) markLogsTerminatedForContainer(container model.Container) {
1124+
func (m *Model) markLogsTerminatedForContainer(container container.Container) {
11211125
m.pages[page.LogsPageType] = m.pages[page.LogsPageType].(page.LogsPage).WithLogsTerminatedForContainer(container)
11221126
m.markContainerLogsTerminatedInBuffer(container)
11231127
}
11241128

1125-
func (m *Model) markContainerLogsTerminatedInBuffer(container model.Container) {
1129+
func (m *Model) markContainerLogsTerminatedInBuffer(container container.Container) {
11261130
for i := range m.pageLogBuffer {
11271131
if m.pageLogBuffer[i].Log.Container.Equals(container) {
11281132
m.pageLogBuffer[i].Terminated = true

internal/command/container.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
tea "github.com/charmbracelet/bubbletea/v2"
66
"github.com/robinovitch61/kl/internal/k8s/client"
7+
"github.com/robinovitch61/kl/internal/k8s/container"
78
"github.com/robinovitch61/kl/internal/model"
89
"k8s.io/apimachinery/pkg/labels"
910
"time"
@@ -36,7 +37,7 @@ func GetContainerListenerCmd(
3637

3738
type GetContainerDeltasMsg struct {
3839
Listener client.ContainerListener
39-
DeltaSet model.ContainerDeltaSet
40+
DeltaSet container.ContainerDeltaSet
4041
Err error
4142
}
4243

@@ -51,7 +52,7 @@ func GetNextContainerDeltasCmd(
5152
if err != nil {
5253
return GetContainerDeltasMsg{
5354
Listener: listener,
54-
DeltaSet: model.ContainerDeltaSet{},
55+
DeltaSet: container.ContainerDeltaSet{},
5556
Err: err,
5657
}
5758
}

0 commit comments

Comments
 (0)