Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
87 changes: 87 additions & 0 deletions pkg/deploy/diagnostics.go
Original file line number Diff line number Diff line change
@@ -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)
}
}