Skip to content

Commit

Permalink
enable logging from operator-lib (#608)
Browse files Browse the repository at this point in the history
Uses a logging type from consul to convert from hclog to logr's
LogSink type. This also turned up a missing CluterRole permission for
getting Nodes from k8s.
  • Loading branch information
tvoran authored Mar 19, 2024
1 parent 424c05b commit 43b6866
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

Bugs:
* Enable logging from operator-lib's leader election (used during auto-tls certificate generation) [GH-608](https://github.com/hashicorp/vault-k8s/pull/608)

## 1.4.0 (March 4, 2024)

Features:
Expand Down
4 changes: 4 additions & 0 deletions deploy/injector-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ rules:
- "list"
- "watch"
- "patch"
- apiGroups: [""]
resources: ["nodes"]
verbs:
- "get"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/cenkalti/backoff/v4 v4.2.1
github.com/evanphx/json-patch v5.9.0+incompatible
github.com/go-logr/logr v1.3.0
github.com/hashicorp/go-hclog v1.6.2
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.8
github.com/hashicorp/go-secure-stdlib/tlsutil v0.1.3
Expand All @@ -21,6 +22,7 @@ require (
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.2
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.16.3
)

require (
Expand All @@ -35,7 +37,6 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand Down Expand Up @@ -85,7 +86,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
sigs.k8s.io/controller-runtime v0.16.3 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-hclog"
operator_leader "github.com/operator-framework/operator-lib/leader"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/log"
)

type Elector interface {
Expand Down Expand Up @@ -39,6 +40,7 @@ func New(ctx context.Context, logger hclog.Logger, clientset kubernetes.Interfac
expBo := backoff.NewExponentialBackOff()
expBo.MaxInterval = time.Second * 30
bo := backoff.WithMaxRetries(expBo, 10)
log.SetLogger(fromHCLogger(logger.Named("operator-lib")))

err := backoff.Retry(func() error {
if err := operator_leader.Become(ctx, "vault-k8s-leader"); err != nil {
Expand All @@ -47,7 +49,6 @@ func New(ctx context.Context, logger hclog.Logger, clientset kubernetes.Interfac
}
return nil
}, bo)

if err != nil {
// Signal the caller to shutdown the injector server, since Become()
// failed all the retries
Expand Down
52 changes: 52 additions & 0 deletions leader/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// Borrowed from https://github.com/hashicorp/consul-api-gateway/blob/4ca5788fa357e389336049bd652f28309ee29d4a/internal/k8s/logger.go

package leader

import (
"github.com/go-logr/logr"

"github.com/hashicorp/go-hclog"
)

func fromHCLogger(log hclog.Logger) logr.Logger {
return logr.New(&logger{log})
}

// logger is a LogSink that wraps hclog
type logger struct {
hclog.Logger
}

// Verify that it actually implements the interface
var _ logr.LogSink = logger{}

func (l logger) Init(logr.RuntimeInfo) {
}

func (l logger) Enabled(_ int) bool {
return true
}

// Info actually logs as debug here, since operator-lib's Info logs are pretty
// chatty, and seem to fit better as debug
func (l logger) Info(_ int, msg string, keysAndValues ...interface{}) {
if l.Logger.GetLevel() <= hclog.Debug {
l.Logger.Debug(msg, keysAndValues...)
}
}

func (l logger) Error(err error, msg string, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{"error", err}, keysAndValues...)
l.Logger.Error(msg, keysAndValues...)
}

func (l logger) WithValues(keysAndValues ...interface{}) logr.LogSink {
return &logger{l.With(keysAndValues...)}
}

func (l logger) WithName(name string) logr.LogSink {
return &logger{l.Named(name)}
}

0 comments on commit 43b6866

Please sign in to comment.