Skip to content

Commit

Permalink
implement POC for having a second executable 'kubectl-dsh'
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Henkel authored and Daniel Henkel committed Oct 21, 2020
1 parent d720eda commit 52c29bf
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 21 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# 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.
- go mod download
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
Expand All @@ -18,6 +31,10 @@ archives:
windows: Windows
386: i386
amd64: x86_64

builds:
- pod-helper
- kubectl-dsh
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
96 changes: 96 additions & 0 deletions cmd/kubectl_dsh/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion main.go → cmd/pod_helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 52c29bf

Please sign in to comment.