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 support for json logging #50

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 deploy/injector-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions subcommand/injector/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
14 changes: 11 additions & 3 deletions subcommand/injector/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

const (
DefaultLogLevel = "info"
DefaultLogLevel = "info"
DefaultLogFormat = "standard"
)

// Specification are the supported environment variables, prefixed with
Expand All @@ -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"`

Expand All @@ -45,14 +49,15 @@ type Specification struct {

// VaultAuthPath is the AGENT_INJECT_VAULT_AUTH_PATH environment variable.
VaultAuthPath string `split_words:"true"`

}

func (c *Command) init() {
c.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
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. "+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because log-level is redefined here (it should be log-format), this is causing the injector to panic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flag redefined: log-level
panic: flag redefined: log-level

goroutine 1 [running]:
flag.(*FlagSet).Var(0xc0001ea5a0, 0x1435de0, 0xc0001fa210, 0x1291171, 0x9, 0x12cd800, 0x3d)
	/usr/local/Cellar/go/1.12.7/libexec/src/flag/flag.go:850 +0x4af
flag.(*FlagSet).StringVar(...)
	/usr/local/Cellar/go/1.12.7/libexec/src/flag/flag.go:753
github.com/hashicorp/vault-k8s/subcommand/injector.(*Command).init(0xc0001fa1e0)
	/Users/jasonodonnell/Git/vault-k8s/subcommand/injector/flags.go:59 +0x1b5
sync.(*Once).Do(0xc0001fa298, 0xc00006bdd0)
	/usr/local/Cellar/go/1.12.7/libexec/src/sync/once.go:44 +0xb3
github.com/hashicorp/vault-k8s/subcommand/injector.(*Command).Run(0xc0001fa1e0, 0xc00003a0b0, 0x1, 0x1, 0x0)
	/Users/jasonodonnell/Git/vault-k8s/subcommand/injector/command.go:53 +0xe1
github.com/mitchellh/cli.(*CLI).Run(0xc0001afe00, 0x9, 0xc0001d8400, 0x128fea8)
	/Users/jasonodonnell/go/pkg/mod/github.com/mitchellh/[email protected]/cli.go:255 +0x1f1
main.main()
	/Users/jasonodonnell/Git/vault-k8s/main.go:17 +0x16b

`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", "",
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions subcommand/injector/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down