@@ -4,13 +4,27 @@ import (
4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
+ "os"
8
+ "os/signal"
9
+ "syscall"
10
+ "time"
11
+
7
12
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
13
+ "github.com/muesli/termenv"
8
14
"k8s.io/client-go/kubernetes"
9
15
"k8s.io/client-go/rest"
10
16
"k8s.io/client-go/tools/remotecommand"
11
17
)
12
18
13
- func OperateQueue (config * rest.Config , kubeCl * kubernetes.Clientset , pathFromOption string ) error {
19
+ func OperateQueue (config * rest.Config , kubeCl * kubernetes.Clientset , pathFromOption string , watch bool ) error {
20
+ if ! watch {
21
+ return executeQueueCommand (config , kubeCl , pathFromOption )
22
+ }
23
+
24
+ return watchQueueCommand (config , kubeCl , pathFromOption )
25
+ }
26
+
27
+ func executeQueueCommand (config * rest.Config , kubeCl * kubernetes.Clientset , pathFromOption string ) error {
14
28
const (
15
29
apiProtocol = "http"
16
30
apiEndpoint = "127.0.0.1"
@@ -25,7 +39,13 @@ func OperateQueue(config *rest.Config, kubeCl *kubernetes.Clientset, pathFromOpt
25
39
fullEndpointUrl := fmt .Sprintf ("%s://%s:%s/%s/%s" , apiProtocol , apiEndpoint , apiPort , queuePath , pathFromOption )
26
40
getApi := []string {"curl" , fullEndpointUrl }
27
41
podName , err := utilk8s .GetDeckhousePod (kubeCl )
42
+ if err != nil {
43
+ return err
44
+ }
28
45
executor , err := utilk8s .ExecInPod (config , kubeCl , getApi , podName , namespace , containerName )
46
+ if err != nil {
47
+ return err
48
+ }
29
49
30
50
var stdout bytes.Buffer
31
51
var stderr bytes.Buffer
@@ -39,5 +59,34 @@ func OperateQueue(config *rest.Config, kubeCl *kubernetes.Clientset, pathFromOpt
39
59
}
40
60
41
61
fmt .Printf ("%s\n " , stdout .String ())
42
- return err
62
+ return nil
63
+ }
64
+
65
+ func watchQueueCommand (config * rest.Config , kubeCl * kubernetes.Clientset , pathFromOption string ) error {
66
+ signals := make (chan os.Signal , 1 )
67
+ signal .Notify (signals , syscall .SIGINT , syscall .SIGTERM )
68
+
69
+ ticker := time .NewTicker (1 * time .Second )
70
+ defer ticker .Stop ()
71
+
72
+ output := termenv .DefaultOutput ()
73
+
74
+ fmt .Println ("Watching queue (press Ctrl+C to stop)..." )
75
+
76
+ for {
77
+ select {
78
+ case <- signals :
79
+ fmt .Println ("\n Watch stopped." )
80
+ return nil
81
+ case <- ticker .C :
82
+ output .ClearScreen ()
83
+ output .MoveCursor (1 , 1 )
84
+ fmt .Printf ("Watching queue - %s\n \n " , time .Now ().Format ("15:04:05" ))
85
+
86
+ err := executeQueueCommand (config , kubeCl , pathFromOption )
87
+ if err != nil {
88
+ fmt .Printf ("Error fetching queue: %v\n " , err )
89
+ }
90
+ }
91
+ }
43
92
}
0 commit comments