Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -log-level flag to controller #348

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 All @@ -40,6 +41,7 @@ require (
k8s.io/api v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/client-go v0.18.6
k8s.io/klog/v2 v2.0.0
sigs.k8s.io/controller-runtime v0.6.3
)

Expand Down
19 changes: 18 additions & 1 deletion subcommand/controller/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ 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"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand All @@ -29,6 +31,7 @@ type Command struct {
flagEnableLeaderElection bool
flagEnableWebhooks bool
flagDatacenter string
flagLogLevel string

// Flags to support Consul Enterprise namespaces.
flagEnableNamespaces bool
Expand Down Expand Up @@ -75,14 +78,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", zapcore.InfoLevel.String(),
fmt.Sprintf("Log verbosity level. Supported values (in order of detail) are "+
"%q, %q, %q, and %q.", zapcore.DebugLevel.String(), zapcore.InfoLevel.String(), zapcore.WarnLevel.String(), zapcore.ErrorLevel.String()))

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,11 +106,23 @@ func (c *Command) Run(args []string) int {
return 1
}

var zapLevel zapcore.Level
if err := zapLevel.UnmarshalText([]byte(c.flagLogLevel)); err != nil {
c.UI.Error(fmt.Sprintf("Error parsing -log-level %q: %s", c.flagLogLevel, err.Error()))
return 1
}
// We set UseDevMode to true because we don't want our logs json
// formatted.
logger := zap.New(zap.UseDevMode(true), zap.Level(zapLevel))
ctrl.SetLogger(logger)
klog.SetLogger(logger)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Port: 9443,
LeaderElection: c.flagEnableLeaderElection,
LeaderElectionID: "consul.hashicorp.com",
Logger: logger,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
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: `Error parsing -log-level "invalid": unrecognized level: "invalid"`,
},
}

for _, c := range cases {
Expand Down