|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/ppiankov/deployscope/internal/k8s" |
| 11 | +) |
| 12 | + |
| 13 | +type DoctorOutput struct { |
| 14 | + K8sConnectivity string `json:"k8s_connectivity"` |
| 15 | + TotalWorkloads int `json:"total_workloads"` |
| 16 | + IgnoredWorkloads int `json:"ignored_workloads"` |
| 17 | + Coverage AnnotationCoverage `json:"annotation_coverage"` |
| 18 | + AgentReadiness float64 `json:"agent_readiness"` |
| 19 | + Warnings []string `json:"warnings,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +type AnnotationCoverage struct { |
| 23 | + Owner float64 `json:"owner"` |
| 24 | + Tier float64 `json:"tier"` |
| 25 | + GitOpsRepo float64 `json:"gitops_repo"` |
| 26 | + Oncall float64 `json:"oncall"` |
| 27 | + Runbook float64 `json:"runbook"` |
| 28 | + DependsOn float64 `json:"depends_on"` |
| 29 | +} |
| 30 | + |
| 31 | +func newDoctorCmd() *cobra.Command { |
| 32 | + var format string |
| 33 | + |
| 34 | + cmd := &cobra.Command{ |
| 35 | + Use: "doctor", |
| 36 | + Short: "Check K8s connectivity, RBAC, and annotation coverage", |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + k8sClient, err := k8s.NewClient() |
| 39 | + if err != nil { |
| 40 | + output := DoctorOutput{ |
| 41 | + K8sConnectivity: fmt.Sprintf("error: %v", err), |
| 42 | + } |
| 43 | + if format == "json" { |
| 44 | + enc := json.NewEncoder(os.Stdout) |
| 45 | + enc.SetIndent("", " ") |
| 46 | + _ = enc.Encode(output) |
| 47 | + } else { |
| 48 | + fmt.Printf("K8s connectivity: FAIL (%v)\n", err) |
| 49 | + } |
| 50 | + os.Exit(1) |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + if err := k8sClient.CheckReady(cmd.Context()); err != nil { |
| 55 | + output := DoctorOutput{ |
| 56 | + K8sConnectivity: fmt.Sprintf("error: %v", err), |
| 57 | + } |
| 58 | + if format == "json" { |
| 59 | + enc := json.NewEncoder(os.Stdout) |
| 60 | + enc.SetIndent("", " ") |
| 61 | + _ = enc.Encode(output) |
| 62 | + } else { |
| 63 | + fmt.Printf("K8s connectivity: FAIL (%v)\n", err) |
| 64 | + } |
| 65 | + os.Exit(1) |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + services, _, err := k8sClient.FetchDeployments(cmd.Context()) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to fetch workloads: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + total := len(services) |
| 75 | + coverage := computeCoverage(services) |
| 76 | + readiness := (coverage.Owner + coverage.Tier + coverage.GitOpsRepo + coverage.Oncall) / 4.0 |
| 77 | + |
| 78 | + var warnings []string |
| 79 | + if coverage.Owner < 0.5 { |
| 80 | + warnings = append(warnings, "less than 50% of workloads have owner annotations") |
| 81 | + } |
| 82 | + if coverage.Tier < 0.5 { |
| 83 | + warnings = append(warnings, "less than 50% of workloads have tier annotations — routing will default to standard") |
| 84 | + } |
| 85 | + if coverage.GitOpsRepo < 0.3 { |
| 86 | + warnings = append(warnings, "less than 30% of workloads have gitops-repo — agents cannot create PRs") |
| 87 | + } |
| 88 | + |
| 89 | + output := DoctorOutput{ |
| 90 | + K8sConnectivity: "ok", |
| 91 | + TotalWorkloads: total, |
| 92 | + Coverage: coverage, |
| 93 | + AgentReadiness: readiness, |
| 94 | + Warnings: warnings, |
| 95 | + } |
| 96 | + |
| 97 | + if format == "json" { |
| 98 | + enc := json.NewEncoder(os.Stdout) |
| 99 | + enc.SetIndent("", " ") |
| 100 | + return enc.Encode(output) |
| 101 | + } |
| 102 | + |
| 103 | + fmt.Printf("K8s connectivity: OK\n") |
| 104 | + fmt.Printf("Total workloads: %d\n", total) |
| 105 | + fmt.Printf("Agent readiness: %.0f%%\n\n", readiness*100) |
| 106 | + fmt.Printf("Annotation coverage:\n") |
| 107 | + fmt.Printf(" owner: %.0f%%\n", coverage.Owner*100) |
| 108 | + fmt.Printf(" tier: %.0f%%\n", coverage.Tier*100) |
| 109 | + fmt.Printf(" gitops-repo: %.0f%%\n", coverage.GitOpsRepo*100) |
| 110 | + fmt.Printf(" oncall: %.0f%%\n", coverage.Oncall*100) |
| 111 | + fmt.Printf(" runbook: %.0f%%\n", coverage.Runbook*100) |
| 112 | + fmt.Printf(" depends-on: %.0f%%\n", coverage.DependsOn*100) |
| 113 | + |
| 114 | + if len(warnings) > 0 { |
| 115 | + fmt.Println("\nWarnings:") |
| 116 | + for _, w := range warnings { |
| 117 | + fmt.Printf(" ⚠ %s\n", w) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + cmd.Flags().StringVar(&format, "format", "text", "Output format: text, json") |
| 126 | + return cmd |
| 127 | +} |
| 128 | + |
| 129 | +func computeCoverage(services []k8s.ServiceStatus) AnnotationCoverage { |
| 130 | + total := float64(len(services)) |
| 131 | + if total == 0 { |
| 132 | + return AnnotationCoverage{} |
| 133 | + } |
| 134 | + |
| 135 | + var owner, tier, gitops, oncall, runbook, depends float64 |
| 136 | + for _, svc := range services { |
| 137 | + if svc.Owner != nil { |
| 138 | + owner++ |
| 139 | + } |
| 140 | + if svc.Tier != nil { |
| 141 | + tier++ |
| 142 | + } |
| 143 | + if svc.Integration.GitOpsRepo != nil { |
| 144 | + gitops++ |
| 145 | + } |
| 146 | + if svc.Integration.Oncall != nil { |
| 147 | + oncall++ |
| 148 | + } |
| 149 | + if svc.Integration.Runbook != nil { |
| 150 | + runbook++ |
| 151 | + } |
| 152 | + if len(svc.DependsOn) > 0 { |
| 153 | + depends++ |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return AnnotationCoverage{ |
| 158 | + Owner: owner / total, |
| 159 | + Tier: tier / total, |
| 160 | + GitOpsRepo: gitops / total, |
| 161 | + Oncall: oncall / total, |
| 162 | + Runbook: runbook / total, |
| 163 | + DependsOn: depends / total, |
| 164 | + } |
| 165 | +} |
0 commit comments