Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.
Open
Changes from all 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
25 changes: 25 additions & 0 deletions cmd/dranet/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sync/atomic"
"syscall"

nrilog "github.com/containerd/nri/pkg/log"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/ext"
"github.com/google/dranet/pkg/driver"
Expand Down Expand Up @@ -70,6 +71,7 @@ func init() {
func main() {
klog.InitFlags(nil)
flag.Parse()
nrilog.Set(&nriLogger{logger: klog.Background().WithName("nri")})

printVersion()
flag.VisitAll(func(f *flag.Flag) {
Expand Down Expand Up @@ -184,3 +186,26 @@ func printVersion() {
}
klog.Infof("dranet go %s build: %s time: %s", info.GoVersion, vcsRevision, vcsTime)
}

// nriLogger is a klog-based implementation of the NRI logger interface, which
// allows the NRI library to log messages with the same structured format as the
// rest of the application.
type nriLogger struct {
logger klog.Logger
}

func (l *nriLogger) Debugf(ctx context.Context, format string, args ...interface{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need contexts for these? I don't see that in klog from what I can tell, e.g https://pkg.go.dev/k8s.io/klog/v2#Errorf

Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAIK klog and logr normally take context on constructing a logger, not on individual logf style calls.

l.logger.V(5).Info(fmt.Sprintf(format, args...))
}

func (l *nriLogger) Infof(ctx context.Context, format string, args ...interface{}) {
l.logger.Info(fmt.Sprintf(format, args...))
}

func (l *nriLogger) Warnf(ctx context.Context, format string, args ...interface{}) {
l.logger.Error(fmt.Errorf(format, args...), "NRI warning")
}

func (l *nriLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
l.logger.Error(fmt.Errorf(format, args...), "NRI error")
}
Loading