Kubectl plugin to run curl commands against kubernetes pods

Overview

kubectl-curl

Kubectl plugin to run curl commands against kubernetes pods

Motivation

Sending http requests to kubernetes pods is unnecessarily complicated, this plugin makes it easy.

The plugin creates a port forwarding from the local network to the kubernetes pod that was selected to receive the request, then instantiate a curl command by rewriting the URL to connect to the forwarded local port, and passing all curl options that were given on the command line.

Installation

If $GOPATH/bin is in the PATH, the plugin can be installed with:

$ go install github.com/segmentio/[email protected]

If it was installed properly, it will be visibile when listing kubectl plugins:

$ kubectl plugin list
The following compatible plugins are available:

/.../kubectl-curl

Usage

kubectl curl [options] URL [container]
  • In the URL, the host part must be the name of the pod to send the request to.
  • If no port number is specified, the request will be sent to a http port.
  • If there are multiple containers with a http port, the name of the container to send to the request to must be specified after the URL.

Examples

This section records common use cases for this kubectl plugin.

Collecting profiles of Go programs

$ kubectl curl "http://{pod}/debug/pprof/profile?debug=1&seconds=10" > ./profile
$ go tool pprof -http :6060 ./profile

Retrieving prometheus metrics

$ kubectl curl http://{pod}/metrics
...
Issues
  • Fix curl options and allow multiple of the same option

    Fix curl options and allow multiple of the same option

    The current way option arguments are parsed and passed to curl is broken. Options are currently passed to curl like --arg=value when curl wants them as --arg value. This was addressed in issue https://github.com/segmentio/kubectl-curl/issues/6 and this PR fixes that and more.

    The crux of this PR was to get this example working:

    kubectl curl --debug -iv -H "Content-Type: application/json" -H "X-Auth-User: bob" http://pod-555464c988-2jb6r:3000/internal/health

    The fix to get the curl options in the correct form was to pass the argument name and value separately to cmd := exec.CommandContext(ctx, "curl", cArgs...). But if you then specify two arguments with the same name and different values like in the example above with multiple headers, only the last header option is passed to curl.

    I couldn't see a straightforward way to do this with the current spf13/pflag implementation, and since the -d/--data option and likely many others support specifying multiple options with curl, I got a little creative. This implementation still validates that the options passed are valid options, but when multiple are present for curl, it just passes those onto curl to let curl handle the validation, like if multiple options are present when they shouldn't be.

    I don't see any unit tests or a simple way to make some. I tested variations of my use cases above, but not anything on the k8s options side eventhough they should be unaffected, so give it a try.

    opened by bartleyg 2
  • cannot load io/fs: malformed module path

    cannot load io/fs: malformed module path "io/fs": missing dot in first path element

    ubuntu 20.04 go version go1.13.8 linux/amd64

    go get github.com/segmentio/[email protected] build github.com/segmentio/kubectl-curl: cannot load io/fs: malformed module path "io/fs": missing dot in first path element

    opened by wajika 1
  • Cannot use -X argument

    Cannot use -X argument

    While trying to use -X POST, the plugin complains such argument is invalid (set URL and CONTAINER to some valid values). I have seen a similar behavior while using -d.

    $ kubectl curl -i -X POST $URL $CONTAINER
    curl: option --request=POST: is unknown
    curl: try 'curl --help' or 'curl --manual' for more information
    

    Reproduced in both macOS and Ubuntu 20.04. Details below.

    MacOS environment:

    $ curl --version
    curl 7.77.0 (x86_64-apple-darwin21.0) libcurl/7.77.0 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.42.0
    Release-Date: 2021-05-26
    Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
    Features: alt-svc AsynchDNS GSS-API HSTS HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets
    
    $ kubectl version --client
    Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T21:51:49Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"darwin/amd64"}
    
    $ sw_vers
    ProductName:	macOS
    ProductVersion:	12.0.1
    BuildVersion:	21A559
    

    Linux environment:

    $ curl --version
    curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3
    Release-Date: 2020-01-08
    Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
    Features: AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets
    
    $ kubectl version --client
    Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.1", GitCommit:"86ec240af8cbd1b60bcc4c03c20da9b98005b92e", GitTreeState:"clean", BuildDate:"2021-12-16T11:41:01Z", GoVersion:"go1.17.5", Compiler:"gc", Platform:"linux/amd64"}
    
    $ lsb_release -a
    LSB Version:	core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
    Distributor ID:	Ubuntu
    Description:	Ubuntu 20.04.3 LTS
    Release:	20.04
    Codename:	focal
    
    opened by rkperes 1
  • fix bool options

    fix bool options

    This PR fixes the use of boolean options, which were forced to have a value before.

    The issue came from the pflag package not detecting boolean values via implementation of interface{ IsBoolFlag() bool } like the standard flag package does, and instead of relying on setting the NoOptDefVal being assigned.

    I also improved a few usage error messages.

    opened by achille-roussel 0
  • BUG: invalid character

    BUG: invalid character "{" in host name

    I installed kubectl-curl from the README and ran into an error.

    Install:

    $ go install github.com/segmentio/[email protected] 
    go: downloading github.com/segmentio/kubectl-curl v0.1.5
    

    Error:

    $ kubectl curl http://{pod}/metrics
    * ERROR: malformed URL: parse "http://{pod}/metrics": invalid character "{" in host name
    
    opened by adamdecaf 0
  • using with go-teleport fails on first char missing

    using with go-teleport fails on first char missing

    Hi! We use go-teleport installed at say tele.host.com. Using kubectl curl I get this:

    m ~/D/  kubectl curl -vvv --debug "http://{podname}:3000/healthz"
    * 2022/05/15 21:46:39 kubectl get -n default pod/{podname}
    * 2022/05/15 21:46:41 forwarding local port 14187 to port 3000 of
    * 2022/05/15 21:46:41 waiting for port fowarding to be established
    * 2022/05/15 21:46:41 error upgrading connection: error sending request: Post "https://ele.host.com:3026/api/v1/namespaces/default/pods/zenith-console-7c64d658bd-mqvdv/portforward": dial tcp: lookup ele.host.com: no such host
    ^C* 2022/05/15 21:47:00 shutting down port forwarder
    * 2022/05/15 21:47:00 waiting for port forwarder to stop
    

    and it hangs forever. There is obviously no ele.host.com. Cause it's tele.host.com. Can't say for sure, this is not a go-teleport bug, sorry.

    opened by antonyc 0
  • Distribute kubectl plugin via Krew

    Distribute kubectl plugin via Krew

    Would it be possible to distribute this plugin via Krew? I believe this would help with the discovery of this plugin & simplifying the installation process 🙂

    enhancement 
    opened by tohjustin 2
Releases(v0.1.5)
Owner
Segment
Segment
Kubectl plugin to ease sniffing on kubernetes pods using tcpdump and wireshark

ksniff A kubectl plugin that utilize tcpdump and Wireshark to start a remote capture on any pod in your Kubernetes cluster. You get the full power of

Eldad Rudich 2.3k Jun 30, 2022
A kubectl plugin to evict pods

kubectl-evict A kubectl plugin to evict pods. This plugin is good to remove a pod from your cluster or to test your PodDistruptionBudget. ?? Installat

Shin'ya Ueoka 10 Jun 27, 2022
A Kubernetes CSI plugin to automatically mount SPIFFE certificates to Pods using ephemeral volumes

csi-driver-spiffe csi-driver-spiffe is a Container Storage Interface (CSI) driver plugin for Kubernetes to work along cert-manager. This CSI driver tr

null 24 Jun 23, 2022
kubectl-fzf provides a fast and powerful fzf autocompletion for kubectl

Kubectl-fzf kubectl-fzf provides a fast and powerful fzf autocompletion for kubectl. Table of Contents Kubectl-fzf Table of Contents Features Requirem

null 1 Nov 3, 2021
Kubectl golang - kubectl krew template repo

kubectl krew template repo There's a lot of scaffolding needed to set up a good

geodis 0 Jan 11, 2022
kubectl plugin for signing Kubernetes manifest YAML files with sigstore

k8s-manifest-sigstore kubectl plugin for signing Kubernetes manifest YAML files with sigstore ⚠️ Still under developement, not ready for production us

sigstore 29 Jun 18, 2022
kubectl plugin for generating nginx-ingress compatible basic-auth secrets on kubernetes clusters

kubectl-htpasswd kubectl plugin for easily generating hashed basic auth secrets. Supported hash algorithms bcrypt Examples Create the secret on the cl

Christian Rebischke 14 Jun 16, 2022
A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

A very simple, silly little kubectl plugin / utility that guesses which language an application running in a kubernetes pod was written in.

Tom Granot 2 Mar 9, 2022
KinK is a helper CLI that facilitates to manage KinD clusters as Kubernetes pods. Designed to ease clusters up for fast testing with batteries included in mind.

kink A helper CLI that facilitates to manage KinD clusters as Kubernetes pods. Table of Contents kink (KinD in Kubernetes) Introduction How it works ?

Trendyol Open Source 353 Jul 3, 2022
gpu-memory-monitor is a metrics server for collecting GPU memory usage of kubernetes pods.

gpu-memory-monitor is a metrics server for collecting GPU memory usage of kubernetes pods. If you have a GPU machine, and some pods are using the GPU device, you can run the container by docker or kubernetes when your GPU device belongs to nvidia. The gpu-memory-monitor will collect the GPU memory usage of pods, you can get those metrics by API of gpu-memory-monitor

null 2 May 17, 2022
Viewnode displays Kubernetes cluster nodes with their pods and containers.

viewnode The viewnode shows Kubernetes cluster nodes with their pods and containers. It is very useful when you need to monitor multiple resources suc

NTTDATA-DACH 8 May 5, 2022
A kubectl plugin for finding decoded secret data with productive search flags.

kubectl-secret-data What is it? This is a kubectl plugin for finding decoded secret data. Since kubectl only outputs base64-encoded secrets, it makes

Keisuke Umegaki 34 Jun 28, 2022
A 'kubectl' plugin for interacting with Clusternet.

kubectl-clusternet A kubectl plugin for interacting with Clusternet. Installation Install With Krew kubectl-clusternet can be installed using Krew, pl

Clusternet 11 May 27, 2022
A kubectl plugin for finding decoded secret data with productive search flags.

kubectl-secret-data What is it? This is a kubectl plugin for finding decoded secret data. Since kubectl outputs base64-encoded secrets basically, it m

Keisuke Umegaki 34 Jun 28, 2022
A kubectl plugin for getting endoflife information about your cluster.

kubectl-endoflife A kubectl plugin that checks your clusters for component compatibility and Kubernetes version end of life. This plugin is meant to a

Ross Edman 3 Oct 10, 2021
🦥 kubectl plugin to easy to view pod

kubectl-lazy Install curl -sSL https://mirror.ghproxy.com/https://raw.githubusercontent.com/togettoyou/kubectl-lazy/main/install.sh | bash Or you can

寻寻觅觅的Gopher 7 Jun 20, 2022
A kubectl plugin to query multiple namespace at the same time.

kubemulti A kubectl plugin to query multiple namespace at the same time. $ kubemulti get pods -n cdi -n default NAMESPACE NAME

R0CKSTAR 3 Mar 1, 2022
Kubectl plugin shows pod x node matrix with suitable colors to mitigate troubleshooting effort.

kubectl-pod-node-matrix WORK IN PROGRESS!! This plugin shows pod x node matrix with suitable colors to mitigate troubleshooting effort. Details Troubl

Arda Güçlü 2 May 11, 2022
A kubectl plugin for easier query and operate k8s cluster.

kube-query A kubectl plug-in that makes it easier to query and manipulate K8S clusters. (what is kubectl plug-in ?) Kube-query support some resource s

Shadow-L 14 Jun 9, 2022