From b517797056d9362391a8fe8c7e3a33e508da0187 Mon Sep 17 00:00:00 2001 From: Daniel Henkel Date: Sat, 17 Oct 2020 14:28:00 +0200 Subject: [PATCH 1/2] remove debug logs from config parsing --- src/config/run_config.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/config/run_config.go b/src/config/run_config.go index f6d4df8..a17b7d3 100644 --- a/src/config/run_config.go +++ b/src/config/run_config.go @@ -1,16 +1,13 @@ package config import ( - "fmt" "github.com/urfave/cli/v2" "strings" ) func newRunConfig(c *cli.Context) RunConfig { command := c.String("command") - commands := strings.Split(command, " ") - fmt.Println(commands) return RunConfig{Command: commands} } From 57d97d353a12d15d2793025b0198807e1b9e6053 Mon Sep 17 00:00:00 2001 From: Daniel Henkel Date: Sat, 17 Oct 2020 21:09:41 +0200 Subject: [PATCH 2/2] implement better log structure * unify result object and output * add entrypoint flag to run command * add tail flag to logs command --- main.go | 11 ++++++++++ src/commands/logs.go | 25 +++------------------ src/commands/run.go | 27 ++++------------------- src/config/logs_config.go | 6 ++++- src/config/run_config.go | 7 ++++-- src/config/types.go | 4 +++- src/kube/pod_logs.go | 3 +++ src/kube/{exec_pod.go => run_pod.go} | 7 +++--- src/types/result.go | 33 ++++++++++++++++++++++++++++ 9 files changed, 70 insertions(+), 53 deletions(-) rename src/kube/{exec_pod.go => run_pod.go} (77%) create mode 100644 src/types/result.go diff --git a/main.go b/main.go index 3f30d6b..4ef79b2 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,12 @@ func main() { Usage: "runs smth", Action: commands.Run, Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "entrypoint", + Aliases: []string{"e", "entry"}, + Required: false, + Value: "/bin/sh -c", + }, &cli.StringFlag{ Name: "command", Aliases: []string{"c"}, @@ -63,6 +69,11 @@ func main() { Aliases: []string{"ci"}, Value: -1, }, + &cli.Int64Flag{ + Name: "tail", + Aliases: []string{"t"}, + Value: -1, + }, }, }, }, diff --git a/src/commands/logs.go b/src/commands/logs.go index 8aaaddc..0d4d923 100644 --- a/src/commands/logs.go +++ b/src/commands/logs.go @@ -3,25 +3,10 @@ package commands import ( "github.com/dhenkel92/pod-helper/src/config" "github.com/dhenkel92/pod-helper/src/kube" - "github.com/dhenkel92/pod-helper/src/log" - . "github.com/logrusorgru/aurora" + "github.com/dhenkel92/pod-helper/src/types" "github.com/urfave/cli/v2" - v1 "k8s.io/api/core/v1" ) -type LogResult struct { - ExecResult kube.ExecResult - Pod v1.Pod -} - -func (result *LogResult) print() { - log.Info.Println("----------------------------------------") - log.Info.Println(Green(result.Pod.Name)) - log.Info.Println(Green("Successful")) - log.Info.Printf("Result:\n%s", result.ExecResult.StdOut) - log.Info.Println("----------------------------------------") -} - func Logs(c *cli.Context) error { cliConf := config.NewConfigFromCliContext(c) @@ -40,17 +25,13 @@ func Logs(c *cli.Context) error { return err } - var results []LogResult for _, pod := range pods.Items { c := make(chan kube.ExecResult) go podExec.Logs(c, &cliConf, &pod) res := <-c - results = append(results, LogResult{ExecResult: res, Pod: pod}) - } - - for _, res := range results { - res.print() + result := types.Result{ExecResult: res, Pod: pod} + result.Print() } return nil diff --git a/src/commands/run.go b/src/commands/run.go index 72a2461..7068a0f 100644 --- a/src/commands/run.go +++ b/src/commands/run.go @@ -3,25 +3,10 @@ package commands import ( "github.com/dhenkel92/pod-helper/src/config" "github.com/dhenkel92/pod-helper/src/kube" - "github.com/dhenkel92/pod-helper/src/log" - . "github.com/logrusorgru/aurora" + "github.com/dhenkel92/pod-helper/src/types" "github.com/urfave/cli/v2" - v1 "k8s.io/api/core/v1" ) -type Result struct { - ExecResult kube.ExecResult - Pod v1.Pod -} - -func (result *Result) print() { - log.Info.Println("----------------------------------------") - log.Info.Println(Green(result.Pod.Name)) - log.Info.Println(Green("Successful")) - log.Info.Printf("Result:\n%s", result.ExecResult.StdOut) - log.Info.Println("----------------------------------------") -} - func Run(c *cli.Context) error { cliConf := config.NewConfigFromCliContext(c) @@ -40,17 +25,13 @@ func Run(c *cli.Context) error { return err } - var results []Result for _, pod := range pods.Items { c := make(chan kube.ExecResult) - go podExec.Exec(c, &cliConf, &pod) + go podExec.Run(c, &cliConf, &pod) res := <-c - results = append(results, Result{ExecResult: res, Pod: pod}) - } - - for _, res := range results { - res.print() + result := types.Result{ExecResult: res, Pod: pod} + result.Print() } return nil diff --git a/src/config/logs_config.go b/src/config/logs_config.go index da997c9..0cea2ac 100644 --- a/src/config/logs_config.go +++ b/src/config/logs_config.go @@ -4,6 +4,10 @@ import "github.com/urfave/cli/v2" func newLogsConfig(c *cli.Context) LogsConfig { containerIndex := c.Int64("container-index") + tail := c.Int64("tail") - return LogsConfig{ContainerIndex: containerIndex} + return LogsConfig{ + ContainerIndex: containerIndex, + Tail: tail, + } } diff --git a/src/config/run_config.go b/src/config/run_config.go index a17b7d3..65785fa 100644 --- a/src/config/run_config.go +++ b/src/config/run_config.go @@ -6,8 +6,11 @@ import ( ) func newRunConfig(c *cli.Context) RunConfig { + entrypoint := c.String("entrypoint") command := c.String("command") - commands := strings.Split(command, " ") - return RunConfig{Command: commands} + return RunConfig{ + Entrypoint: strings.Split(entrypoint, " "), + Command: command, + } } diff --git a/src/config/types.go b/src/config/types.go index d9a843b..30757cf 100644 --- a/src/config/types.go +++ b/src/config/types.go @@ -9,9 +9,11 @@ type Config struct { } type RunConfig struct { - Command []string + Entrypoint []string + Command string } type LogsConfig struct { ContainerIndex int64 + Tail int64 } diff --git a/src/kube/pod_logs.go b/src/kube/pod_logs.go index 3201054..ff0a31b 100644 --- a/src/kube/pod_logs.go +++ b/src/kube/pod_logs.go @@ -12,6 +12,9 @@ func (podExec *PodExecutor) Logs(c chan ExecResult, conf *config.Config, pod *v1 if conf.LogsConfig.ContainerIndex >= 0 { options.Container = pod.Spec.Containers[conf.LogsConfig.ContainerIndex].Name } + if conf.LogsConfig.Tail >= 0 { + options.TailLines = &conf.LogsConfig.Tail + } req := podExec.Clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &options) podLogs, err := req.Stream() diff --git a/src/kube/exec_pod.go b/src/kube/run_pod.go similarity index 77% rename from src/kube/exec_pod.go rename to src/kube/run_pod.go index 568c8ef..3db2eb8 100644 --- a/src/kube/exec_pod.go +++ b/src/kube/run_pod.go @@ -3,13 +3,12 @@ package kube import ( "bytes" "github.com/dhenkel92/pod-helper/src/config" - v1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/remotecommand" ) -func (podExec *PodExecutor) Exec(c chan ExecResult, conf *config.Config, pod *v1.Pod) { +func (podExec *PodExecutor) Run(c chan ExecResult, conf *config.Config, pod *v1.Pod) { req := podExec.Clientset. CoreV1(). RESTClient(). @@ -20,7 +19,7 @@ func (podExec *PodExecutor) Exec(c chan ExecResult, conf *config.Config, pod *v1 SubResource("exec") option := &v1.PodExecOptions{ - Command: conf.RunConfig.Command, + Command: append(conf.RunConfig.Entrypoint, conf.RunConfig.Command), Stdin: false, Stdout: true, Stderr: true, @@ -39,7 +38,7 @@ func (podExec *PodExecutor) Exec(c chan ExecResult, conf *config.Config, pod *v1 Stderr: &stderr, }) if err != nil { - c <- ExecResult{Error: err} + c <- ExecResult{Error: err, StdErr: stderr.String(), StdOut: stdout.String()} return } diff --git a/src/types/result.go b/src/types/result.go new file mode 100644 index 0000000..6f0439d --- /dev/null +++ b/src/types/result.go @@ -0,0 +1,33 @@ +package types + +import ( + "github.com/dhenkel92/pod-helper/src/kube" + "github.com/dhenkel92/pod-helper/src/log" + "github.com/logrusorgru/aurora" + v1 "k8s.io/api/core/v1" + "strings" +) + +type Result struct { + ExecResult kube.ExecResult + Pod v1.Pod +} + +func (result *Result) Print() { + var text string + var resText string + if result.ExecResult.Error == nil { + text = aurora.Sprintf(aurora.Green("Success on %s"), result.Pod.Name) + resText = result.ExecResult.StdOut + } else { + text = aurora.Sprintf(aurora.Red("Failed on %s"), result.Pod.Name) + resText = result.ExecResult.StdOut + } + + lineByLine := strings.Split(resText, "\n") + + log.Info.Println("----------------------------------------") + log.Info.Println(text) + log.Info.Printf("Result:\n\n\t%s", strings.Join(lineByLine, "\n\t")) + log.Info.Println("----------------------------------------") +}