|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/kubetidy/kubetidy/internal/manifest" |
| 14 | + "github.com/kubetidy/kubetidy/internal/model" |
| 15 | +) |
| 16 | + |
| 17 | +type costFlags struct { |
| 18 | + base string |
| 19 | + head string |
| 20 | + cpuCoreMonth float64 |
| 21 | + memGiBMonth float64 |
| 22 | + failOver float64 |
| 23 | + failOverSet bool |
| 24 | + output string |
| 25 | +} |
| 26 | + |
| 27 | +func newCostCommand() *cobra.Command { |
| 28 | + f := &costFlags{} |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "cost [manifests...]", |
| 31 | + Short: "Estimate the $/mo of resource requests in manifests; diff a PR's before/after", |
| 32 | + Long: "cost prices the CPU/memory requests in Kubernetes manifests (no cluster needed) and, " + |
| 33 | + "given --base and --head, reports the monthly $ a change adds or saves — the CI cost-guardrail " + |
| 34 | + "(\"this PR adds $400/mo\"). Use --fail-over to fail CI when the net increase exceeds a budget.", |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + f.failOverSet = cmd.Flags().Changed("fail-over") |
| 37 | + return runCost(cmd, f, args) |
| 38 | + }, |
| 39 | + } |
| 40 | + flags := cmd.Flags() |
| 41 | + flags.StringVar(&f.base, "base", "", "base manifest file or dir (the 'before' — e.g. main branch)") |
| 42 | + flags.StringVar(&f.head, "head", "", "head manifest file or dir (the 'after' — e.g. the PR). Defaults to positional args") |
| 43 | + flags.Float64Var(&f.cpuCoreMonth, "cpu-cost", 0, "override $ per CPU core-month (0 = default)") |
| 44 | + flags.Float64Var(&f.memGiBMonth, "mem-cost", 0, "override $ per GiB-month (0 = default)") |
| 45 | + flags.Float64Var(&f.failOver, "fail-over", 0, "exit non-zero if the net monthly increase exceeds this $ amount") |
| 46 | + flags.StringVarP(&f.output, "output", "o", "table", "output format: table|json") |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +func runCost(cmd *cobra.Command, f *costFlags, args []string) error { |
| 51 | + price := manifest.DefaultPrice(f.cpuCoreMonth, f.memGiBMonth) |
| 52 | + |
| 53 | + // Head = --head, or positional args (so `kubetidy cost deploy.yaml` works). |
| 54 | + headPaths := args |
| 55 | + if f.head != "" { |
| 56 | + headPaths = append([]string{f.head}, args...) |
| 57 | + } |
| 58 | + if len(headPaths) == 0 { |
| 59 | + return fmt.Errorf("cost: provide manifests as arguments or via --head") |
| 60 | + } |
| 61 | + headWls, err := loadManifestPaths(headPaths) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + headCost := manifest.CostWorkloads(headWls, price) |
| 66 | + |
| 67 | + var baseCost []manifest.WorkloadCost |
| 68 | + if f.base != "" { |
| 69 | + baseWls, err := loadManifestPaths([]string{f.base}) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + baseCost = manifest.CostWorkloads(baseWls, price) |
| 74 | + } |
| 75 | + |
| 76 | + report := manifest.Compare(baseCost, headCost) |
| 77 | + if err := renderCost(cmd.OutOrStdout(), report, f.base != "", f.output); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + // CI guardrail: fail when the net increase exceeds the budget. |
| 82 | + if f.failOverSet && report.NetDelta > f.failOver { |
| 83 | + return fmt.Errorf("cost guardrail: net increase %s/mo exceeds budget %s/mo", |
| 84 | + signedDollars(report.NetDelta), signedDollars(f.failOver)) |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// loadManifestPaths reads every .yaml/.yml/.json under the given files/dirs and parses workloads. |
| 90 | +func loadManifestPaths(paths []string) ([]model.Workload, error) { |
| 91 | + var all []model.Workload |
| 92 | + for _, p := range paths { |
| 93 | + info, err := os.Stat(p) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("cost: %w", err) |
| 96 | + } |
| 97 | + var files []string |
| 98 | + if info.IsDir() { |
| 99 | + entries, err := os.ReadDir(p) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("cost: reading dir %s: %w", p, err) |
| 102 | + } |
| 103 | + for _, e := range entries { |
| 104 | + if !e.IsDir() && isManifestFile(e.Name()) { |
| 105 | + files = append(files, filepath.Join(p, e.Name())) |
| 106 | + } |
| 107 | + } |
| 108 | + } else { |
| 109 | + files = []string{p} |
| 110 | + } |
| 111 | + for _, file := range files { |
| 112 | + b, err := os.ReadFile(file) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("cost: reading %s: %w", file, err) |
| 115 | + } |
| 116 | + wls, err := manifest.ParseWorkloadsBytes(b) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("cost: parsing %s: %w", file, err) |
| 119 | + } |
| 120 | + all = append(all, wls...) |
| 121 | + } |
| 122 | + } |
| 123 | + return all, nil |
| 124 | +} |
| 125 | + |
| 126 | +func isManifestFile(name string) bool { |
| 127 | + switch strings.ToLower(filepath.Ext(name)) { |
| 128 | + case ".yaml", ".yml", ".json": |
| 129 | + return true |
| 130 | + default: |
| 131 | + return false |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func renderCost(w io.Writer, rep manifest.CostReport, diff bool, output string) error { |
| 136 | + if output == "json" { |
| 137 | + enc := json.NewEncoder(w) |
| 138 | + enc.SetIndent("", " ") |
| 139 | + return enc.Encode(rep) |
| 140 | + } |
| 141 | + if output != "" && output != "table" { |
| 142 | + return fmt.Errorf("unknown output format %q (want table|json)", output) |
| 143 | + } |
| 144 | + |
| 145 | + var b strings.Builder |
| 146 | + if !diff { |
| 147 | + // Single set: just the total monthly cost. |
| 148 | + fmt.Fprintf(&b, "kubetidy cost\n\n Total: %s/mo (resource requests)\n\n", signedDollars(rep.AfterTotal)) |
| 149 | + for _, c := range rep.Changes { |
| 150 | + fmt.Fprintf(&b, " %-44s %s/mo\n", c.Ref, signedDollars(c.After)) |
| 151 | + } |
| 152 | + _, err := io.WriteString(w, b.String()) |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + verb := "adds" |
| 157 | + if rep.NetDelta < 0 { |
| 158 | + verb = "saves" |
| 159 | + } |
| 160 | + headline := fmt.Sprintf("this change %s %s/mo", verb, signedDollars(abs64f(rep.NetDelta))) |
| 161 | + if rep.NetDelta == 0 { |
| 162 | + headline = "no monthly cost change" |
| 163 | + } |
| 164 | + fmt.Fprintf(&b, "kubetidy cost · base → head\n\n %s (%s/mo → %s/mo)\n\n", |
| 165 | + headline, signedDollars(rep.BeforeTotal), signedDollars(rep.AfterTotal)) |
| 166 | + |
| 167 | + fmt.Fprintf(&b, " %-40s %10s %10s %10s\n", "WORKLOAD", "BEFORE", "AFTER", "DELTA") |
| 168 | + for _, c := range rep.Changes { |
| 169 | + if c.Status == manifest.Unchanged { |
| 170 | + continue |
| 171 | + } |
| 172 | + fmt.Fprintf(&b, " %-40s %10s %10s %10s (%s)\n", |
| 173 | + truncate(c.Ref, 40), dollarsOrDash(c.Before, c.Status == manifest.Added), |
| 174 | + dollarsOrDash(c.After, c.Status == manifest.Removed), signedDelta(c.Delta), c.Status) |
| 175 | + } |
| 176 | + _, err := io.WriteString(w, b.String()) |
| 177 | + return err |
| 178 | +} |
| 179 | + |
| 180 | +func dollarsOrDash(v float64, dash bool) string { |
| 181 | + if dash { |
| 182 | + return "—" |
| 183 | + } |
| 184 | + return signedDollars(v) |
| 185 | +} |
| 186 | + |
| 187 | +func signedDelta(v float64) string { |
| 188 | + if v > 0 { |
| 189 | + return "+" + signedDollars(v) |
| 190 | + } |
| 191 | + if v < 0 { |
| 192 | + return "-" + signedDollars(abs64f(v)) |
| 193 | + } |
| 194 | + return "$0" |
| 195 | +} |
| 196 | + |
| 197 | +func abs64f(v float64) float64 { |
| 198 | + if v < 0 { |
| 199 | + return -v |
| 200 | + } |
| 201 | + return v |
| 202 | +} |
| 203 | + |
| 204 | +func truncate(s string, n int) string { |
| 205 | + if len(s) <= n { |
| 206 | + return s |
| 207 | + } |
| 208 | + return s[:n-1] + "…" |
| 209 | +} |
0 commit comments