Skip to content

Commit

Permalink
Configure controller-runtime to log with klog (#626)
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Wall <[email protected]>
  • Loading branch information
wallrj authored Nov 21, 2024
1 parent ab786eb commit eb3c30a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions hack/e2e/values.venafi-kubernetes-agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ authentication:

extraArgs:
- --logging-format=json
- --log-level=2
7 changes: 0 additions & 7 deletions pkg/client/client_venconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"

Check failure on line 12 in pkg/client/client_venconn.go

View workflow job for this annotation

GitHub Actions / govulncheck

could not import net/http (no metadata for net/http)
"time"

"github.com/go-logr/logr"
venapi "github.com/jetstack/venafi-connection-lib/api/v1alpha1"

Check failure on line 15 in pkg/client/client_venconn.go

View workflow job for this annotation

GitHub Actions / govulncheck

github.com/jetstack/[email protected]: invalid version: git ls-remote -q origin in /home/runner/go/pkg/mod/cache/vcs/e1445deb79b4ca14ee954e0f79f4034748b368f02a81510920a656b6ce2c6380: exit status 128:
"github.com/jetstack/venafi-connection-lib/venafi_client"
"github.com/jetstack/venafi-connection-lib/venafi_client/auth"
Expand All @@ -22,7 +21,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/jetstack/preflight/api"
"github.com/jetstack/preflight/pkg/version"
Expand Down Expand Up @@ -57,11 +55,6 @@ type VenConnClient struct {
// `restcfg` is not mutated. `trustedCAs` is only used for connecting to Venafi
// Cloud and Vault and can be left nil.
func NewVenConnClient(restcfg *rest.Config, agentMetadata *api.AgentMetadata, installNS, venConnName, venConnNS string, trustedCAs *x509.CertPool, disableCompression bool) (*VenConnClient, error) {
// TODO(mael): The rest of the codebase uses the standard "log" package,
// venafi-connection-lib uses "go-logr/logr", and client-go uses "klog". We
// should standardize on one of them, probably "slog".
ctrlruntimelog.SetLogger(logr.Logger{})

if installNS == "" {
return nil, errors.New("programmer mistake: installNS must be provided")
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"
)

// venafi-kubernetes-agent follows [Kubernetes Logging Conventions] and writes
Expand Down Expand Up @@ -131,6 +133,11 @@ func Initialize() error {
// to the global log logger. It can be removed when this is fixed upstream
// in vcert: https://github.com/Venafi/vcert/pull/512
vcertLog.SetPrefix("")

// The venafi-connection-lib client uses various controller-runtime packages
// which emit log messages. Make sure those log messages are not discarded.
ctrlruntimelog.SetLogger(klog.Background().WithValues("source", "controller-runtime"))

return nil
}

Expand Down

0 comments on commit eb3c30a

Please sign in to comment.