Files
troubleshoot/cmd/collect/cli/http.go
Salah Al Saleh 2a87b280bf Add per-collector subcommands to the collect binary (#2071)
* Add per-collector subcommands to the collect binary

Add `collect http|postgres|mysql|mssql|redis` subcommands that run a single
collector and print its native result JSON to stdout, so a collector can run
inside a Pod via the troubleshoot image (e.g. as a runPod collector).

Each has its own flags mapping to the collector's spec fields and its own help.
The Kubernetes client (used only for TLS-from-Secret) is built to match the
production preflight/support-bundle path (QPS/Burst/UserAgent).

* Add clickhouse subcommand to the collect binary

Add `collect clickhouse`, matching the other per-collector subcommands, so the
clickhouse collector can run inside a Pod via the troubleshoot image (e.g. as a
runPod collector). Same flags as the other database collectors (--uri + --tls-*).

* Make global flags work on the collect subcommands

Register --debug and the Kubernetes flags (--kubeconfig, --context, etc.) as
persistent flags, and move the shared setup to PersistentPreRun/PersistentPostRun,
so the per-collector subcommands inherit the global flags and run the same
logger/profiling setup as `collect [url]`. Previously these were local flags with
setup in PreRun, so subcommands rejected --debug/--kubeconfig and ignored the setup.
2026-07-10 09:31:14 -07:00

80 lines
2.8 KiB
Go

package cli
import (
"fmt"
"strings"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/spf13/cobra"
)
// HTTPCmd runs the http collector against a single endpoint and prints the
// native result JSON ({"response":{"status":...}} or {"error":{...}}).
func HTTPCmd() *cobra.Command {
var (
method string
url string
headers map[string]string
body string
timeout string
proxy string
insecure bool
caCert string
)
cmd := &cobra.Command{
Use: "http",
Short: "Run the http collector against an endpoint",
Long: `Run the http collector: issue an HTTP request and print the collector result as JSON.
The result contains the response status, body and headers, or an error object.
Examples:
collect http --url http://myapp.default.svc:8080/healthz
collect http --method POST --url https://api.internal/ping \
--header 'Content-Type=application/json' --body '{"ping":true}'`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
var tls *troubleshootv1beta2.TLSParams
if caCert != "" {
tls = &troubleshootv1beta2.TLSParams{CACert: caCert}
}
spec := &troubleshootv1beta2.HTTP{
CollectorMeta: troubleshootv1beta2.CollectorMeta{CollectorName: "http"},
}
switch strings.ToUpper(method) {
case "GET":
spec.Get = &troubleshootv1beta2.Get{URL: url, Headers: headers, Timeout: timeout, Proxy: proxy, InsecureSkipVerify: insecure, TLS: tls}
case "POST":
spec.Post = &troubleshootv1beta2.Post{URL: url, Headers: headers, Body: body, Timeout: timeout, Proxy: proxy, InsecureSkipVerify: insecure, TLS: tls}
case "PUT":
spec.Put = &troubleshootv1beta2.Put{URL: url, Headers: headers, Body: body, Timeout: timeout, Proxy: proxy, InsecureSkipVerify: insecure, TLS: tls}
default:
return fmt.Errorf("unsupported --method %q (use GET, POST, or PUT)", method)
}
c := &collect.CollectHTTP{Collector: spec}
res, err := c.Collect(nil)
if err != nil {
return err
}
return printCollectorResult(res)
},
}
f := cmd.Flags()
f.StringVar(&url, "url", "", "request URL (required)")
cmd.MarkFlagRequired("url")
f.StringVar(&method, "method", "GET", "HTTP method: GET, POST, or PUT")
f.StringToStringVar(&headers, "header", nil, "request header as key=value (repeatable)")
f.StringVar(&body, "body", "", "request body (POST/PUT only)")
f.StringVar(&timeout, "timeout", "", "request timeout, e.g. 15s (empty = no timeout)")
f.StringVar(&proxy, "proxy", "", "proxy URL to use for the request")
f.BoolVar(&insecure, "insecure-skip-verify", false, "do not verify the server's TLS certificate")
f.StringVar(&caCert, "tls-cacert", "", "CA certificate to trust (PEM contents or a file path)")
return cmd
}