Skip to content

Commit ef2c7b4

Browse files
authored
[system] Add system queue list watch flag (#156)
[system] Add system queue list watch flag
2 parents ceb2c42 + e13678b commit ef2c7b4

File tree

7 files changed

+80
-5
lines changed

7 files changed

+80
-5
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ toolchain go1.24.4
77
require (
88
github.com/Masterminds/semver/v3 v3.3.0
99
github.com/deckhouse/virtualization/src/cli v0.0.0-20250617111832-70fdc2799bf3
10+
github.com/fatih/color v1.17.0
1011
github.com/google/go-containerregistry v0.20.0
1112
github.com/gosimple/slug v1.15.0
1213
github.com/hashicorp/go-cleanhttp v0.5.2
@@ -107,6 +108,7 @@ require (
107108
github.com/aws/aws-sdk-go-v2/service/sts v1.33.9 // indirect
108109
github.com/aws/smithy-go v1.22.1 // indirect
109110
github.com/axiomhq/hyperloglog v0.0.0-20220105174342-98591331716a // indirect
111+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
110112
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
111113
github.com/beorn7/perks v1.0.1 // indirect
112114
github.com/bgentry/speakeasy v0.1.0 // indirect
@@ -186,7 +188,6 @@ require (
186188
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
187189
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
188190
github.com/fatih/camelcase v1.0.0 // indirect
189-
github.com/fatih/color v1.17.0 // indirect
190191
github.com/fatih/structs v1.1.0 // indirect
191192
github.com/felixge/httpsnoop v1.0.4 // indirect
192193
github.com/fluxcd/flagger v1.36.1 // indirect
@@ -374,6 +375,7 @@ require (
374375
github.com/linode/linodego v0.7.1 // indirect
375376
github.com/lithammer/dedent v1.1.0 // indirect
376377
github.com/looplab/fsm v1.0.2 // indirect
378+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
377379
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
378380
github.com/mailru/easyjson v0.7.7 // indirect
379381
github.com/manifoldco/promptui v0.9.0 // indirect
@@ -416,6 +418,7 @@ require (
416418
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
417419
github.com/montanaflynn/stats v0.7.0 // indirect
418420
github.com/morikuni/aec v1.0.0 // indirect
421+
github.com/muesli/termenv v0.16.0 // indirect
419422
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
420423
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
421424
github.com/natefinch/atomic v1.0.1 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
226226
github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
227227
github.com/axiomhq/hyperloglog v0.0.0-20220105174342-98591331716a h1:eqjiAL3qooftPm8b9C1GsSSRcmlw7iOva8vdBTmV2PY=
228228
github.com/axiomhq/hyperloglog v0.0.0-20220105174342-98591331716a/go.mod h1:2stgcRjl6QmW+gU2h5E7BQXg4HU0gzxKWDuT5HviN9s=
229+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
230+
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
229231
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
230232
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
231233
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
@@ -1190,6 +1192,8 @@ github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffkt
11901192
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
11911193
github.com/looplab/fsm v1.0.2 h1:f0kdMzr4CRpXtaKKRUxwLYJ7PirTdwrtNumeLN+mDx8=
11921194
github.com/looplab/fsm v1.0.2/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
1195+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
1196+
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
11931197
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
11941198
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
11951199
github.com/magiconair/properties v1.5.3/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@@ -1315,6 +1319,8 @@ github.com/montanaflynn/stats v0.7.0 h1:r3y12KyNxj/Sb/iOE46ws+3mS1+MZca1wlHQFPsY
13151319
github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
13161320
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
13171321
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
1322+
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
1323+
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
13181324
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
13191325
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
13201326
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=

internal/system/cmd/queue/flags/validation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ func ValidateParameters(cmd *cobra.Command, args []string) error {
3131
if _, valid := allowedOutput[outputFormat]; !valid {
3232
return fmt.Errorf("Please provide valid output: text, yaml, json. Got '%s', try --help\n", outputFormat)
3333
}
34+
35+
watch, _ := cmd.Flags().GetBool("watch")
36+
if watch && outputFormat != "text" {
37+
return fmt.Errorf("Watch mode is only supported with text output format. Current format: %s", outputFormat)
38+
}
39+
3440
return nil
3541
}

internal/system/cmd/queue/list/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ func AddFlags(flagSet *pflag.FlagSet) {
2727
false,
2828
"Show empty queues.",
2929
)
30+
flagSet.BoolP(
31+
"watch",
32+
"w",
33+
false,
34+
"Watch for changes to the queue list.",
35+
)
3036
}

internal/system/cmd/queue/list/list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func listModule(cmd *cobra.Command, _ []string) error {
6868
return fmt.Errorf("Failed to show empty queues from flag: %w", err)
6969
}
7070

71+
watch, err := cmd.Flags().GetBool("watch")
72+
if err != nil {
73+
return fmt.Errorf("Failed to get watch flag: %w", err)
74+
}
75+
7176
format, err := cmd.Flags().GetString("output")
7277
if err != nil {
7378
return fmt.Errorf("Failed to get output format: %w", err)
@@ -78,7 +83,7 @@ func listModule(cmd *cobra.Command, _ []string) error {
7883
pathFromOption = pathFromOption + "?showEmpty=true"
7984
}
8085

81-
err = operatequeue.OperateQueue(config, kubeCl, pathFromOption)
86+
err = operatequeue.OperateQueue(config, kubeCl, pathFromOption, watch)
8287
if err != nil {
8388
return fmt.Errorf("Error list queues: %w", err)
8489
}

internal/system/cmd/queue/mainqueue/mainqueue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func mainQueue(cmd *cobra.Command, _ []string) error {
7070

7171
pathFromOption := "main." + format
7272

73-
err = operatequeue.OperateQueue(config, kubeCl, pathFromOption)
73+
err = operatequeue.OperateQueue(config, kubeCl, pathFromOption, false)
7474
if err != nil {
7575
return fmt.Errorf("Error list main queue: %w", err)
7676
}

internal/system/cmd/queue/operatequeue/operatequeue.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
712
"github.com/deckhouse/deckhouse-cli/internal/utilk8s"
13+
"github.com/muesli/termenv"
814
"k8s.io/client-go/kubernetes"
915
"k8s.io/client-go/rest"
1016
"k8s.io/client-go/tools/remotecommand"
1117
)
1218

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 {
1428
const (
1529
apiProtocol = "http"
1630
apiEndpoint = "127.0.0.1"
@@ -25,7 +39,13 @@ func OperateQueue(config *rest.Config, kubeCl *kubernetes.Clientset, pathFromOpt
2539
fullEndpointUrl := fmt.Sprintf("%s://%s:%s/%s/%s", apiProtocol, apiEndpoint, apiPort, queuePath, pathFromOption)
2640
getApi := []string{"curl", fullEndpointUrl}
2741
podName, err := utilk8s.GetDeckhousePod(kubeCl)
42+
if err != nil {
43+
return err
44+
}
2845
executor, err := utilk8s.ExecInPod(config, kubeCl, getApi, podName, namespace, containerName)
46+
if err != nil {
47+
return err
48+
}
2949

3050
var stdout bytes.Buffer
3151
var stderr bytes.Buffer
@@ -39,5 +59,34 @@ func OperateQueue(config *rest.Config, kubeCl *kubernetes.Clientset, pathFromOpt
3959
}
4060

4161
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("\nWatch 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+
}
4392
}

0 commit comments

Comments
 (0)