|
| 1 | +// kubectl-claw is a kubectl plugin for k8s4claw operations. |
| 2 | +// |
| 3 | +// Subcommands: |
| 4 | +// |
| 5 | +// approve <escalation> Mark a ClawOpsEscalation as Approved. |
| 6 | +// reject <escalation> Mark a ClawOpsEscalation as Rejected. |
| 7 | +// |
| 8 | +// Install: place this binary on $PATH as `kubectl-claw`. Then run |
| 9 | +// `kubectl claw approve <name>`. |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "flag" |
| 15 | + "fmt" |
| 16 | + "os" |
| 17 | + |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + "k8s.io/client-go/kubernetes/scheme" |
| 20 | + ctrl "sigs.k8s.io/controller-runtime" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | + |
| 23 | + v1alpha1 "github.com/Prismer-AI/k8s4claw/api/v1alpha1" |
| 24 | +) |
| 25 | + |
| 26 | +const usage = `kubectl-claw — operator-side approval CLI for k8s4claw |
| 27 | +
|
| 28 | +Usage: |
| 29 | + kubectl claw approve <escalation> [-n namespace] [--by user@example.com] |
| 30 | + kubectl claw reject <escalation> [-n namespace] [--reason "..."] |
| 31 | +
|
| 32 | +Examples: |
| 33 | + kubectl claw approve my-claw-ops-abc -n ai-agents --by sre@corp.com |
| 34 | + kubectl claw reject my-claw-ops-xyz -n default --reason "manual rollback already done" |
| 35 | +` |
| 36 | + |
| 37 | +func main() { |
| 38 | + if len(os.Args) < 2 { |
| 39 | + fmt.Fprint(os.Stderr, usage) |
| 40 | + os.Exit(2) |
| 41 | + } |
| 42 | + |
| 43 | + cmd := os.Args[1] |
| 44 | + args := os.Args[2:] |
| 45 | + |
| 46 | + switch cmd { |
| 47 | + case "approve": |
| 48 | + os.Exit(runApprove(args)) |
| 49 | + case "reject": |
| 50 | + os.Exit(runReject(args)) |
| 51 | + case "-h", "--help", "help": |
| 52 | + fmt.Print(usage) |
| 53 | + default: |
| 54 | + fmt.Fprintf(os.Stderr, "unknown subcommand: %q\n\n", cmd) |
| 55 | + fmt.Fprint(os.Stderr, usage) |
| 56 | + os.Exit(2) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// commonFlags returns the parsed name + namespace + extra arg. |
| 61 | +func commonFlags(args []string, extraName string) (name, namespace, extra string, err error) { |
| 62 | + fs := flag.NewFlagSet(extraName, flag.ContinueOnError) |
| 63 | + fs.StringVar(&namespace, "n", "default", "namespace") |
| 64 | + fs.StringVar(&namespace, "namespace", "default", "namespace") |
| 65 | + fs.StringVar(&extra, extraName, "", extraName+" annotation") |
| 66 | + if err := fs.Parse(args); err != nil { |
| 67 | + return "", "", "", err |
| 68 | + } |
| 69 | + if fs.NArg() < 1 { |
| 70 | + return "", "", "", fmt.Errorf("missing escalation name") |
| 71 | + } |
| 72 | + return fs.Arg(0), namespace, extra, nil |
| 73 | +} |
| 74 | + |
| 75 | +func newClient() (client.Client, error) { |
| 76 | + cfg, err := ctrl.GetConfig() |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to load kubeconfig: %w", err) |
| 79 | + } |
| 80 | + if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil { |
| 81 | + return nil, fmt.Errorf("failed to register scheme: %w", err) |
| 82 | + } |
| 83 | + return client.New(cfg, client.Options{Scheme: scheme.Scheme}) |
| 84 | +} |
| 85 | + |
| 86 | +func runApprove(args []string) int { |
| 87 | + name, ns, by, err := commonFlags(args, "by") |
| 88 | + if err != nil { |
| 89 | + fmt.Fprintln(os.Stderr, err) |
| 90 | + return 2 |
| 91 | + } |
| 92 | + if by == "" { |
| 93 | + // Default to whoami-style identity from KUBECONFIG context. |
| 94 | + by = currentUser() |
| 95 | + } |
| 96 | + |
| 97 | + c, err := newClient() |
| 98 | + if err != nil { |
| 99 | + fmt.Fprintln(os.Stderr, err) |
| 100 | + return 1 |
| 101 | + } |
| 102 | + ctx := context.Background() |
| 103 | + |
| 104 | + var esc v1alpha1.ClawOpsEscalation |
| 105 | + if err := c.Get(ctx, client.ObjectKey{Name: name, Namespace: ns}, &esc); err != nil { |
| 106 | + fmt.Fprintf(os.Stderr, "failed to get escalation %s/%s: %v\n", ns, name, err) |
| 107 | + return 1 |
| 108 | + } |
| 109 | + |
| 110 | + if esc.Status.Phase != v1alpha1.EscalationPhaseAwaitingApproval { |
| 111 | + fmt.Fprintf(os.Stderr, "escalation %s is in phase %q (must be %q to approve)\n", |
| 112 | + name, esc.Status.Phase, v1alpha1.EscalationPhaseAwaitingApproval) |
| 113 | + return 1 |
| 114 | + } |
| 115 | + if esc.Status.ProposedAction == "" { |
| 116 | + fmt.Fprintf(os.Stderr, "escalation %s has empty proposedAction — nothing to approve\n", name) |
| 117 | + return 1 |
| 118 | + } |
| 119 | + |
| 120 | + now := metav1.Now() |
| 121 | + esc.Status.Phase = v1alpha1.EscalationPhaseApproved |
| 122 | + esc.Status.ApprovedBy = by |
| 123 | + esc.Status.ApprovedAt = &now |
| 124 | + |
| 125 | + if err := c.Status().Update(ctx, &esc); err != nil { |
| 126 | + fmt.Fprintf(os.Stderr, "failed to update status: %v\n", err) |
| 127 | + return 1 |
| 128 | + } |
| 129 | + fmt.Printf("approved %s/%s by %s\nproposedAction will be executed by ClawOpsController\n", ns, name, by) |
| 130 | + return 0 |
| 131 | +} |
| 132 | + |
| 133 | +func runReject(args []string) int { |
| 134 | + name, ns, reason, err := commonFlags(args, "reason") |
| 135 | + if err != nil { |
| 136 | + fmt.Fprintln(os.Stderr, err) |
| 137 | + return 2 |
| 138 | + } |
| 139 | + if reason == "" { |
| 140 | + reason = "rejected via kubectl-claw" |
| 141 | + } |
| 142 | + |
| 143 | + c, err := newClient() |
| 144 | + if err != nil { |
| 145 | + fmt.Fprintln(os.Stderr, err) |
| 146 | + return 1 |
| 147 | + } |
| 148 | + ctx := context.Background() |
| 149 | + |
| 150 | + var esc v1alpha1.ClawOpsEscalation |
| 151 | + if err := c.Get(ctx, client.ObjectKey{Name: name, Namespace: ns}, &esc); err != nil { |
| 152 | + fmt.Fprintf(os.Stderr, "failed to get escalation %s/%s: %v\n", ns, name, err) |
| 153 | + return 1 |
| 154 | + } |
| 155 | + |
| 156 | + if v1alpha1.IsTerminalPhase(esc.Status.Phase) { |
| 157 | + fmt.Fprintf(os.Stderr, "escalation %s is already terminal (phase=%q)\n", name, esc.Status.Phase) |
| 158 | + return 1 |
| 159 | + } |
| 160 | + |
| 161 | + esc.Status.Phase = v1alpha1.EscalationPhaseRejected |
| 162 | + esc.Status.RejectionReason = reason |
| 163 | + |
| 164 | + if err := c.Status().Update(ctx, &esc); err != nil { |
| 165 | + fmt.Fprintf(os.Stderr, "failed to update status: %v\n", err) |
| 166 | + return 1 |
| 167 | + } |
| 168 | + fmt.Printf("rejected %s/%s: %s\n", ns, name, reason) |
| 169 | + return 0 |
| 170 | +} |
| 171 | + |
| 172 | +// currentUser returns a best-effort identity string for the approval audit trail. |
| 173 | +// Falls back to USER env var, then "unknown". |
| 174 | +func currentUser() string { |
| 175 | + if u := os.Getenv("USER"); u != "" { |
| 176 | + return u |
| 177 | + } |
| 178 | + return "unknown" |
| 179 | +} |
0 commit comments