Skip to content

Commit

Permalink
Remove logs scrolling while printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Jul 13, 2023
1 parent 7a8b838 commit 6c13bd7
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 163 deletions.
2 changes: 0 additions & 2 deletions cmd/cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func NewInstall() *cobra.Command {
flags := installCmd.Flags()

kubex.RegisterKubeconfigFlag(flags)
flags.BoolVar(&opts.LogsReportTimestamp, "logs-report-timestamp", false, "Print timestamp prefix to the Botkube logs entries")
flags.IntVar(&opts.LogsScrollingHeight, "logs-scrolling-height", 10, "")
flags.DurationVar(&opts.Timeout, "timeout", 10*time.Minute, `Maximum time during which the Botkube installation is being watched, where "0" means "infinite". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`)
flags.BoolVarP(&opts.Watch, "watch", "w", true, "Watches the status of the Botkube installation until it finish or the defined `--timeout` occurs.")

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/aws/aws-sdk-go v1.44.122
github.com/briandowns/spinner v1.23.0
github.com/bwmarrin/discordgo v0.25.0
github.com/charmbracelet/log v0.2.2
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.13.0
github.com/go-playground/locales v0.14.0
Expand All @@ -34,6 +35,7 @@ require (
github.com/mattermost/mattermost-server/v6 v6.7.2
github.com/mattn/go-isatty v0.0.19
github.com/mattn/go-shellwords v1.0.12
github.com/morikuni/aec v1.0.0
github.com/muesli/reflow v0.3.0
github.com/olivere/elastic v6.2.37+incompatible
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
Expand Down Expand Up @@ -95,7 +97,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/charmbracelet/log v0.2.2 // indirect
github.com/containerd/containerd v1.7.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
Expand Down Expand Up @@ -194,7 +195,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oklog/run v1.1.0 // indirect
Expand Down
4 changes: 3 additions & 1 deletion helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,10 @@ settings:
log:
# -- Sets one of the log levels. Allowed values: `info`, `warn`, `debug`, `error`, `fatal`, `panic`.
level: info
# -- If true, disable ANSI colors in logging.
# -- If true, disable ANSI colors in logging. Ignored when `json` formatter is used.
disableColors: false
# -- Configures log format. Allowed values: `text`, `json`.
formatter: json

# -- Botkube's system ConfigMap where internal data is stored.
systemConfigMap:
Expand Down
10 changes: 4 additions & 6 deletions internal/cli/install/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ const (

// Config holds parameters for Botkube installation on cluster.
type Config struct {
Kubeconfig string
HelmParams helm.Config
LogsReportTimestamp bool
LogsScrollingHeight int
Watch bool
Timeout time.Duration
Kubeconfig string
HelmParams helm.Config
Watch bool
Timeout time.Duration
}
9 changes: 6 additions & 3 deletions internal/cli/install/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ func NewHelm(k8sCfg *rest.Config, forNamespace string) (*Helm, error) {
// Install installs a given Helm chart.
func (c *Helm) Install(ctx context.Context, status *printer.StatusPrinter, opts Config) (*release.Release, error) {
histClient := action.NewHistory(c.helmCfg)
//histClient.Max = 1
rels, err := histClient.Run(opts.ReleaseName)
var runFn Run
switch {
case err == nil:
if err := PrintReleaseStatus("Detected existing Botkube installation:", status, rels[len(rels)-1]); err != nil {
return nil, err
if len(rels) > 0 { // it shouldn't happen, because there is not found error in such cases, however it better to be on the safe side.
if err := PrintReleaseStatus("Detected existing Botkube installation:", status, rels[len(rels)-1]); err != nil {
return nil, err
}
} else {
status.Infof("Detected existing Botkube installation")
}

prompt := &survey.Confirm{
Expand Down
18 changes: 12 additions & 6 deletions internal/cli/install/helm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ func PrintReleaseStatus(header string, status *printer.StatusPrinter, r *release

properties := make(map[string]string)
properties["Name"] = r.Name
if !r.Info.LastDeployed.IsZero() {
properties["LastDeployed"] = r.Info.LastDeployed.Format(time.ANSIC)
}
properties["Namespace"] = r.Namespace
properties["Status"] = r.Info.Status.String()
properties["Version"] = r.Chart.AppVersion()
properties["Revision"] = fmt.Sprintf("%d", r.Version)
properties["Description"] = r.Info.Description

if r.Info != nil {
if !r.Info.LastDeployed.IsZero() {
properties["LastDeployed"] = r.Info.LastDeployed.Format(time.ANSIC)
}
properties["Status"] = r.Info.Status.String()
properties["Description"] = r.Info.Description
}

if r.Chart != nil {
properties["Version"] = r.Chart.AppVersion()
}

desc, err := renderer.Render(properties, true)
if err != nil {
Expand Down
26 changes: 14 additions & 12 deletions internal/cli/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/kubeshop/botkube/internal/kubex"
)

const messageInitialBufferSize = 100

// Install installs Botkube Helm chart into cluster.
func Install(ctx context.Context, w io.Writer, k8sCfg *kubex.ConfigWithMeta, opts Config) (err error) {
ctxWithTimeout, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -77,12 +79,12 @@ func Install(ctx context.Context, w io.Writer, k8sCfg *kubex.ConfigWithMeta, opt
}

parallel, _ := errgroup.WithContext(ctxWithTimeout)

podScheduledIndicator := make(chan string)
podWaitErrors := make(chan error, 1)
podWaitPhase := make(chan string, 10)
podWaitResult := make(chan error, 1)
parallel.Go(func() error {
err := kubex.WaitForPod(ctxWithTimeout, clientset, opts.HelmParams.Namespace, opts.HelmParams.ReleaseName, kubex.PodReady(podScheduledIndicator, podWaitPhase, time.Now()))
podWaitErrors <- err
err := kubex.WaitForPod(ctxWithTimeout, clientset, opts.HelmParams.Namespace, opts.HelmParams.ReleaseName, kubex.PodReady(podScheduledIndicator, time.Now()))
podWaitResult <- err
return nil
})

Expand All @@ -109,29 +111,28 @@ func Install(ctx context.Context, w io.Writer, k8sCfg *kubex.ConfigWithMeta, opt
return fmt.Errorf("Timed out waiting for Pod")
}

messages := make(chan []byte, opts.LogsScrollingHeight)
messages := make(chan []byte, messageInitialBufferSize)
streamLogCtx, cancelStreamLogs := context.WithCancel(context.Background())
defer cancelStreamLogs()
parallel.Go(func() error {
defer close(messages)
return logs.StartsLogsStreaming(streamLogCtx, clientset, opts.HelmParams.Namespace, podName, messages)
})

logsPrinter := logs.NewFixedHeightPrinter(
opts.LogsScrollingHeight,
logsPrinter := logs.NewPrinter(
podName,
)

parallel.Go(func() error {
logsPrinter.Start(ctxWithTimeout)
logsPrinter.Start(ctxWithTimeout, status)
return nil
})
parallel.Go(func() error {
for {
select {
case <-ctxWithTimeout.Done(): // it's canceled on OS signals or if function passed to 'Go' method returns a non-nil error
return ctxWithTimeout.Err()
case err := <-podWaitErrors:
case err := <-podWaitResult:
time.Sleep(time.Second)
cancelStreamLogs()
return err
Expand All @@ -144,8 +145,6 @@ func Install(ctx context.Context, w io.Writer, k8sCfg *kubex.ConfigWithMeta, opt
select {
case <-ctxWithTimeout.Done(): // it's canceled on OS signals or if function passed to 'Go' method returns a non-nil error
return ctxWithTimeout.Err()
case ph := <-podWaitPhase:
logsPrinter.UpdatePodPhase(ph)
case entry, ok := <-messages:
if !ok {
logsPrinter.Stop()
Expand All @@ -158,7 +157,10 @@ func Install(ctx context.Context, w io.Writer, k8sCfg *kubex.ConfigWithMeta, opt

err = parallel.Wait()
if err != nil {
printFailedInstallMessage(opts.HelmParams.Version, opts.HelmParams.Namespace, podName, w)
printErr := printFailedInstallMessage(opts.HelmParams.Version, opts.HelmParams.Namespace, podName, w)
if printErr != nil {
return fmt.Errorf("%s: %v", printErr, err)
}
return err
}

Expand Down
29 changes: 12 additions & 17 deletions internal/cli/install/logs/json_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,19 @@ import (
"github.com/kubeshop/botkube/internal/cli"
)

// JSONParser knows how to parse JSON formatted logs.
type JSONParser struct{}

func (p *JSONParser) ParseLine(line string) map[string]any {
var out map[string]any
err := json.Unmarshal([]byte(line), &out)
if err != nil {
return nil
}
return out
}

// ParseLineIntoCharm returns parsed log line with charm logger support.
func (k *JSONParser) ParseLineIntoCharm(line string) ([]any, charmlog.Level) {
result := k.ParseLine(line)
func (j *JSONParser) ParseLineIntoCharm(line string) ([]any, charmlog.Level) {
result := j.parseLine(line)
if result == nil {
return nil, 0
}

var fields []any

//if k.ReportTimestamp {
// parseAny, _ := dateparse.ParseAny(result["time"])
// fields = append(fields, charmlog.TimestampKey, parseAny)
//}

lvl := charmlog.ParseLevel(fmt.Sprint(result["level"]))
// todo, check and ignore debug
fields = append(fields, charmlog.LevelKey, lvl)
fields = append(fields, charmlog.MessageKey, result["msg"])

Expand All @@ -58,3 +44,12 @@ func (k *JSONParser) ParseLineIntoCharm(line string) ([]any, charmlog.Level) {

return fields, lvl
}

func (*JSONParser) parseLine(line string) map[string]any {
var out map[string]any
err := json.Unmarshal([]byte(line), &out)
if err != nil {
return nil
}
return out
}
3 changes: 0 additions & 3 deletions internal/cli/install/logs/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ func StartsLogsStreaming(ctx context.Context, clientset *kubernetes.Clientset, n
bytes, readErr := r.ReadBytes('\n')
// write first, as there might be some chars already loaded
out <- bytes
//if strings.Contains(string(bytes), "Botkube connected to") {
// return nil
//}
switch readErr {
case nil:
case io.EOF:
Expand Down
Loading

0 comments on commit 6c13bd7

Please sign in to comment.