diff --git a/pkg/deploy/deploy.go b/pkg/deploy/deploy.go index d4fc39c..af2e650 100644 --- a/pkg/deploy/deploy.go +++ b/pkg/deploy/deploy.go @@ -9,18 +9,12 @@ func Run(envName string, dryRun bool) error { return err } - //////////////////////////////////// // PREFLIGHT - //////////////////////////////////// - if err := Preflight(environment); err != nil { return err } - //////////////////////////////////// // ENSURE NAMESPACE - //////////////////////////////////// - if err := EnsureNamespace(environment); err != nil { return err } @@ -40,17 +34,18 @@ func Run(envName string, dryRun bool) error { return err } - //////////////////////////////////// - // SKIP ROLLOUT FOR DRY RUN - //////////////////////////////////// - if dryRun { return nil } - //////////////////////////////////// // MONITOR ROLLOUT - //////////////////////////////////// + if err := MonitorRollout(environment); err != nil { + + // fetch diagnostics on failure + FetchDiagnostics(environment) + + return err + } - return MonitorRollout(environment) + return nil } diff --git a/pkg/deploy/diagnostics.go b/pkg/deploy/diagnostics.go new file mode 100644 index 0000000..8a45011 --- /dev/null +++ b/pkg/deploy/diagnostics.go @@ -0,0 +1,87 @@ +package deploy + +import ( + "fmt" + "os/exec" + "strings" + + "github.com/aryansharma9917/codewise-cli/pkg/env" +) + +func getPods(namespace string, context string) ([]string, error) { + + args := []string{ + "get", + "pods", + "-n", + namespace, + "-o", + "name", + } + + if context != "" { + args = append(args, "--context", context) + } + + cmd := exec.Command("kubectl", args...) + + out, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("failed to list pods") + } + + lines := strings.Split(strings.TrimSpace(string(out)), "\n") + + var pods []string + for _, line := range lines { + if strings.TrimSpace(line) != "" { + pods = append(pods, line) + } + } + + return pods, nil +} + +func describePod(pod string, namespace string, context string) { + + fmt.Printf("\nDiagnostics for %s:\n", pod) + + args := []string{ + "describe", + pod, + "-n", + namespace, + } + + if context != "" { + args = append(args, "--context", context) + } + + cmd := exec.Command("kubectl", args...) + out, err := cmd.CombinedOutput() + + if err != nil { + fmt.Println("failed to describe pod") + return + } + + fmt.Println(string(out)) +} + +func FetchDiagnostics(environment *env.Env) { + + ns := environment.K8s.Namespace + ctx := environment.K8s.Context + + fmt.Println("\nFetching failure diagnostics...") + + pods, err := getPods(ns, ctx) + if err != nil || len(pods) == 0 { + fmt.Println("No pods found for diagnostics.") + return + } + + for _, pod := range pods { + describePod(pod, ns, ctx) + } +}