@@ -3,6 +3,10 @@ package internal
3
3
import (
4
4
"context"
5
5
"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"
6
10
"math"
7
11
"strconv"
8
12
"strings"
@@ -56,10 +60,10 @@ type components struct {
56
60
whenPromptConfirm func () (Model , tea.Cmd )
57
61
toast toast.Model
58
62
// TODO: move entitytree to own package
59
- entityTree model .EntityTree
63
+ entityTree entity .EntityTree
60
64
// 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
63
67
}
64
68
65
69
type Model struct {
@@ -315,7 +319,7 @@ func (m Model) topBar() string {
315
319
var numPending , numSelected int
316
320
containerEntities := m .components .entityTree .GetContainerEntities ()
317
321
for _ , e := range containerEntities {
318
- if e .State == model .ScannerStarting || e .State == model .WantScanning {
322
+ if e .State == entity .ScannerStarting || e .State == entity .WantScanning {
319
323
numPending ++
320
324
}
321
325
if e .State .MayHaveLogs () {
@@ -581,7 +585,7 @@ func (m Model) handleEntitiesPageKeyMsg(msg tea.KeyMsg) (Model, tea.Cmd) {
581
585
582
586
// handle deselecting all containers
583
587
if key .Matches (msg , m .keyMap .DeselectAll ) {
584
- selectionActions := make (map [model .Entity ]bool )
588
+ selectionActions := make (map [entity .Entity ]bool )
585
589
containerEntities := m .components .entityTree .GetContainerEntities ()
586
590
for i := range containerEntities {
587
591
if ! containerEntities [i ].State .ActivatesWhenSelected () {
@@ -608,24 +612,24 @@ func (m Model) handleEntitiesPageKeyMsg(msg tea.KeyMsg) (Model, tea.Cmd) {
608
612
return m , nil
609
613
}
610
614
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 ) {
612
616
m .components .prompt = prompt .New (true , m .state .width , m .state .height - m .data .topBarHeight , text , m .data .styles .Inverse )
613
617
m .components .whenPromptConfirm = func () (Model , tea.Cmd ) { return m .doSelectionActions (selectionActions ) }
614
618
return m , nil
615
619
}
616
620
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 ) {
618
622
var cmd tea.Cmd
619
623
var cmds []tea.Cmd
620
624
621
- for entity , startLogScanner := range selectionActions {
625
+ for ent , startLogScanner := range selectionActions {
622
626
if startLogScanner {
623
- newEntity , newTree , actions := entity .Activate (m .components .entityTree )
627
+ newEntity , newTree , actions := ent .Activate (m .components .entityTree )
624
628
m .components .entityTree = newTree
625
629
m , cmd = m .doActions (newEntity , actions )
626
630
cmds = append (cmds , cmd )
627
631
} else {
628
- newEntity , newTree , actions := entity .Deactivate (m .components .entityTree )
632
+ newEntity , newTree , actions := ent .Deactivate (m .components .entityTree )
629
633
m .components .entityTree = newTree
630
634
m , cmd = m .doActions (newEntity , actions )
631
635
cmds = append (cmds , cmd )
@@ -636,24 +640,24 @@ func (m Model) doSelectionActions(selectionActions map[model.Entity]bool) (Model
636
640
return m , tea .Batch (cmds ... )
637
641
}
638
642
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 ) {
640
644
// ensure the entity is a container
641
- err := entity .AssertIsContainer ()
645
+ err := ent .AssertIsContainer ()
642
646
if err != nil {
643
647
m .state .err = err
644
648
return m , nil
645
649
}
646
650
647
651
// ensure the entity does not already have an active log scanner
648
- if entity .LogScanner != nil {
652
+ if ent .LogScanner != nil {
649
653
return m , nil
650
654
}
651
655
652
656
// check the limit of active log scanners isn't reached
653
657
numPendingOrActive := 0
654
658
for _ , ce := range m .components .entityTree .GetContainerEntities () {
655
659
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 :
657
661
numPendingOrActive ++
658
662
default :
659
663
}
@@ -664,7 +668,7 @@ func (m Model) getStartLogScannerCmd(client client.K8sClient, entity model.Entit
664
668
return m , tea .Tick (time .Second * 5 , func (t time.Time ) tea.Msg { return toast.TimeoutMsg {ID : newToast .ID } })
665
669
}
666
670
667
- return m , command .StartLogScannerCmd (client , entity .Container , sinceTime )
671
+ return m , command .StartLogScannerCmd (client , ent .Container , sinceTime )
668
672
}
669
673
670
674
func (m Model ) handleLogsPageKeyMsg (msg tea.KeyMsg ) (Model , tea.Cmd ) {
@@ -822,7 +826,7 @@ func (m Model) handleContainerDeltasMsg(msg command.GetContainerDeltasMsg) (Mode
822
826
823
827
for _ , delta := range msg .DeltaSet .OrderedDeltas () {
824
828
// get the existing entity for the container, if it exists
825
- var existingContainerEntity * model .Entity
829
+ var existingContainerEntity * entity .Entity
826
830
for _ , containerEntity := range existingContainerEntities {
827
831
if containerEntity .Container .Equals (delta .Container ) {
828
832
existingContainerEntity = & containerEntity
@@ -832,24 +836,24 @@ func (m Model) handleContainerDeltasMsg(msg command.GetContainerDeltasMsg) (Mode
832
836
833
837
if delta .ToDelete {
834
838
if existingContainerEntity != nil {
835
- entity , newTree , actions := existingContainerEntity .Delete (m .components .entityTree , delta )
839
+ ent , newTree , actions := existingContainerEntity .Delete (m .components .entityTree , delta )
836
840
m .components .entityTree = newTree
837
- m , cmd = m .doActions (entity , actions )
841
+ m , cmd = m .doActions (ent , actions )
838
842
cmds = append (cmds , cmd )
839
843
}
840
844
} else {
841
845
if existingContainerEntity == nil {
842
- entity := model .Entity {
846
+ ent := entity .Entity {
843
847
Container : delta .Container ,
844
848
}
845
- newEntity , newTree , actions := entity .Create (m .components .entityTree , delta )
849
+ newEntity , newTree , actions := ent .Create (m .components .entityTree , delta )
846
850
m .components .entityTree = newTree
847
851
m , cmd = m .doActions (newEntity , actions )
848
852
cmds = append (cmds , cmd )
849
853
} else {
850
- entity , newTree , actions := existingContainerEntity .Update (m .components .entityTree , delta )
854
+ ent , newTree , actions := existingContainerEntity .Update (m .components .entityTree , delta )
851
855
m .components .entityTree = newTree
852
- m , cmd = m .doActions (entity , actions )
856
+ m , cmd = m .doActions (ent , actions )
853
857
cmds = append (cmds , cmd )
854
858
}
855
859
}
@@ -864,7 +868,7 @@ func (m Model) handleStartedLogScannerMsg(msg command.StartedLogScannerMsg) (Mod
864
868
var cmd tea.Cmd
865
869
var cmds []tea.Cmd
866
870
existingContainerEntities := m .components .entityTree .GetContainerEntities ()
867
- var startedContainerEntity * model .Entity
871
+ var startedContainerEntity * entity .Entity
868
872
for _ , containerEntity := range existingContainerEntities {
869
873
if msg .LogScanner .Container .Equals (containerEntity .Container ) {
870
874
startedContainerEntity = & containerEntity
@@ -876,9 +880,9 @@ func (m Model) handleStartedLogScannerMsg(msg command.StartedLogScannerMsg) (Mod
876
880
return m , nil
877
881
}
878
882
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 )
880
884
m .components .entityTree = newTree
881
- m , cmd = m .doActions (entity , actions )
885
+ m , cmd = m .doActions (ent , actions )
882
886
cmds = append (cmds , cmd )
883
887
884
888
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
896
900
for _ , existingEntity := range existingEntities {
897
901
for _ , stoppedContainer := range msg .Containers {
898
902
if existingEntity .Container .Equals (stoppedContainer ) {
899
- entity , newTree , actions := existingEntity .ScannerStopped (m .components .entityTree )
903
+ ent , newTree , actions := existingEntity .ScannerStopped (m .components .entityTree )
900
904
m .components .entityTree = newTree
901
- m , cmd = m .doActions (entity , actions )
905
+ m , cmd = m .doActions (ent , actions )
902
906
cmds = append (cmds , cmd )
903
907
if msg .Restart {
904
- entity , newTree , actions = entity .Activate (m .components .entityTree )
908
+ ent , newTree , actions = ent .Activate (m .components .entityTree )
905
909
m .components .entityTree = newTree
906
- m , cmd = m .doActions (entity , actions )
910
+ m , cmd = m .doActions (ent , actions )
907
911
cmds = append (cmds , cmd )
908
912
}
909
913
}
@@ -928,32 +932,32 @@ func (m Model) handleNewLogsMsg(msg command.GetNewLogsMsg) (Model, tea.Cmd) {
928
932
}
929
933
930
934
// 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 {
933
937
return m , nil
934
938
}
935
939
936
940
// 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 ) {
938
942
return m , nil
939
943
}
940
944
941
945
var err error
942
946
var newLogs []model.PageLog
943
947
for i := range msg .NewLogs {
944
- shortName := model. PageLogContainerName {}
948
+ shortName := k8s_model. ContainerNameAndPrefix {}
945
949
if m .components .containerToShortName != nil {
946
950
shortName , err = m .components .containerToShortName (msg .NewLogs [i ].Container )
947
951
if err != nil {
948
952
m .state .err = err
949
953
return m , nil
950
954
}
951
955
}
952
- fullName := model. PageLogContainerName {
956
+ fullName := k8s_model. ContainerNameAndPrefix {
953
957
Prefix : msg .NewLogs [i ].Container .IDWithoutContainerName (),
954
958
ContainerName : msg .NewLogs [i ].Container .Name ,
955
959
}
956
- var containerColors model .ContainerColors
960
+ var containerColors container .ContainerColors
957
961
if m .components .containerIdToColors != nil {
958
962
containerColors = m .components .containerIdToColors [msg .NewLogs [i ].Container .ID ()]
959
963
}
@@ -969,7 +973,7 @@ func (m Model) handleNewLogsMsg(msg command.GetNewLogsMsg) (Model, tea.Cmd) {
969
973
Short : localTime .Format (time .TimeOnly ),
970
974
Full : localTime .Format ("2006-01-02T15:04:05.000Z07:00" ),
971
975
},
972
- Terminated : entity .Container .Status .State == model .ContainerTerminated ,
976
+ Terminated : ent .Container .Status .State == container .ContainerTerminated ,
973
977
Styles : & m .data .styles ,
974
978
}
975
979
newLogs = append (newLogs , newLog )
@@ -1008,27 +1012,27 @@ func (m Model) doUpdateSinceTime() (Model, tea.Cmd) {
1008
1012
m .state .pendingSinceTime = nil
1009
1013
1010
1014
// 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
1012
1016
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 )
1015
1019
m .components .entityTree = newTree
1016
- m , cmd = m .doActions (entity , actions )
1020
+ m , cmd = m .doActions (ent , actions )
1017
1021
cmds = append (cmds , cmd )
1018
- logScannersToStopAndRestart = append (logScannersToStopAndRestart , * entity .LogScanner )
1022
+ logScannersToStopAndRestart = append (logScannersToStopAndRestart , * ent .LogScanner )
1019
1023
}
1020
1024
}
1021
1025
// bulk stop log scanners together so they begin restarting one by one only after all have stopped
1022
1026
cmds = append (cmds , command .StopLogScannersInPrepForNewSinceTimeCmd (logScannersToStopAndRestart ))
1023
1027
return m , tea .Batch (cmds ... )
1024
1028
}
1025
1029
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 ) {
1027
1031
var cmd tea.Cmd
1028
1032
var cmds []tea.Cmd
1029
1033
1030
1034
// ensure actions are unique to avoid duplicate processing
1031
- actionSet := make (map [model .EntityAction ]bool )
1035
+ actionSet := make (map [entity .EntityAction ]bool )
1032
1036
for _ , action := range actions {
1033
1037
if actionSet [action ] {
1034
1038
dev .Debug (fmt .Sprintf ("duplicate action detected: %s" , action ))
@@ -1038,19 +1042,19 @@ func (m Model) doActions(entity model.Entity, actions []model.EntityAction) (Mod
1038
1042
1039
1043
for action := range actionSet {
1040
1044
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 )
1043
1047
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 )
1054
1058
default :
1055
1059
panic (fmt .Sprintf ("unknown entity action: %s" , action ))
1056
1060
}
@@ -1062,9 +1066,9 @@ func (m Model) doActions(entity model.Entity, actions []model.EntityAction) (Mod
1062
1066
// it should be called every time the set of active containers changes
1063
1067
func (m Model ) withUpdatedContainerShortNames () Model {
1064
1068
containers := m .components .entityTree .GetContainerEntities ()
1065
- m .components .containerIdToColors = make (map [string ]model .ContainerColors )
1069
+ m .components .containerIdToColors = make (map [string ]container .ContainerColors )
1066
1070
for _ , containerEntity := range containers {
1067
- m .components .containerIdToColors [containerEntity .Container .ID ()] = model .ContainerColors {
1071
+ m .components .containerIdToColors [containerEntity .Container .ID ()] = container .ContainerColors {
1068
1072
ID : color .GetColor (containerEntity .Container .ID ()),
1069
1073
Name : color .GetColor (containerEntity .Container .Name ),
1070
1074
}
@@ -1102,12 +1106,12 @@ func (m *Model) updateShortNamesInBuffer() error {
1102
1106
return nil
1103
1107
}
1104
1108
1105
- func (m * Model ) removeLogsForContainer (container model .Container ) {
1109
+ func (m * Model ) removeLogsForContainer (container container .Container ) {
1106
1110
m .pages [page .LogsPageType ] = m .pages [page .LogsPageType ].(page.LogsPage ).WithLogsRemovedForContainer (container )
1107
1111
m .removeContainerLogsFromBuffer (container )
1108
1112
}
1109
1113
1110
- func (m * Model ) removeContainerLogsFromBuffer (container model .Container ) {
1114
+ func (m * Model ) removeContainerLogsFromBuffer (container container .Container ) {
1111
1115
bufferedLogs := m .pageLogBuffer
1112
1116
m .pageLogBuffer = nil
1113
1117
for _ , bufferedLog := range bufferedLogs {
@@ -1117,12 +1121,12 @@ func (m *Model) removeContainerLogsFromBuffer(container model.Container) {
1117
1121
}
1118
1122
}
1119
1123
1120
- func (m * Model ) markLogsTerminatedForContainer (container model .Container ) {
1124
+ func (m * Model ) markLogsTerminatedForContainer (container container .Container ) {
1121
1125
m .pages [page .LogsPageType ] = m .pages [page .LogsPageType ].(page.LogsPage ).WithLogsTerminatedForContainer (container )
1122
1126
m .markContainerLogsTerminatedInBuffer (container )
1123
1127
}
1124
1128
1125
- func (m * Model ) markContainerLogsTerminatedInBuffer (container model .Container ) {
1129
+ func (m * Model ) markContainerLogsTerminatedInBuffer (container container .Container ) {
1126
1130
for i := range m .pageLogBuffer {
1127
1131
if m .pageLogBuffer [i ].Log .Container .Equals (container ) {
1128
1132
m .pageLogBuffer [i ].Terminated = true
0 commit comments