Skip to content

Commit

Permalink
run kubectl cmds in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
zmalik committed Mar 21, 2021
1 parent 27c27f3 commit 5b1dc7e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/kubectl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"strings"
"sync"
)

//Opts are Options for the contexts configuration
Expand All @@ -24,24 +25,28 @@ func RunCommand(args []string) {
log.Fatal("error loading contexts from kubeconfig", err)
}

var wg sync.WaitGroup
for _, ctx := range ctxs {

if !contextsFromFile.ContextExists(ctx) {
fmt.Printf("Skipping context %s as cannot be found in KUBECONFIG file\n", ctx)
continue
}
runCommand(ctx, args)
wg.Add(1)
go runCommand(ctx, args, &wg)
}
wg.Wait()

}

func runCommand(ctx string, args []string) {
func runCommand(ctx string, args []string, wg *sync.WaitGroup) {
defer wg.Done()
tmpFile, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("kconfig-%s", ctx))
if err != nil {
log.Fatal("Cannot create temporary file", err)
}
defer os.Remove(tmpFile.Name())

fmt.Println("context : " + ctx)
execCmd("kubectl", args, getKubeconfigPath(), ctx, false)
}

Expand All @@ -58,7 +63,7 @@ func execCmd(execCmd string, args []string, configPath string, ctx string, silen
output, err := krun.CombinedOutput()

if !silent {
fmt.Println(string(output))
fmt.Println(fmt.Sprintf("cluster:%s\n%s", ctx, string(output)))
}
if err != nil {
fmt.Println(string(output))
Expand Down

0 comments on commit 5b1dc7e

Please sign in to comment.