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 or an error if level is invalid and enables JSON logging if requested.
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-output-json", false,
kschoche marked this conversation as resolved.
Show resolved Hide resolved
"Toggle for logging to be output in JSON format.")
kschoche marked this conversation as resolved.
Show resolved Hide resolved

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-output-json", false,
"Toggle for logging to be output in JSON format.")

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
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-output-json", false,
"Toggle for logging to be output in JSON format.")

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-output-json", false,
"Toggle for logging to be output in JSON format.")
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-output-json", false,
"Toggle for logging to be output in JSON format.")

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
9 changes: 6 additions & 3 deletions subcommand/server-acl-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,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

clientset kubernetes.Interface

Expand Down Expand Up @@ -197,6 +198,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-output-json", false,
"Toggle for logging to be output in JSON format.")

c.k8s = &k8sflags.K8SFlags{}
flags.Merge(c.flags, c.k8s.Flags())
Expand Down Expand Up @@ -271,7 +274,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 @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/hashicorp/consul-k8s/subcommand/common"
"io/ioutil"
"net"
"sync"
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-output-json", false,
"Toggle for logging to be output in JSON format.")

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-output-json", false,
"Toggle for logging to be output in JSON format.")

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
9 changes: 6 additions & 3 deletions subcommand/tls-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ type Command struct {
flagNamePrefix string

// log
log hclog.Logger
flagLogLevel string
log hclog.Logger
flagLogLevel string
flagLogOutputJSON bool

ctx context.Context

Expand All @@ -77,7 +78,7 @@ func (c *Command) Run(args []string) int {
}

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 Expand Up @@ -293,6 +294,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-output-json", false,
"Toggle for logging to be output in JSON format.")
c.k8sFlags = &flags.K8SFlags{}
flags.Merge(c.flags, c.k8sFlags.Flags())
c.help = flags.Usage(help, c.flags)
Expand Down
9 changes: 6 additions & 3 deletions subcommand/webhook-cert-manager/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type Command struct {
flagSet *flag.FlagSet
k8s *flags.K8SFlags

flagConfigFile string
flagLogLevel string
flagConfigFile string
flagLogLevel string
flagLogOutputJSON bool

clientset kubernetes.Interface

Expand All @@ -62,6 +63,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-output-json", false,
"Toggle for logging to be output in JSON format.")

c.k8s = &flags.K8SFlags{}
flags.Merge(c.flagSet, c.k8s.Flags())
Expand Down Expand Up @@ -108,7 +111,7 @@ func (c *Command) Run(args []string) int {

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