@@ -3,47 +3,94 @@ package deploy
33import (
44 "fmt"
55 "os/exec"
6+ "strings"
67 "time"
78
89 "github.com/aryansharma9917/codewise-cli/pkg/env"
910)
1011
11- func MonitorRollout ( environment * env. Env ) error {
12+ func getDeployments ( namespace string , context string ) ([] string , error ) {
1213
13- release := environment .Helm .Release
14- namespace := environment .K8s .Namespace
15- context := environment .K8s .Context
14+ args := []string {
15+ "get" ,
16+ "deployments" ,
17+ "-n" ,
18+ namespace ,
19+ "-o" ,
20+ "name" ,
21+ }
1622
17- fmt .Println ("Waiting for deployment rollout..." )
23+ if context != "" {
24+ args = append (args , "--context" , context )
25+ }
26+
27+ cmd := exec .Command ("kubectl" , args ... )
28+ out , err := cmd .Output ()
29+ if err != nil {
30+ return nil , fmt .Errorf ("failed to list deployments" )
31+ }
32+
33+ lines := strings .Split (strings .TrimSpace (string (out )), "\n " )
1834
19- // We assume Helm creates a Deployment with the release name.
20- // This is standard Helm behavior unless overridden.
35+ var deployments []string
36+ for _ , line := range lines {
37+ if strings .TrimSpace (line ) != "" {
38+ deployments = append (deployments , line )
39+ }
40+ }
41+
42+ return deployments , nil
43+ }
44+
45+ func waitForDeployment (deployment string , namespace string , context string ) error {
2146
2247 args := []string {
2348 "rollout" ,
2449 "status" ,
25- " deployment/" + release ,
50+ deployment ,
2651 "-n" ,
2752 namespace ,
53+ "--timeout=120s" ,
2854 }
2955
3056 if context != "" {
3157 args = append (args , "--context" , context )
3258 }
3359
3460 cmd := exec .Command ("kubectl" , args ... )
35-
36- // Stream output live
3761 cmd .Stdout = nil
3862 cmd .Stderr = nil
3963
40- err := cmd .Run ()
64+ return cmd .Run ()
65+ }
66+
67+ func MonitorRollout (environment * env.Env ) error {
68+
69+ ns := environment .K8s .Namespace
70+ ctx := environment .K8s .Context
71+
72+ fmt .Println ("Waiting for deployment rollout..." )
73+
74+ deployments , err := getDeployments (ns , ctx )
4175 if err != nil {
42- return fmt .Errorf ("deployment failed to roll out" )
76+ return err
77+ }
78+
79+ if len (deployments ) == 0 {
80+ fmt .Println ("No deployments found. Skipping rollout monitoring." )
81+ return nil
4382 }
4483
45- time .Sleep (1 * time .Second )
84+ for _ , deploy := range deployments {
85+ fmt .Printf ("Monitoring %s...\n " , deploy )
86+
87+ if err := waitForDeployment (deploy , ns , ctx ); err != nil {
88+ return fmt .Errorf ("deployment %s failed to roll out" , deploy )
89+ }
90+
91+ time .Sleep (500 * time .Millisecond )
92+ }
4693
47- fmt .Println ("Deployment successfully rolled out." )
94+ fmt .Println ("All deployments successfully rolled out." )
4895 return nil
4996}
0 commit comments