Skip to content

Commit

Permalink
add debug logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Oct 2, 2019
1 parent 31328bb commit c82624f
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 67 deletions.
3 changes: 2 additions & 1 deletion pkg/kapp/clusterapply/add_or_update_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

ctldiff "github.com/k14s/kapp/pkg/kapp/diff"
"github.com/k14s/kapp/pkg/kapp/logger"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
ctlresm "github.com/k14s/kapp/pkg/kapp/resourcesmisc"
"github.com/k14s/kapp/pkg/kapp/util"
Expand Down Expand Up @@ -120,7 +121,7 @@ type SpecificResource interface {
}

func (c AddOrUpdateChange) IsDoneApplying() (ctlresm.DoneApplyState, []string, error) {
labeledResources := ctlres.NewLabeledResources(nil, c.identifiedResources)
labeledResources := ctlres.NewLabeledResources(nil, c.identifiedResources, logger.NewTODOLogger())

// Refresh resource with latest changes from the server
parentRes, err := c.identifiedResources.Get(c.change.NewResource())
Expand Down
8 changes: 5 additions & 3 deletions pkg/kapp/cmd/app/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cmdtools "github.com/k14s/kapp/pkg/kapp/cmd/tools"
ctlconf "github.com/k14s/kapp/pkg/kapp/config"
ctldiff "github.com/k14s/kapp/pkg/kapp/diff"
"github.com/k14s/kapp/pkg/kapp/logger"
ctllogs "github.com/k14s/kapp/pkg/kapp/logs"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/spf13/cobra"
Expand All @@ -22,6 +23,7 @@ import (
type DeployOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory
logger logger.Logger

AppFlags AppFlags
FileFlags cmdtools.FileFlags
Expand All @@ -32,8 +34,8 @@ type DeployOptions struct {
LabelFlags LabelFlags
}

func NewDeployOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *DeployOptions {
return &DeployOptions{ui: ui, depsFactory: depsFactory}
func NewDeployOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *DeployOptions {
return &DeployOptions{ui: ui, depsFactory: depsFactory, logger: logger}
}

func NewDeployCmd(o *DeployOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command {
Expand Down Expand Up @@ -116,7 +118,7 @@ func (o *DeployOptions) Run() error {
return err
}

labeledResources := ctlres.NewLabeledResources(labelSelector, identifiedResources)
labeledResources := ctlres.NewLabeledResources(labelSelector, identifiedResources, o.logger)

err = labeledResources.Prepare(newResources, conf.OwnershipLabelMods(), conf.LabelScopingMods(), conf.AdditionalLabels())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kapp/cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
ctlapp "github.com/k14s/kapp/pkg/kapp/app"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
"github.com/k14s/kapp/pkg/kapp/logger"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -21,7 +22,7 @@ func AppFactoryClients(depsFactory cmdcore.DepsFactory, nsFlags cmdcore.Namespac
}

identifiedResources := ctlres.NewIdentifiedResources(
coreClient, dynamicClient, []string{nsFlags.Name})
coreClient, dynamicClient, []string{nsFlags.Name}, logger.NewTODOLogger())

apps := ctlapp.NewApps(nsFlags.Name, coreClient, identifiedResources)

Expand Down
8 changes: 5 additions & 3 deletions pkg/kapp/cmd/appgroup/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
cmdapp "github.com/k14s/kapp/pkg/kapp/cmd/app"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
cmdtools "github.com/k14s/kapp/pkg/kapp/cmd/tools"
"github.com/k14s/kapp/pkg/kapp/logger"
"github.com/spf13/cobra"
)

type DeployOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory
logger logger.Logger

AppGroupFlags AppGroupFlags
DeployFlags DeployFlags
Expand All @@ -30,8 +32,8 @@ type DeployAppFlags struct {
LabelFlags cmdapp.LabelFlags
}

func NewDeployOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *DeployOptions {
return &DeployOptions{ui: ui, depsFactory: depsFactory}
func NewDeployOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *DeployOptions {
return &DeployOptions{ui: ui, depsFactory: depsFactory, logger: logger}
}

func NewDeployCmd(o *DeployOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command {
Expand Down Expand Up @@ -122,7 +124,7 @@ func (o *DeployOptions) deployApp(app appGroupApp) error {
o.ui.PrintLinef("--- deploying app '%s' (namespace: %s) from %s",
app.Name, o.AppGroupFlags.NamespaceFlags.Name, app.Path)

deployOpts := cmdapp.NewDeployOptions(o.ui, o.depsFactory)
deployOpts := cmdapp.NewDeployOptions(o.ui, o.depsFactory, o.logger)
deployOpts.AppFlags = cmdapp.AppFlags{
Name: app.Name,
NamespaceFlags: o.AppGroupFlags.NamespaceFlags,
Expand Down
46 changes: 0 additions & 46 deletions pkg/kapp/cmd/core/logger.go

This file was deleted.

18 changes: 13 additions & 5 deletions pkg/kapp/cmd/kapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ import (
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
cmdsa "github.com/k14s/kapp/pkg/kapp/cmd/serviceaccount"
cmdtools "github.com/k14s/kapp/pkg/kapp/cmd/tools"
"github.com/k14s/kapp/pkg/kapp/logger"
"github.com/spf13/cobra"
)

type KappOptions struct {
ui *ui.ConfUI
logger *logger.UILogger
configFactory cmdcore.ConfigFactory
depsFactory cmdcore.DepsFactory

UIFlags UIFlags
LoggerFlags LoggerFlags
KubeconfigFlags cmdcore.KubeconfigFlags
}

func NewKappOptions(ui *ui.ConfUI, configFactory cmdcore.ConfigFactory, depsFactory cmdcore.DepsFactory) *KappOptions {
return &KappOptions{ui: ui, configFactory: configFactory, depsFactory: depsFactory}
func NewKappOptions(ui *ui.ConfUI, configFactory cmdcore.ConfigFactory,
depsFactory cmdcore.DepsFactory) *KappOptions {

return &KappOptions{ui: ui, logger: logger.NewUILogger(ui),
configFactory: configFactory, depsFactory: depsFactory}
}

func NewDefaultKappCmd(ui *ui.ConfUI) *cobra.Command {
Expand Down Expand Up @@ -63,6 +69,7 @@ func NewKappCmd(o *KappOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman
}))

o.UIFlags.Set(cmd, flagsFactory)
o.LoggerFlags.Set(cmd, flagsFactory)
o.KubeconfigFlags.Set(cmd, flagsFactory)

o.configFactory.ConfigurePathResolver(o.KubeconfigFlags.Path.Value)
Expand All @@ -72,15 +79,15 @@ func NewKappCmd(o *KappOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman

cmd.AddCommand(cmdapp.NewListCmd(cmdapp.NewListOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewInspectCmd(cmdapp.NewInspectOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewDeployCmd(cmdapp.NewDeployOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewDeployCmd(cmdapp.NewDeployOptions(o.ui, o.depsFactory, o.logger), flagsFactory))
cmd.AddCommand(cmdapp.NewDeployConfigCmd(cmdapp.NewDeployConfigOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewDeleteCmd(cmdapp.NewDeleteOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewRenameCmd(cmdapp.NewRenameOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewLogsCmd(cmdapp.NewLogsOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(cmdapp.NewLabelCmd(cmdapp.NewLabelOptions(o.ui, o.depsFactory), flagsFactory))

agCmd := cmdag.NewCmd()
agCmd.AddCommand(cmdag.NewDeployCmd(cmdag.NewDeployOptions(o.ui, o.depsFactory), flagsFactory))
agCmd.AddCommand(cmdag.NewDeployCmd(cmdag.NewDeployOptions(o.ui, o.depsFactory, o.logger), flagsFactory))
agCmd.AddCommand(cmdag.NewDeleteCmd(cmdag.NewDeleteOptions(o.ui, o.depsFactory), flagsFactory))
cmd.AddCommand(agCmd)

Expand All @@ -100,7 +107,7 @@ func NewKappCmd(o *KappOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman
appCmd := cmdtools.NewCmd()
appCmd.AddCommand(cmdtools.NewInspectCmd(cmdtools.NewInspectOptions(o.ui, o.depsFactory), flagsFactory))
appCmd.AddCommand(cmdtools.NewDiffCmd(cmdtools.NewDiffOptions(o.ui, o.depsFactory), flagsFactory))
appCmd.AddCommand(cmdtools.NewListLabelsCmd(cmdtools.NewListLabelsOptions(o.ui, o.depsFactory), flagsFactory))
appCmd.AddCommand(cmdtools.NewListLabelsCmd(cmdtools.NewListLabelsOptions(o.ui, o.depsFactory, o.logger), flagsFactory))
cmd.AddCommand(appCmd)

cmd.AddCommand(NewWebsiteCmd(NewWebsiteOptions()))
Expand All @@ -111,6 +118,7 @@ func NewKappCmd(o *KappOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Comman

cobrautil.VisitCommands(cmd, cobrautil.WrapRunEForCmd(func(*cobra.Command, []string) error {
o.UIFlags.ConfigureUI(o.ui)
o.LoggerFlags.Configure(o.logger)
return nil
}))

Expand Down
19 changes: 19 additions & 0 deletions pkg/kapp/cmd/logger_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
"github.com/k14s/kapp/pkg/kapp/logger"
"github.com/spf13/cobra"
)

type LoggerFlags struct {
Debug bool
}

func (f *LoggerFlags) Set(cmd *cobra.Command, flagsFactory cmdcore.FlagsFactory) {
cmd.PersistentFlags().BoolVar(&f.Debug, "debug", false, "Include debug output")
}

func (f *LoggerFlags) Configure(logger *logger.UILogger) {
logger.SetDebug(f.Debug)
}
10 changes: 6 additions & 4 deletions pkg/kapp/cmd/tools/list_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cppforlife/go-cli-ui/ui"
uitable "github.com/cppforlife/go-cli-ui/ui/table"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
"github.com/k14s/kapp/pkg/kapp/logger"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -14,14 +15,15 @@ import (
type ListLabelsOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory
logger logger.Logger

FileFlags FileFlags
Accessor string
Values bool
}

func NewListLabelsOptions(ui ui.UI, depsFactory cmdcore.DepsFactory) *ListLabelsOptions {
return &ListLabelsOptions{ui: ui, depsFactory: depsFactory}
func NewListLabelsOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *ListLabelsOptions {
return &ListLabelsOptions{ui: ui, depsFactory: depsFactory, logger: logger}
}

func NewListLabelsCmd(o *ListLabelsOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command {
Expand Down Expand Up @@ -122,14 +124,14 @@ func (o *ListLabelsOptions) listResources() ([]ctlres.Resource, error) {
return nil, err
}

identifiedResources := ctlres.NewIdentifiedResources(coreClient, dynamicClient, nil)
identifiedResources := ctlres.NewIdentifiedResources(coreClient, dynamicClient, nil, o.logger)

labelSelector, err := labels.Parse("!kapp")
if err != nil {
return nil, err
}

return ctlres.NewLabeledResources(labelSelector, identifiedResources).All()
return ctlres.NewLabeledResources(labelSelector, identifiedResources, o.logger).All()
}

type resourceAccessor struct {
Expand Down
14 changes: 14 additions & 0 deletions pkg/kapp/logger/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package logger

type Logger interface {
DebugFunc(name string) FuncLogger
NewPrefixed(name string) Logger

Error(msg string, args ...interface{})
Info(msg string, args ...interface{})
Debug(msg string, args ...interface{})
}

type FuncLogger interface {
Finish()
}
20 changes: 20 additions & 0 deletions pkg/kapp/logger/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package logger

type NoopLogger struct{}

var _ Logger = NoopLogger{}

func NewNoopLogger() NoopLogger { return NoopLogger{} }
func NewTODOLogger() NoopLogger { return NewNoopLogger() }

func (l NoopLogger) Error(msg string, args ...interface{}) {}
func (l NoopLogger) Info(msg string, args ...interface{}) {}
func (l NoopLogger) Debug(msg string, args ...interface{}) {}
func (l NoopLogger) DebugFunc(name string) FuncLogger { return NoopFuncLogger{} }
func (l NoopLogger) NewPrefixed(name string) Logger { return l }

type NoopFuncLogger struct{}

var _ FuncLogger = NoopFuncLogger{}

func (l NoopFuncLogger) Finish() {}
69 changes: 69 additions & 0 deletions pkg/kapp/logger/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package logger

import (
"fmt"
"time"

"github.com/cppforlife/go-cli-ui/ui"
)

const (
loggerLevelError = "error"
loggerLevelInfo = "info"
loggerLevelDebug = "debug"
)

type UILogger struct {
prefix string
ui ui.UI
debug bool
}

var _ Logger = &UILogger{}

func NewUILogger(ui ui.UI) *UILogger { return &UILogger{"", ui, false} }

func (l *UILogger) SetDebug(debug bool) { l.debug = debug }

func (l *UILogger) Error(msg string, args ...interface{}) {
l.ui.BeginLinef(l.msg(loggerLevelDebug, msg), args...)
}

func (l *UILogger) Info(msg string, args ...interface{}) {
l.ui.BeginLinef(l.msg(loggerLevelDebug, msg), args...)
}

func (l *UILogger) Debug(msg string, args ...interface{}) {
if l.debug {
l.ui.BeginLinef(l.msg(loggerLevelDebug, msg), args...)
}
}

func (l *UILogger) DebugFunc(name string) FuncLogger {
funcLogger := &UIFuncLogger{name, l.NewPrefixed(name)}
funcLogger.Start()
return funcLogger
}

func (l *UILogger) NewPrefixed(name string) Logger {
if len(l.prefix) > 0 {
name = l.prefix + name
}
name += ": "
return &UILogger{name, l.ui, l.debug}
}

func (l *UILogger) msg(level, msg string) string {
ts := time.Now().Format("03:04:05PM")
return fmt.Sprintf("%s: %s: %s%s\n", ts, level, l.prefix, msg)
}

type UIFuncLogger struct {
name string
logger Logger
}

var _ FuncLogger = &UIFuncLogger{}

func (l *UIFuncLogger) Start() { l.logger.Debug("start") }
func (l *UIFuncLogger) Finish() { l.logger.Debug("end") }
Loading

0 comments on commit c82624f

Please sign in to comment.