Giter Club home page Giter Club logo

kubectl-foreach's Introduction

kubectl foreach

Run a kubectl command in one or more contexts (clusters) in parallel (similar to GNU parallel/xargs).

Usage

Usage:
    kubectl foreach [OPTIONS] [PATTERN]... -- [KUBECTL_ARGS...]

Patterns can be used to match context names from kubeconfig:
      (empty): matches all contexts
         NAME: matches context with exact name
    /PATTERN/: matches context with regular expression
        ^NAME: remove context with exact name from the matched results
   ^/PATTERN/: remove contexts matching the regular expression from the results
    
Options:
    -c=NUM     Limit parallel executions (default: 0, unlimited)
    -I=VAL     Replace VAL occurring in KUBECTL_ARGS with context name
    -q         Disable and accept confirmation prompts ($KUBECTL_FOREACH_DISABLE_PROMPTS) 
    -h/--help  Print help

Demo

Query a pod by label in minikube and *-prod* contexts:

$ kubectl foreach /-prod/ minikube -- get pods -n kube-system --selector compute.twitter.com/app=coredns --no-headers

     eu-prod | coredns-59bd9867bb-6rbx7   2/2     Running   0          78d
     eu-prod | coredns-59bd9867bb-9xczh   2/2     Running   0          78d
     eu-prod | coredns-59bd9867bb-fvn6t   2/2     Running   0          78d
    minikube | No resources found in kube-system namespace.
 useast-prod | coredns-6fd4bd9db4-7w9wv   2/2     Running   0          78d
 useast-prod | coredns-6fd4bd9db4-9pk8n   2/2     Running   0          78d
 useast-prod | coredns-6fd4bd9db4-xphr4   2/2     Running   0          78d
 uswest-prod | coredns-6f987df9bc-6fgc2   2/2     Running   0          78d
 uswest-prod | coredns-6f987df9bc-9gxvt   2/2     Running   0          78d
 uswest-prod | coredns-6f987df9bc-d88jk   2/2     Running   0          78d

Examples

Match to contexts by name: Run a command ("kubectl version") on contexts c1, c2 and c3:

kubectl foreach c1 c2 c3 -- version

Match to contexts by pattern: Run a command on contexts starting with gke (regular expression syntax):

kubectl foreach /^gke/ -- get pods

Match all contexts: empty context matches all contexts.

kubectl foreach -- version

Excluding contexts: Use the matching syntaxes with a ^ prefix to use them for exclusion. If no matching contexts are specified.

e.g. match all contexts except c1 and except those ending with prod (single quotes for escaping $ in the shell):

kubectl foreach ^c1 ^/prod'$'/ -- version

Using with kubectl plugins: Customize how context name is passed to the command (useful for kubectl plugins as --context must be specified after plugin name).

In this example, _ is replaced with the context name when calling "kubectl my_plugin".

kubectl foreach -I _ -- my_plugin -ctx=_

Limit parallelization: Only run 3 commands at a time:

kubectl foreach -c 3 /^gke-/

Install

Use Krew kubectl plugin manager:

kubectl krew install foreach

You can also build from source but you won't receive new version updates:

go install github.com/ahmetb/kubectl-foreach@latest

Remarks

Do not use this tool programmatically yet:

This tool is not intended for deploying workloads to clusters, or using programmatically. Therefore, it does not provide a structured output format or ordered printing that is meant to be parsed by or piped to other programs (maybe except for grep).

kubectl-foreach's People

Contributors

ahmetb avatar pehlicd avatar rajatjindal avatar semihbkgr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

kubectl-foreach's Issues

swallowing output for non \n terminated lines

e.g.

...  -- get nodes -o=jsonpath='{.items[*].status.capacity.cpu}'`

doesn't print output that ends with a newline character, and nothing gets printed by the tool as a result.

The fix for this might be rather complicated, as prefixingWriter flushes on \n and currently there's no way to signal prefixingWriter to Flush(). (see Test_prefixingWriter https://github.com/ahmetb/kubectl-foreach/blob/4ac672dadc834895e723af04880d537df6b55af6/output_test.go#L43C6-L66).

[Idea] allow non-kubectl commands with -I

This can allow calling non-kubectl tools like

kubectl allctx -I _ [...] -- clusterctl --context=_ [...]

but it would complicate the UX notably. Maybe something like an -0 flag that indicates user must specify a command (and we'd print the command to be executed):

kubectl allctx -0 -I x -- kind delete clusters x

error on 'go install'

when try to install

go install github.com/ahmetb/kubectl-allctx@latest

the output is

go: downloading github.com/ahmetb/kubectl-allctx v0.0.0-20220708173706-f2e07edffc5b
go: github.com/ahmetb/kubectl-allctx@latest: github.com/ahmetb/[email protected]: parsing go.mod:
	module declares its path as: kubectl-allctx
	        but was required as: github.com/ahmetb/kubectl-allctx

detect and fail when replacement character (-I) is not used

The other day I ran:

kubectl foreach -I _ [...bunch of filters...] -- get pods 

but I didn't realize I was getting the same result from all clusters (because I didn't use _ anywhere, and -I caused --context= from not being passed to the kubectl cmd).

Fail if nothing is replaced among args when used with -I and tell the user they specified -I sequence but never used it.

[Idea] Generate structured output

If the cmd args contain -o=csv|yaml|json, maybe the tool can reuse that (or accept its own) to generate results to add context name and reformat the output.

[Idea] Being able to find clusters/contexts that have a matching k8s resource

So I forked this and added a feature that my team has been wanting, which is a command that allows you to find a context that contains a K8s resource we're looking for. This often comes up during oncall issues where a service or deployment is acting strange but we don't know where it's located.

Ex. kubectl foreach -- find svc foo -n bar

This would return any and all contexts that contain this resource. Not sure if this is something you'd be interested in for upstream or find useful, but figured I'll bring it up.

[Bug] do not pass stdin

Prevent os.Exec() from inheriting the stdin so that commands like kubectl edit/exec fail, instead of hanging.

output table misalignment

im getting pods from multi clusters. result is looking like. is there bug or needed options or smth?

image

Some `Fprintf` statements aren't getting logged

I noticed that on my Apple M1, some of the Fprintf statements aren't getting properly logged and I'm not sure why. FWIW, I tested it in a Linux container and it worked fine. And when I changed the code to log using Fprintln, it worked fine.

For some reason Fprintf and Fprint don't log on certain statements unless a \n is added.

I've only really noticed this with the Continue? [Y/n] statement, but might be happening with other ones that I haven't noticed. I'm guessing it might be due to my M1. This happens on both Terminal and iTerm.

Adding a screen recording to demonstrate what I mean: https://recordit.co/H3j8XTSUMo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.