Skip to content

Commit 610abc5

Browse files
author
Lars Ekman
committed
Use structured logging in ipam
1 parent 7f0ae28 commit 610abc5

File tree

5 files changed

+47
-44
lines changed

5 files changed

+47
-44
lines changed

cmd/ipam/main.go

+19-28
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ import (
2626
"strings"
2727
"syscall"
2828

29+
"github.com/go-logr/logr"
2930
"github.com/kelseyhightower/envconfig"
3031
ipamAPI "github.com/nordix/meridio/api/ipam/v1"
3132
"github.com/nordix/meridio/pkg/health"
3233
"github.com/nordix/meridio/pkg/health/connection"
3334
"github.com/nordix/meridio/pkg/health/probe"
3435
"github.com/nordix/meridio/pkg/ipam"
3536
"github.com/nordix/meridio/pkg/ipam/types"
37+
"github.com/nordix/meridio/pkg/log"
3638
"github.com/nordix/meridio/pkg/security/credentials"
37-
"github.com/sirupsen/logrus"
3839
"google.golang.org/grpc"
3940
grpcHealth "google.golang.org/grpc/health"
4041
"google.golang.org/grpc/health/grpc_health_v1"
@@ -63,11 +64,16 @@ func main() {
6364
os.Exit(0)
6465
}
6566

66-
logrus.SetOutput(os.Stdout)
67-
logrus.SetLevel(logrus.DebugLevel)
67+
var config Config
68+
err := envconfig.Process("ipam", &config)
69+
if err != nil {
70+
panic(err)
71+
}
72+
logger := log.New("Meridio-ipam", config.LogLevel)
73+
logger.Info("Configuration read", "config", config)
6874

6975
ctx, cancel := signal.NotifyContext(
70-
context.Background(),
76+
logr.NewContext(context.Background(), logger),
7177
os.Interrupt,
7278
syscall.SIGHUP,
7379
syscall.SIGTERM,
@@ -78,25 +84,9 @@ func main() {
7884
// create and start health server
7985
ctx = health.CreateChecker(ctx)
8086
if err := health.RegisterReadinesSubservices(ctx, health.IPAMReadinessServices...); err != nil {
81-
logrus.Warnf("%v", err)
87+
logger.Error(err, "RegisterReadinesSubservices")
8288
}
8389

84-
var config Config
85-
err := envconfig.Process("ipam", &config)
86-
if err != nil {
87-
logrus.Fatalf("%v", err)
88-
}
89-
logrus.Infof("rootConf: %+v", config)
90-
91-
logrus.SetLevel(func() logrus.Level {
92-
93-
l, err := logrus.ParseLevel(config.LogLevel)
94-
if err != nil {
95-
logrus.Fatalf("invalid log level %s", config.LogLevel)
96-
}
97-
return l
98-
}())
99-
10090
// connect NSP
10191
conn, err := grpc.DialContext(
10292
ctx,
@@ -108,13 +98,13 @@ func main() {
10898
grpc.WaitForReady(true),
10999
))
110100
if err != nil {
111-
logrus.Fatalf("Dial NSP err: %v", err)
101+
log.Fatal(logger, "Dial NSP err", "error", err)
112102
}
113103
defer conn.Close()
114104

115105
// monitor status of NSP connection and adjust probe status accordingly
116106
if err := connection.Monitor(ctx, health.NSPCliSvc, conn); err != nil {
117-
logrus.Warnf("NSP connection state monitor err: %v", err)
107+
logger.Error(err, "NSP connection state monitor")
118108
}
119109

120110
prefixLengths := make(map[ipamAPI.IPFamily]*types.PrefixLengths)
@@ -133,9 +123,10 @@ func main() {
133123
}
134124

135125
// cteate IPAM server
136-
ipamServer, err := ipam.NewServer(config.Datasource, config.TrenchName, conn, cidrs, prefixLengths)
126+
ipamServer, err := ipam.NewServer(
127+
ctx, config.Datasource, config.TrenchName, conn, cidrs, prefixLengths)
137128
if err != nil {
138-
logrus.Fatalf("Unable to create ipam server: %v", err)
129+
logger.Error(err, "Unable to create ipam server")
139130
}
140131

141132
server := grpc.NewServer(grpc.Creds(
@@ -145,17 +136,17 @@ func main() {
145136
healthServer := grpcHealth.NewServer()
146137
grpc_health_v1.RegisterHealthServer(server, healthServer)
147138

148-
logrus.Infof("IPAM Service: Start the service (port: %v)", config.Port)
139+
logger.Info("Start the service", "port", config.Port)
149140
listener, err := net.Listen("tcp", fmt.Sprintf("[::]:%d", config.Port))
150141
if err != nil {
151-
logrus.Fatalf("IPAM Service: failed to listen: %v", err)
142+
log.Fatal(logger, "Failed to listen", "error", err)
152143
}
153144

154145
// internal probe checking health of IPAM server
155146
probe.CreateAndRunGRPCHealthProbe(ctx, health.IPAMSvc, probe.WithAddress(fmt.Sprintf(":%d", config.Port)), probe.WithSpiffe())
156147

157148
if err := startServer(ctx, server, listener); err != nil {
158-
logrus.Errorf("IPAM Service: failed to serve: %v", err)
149+
logger.Error(err, "IPAM Service: failed to serve: %v")
159150
}
160151
}
161152

deployments/helm/templates/ipam.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ spec:
5656
value: "{{ .Values.subnetPool.nodePrefixLength.ipv6 }}"
5757
- name: IPAM_IP_FAMILY
5858
value: "{{ .Values.ipFamily }}"
59+
- name: LOG_FORMAT
60+
value: "ADP"
5961
securityContext:
6062
runAsNonRoot: true
6163
readOnlyRootFilesystem: true

pkg/health/checker.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"path"
2727
"sync"
2828

29-
"github.com/sirupsen/logrus"
29+
"github.com/nordix/meridio/pkg/log"
3030
"google.golang.org/grpc"
3131
"google.golang.org/grpc/health"
3232
"google.golang.org/grpc/health/grpc_health_v1"
@@ -57,7 +57,10 @@ func (c *Checker) RegisterServices(probeSvc string, services ...string) {
5757
c.mu.Lock()
5858
defer c.mu.Unlock()
5959

60-
logrus.Debugf("Health server: Register subservices for probeSvc=%v, services=%v", probeSvc, services)
60+
logger := log.FromContextOrDefault(c.ctx)
61+
logger.V(1).Info(
62+
"Health server: Register subservices", "probeSvc", probeSvc,
63+
"services", services)
6164

6265
components, ok := c.subServices[probeSvc]
6366
if !ok {
@@ -79,7 +82,6 @@ func (c *Checker) RegisterServices(probeSvc string, services ...string) {
7982
// Re-implements Check() function of grpc_health_v1.HealthServer interface
8083
// in order to log if a probe service is about to fail.
8184
func (c *Checker) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
82-
//logrus.Debugf("Checker: Check service=%v", in.Service)
8385

8486
switch in.Service {
8587
case Startup:
@@ -103,7 +105,10 @@ func (c *Checker) Check(ctx context.Context, in *grpc_health_v1.HealthCheckReque
103105
}
104106
svcInfo += `]`
105107
}
106-
logrus.Infof(`Probe service "%v" [%v]%v`, in.Service, resp.Status, svcInfo)
108+
logger := log.FromContextOrDefault(c.ctx)
109+
logger.Info(
110+
"Probe service", "Service", in.Service,
111+
"Status", resp.Status, "svcInfo", svcInfo)
107112
}
108113
return resp, err
109114
default:
@@ -117,7 +122,6 @@ func (c *Checker) SetServingStatus(service string, servingStatus grpc_health_v1.
117122
c.mu.Lock()
118123
defer c.mu.Unlock()
119124

120-
//logrus.Debugf("Checker: Set serving status for service=%v, status=%v", service, servingStatus)
121125
switch service {
122126
case Startup:
123127
fallthrough
@@ -149,7 +153,6 @@ func (c *Checker) SetServingStatus(service string, servingStatus grpc_health_v1.
149153
}
150154
}
151155
}
152-
//logrus.Debugf(`SetServingStatus: probe service "%v" [%v]`, probeSvc, probeStatus)
153156
c.HealthServerStatusModifier.SetServingStatus(probeSvc, probeStatus)
154157
}
155158
}

pkg/ipam/server.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,35 @@ import (
2121
"fmt"
2222
"net"
2323

24+
"github.com/go-logr/logr"
2425
ipamAPI "github.com/nordix/meridio/api/ipam/v1"
2526
"github.com/nordix/meridio/pkg/ipam/prefix"
2627
"github.com/nordix/meridio/pkg/ipam/storage/sqlite"
2728
"github.com/nordix/meridio/pkg/ipam/trench"
2829
"github.com/nordix/meridio/pkg/ipam/types"
29-
"github.com/sirupsen/logrus"
3030
"google.golang.org/grpc"
3131
"google.golang.org/protobuf/types/known/emptypb"
3232
)
3333

3434
type IpamServer struct {
35+
ctx context.Context
36+
logger logr.Logger
3537
ipamAPI.UnimplementedIpamServer
3638
Trenches map[ipamAPI.IPFamily]types.Trench
3739
PrefixLengths map[ipamAPI.IPFamily]*types.PrefixLengths
3840
}
3941

4042
// NewIpam -
41-
func NewServer(datastore string,
43+
func NewServer(
44+
ctx context.Context,
45+
datastore string,
4246
trenchName string,
4347
nspConn *grpc.ClientConn,
4448
cidrs map[ipamAPI.IPFamily]string,
4549
prefixLengths map[ipamAPI.IPFamily]*types.PrefixLengths) (ipamAPI.IpamServer, error) {
4650
is := &IpamServer{
51+
ctx: ctx,
52+
logger: logr.FromContextOrDiscard(ctx),
4753
Trenches: make(map[ipamAPI.IPFamily]types.Trench),
4854
PrefixLengths: prefixLengths,
4955
}
@@ -56,14 +62,14 @@ func NewServer(datastore string,
5662
for ipFamily, cidr := range cidrs {
5763
name := getTrenchName(trenchName, ipFamily)
5864
prefix := prefix.New(name, cidr, nil)
59-
newTrench, err := trench.New(context.TODO(), prefix, store, is.PrefixLengths[ipFamily])
65+
newTrench, err := trench.New(ctx, prefix, store, is.PrefixLengths[ipFamily])
6066
if err != nil {
6167
return nil, err
6268
}
6369
is.Trenches[ipFamily] = newTrench
6470
trenchWatchers = append(trenchWatchers, newTrench)
6571
}
66-
conduitWatcher, err := trench.NewConduitWatcher(context.TODO(), nspConn, trenchName, trenchWatchers)
72+
conduitWatcher, err := trench.NewConduitWatcher(ctx, nspConn, trenchName, trenchWatchers)
6773
if err != nil {
6874
return nil, err
6975
}
@@ -73,7 +79,7 @@ func NewServer(datastore string,
7379
}
7480

7581
func (is *IpamServer) Allocate(ctx context.Context, child *ipamAPI.Child) (*ipamAPI.Prefix, error) {
76-
logrus.Infof("Allocate: %v", child)
82+
is.logger.Info("Allocate", "child", child)
7783
trench, exists := is.Trenches[child.GetSubnet().GetIpFamily()]
7884
if !exists {
7985
return nil, fmt.Errorf("cannot allocate in this ip family")
@@ -120,7 +126,7 @@ func (is *IpamServer) Allocate(ctx context.Context, child *ipamAPI.Child) (*ipam
120126
}
121127

122128
func (is *IpamServer) Release(ctx context.Context, child *ipamAPI.Child) (*emptypb.Empty, error) {
123-
logrus.Infof("Release: %v", child)
129+
is.logger.Info("Release", "child", child)
124130
trench, exists := is.Trenches[child.GetSubnet().GetIpFamily()]
125131
if !exists {
126132
return &emptypb.Empty{}, nil

pkg/security/credentials/credentials.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package credentials
33
import (
44
"context"
55

6-
"github.com/sirupsen/logrus"
6+
"github.com/nordix/meridio/pkg/log"
77
"github.com/spiffe/go-spiffe/v2/spiffegrpc/grpccredentials"
88
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
99
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
@@ -30,15 +30,16 @@ func GetServerWithSource(ctx context.Context, source *workloadapi.X509Source) cr
3030

3131
func GetX509Source(ctx context.Context) *workloadapi.X509Source {
3232
// todo: retry if source in nil or empty
33+
logger := log.FromContextOrDefault(ctx)
3334
source, err := workloadapi.NewX509Source(ctx)
3435
if err != nil {
35-
logrus.Errorf("error getting x509 source: %v", err)
36+
logger.Error(err, "error getting x509 source")
3637
}
3738
var svid *x509svid.SVID
3839
svid, err = source.GetX509SVID()
3940
if err != nil {
40-
logrus.Errorf("error getting x509 svid: %v", err)
41+
logger.Error(err, "error getting x509 svid")
4142
}
42-
logrus.Infof("sVID: %q", svid.ID)
43+
logger.Info("GetX509Source", "sVID", svid.ID)
4344
return source
4445
}

0 commit comments

Comments
 (0)