66 "os"
77 "os/exec"
88 "path/filepath"
9+ "time"
910
10- k3dutil "github.com/k3d-io/k3d/v5/cmd/util"
1111 "github.com/sirupsen/logrus"
1212)
1313
@@ -16,87 +16,73 @@ const (
1616)
1717
1818// EnableExplorer will start kube-explorer with random port for specified K3s cluster
19- func EnableExplorer (ctx context.Context , config string ) ( int , error ) {
19+ func EnableExplorer (ctx context.Context , config string ) error {
2020 if _ , ok := ExplorerWatchers [config ]; ok {
21- return 0 , fmt .Errorf ("kube-explorer for cluster %s has already started" , config )
21+ return fmt .Errorf ("kube-explorer for cluster %s has already started" , config )
2222 }
2323 if err := CheckCommandExist (KubeExplorerCommand ); err != nil {
24- return 0 , err
24+ return err
2525 }
2626
2727 // command execution validate
2828 if err := checkExplorerCmd (); err != nil {
29- return 0 , err
29+ return err
3030 }
3131
3232 // save config for kube-explorer
3333 exp , err := DefaultDB .GetExplorer (config )
3434 if err != nil {
35- return 0 , err
35+ return err
3636 }
3737
3838 if exp == nil || ! exp .Enabled {
39- var port int
40- if exp == nil {
41- port , err = k3dutil .GetFreePort ()
42- if err != nil {
43- return 0 , err
44- }
45- } else {
46- port = exp .Port
47- }
4839 exp = & Explorer {
4940 ContextName : config ,
50- Port : port ,
5141 Enabled : true ,
5242 }
5343 if err = DefaultDB .SaveExplorer (exp ); err != nil {
54- return 0 , err
44+ return err
5545 }
5646 }
5747
5848 // start kube-explorer
5949 explorerCtx , cancel := context .WithCancel (ctx )
6050 ExplorerWatchers [config ] = cancel
61- go func (ctx context.Context , config string , port int ) {
62- _ = StartKubeExplorer (ctx , config , port )
63- }(explorerCtx , config , exp .Port )
64- return exp .Port , nil
51+
52+ if _ , err := StartKubeExplorer (explorerCtx , config ); err != nil {
53+ return err
54+ }
55+
56+ return nil
6557}
6658
6759// DisableExplorer will stop kube-explorer server for specified K3s cluster
6860func DisableExplorer (config string ) error {
69- if _ , ok := ExplorerWatchers [config ]; ! ok {
61+ cancelFunc , ok := ExplorerWatchers [config ]
62+ if ! ok {
7063 return fmt .Errorf ("cann't disable unactive kube-explorer for cluster %s" , config )
7164 }
65+
7266 // update kube-explorer settings
7367 exp , err := DefaultDB .GetExplorer (config )
7468 if err != nil {
7569 return err
7670 }
71+ if exp != nil && exp .Enabled {
72+ // stop kube-explorer
73+ cancelFunc ()
74+ delete (ExplorerWatchers , config )
75+ }
7776 if exp == nil || exp .Enabled {
78- var port int
79- if exp == nil {
80- port , err = k3dutil .GetFreePort ()
81- if err != nil {
82- return err
83- }
84- } else {
85- port = exp .Port
86- }
8777 err = DefaultDB .SaveExplorer (& Explorer {
8878 ContextName : config ,
89- Port : port ,
9079 Enabled : false ,
9180 })
9281 if err != nil {
9382 return err
9483 }
9584 }
9685
97- // stop kube-explorer
98- ExplorerWatchers [config ]()
99- delete (ExplorerWatchers , config )
10086 return nil
10187}
10288
@@ -110,26 +96,34 @@ func InitExplorer(ctx context.Context) {
11096 for _ , exp := range expList {
11197 if exp .Enabled {
11298 logrus .Infof ("start kube-explorer for cluster %s" , exp .ContextName )
113- go func (ctx context.Context , name string ) {
114- if _ , err = EnableExplorer (ctx , name ); err != nil {
115- logrus .Errorf ("failed to start kube-explorer for cluster %s: %v" , name , err )
116- }
117- }(ctx , exp .ContextName )
99+ if err = EnableExplorer (ctx , exp .ContextName ); err != nil {
100+ logrus .Errorf ("failed to start kube-explorer for cluster %s: %v" , exp .ContextName , err )
101+ }
118102 }
119103 }
120104}
121105
122106// StartKubeExplorer start kube-explorer server listen on specified port
123- func StartKubeExplorer (ctx context.Context , config string , port int ) error {
107+ func StartKubeExplorer (ctx context.Context , clusterID string ) (chan int , error ) {
108+ socketName := GetSocketName (clusterID )
124109 explorer := exec .CommandContext (ctx , KubeExplorerCommand , fmt .Sprintf ("--kubeconfig=%s" , filepath .Join (CfgPath , KubeCfgFile )),
125- fmt .Sprintf ("--context=%s" , config ), fmt .Sprintf ("--http-listen-port=%d " , port ), "--https-listen-port=0" )
110+ fmt .Sprintf ("--context=%s" , clusterID ), fmt .Sprintf ("--bind-address=%s " , socketName ) )
126111 explorer .Stdout = os .Stdout
127112 explorer .Stderr = os .Stderr
113+ explorer .Cancel = func () error {
114+ return explorer .Process .Signal (os .Interrupt )
115+ }
116+ explorer .WaitDelay = 10 * time .Second
128117 if err := explorer .Start (); err != nil {
129- logrus .Errorf ("fail to start kube-explorer for cluster %s: %v" , config , err )
118+ logrus .Errorf ("fail to start kube-explorer for cluster %s: %v" , clusterID , err )
130119 }
131- logrus .Infof ("kube-explorer for %s K3s cluster will listen on 127.0.0.1:%d ..." , config , port )
132- return explorer .Wait ()
120+ logrus .Infof ("kube-explorer for %s K3s cluster will listen on %s ..." , clusterID , socketName )
121+ stopChan := make (chan int )
122+ go func () {
123+ _ = explorer .Wait ()
124+ close (stopChan )
125+ }()
126+ return stopChan , nil
133127}
134128
135129func CheckCommandExist (cmd string ) error {
0 commit comments