Skip to content

Commit

Permalink
Add -log-level flag to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Oct 2, 2020
1 parent 40d03ab commit 62b1ccb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/radovskyb/watcher v1.0.2
github.com/stretchr/testify v1.5.1
go.opencensus.io v0.22.0 // indirect
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 // indirect
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 // indirect
Expand Down
39 changes: 38 additions & 1 deletion subcommand/controller/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/consul-k8s/controller"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/mitchellh/cli"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -29,6 +30,7 @@ type Command struct {
flagEnableLeaderElection bool
flagEnableWebhooks bool
flagDatacenter string
flagLogLevel string

// Flags to support Consul Enterprise namespaces.
flagEnableNamespaces bool
Expand All @@ -46,6 +48,13 @@ var (
setupLog = ctrl.Log.WithName("setup")
)

const (
LogLevelDebug = "debug"
LogLevelInfo = "info"
LogLevelWarn = "warn"
LogLevelError = "error"
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
Expand Down Expand Up @@ -75,14 +84,16 @@ func (c *Command) init() {
"Directory that contains the TLS cert and key required for the webhook. The cert and key files must be named 'tls.crt' and 'tls.key' respectively.")
c.flagSet.BoolVar(&c.flagEnableWebhooks, "enable-webhooks", true,
"Enable webhooks. Disable when running locally since Kube API server won't be able to route to local server.")
c.flagSet.StringVar(&c.flagLogLevel, "log-level", LogLevelInfo,
fmt.Sprintf("Log verbosity level. Supported values (in order of detail) are "+
"%q, %q, %q, and %q.", LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError))

c.httpFlags = &flags.HTTPFlags{}
flags.Merge(c.flagSet, c.httpFlags.Flags())
c.help = flags.Usage(help, c.flagSet)
}

func (c *Command) Run(args []string) int {
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
c.once.Do(c.init)
if err := c.flagSet.Parse(args); err != nil {
c.UI.Error(fmt.Sprintf("Parsing flagset: %s", err.Error()))
Expand All @@ -101,6 +112,13 @@ func (c *Command) Run(args []string) int {
return 1
}

zapLevel, useDevMode, err := toLevel(c.flagLogLevel)
if err != nil {
c.UI.Error(err.Error())
return 1
}
ctrl.SetLogger(zap.New(zap.UseDevMode(useDevMode), zap.Level(zapLevel)))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Port: 9443,
Expand Down Expand Up @@ -231,6 +249,25 @@ func (c *Command) Run(args []string) int {
return 0
}

// toLevel returns the zapcore level, a boolean to indicate if we should
// use dev mode and an error if lvl is not supported.
func toLevel(lvl string) (zapcore.Level, bool, error) {
switch lvl {
case LogLevelDebug:
return zapcore.DebugLevel, true, nil
case LogLevelInfo:
return zapcore.InfoLevel, false, nil
case LogLevelWarn:
return zapcore.WarnLevel, false, nil
case LogLevelError:
return zapcore.ErrorLevel, false, nil
default:
return zapcore.DebugLevel, false,
fmt.Errorf("invalid -log-level %q, must be one of %q, %q, %q, %q",
lvl, LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError)
}
}

func (c *Command) Help() string {
c.once.Do(c.init)
return c.help
Expand Down
4 changes: 4 additions & 0 deletions subcommand/controller/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func TestRun_FlagValidation(t *testing.T) {
flags: []string{"-webhook-tls-cert-dir", "/foo"},
expErr: "-datacenter must be set",
},
{
flags: []string{"-webhook-tls-cert-dir", "/foo", "-datacenter", "foo", "-log-level", "invalid"},
expErr: `invalid -log-level "invalid", must be one of "debug", "info", "warn", "error"`,
},
}

for _, c := range cases {
Expand Down

0 comments on commit 62b1ccb

Please sign in to comment.