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 flag to enable logging output in json to consul-k8s #523

Merged
merged 17 commits into from
Jun 29, 2021
Merged
9 changes: 5 additions & 4 deletions subcommand/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ const (
ACLTokenSecretKey = "token"
)

// Logger returns an hclog instance or an error if level is invalid.
func Logger(level string) (hclog.Logger, error) {
// Logger returns an hclog instance with log level set and JSON logging enabled/disabled, or an error if level is invalid.
func Logger(level string, jsonLogging bool) (hclog.Logger, error) {
parsedLevel := hclog.LevelFromString(level)
if parsedLevel == hclog.NoLevel {
return nil, fmt.Errorf("unknown log level: %s", level)
}
return hclog.New(&hclog.LoggerOptions{
Level: parsedLevel,
Output: os.Stderr,
JSONFormat: jsonLogging,
Level: parsedLevel,
Output: os.Stderr,
}), nil
}

Expand Down
4 changes: 2 additions & 2 deletions subcommand/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
)

func TestLogger_InvalidLogLevel(t *testing.T) {
_, err := Logger("invalid")
_, err := Logger("invalid", false)
require.EqualError(t, err, "unknown log level: invalid")
}

func TestLogger(t *testing.T) {
lgr, err := Logger("debug")
lgr, err := Logger("debug", false)
require.NoError(t, err)
require.NotNil(t, lgr)
require.True(t, lgr.IsDebug())
Expand Down
5 changes: 4 additions & 1 deletion subcommand/connect-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Command struct {
flagServiceAccountName string // Service account name.
flagServiceName string // Service name.
flagLogLevel string
flagLogOutputJSON bool
kschoche marked this conversation as resolved.
Show resolved Hide resolved

bearerTokenFile string // Location of the bearer token. Default is /var/run/secrets/kubernetes.io/serviceaccount/token.
tokenSinkFile string // Location to write the output token. Default is defaultTokenSinkFile.
Expand All @@ -65,6 +66,8 @@ func (c *Command) init() {
c.flagSet.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flagSet.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

if c.bearerTokenFile == "" {
c.bearerTokenFile = defaultBearerTokenFile
Expand Down Expand Up @@ -109,7 +112,7 @@ func (c *Command) Run(args []string) int {
// Set up logging.
if c.logger == nil {
var err error
c.logger, err = common.Logger(c.flagLogLevel)
c.logger, err = common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
5 changes: 4 additions & 1 deletion subcommand/consul-sidecar/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Command struct {
flagSyncPeriod time.Duration
flagSet *flag.FlagSet
flagLogLevel string
flagLogOutputJSON bool

// Flags to configure metrics merging
flagEnableMetricsMerging bool
Expand Down Expand Up @@ -67,6 +68,8 @@ func (c *Command) init() {
c.flagSet.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\". Defaults to info.")
c.flagSet.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.flagSet.BoolVar(&c.flagEnableMetricsMerging, "enable-metrics-merging", false, "Enables consul sidecar to run a merged metrics endpoint. Defaults to false.")
// -merged-metrics-port, -service-metrics-port, and -service-metrics-path
Expand Down Expand Up @@ -109,7 +112,7 @@ func (c *Command) Run(args []string) int {
return 1
}

logger, err := common.Logger(c.flagLogLevel)
logger, err := common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
20 changes: 14 additions & 6 deletions subcommand/controller/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"sync"

"github.com/go-logr/logr"
"github.com/hashicorp/consul-k8s/api/common"
"github.com/hashicorp/consul-k8s/api/v1alpha1"
"github.com/hashicorp/consul-k8s/controller"
Expand Down Expand Up @@ -32,6 +33,7 @@ type Command struct {
flagEnableWebhooks bool
flagDatacenter string
flagLogLevel string
flagLogOutputJSON bool

// Flags to support Consul Enterprise namespaces.
flagEnableNamespaces bool
Expand Down Expand Up @@ -81,6 +83,8 @@ func (c *Command) init() {
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.flagSet.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.httpFlags = &flags.HTTPFlags{}
flags.Merge(c.flagSet, c.httpFlags.Flags())
Expand Down Expand Up @@ -111,18 +115,22 @@ func (c *Command) Run(args []string) int {
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)

var zapLogger logr.Logger
if c.flagLogOutputJSON {
zapLogger = zap.New(zap.UseDevMode(true), zap.Level(zapLevel), zap.JSONEncoder())
kschoche marked this conversation as resolved.
Show resolved Hide resolved
} else {
zapLogger = zap.New(zap.UseDevMode(true), zap.Level(zapLevel), zap.ConsoleEncoder())
kschoche marked this conversation as resolved.
Show resolved Hide resolved
}
ctrl.SetLogger(zapLogger)
klog.SetLogger(zapLogger)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Port: 9443,
LeaderElection: c.flagEnableLeaderElection,
LeaderElectionID: "consul.hashicorp.com",
Logger: logger,
Logger: zapLogger,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
5 changes: 4 additions & 1 deletion subcommand/create-federation-secret/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Command struct {
flagResourcePrefix string
flagK8sNamespace string
flagLogLevel string
flagLogOutputJSON bool
flagMeshGatewayServiceName string

k8sClient kubernetes.Interface
Expand Down Expand Up @@ -89,6 +90,8 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flags.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.http = &flags.HTTPFlags{}
c.k8s = &flags.K8SFlags{}
Expand All @@ -108,7 +111,7 @@ func (c *Command) Run(args []string) int {
return 1
}

logger, err := common.Logger(c.flagLogLevel)
logger, err := common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
19 changes: 12 additions & 7 deletions subcommand/delete-completed-job/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)

const logLevel = "info"

// Command is the command for deleting completed jobs.
type Command struct {
UI cli.Ui

flags *flag.FlagSet
k8s *flags.K8SFlags
flagNamespace string
flagTimeout string
flags *flag.FlagSet
k8s *flags.K8SFlags
flagNamespace string
flagTimeout string
flagLogLevel string
flagLogOutputJSON bool

once sync.Once
help string
Expand All @@ -45,6 +45,11 @@ func (c *Command) init() {
"Name of Kubernetes namespace where the job is deployed")
c.flags.StringVar(&c.flagTimeout, "timeout", "30m",
"How long we'll wait for the job to complete before timing out, e.g. 1ms, 2s, 3m")
c.flags.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flags.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")
flags.Merge(c.flags, c.k8s.Flags())
c.help = flags.Usage(help, c.flags)

Expand Down Expand Up @@ -96,7 +101,7 @@ func (c *Command) Run(args []string) int {
}
}

logger, err := common.Logger(logLevel)
logger, err := common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
5 changes: 4 additions & 1 deletion subcommand/get-consul-client-ca/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Command struct {
flagTLSServerName string
flagPollingInterval time.Duration
flagLogLevel string
flagLogOutputJSON bool

once sync.Once
help string
Expand All @@ -58,6 +59,8 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flags.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.help = flags.Usage(help, c.flags)
}
Expand All @@ -82,7 +85,7 @@ func (c *Command) Run(args []string) int {
return 1
}

logger, err := common.Logger(c.flagLogLevel)
logger, err := common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
13 changes: 11 additions & 2 deletions subcommand/inject-connect/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"github.com/go-logr/logr"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -48,6 +49,7 @@ type Command struct {
flagConsulCACert string // [Deprecated] Path to CA Certificate to use when communicating with Consul clients
flagEnvoyExtraArgs string // Extra envoy args when starting envoy
flagLogLevel string
flagLogOutputJSON bool

flagAllowK8sNamespacesList []string // K8s namespaces to explicitly inject
flagDenyK8sNamespacesList []string // K8s namespaces to deny injection (has precedence)
Expand Down Expand Up @@ -165,6 +167,8 @@ func (c *Command) init() {
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.flagSet.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

// Proxy sidecar resource setting flags.
c.flagSet.StringVar(&c.flagDefaultSidecarProxyCPURequest, "default-sidecar-proxy-cpu-request", "", "Default sidecar proxy CPU request.")
Expand Down Expand Up @@ -359,10 +363,15 @@ func (c *Command) Run(args []string) int {
return 1
}

// We set UseDevMode to true because we don't want our logs json formatted.
zapLogger := zap.New(zap.UseDevMode(true), zap.Level(zapLevel))
var zapLogger logr.Logger
if c.flagLogOutputJSON {
zapLogger = zap.New(zap.UseDevMode(true), zap.Level(zapLevel), zap.JSONEncoder())
} else {
zapLogger = zap.New(zap.UseDevMode(true), zap.Level(zapLevel), zap.ConsoleEncoder())
}
ctrl.SetLogger(zapLogger)
klog.SetLogger(zapLogger)

listenSplits := strings.SplitN(c.flagListen, ":", 2)
if len(listenSplits) < 2 {
c.UI.Error(fmt.Sprintf("missing port in address: %s", c.flagListen))
Expand Down
9 changes: 6 additions & 3 deletions subcommand/server-acl-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ type Command struct {
// Flag to support a custom bootstrap token
flagBootstrapTokenFile string

flagLogLevel string
flagTimeout time.Duration
flagLogLevel string
flagLogOutputJSON bool
flagTimeout time.Duration

// flagFederation is used to determine which ACL policies to write and whether or not to provide suffixing
// to the policy names when creating the policy in cases where federation is used.
Expand Down Expand Up @@ -205,6 +206,8 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flags.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.k8s = &k8sflags.K8SFlags{}
flags.Merge(c.flags, c.k8s.Flags())
Expand Down Expand Up @@ -279,7 +282,7 @@ func (c *Command) Run(args []string) int {
defer cancel()

var err error
c.log, err = common.Logger(c.flagLogLevel)
c.log, err = common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
16 changes: 14 additions & 2 deletions subcommand/service-address/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cenkalti/backoff"
"github.com/hashicorp/consul-k8s/subcommand"
"github.com/hashicorp/consul-k8s/subcommand/common"
"github.com/hashicorp/consul-k8s/subcommand/flags"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/go-hclog"
Expand All @@ -31,6 +32,8 @@ type Command struct {
flagServiceName string
flagOutputFile string
flagResolveHostnames bool
flagLogLevel string
flagLogOutputJSON bool

retryDuration time.Duration
k8sClient kubernetes.Interface
Expand All @@ -48,6 +51,11 @@ func (c *Command) init() {
"Path to file to write load balancer address")
c.flags.BoolVar(&c.flagResolveHostnames, "resolve-hostnames", false,
"If true we will resolve any hostnames and use their first IP address")
c.flags.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flags.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.k8sFlags = &k8sflags.K8SFlags{}
flags.Merge(c.flags, c.k8sFlags.Flags())
Expand Down Expand Up @@ -78,7 +86,11 @@ func (c *Command) Run(args []string) int {
if c.retryDuration == 0 {
c.retryDuration = 1 * time.Second
}
logger := hclog.Default()
logger, err := common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
}

// Run until we get an address from the service.
var address string
Expand Down Expand Up @@ -125,7 +137,7 @@ func (c *Command) Run(args []string) int {
}

// Write the address to file.
err := ioutil.WriteFile(c.flagOutputFile, []byte(address), 0600)
err = ioutil.WriteFile(c.flagOutputFile, []byte(address), 0600)
if err != nil {
c.UI.Error(fmt.Sprintf("Unable to write address to file: %s", err))
return 1
Expand Down
5 changes: 4 additions & 1 deletion subcommand/sync-catalog/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Command struct {
flagNodePortSyncType string
flagAddK8SNamespaceSuffix bool
flagLogLevel string
flagLogOutputJSON bool

// Flags to support namespaces
flagEnableNamespaces bool // Use namespacing on all components
Expand Down Expand Up @@ -122,6 +123,8 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagLogLevel, "log-level", "info",
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")
c.flags.BoolVar(&c.flagLogOutputJSON, "log-json", false,
"Enable or disable JSON output format for logging.")

c.flags.Var((*flags.AppendSliceValue)(&c.flagAllowK8sNamespacesList), "allow-k8s-namespace",
"K8s namespaces to explicitly allow. May be specified multiple times.")
Expand Down Expand Up @@ -200,7 +203,7 @@ func (c *Command) Run(args []string) int {
// Set up logging
if c.logger == nil {
var err error
c.logger, err = common.Logger(c.flagLogLevel)
c.logger, err = common.Logger(c.flagLogLevel, c.flagLogOutputJSON)
if err != nil {
c.UI.Error(err.Error())
return 1
Expand Down
Loading