Skip to content

Commit

Permalink
Merge pull request #2 from dhenkel92/update_logging
Browse files Browse the repository at this point in the history
Update logging
  • Loading branch information
dhenkel92 authored Oct 17, 2020
2 parents a8cc8b4 + 57d97d3 commit 9dfac96
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 56 deletions.
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -63,6 +69,11 @@ func main() {
Aliases: []string{"ci"},
Value: -1,
},
&cli.Int64Flag{
Name: "tail",
Aliases: []string{"t"},
Value: -1,
},
},
},
},
Expand Down
25 changes: 3 additions & 22 deletions src/commands/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
27 changes: 4 additions & 23 deletions src/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/config/logs_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
10 changes: 5 additions & 5 deletions src/config/run_config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package config

import (
"fmt"
"github.com/urfave/cli/v2"
"strings"
)

func newRunConfig(c *cli.Context) RunConfig {
entrypoint := c.String("entrypoint")
command := c.String("command")

commands := strings.Split(command, " ")
fmt.Println(commands)

return RunConfig{Command: commands}
return RunConfig{
Entrypoint: strings.Split(entrypoint, " "),
Command: command,
}
}
4 changes: 3 additions & 1 deletion src/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ type Config struct {
}

type RunConfig struct {
Command []string
Entrypoint []string
Command string
}

type LogsConfig struct {
ContainerIndex int64
Tail int64
}
3 changes: 3 additions & 0 deletions src/kube/pod_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions src/kube/exec_pod.go → src/kube/run_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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,
Expand All @@ -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
}

Expand Down
33 changes: 33 additions & 0 deletions src/types/result.go
Original file line number Diff line number Diff line change
@@ -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("----------------------------------------")
}

0 comments on commit 9dfac96

Please sign in to comment.