From 52c29bfc5e575b86377448ee2fb75faac7d453e0 Mon Sep 17 00:00:00 2001 From: Daniel Henkel Date: Wed, 21 Oct 2020 08:57:41 +0200 Subject: [PATCH] implement POC for having a second executable 'kubectl-dsh' --- .github/workflows/release.yml | 19 ------ .goreleaser.yml | 17 ++++++ .pre-commit-config.yaml | 4 +- cmd/kubectl_dsh/main.go | 96 +++++++++++++++++++++++++++++++ main.go => cmd/pod_helper/main.go | 2 +- 5 files changed, 117 insertions(+), 21 deletions(-) create mode 100644 cmd/kubectl_dsh/main.go rename main.go => cmd/pod_helper/main.go (99%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d5f2a86..4afe0db 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,28 +6,9 @@ on: - '*' jobs: - style: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.15 - - uses: actions/setup-python@v2 - - name: Install required GO libs - run: | - go mod download - go get github.com/fzipp/gocyclo/cmd/gocyclo - - uses: pre-commit/action@v2.0.0 release: runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/') - needs: - - style steps: - name: Checkout uses: actions/checkout@v2 diff --git a/.goreleaser.yml b/.goreleaser.yml index b633913..f343abe 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,5 +1,6 @@ # This is an example goreleaser.yaml file with some sane defaults. # Make sure to check the documentation at http://goreleaser.com +project_name: pod-helper before: hooks: # You may remove this if you don't use go modules. @@ -7,6 +8,18 @@ before: builds: - env: - CGO_ENABLED=0 + main: ./cmd/pod_helper/main.go + id: pod-helper + binary: pod-helper + goos: + - linux + - windows + - darwin + - env: + - CGO_ENABLED=0 + main: ./cmd/kubectl_dsh/main.go + id: kubectl-dsh + binary: kubectl-dsh goos: - linux - windows @@ -18,6 +31,10 @@ archives: windows: Windows 386: i386 amd64: x86_64 + + builds: + - pod-helper + - kubectl-dsh checksum: name_template: 'checksums.txt' snapshot: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 61b6209..9c6dc13 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,6 +11,8 @@ repos: hooks: - id: go-fmt - id: go-imports - - id: go-vet +# todo: hook does not support multiple binaries in one repo +# https://github.com/dnephin/pre-commit-golang/issues/30 +# - id: go-vet # - id: go-lint - id: go-cyclo diff --git a/cmd/kubectl_dsh/main.go b/cmd/kubectl_dsh/main.go new file mode 100644 index 0000000..9d32d1a --- /dev/null +++ b/cmd/kubectl_dsh/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "sort" + + "github.com/dhenkel92/pod-helper/src/config" + + "github.com/dhenkel92/pod-helper/src/executor" + + "k8s.io/client-go/util/homedir" + + "github.com/dhenkel92/pod-helper/src/log" + "github.com/urfave/cli/v2" +) + +var ( + version = "dev" + commit = "unknown" + date = "unknown" +) + +func main() { + app := &cli.App{ + Name: "kubernetes-dsh", + Usage: "A tool to easily operate on mutliple pods at the same time.", + Version: fmt.Sprintf("%s, built on %s (%s)", version, date, commit), + Action: func(c *cli.Context) error { + conf := config.NewConfigFromCliContext(c) + return executor.Execute(&conf, executor.RunStrategy) + }, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "kubeconfig", + Aliases: []string{"config"}, + Usage: "path to the kubeconfig file that will be used to authenticate to your cluster.", + Value: filepath.Join(homedir.HomeDir(), ".kube", "config"), + }, + &cli.StringSliceFlag{ + Name: "namespace", + Aliases: []string{"n"}, + Usage: "the namespaces that are used for discovering the pods. If none is set it will use all of them.", + Required: false, + Value: cli.NewStringSlice(""), + }, + &cli.StringSliceFlag{ + Name: "labels", + Usage: "set of labels which are used to filter the pods.", + Aliases: []string{"l"}, + Value: &cli.StringSlice{}, + Required: false, + }, + &cli.Int64Flag{ + Name: "container-index", + Usage: "many pods do have more than one container, but often you don't know the specific container name or you want to execute the command always on the first one. With this flag you can define the index (beginning at 0) which should be used to get the container.", + Aliases: []string{"ci"}, + Value: -1, + }, + &cli.StringFlag{ + Name: "container", + Usage: "define a container name which should be searched for within the pod. If the pod doesn't have a container with the given name, it will return an error.", + Aliases: []string{"con"}, + Value: "", + }, + &cli.IntFlag{ + Name: "batch-size", + Usage: "specify on how many pods the given command is executed in parallel. This is also used for getting the logs from multiple containers.", + Aliases: []string{"batch", "b"}, + Value: 5, + }, + &cli.StringFlag{ + Name: "entrypoint", + Aliases: []string{"e", "entry"}, + Required: false, + Value: "/bin/sh -c", + Usage: "by default every command will be executed in the /bin/sh shell. If you want to use a different one (e.g. /bin/bash) you can set it here.", + }, + &cli.StringFlag{ + Name: "command", + Aliases: []string{"c"}, + Required: true, + Usage: "the command that should be executed. If it contains a whitespace, it should be quoted.", + }, + }, + } + + sort.Sort(cli.FlagsByName(app.Flags)) + sort.Sort(cli.CommandsByName(app.Commands)) + + err := app.Run(os.Args) + if err != nil { + log.Error.Fatal(err) + } +} diff --git a/main.go b/cmd/pod_helper/main.go similarity index 99% rename from main.go rename to cmd/pod_helper/main.go index 74349bd..9c58a4f 100644 --- a/main.go +++ b/cmd/pod_helper/main.go @@ -24,7 +24,7 @@ var ( func main() { app := &cli.App{ - Name: "pod-exec", + Name: "pod-helper", Usage: "A tool to easily operate on mutliple pods at the same time.", Version: fmt.Sprintf("%s, built on %s (%s)", version, date, commit), Flags: []cli.Flag{