Skip to content

Commit 2982cab

Browse files
Add preflight checks to validate cluster connectivity before deployment
1 parent 0afe719 commit 2982cab

2 files changed

Lines changed: 72 additions & 17 deletions

File tree

pkg/deploy/deploy.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
package deploy
22

3-
import (
4-
"fmt"
5-
"os/exec"
6-
)
7-
8-
func checkDependency(name string) error {
9-
10-
_, err := exec.LookPath(name)
11-
if err != nil {
12-
return fmt.Errorf("%s not found in PATH. please install it to continue", name)
13-
}
14-
15-
return nil
16-
}
3+
import "fmt"
174

185
func Run(envName string, dryRun bool) error {
196

@@ -22,18 +9,24 @@ func Run(envName string, dryRun bool) error {
229
return err
2310
}
2411

25-
command, _, err := BuildCommand(environment)
26-
if err != nil {
12+
////////////////////////////////////////////////////
13+
// PREFLIGHT FIRST
14+
////////////////////////////////////////////////////
15+
16+
if err := Preflight(environment); err != nil {
2717
return err
2818
}
2919

30-
if err := checkDependency(command.Name); err != nil {
20+
command, _, err := BuildCommand(environment)
21+
if err != nil {
3122
return err
3223
}
3324

3425
executor := Executor{
3526
DryRun: dryRun,
3627
}
3728

29+
fmt.Println("Starting deployment...")
30+
3831
return executor.Run(command.Name, command.Args...)
3932
}

pkg/deploy/preflight.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package deploy
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
7+
"github.com/aryansharma9917/codewise-cli/pkg/env"
8+
)
9+
10+
func runCheck(name string, args ...string) error {
11+
12+
cmd := exec.Command(name, args...)
13+
14+
if err := cmd.Run(); err != nil {
15+
return err
16+
}
17+
18+
return nil
19+
}
20+
21+
func Preflight(environment *env.Env) error {
22+
23+
fmt.Println("Running preflight checks...")
24+
25+
strategy := ResolveStrategy(environment)
26+
27+
////////////////////////////////////////////////////
28+
// Check binary availability
29+
////////////////////////////////////////////////////
30+
31+
switch strategy {
32+
33+
case StrategyHelm:
34+
35+
if err := runCheck("helm", "version"); err != nil {
36+
return fmt.Errorf("helm not available or not functioning")
37+
}
38+
39+
case StrategyKubectl:
40+
41+
if err := runCheck("kubectl", "version", "--client"); err != nil {
42+
return fmt.Errorf("kubectl not available or not functioning")
43+
}
44+
}
45+
46+
////////////////////////////////////////////////////
47+
// Cluster connectivity check
48+
////////////////////////////////////////////////////
49+
50+
args := []string{"cluster-info"}
51+
52+
if environment.K8s.Context != "" {
53+
args = append(args, "--context", environment.K8s.Context)
54+
}
55+
56+
if err := runCheck("kubectl", args...); err != nil {
57+
return fmt.Errorf("cannot reach kubernetes cluster. check kube-context")
58+
}
59+
60+
fmt.Println("Cluster reachable")
61+
return nil
62+
}

0 commit comments

Comments
 (0)