From 1a102e2eab30201bc50f287104280aff2ffcb914 Mon Sep 17 00:00:00 2001 From: Leon Albers Date: Tue, 14 Jan 2020 15:59:56 +0100 Subject: [PATCH 1/5] Add support for json logging --- deploy/injector-deployment.yaml | 2 ++ subcommand/injector/command.go | 7 +++++-- subcommand/injector/flags.go | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/deploy/injector-deployment.yaml b/deploy/injector-deployment.yaml index 013ce079..3b0fddfe 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: "json" - 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 dfab3a27..168e1744 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 @@ -101,8 +102,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 557a130b..8613ba53 100644 --- a/subcommand/injector/flags.go +++ b/subcommand/injector/flags.go @@ -25,6 +25,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"` @@ -83,7 +86,6 @@ func (c *Command) logLevel() (hclog.Level, error) { default: return level, fmt.Errorf("unknown log level: %s", c.flagLogLevel) } - return level, nil } @@ -103,6 +105,10 @@ func (c *Command) parseEnvs() error { c.flagLogLevel = envs.LogLevel } + if envs.LogFormat != "" { + c.flagLogFormat = envs.LogFormat + } + if envs.TLSAuto != "" { c.flagAutoName = envs.TLSAuto } From 9f2103afc9dc0df51899587d8ec0f7c025b4f3a9 Mon Sep 17 00:00:00 2001 From: Leon Albers Date: Wed, 22 Jan 2020 10:57:05 +0100 Subject: [PATCH 2/5] adjust default logging format to txt --- deploy/injector-deployment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/injector-deployment.yaml b/deploy/injector-deployment.yaml index 3b0fddfe..3feca55b 100644 --- a/deploy/injector-deployment.yaml +++ b/deploy/injector-deployment.yaml @@ -33,7 +33,7 @@ spec: - name: AGENT_INJECT_LOG_LEVEL value: "info" - name: AGENT_INJECT_LOG_FORMAT - - value: "json" + value: "standard" - name: AGENT_INJECT_VAULT_ADDR value: "https://vault.$(NAMESPACE).svc:8200" - name: AGENT_INJECT_VAULT_IMAGE From e37f619f1e9ec4c4696cb7f4b4cf3b4d2d1f8862 Mon Sep 17 00:00:00 2001 From: Leon Albers Date: Wed, 22 Jan 2020 11:09:55 +0100 Subject: [PATCH 3/5] add unit test for log_format env --- subcommand/injector/command.go | 20 ++++++++++---------- subcommand/injector/flags.go | 6 ++++-- subcommand/injector/flags_test.go | 1 + 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/subcommand/injector/command.go b/subcommand/injector/command.go index d4636f0f..9ff82317 100644 --- a/subcommand/injector/command.go +++ b/subcommand/injector/command.go @@ -27,16 +27,16 @@ import ( type Command struct { UI cli.Ui - flagListen string // Address of Vault Server - flagLogLevel string // Log verbosity - lagLogFormat string // Log format - flagCertFile string // TLS Certificate to serve - flagKeyFile string // TLS private key to serve - flagAutoName string // MutatingWebhookConfiguration for updating - flagAutoHosts string // SANs for the auto-generated TLS cert. - flagVaultService string // Name of the Vault service - flagVaultImage string // Name of the Vault Image to use - flagVaultAuthPath string // Mount Path of the Vault Kubernetes Auth Method + 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 + flagAutoHosts string // SANs for the auto-generated TLS cert. + flagVaultService string // Name of the Vault service + flagVaultImage string // Name of the Vault Image to use + flagVaultAuthPath string // Mount Path of the Vault Kubernetes Auth Method flagSet *flag.FlagSet diff --git a/subcommand/injector/flags.go b/subcommand/injector/flags.go index c2cf822a..31310b79 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 @@ -48,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() { @@ -56,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-level", 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", "", 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 { From a77f6af46a96c092004e74e2c347b539a39715d9 Mon Sep 17 00:00:00 2001 From: Leon Albers Date: Wed, 22 Jan 2020 11:23:30 +0100 Subject: [PATCH 4/5] correct code indents for command.go --- subcommand/injector/command.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/subcommand/injector/command.go b/subcommand/injector/command.go index 9ff82317..be18f274 100644 --- a/subcommand/injector/command.go +++ b/subcommand/injector/command.go @@ -27,16 +27,16 @@ import ( type Command struct { UI cli.Ui - 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 - flagAutoHosts string // SANs for the auto-generated TLS cert. - flagVaultService string // Name of the Vault service - flagVaultImage string // Name of the Vault Image to use - flagVaultAuthPath string // Mount Path of the Vault Kubernetes Auth Method + 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 + flagAutoHosts string // SANs for the auto-generated TLS cert. + flagVaultService string // Name of the Vault service + flagVaultImage string // Name of the Vault Image to use + flagVaultAuthPath string // Mount Path of the Vault Kubernetes Auth Method flagSet *flag.FlagSet From 66e4c0eee7896bc3ac9ca3166826e2056b713aac Mon Sep 17 00:00:00 2001 From: Leon Albers <5120677+lalbers@users.noreply.github.com> Date: Fri, 14 Feb 2020 11:49:33 +0100 Subject: [PATCH 5/5] Update flags.go --- subcommand/injector/flags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subcommand/injector/flags.go b/subcommand/injector/flags.go index 31310b79..bfb7449b 100644 --- a/subcommand/injector/flags.go +++ b/subcommand/injector/flags.go @@ -56,7 +56,7 @@ 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-level", DefaultLogFormat, "Log output format. "+ + 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.")