diff --git a/deploy/injector-deployment.yaml b/deploy/injector-deployment.yaml index 2a4c53c9..154d4217 100644 --- a/deploy/injector-deployment.yaml +++ b/deploy/injector-deployment.yaml @@ -32,6 +32,8 @@ spec: value: ":8080" - name: AGENT_INJECT_LOG_LEVEL value: "info" + - name: AGENT_INJECT_LOG_FORMAT + value: "standard" - name: AGENT_INJECT_VAULT_ADDR value: "https://vault.$(NAMESPACE).svc:8200" - name: AGENT_INJECT_VAULT_IMAGE diff --git a/subcommand/injector/command.go b/subcommand/injector/command.go index 584e78ed..be18f274 100644 --- a/subcommand/injector/command.go +++ b/subcommand/injector/command.go @@ -29,6 +29,7 @@ type Command struct { flagListen string // Address of Vault Server flagLogLevel string // Log verbosity + flagLogFormat string // Log format flagCertFile string // TLS Certificate to serve flagKeyFile string // TLS private key to serve flagAutoName string // MutatingWebhookConfiguration for updating @@ -102,8 +103,10 @@ func (c *Command) Run(args []string) int { return 1 } - logger := hclog.Default().Named("handler") - logger.SetLevel(level) + logger := hclog.New(&hclog.LoggerOptions{ + Name: "handler", + Level: level, + JSONFormat: (c.flagLogFormat == "json")}) // Build the HTTP handler and server injector := agentInject.Handler{ diff --git a/subcommand/injector/flags.go b/subcommand/injector/flags.go index be92f535..bfb7449b 100644 --- a/subcommand/injector/flags.go +++ b/subcommand/injector/flags.go @@ -12,7 +12,8 @@ import ( ) const ( - DefaultLogLevel = "info" + DefaultLogLevel = "info" + DefaultLogFormat = "standard" ) // Specification are the supported environment variables, prefixed with @@ -25,6 +26,9 @@ type Specification struct { // LogLevel is the AGENT_INJECT_LOG_LEVEL environment variable. LogLevel string `split_words:"true"` + // LogFormat is the AGENT_INJECT_LOG_FORMAT environment variable + LogFormat string `split_words:"true"` + // TLSAuto is the AGENT_INJECT_TLS_AUTO environment variable. TLSAuto string `envconfig:"tls_auto"` @@ -45,7 +49,6 @@ type Specification struct { // VaultAuthPath is the AGENT_INJECT_VAULT_AUTH_PATH environment variable. VaultAuthPath string `split_words:"true"` - } func (c *Command) init() { @@ -53,6 +56,8 @@ func (c *Command) init() { c.flagSet.StringVar(&c.flagListen, "listen", ":8080", "Address to bind listener to.") c.flagSet.StringVar(&c.flagLogLevel, "log-level", DefaultLogLevel, "Log verbosity level. Supported values "+ `(in order of detail) are "trace", "debug", "info", "warn", and "err".`) + c.flagSet.StringVar(&c.flagLogFormat, "log-format", DefaultLogFormat, "Log output format. "+ + `Supported log formats: "standard", "json".`) c.flagSet.StringVar(&c.flagAutoName, "tls-auto", "", "MutatingWebhookConfiguration name. If specified, will auto generate cert bundle.") c.flagSet.StringVar(&c.flagAutoHosts, "tls-auto-hosts", "", @@ -89,7 +94,6 @@ func (c *Command) logLevel() (hclog.Level, error) { default: return level, fmt.Errorf("unknown log level: %s", c.flagLogLevel) } - return level, nil } @@ -109,6 +113,10 @@ func (c *Command) parseEnvs() error { c.flagLogLevel = envs.LogLevel } + if envs.LogFormat != "" { + c.flagLogFormat = envs.LogFormat + } + if envs.TLSAuto != "" { c.flagAutoName = envs.TLSAuto } diff --git a/subcommand/injector/flags_test.go b/subcommand/injector/flags_test.go index 82488b3f..891d97ba 100644 --- a/subcommand/injector/flags_test.go +++ b/subcommand/injector/flags_test.go @@ -119,6 +119,7 @@ func TestCommandEnvs(t *testing.T) { {env: "AGENT_INJECT_TLS_AUTO_HOSTS", value: "foobar.com", cmdPtr: &cmd.flagAutoHosts}, {env: "AGENT_INJECT_TLS_AUTO", value: "mutationWebhook", cmdPtr: &cmd.flagAutoName}, {env: "AGENT_INJECT_LOG_LEVEL", value: "info", cmdPtr: &cmd.flagLogLevel}, + {env: "AGENT_INJECT_LOG_FORMAT", value: "standard", cmdPtr: &cmd.flagLogFormat}, } for _, tt := range tests {