Skip to content

Commit

Permalink
client: allow specifying a logger
Browse files Browse the repository at this point in the history
Uses logr as the interface, as it seems to support most of what people
like.

Signed-off-by: Casey Callendrello <cdc@redhat.com>
  • Loading branch information
squeed committed Aug 30, 2021
1 parent 3ad9102 commit 454041d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
15 changes: 7 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/url"
"reflect"
Expand Down Expand Up @@ -176,7 +175,7 @@ func (o *ovsdbClient) connect(ctx context.Context, reconnect bool) error {
fmt.Errorf("failed to connect to %s: %w", endpoint, err))
continue
} else {
log.Printf("libovsdb: connected to %s", endpoint)
o.options.logger.V(3).Info("successfully connected", "endpoint", endpoint)
o.activeEndpoint = endpoint
connected = true
break
Expand All @@ -197,7 +196,7 @@ func (o *ovsdbClient) connect(ctx context.Context, reconnect bool) error {

// if we're reconnecting, re-start all the monitors
if reconnect {
log.Printf("libovsdb: reconnected - restarting monitors")
o.options.logger.V(5).Info("reconnected - restarting monitors")
for dbName, db := range o.databases {
db.monitorsMutex.Lock()
defer db.monitorsMutex.Unlock()
Expand All @@ -219,7 +218,7 @@ func (o *ovsdbClient) connect(ctx context.Context, reconnect bool) error {
}

func (o *ovsdbClient) tryEndpoint(ctx context.Context, u *url.URL) error {
log.Printf("libovsdb: trying to connect to DB %s", u)
o.options.logger.V(5).Info("trying to connect", "endpoint", u)
var dialer net.Dialer
var err error
var c net.Conn
Expand Down Expand Up @@ -392,7 +391,7 @@ func (o *ovsdbClient) isEndpointLeader(ctx context.Context) (bool, error) {

// Extremely unlikely: there is no _Server row for the desired DB (which we made sure existed)
// for now, just continue
log.Println("libovsdb: couldn't find a matching entry in _Server!")
o.options.logger.V(3).Info("couldn't find a matching entry in _Server.")
return true, nil
}

Expand Down Expand Up @@ -739,7 +738,7 @@ func (o *ovsdbClient) watchForLeaderChange() error {
}

if dbInfo.Model == serverdb.DatabaseModelClustered && !dbInfo.Leader {
log.Printf("libovsdb: endpoint %s lost leader, reconnecting", o.activeEndpoint)
o.options.logger.V(2).Info("lost leader, disconnecting", "endpoint", o.activeEndpoint)
o.Disconnect()
}
}
Expand All @@ -760,11 +759,11 @@ func (o *ovsdbClient) handleDisconnectNotification() {
defer cancel()
err := o.connect(ctx, true)
if err != nil {
log.Printf("libovsdb: failed to reconnect: %s", err)
o.options.logger.V(3).Error(err, "failed to reconnect")
}
return err
}
log.Printf("libovsdb: connection to %s lost, reconnecting...", o.activeEndpoint)
o.options.logger.V(2).Info("connection lost, reconnecting...", "endpoint", o.activeEndpoint)
err := backoff.Retry(connect, o.options.backoff)
if err != nil {
// TODO: We should look at passing this back to the
Expand Down
15 changes: 15 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package client

import (
"crypto/tls"
"log"
"net/url"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
)

const (
Expand All @@ -21,6 +24,7 @@ type options struct {
leaderOnly bool
timeout time.Duration
backoff backoff.BackOff
logger logr.Logger
}

type Option func(o *options) error
Expand All @@ -36,6 +40,8 @@ func newOptions(opts ...Option) (*options, error) {
if len(o.endpoints) == 0 {
o.endpoints = []string{defaultUnixEndpoint}
}

o.logger = stdr.New(log.Default()).WithName("libovsdb")
return o, nil
}

Expand Down Expand Up @@ -101,3 +107,12 @@ func WithReconnect(timeout time.Duration, backoff backoff.BackOff) Option {
return nil
}
}

// WithLogger sets the connection's logger. Otherwise, the standard
// log.DefaultLogger() is used.
func WithLogger(l logr.Logger) Option {
return func(o *options) error {
o.logger = l
return nil
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/cenkalti/backoff/v4 v4.1.1
github.com/cenkalti/hub v1.0.1 // indirect
github.com/cenkalti/rpc2 v0.0.0-20210604223624-c1acbc6ec984
github.com/go-logr/logr v1.1.0 // indirect
github.com/go-logr/stdr v1.1.0 // indirect
github.com/google/uuid v1.2.0
github.com/ory/dockertest/v3 v3.7.0
github.com/stretchr/testify v1.6.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/go-logr/logr v1.1.0 h1:nAbevmWlS2Ic4m4+/An5NXkaGqlqpbBgdcuThZxnZyI=
github.com/go-logr/logr v1.1.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.1.0 h1:WSypkOvL7AfqHep42aGGLagyxUjJCQFFs/2nIitlVTQ=
github.com/go-logr/stdr v1.1.0/go.mod h1:Xff/PTSzmJ+zDsu/KDy4l6Axizfso1w7QcxLnWTdto4=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down

0 comments on commit 454041d

Please sign in to comment.