Skip to content

Commit 516176c

Browse files
authored
Migrate to slog, get rid of all format based logging (#18)
1 parent 66494bb commit 516176c

File tree

17 files changed

+116
-145
lines changed

17 files changed

+116
-145
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ jobs:
2222

2323
steps:
2424
- name: Log in to the Container registry
25-
uses: docker/login-action@v2
25+
uses: docker/login-action@v3
2626
with:
2727
registry: ${{ env.REGISTRY }}
2828
username: ${{ github.actor }}
2929
password: ${{ secrets.GITHUB_TOKEN }}
3030

3131
- name: Check out code into the Go module directory
32-
uses: actions/checkout@v3
32+
uses: actions/checkout@v4
3333

34-
- name: Set up Go 1.21
34+
- name: Set up Go 1.22
3535
uses: actions/setup-go@v4
3636
with:
37-
go-version: "1.21"
37+
go-version: "1.22"
3838
cache: false
3939

4040
- name: Lint

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# be aware that bookworm has a newer gcc which can not compile the older ipxe
2-
FROM golang:1.21-bullseye as builder
2+
FROM golang:1.22-bullseye as builder
33
WORKDIR /work
44
COPY . .
55
RUN apt update \

dhcp4/conn_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"reflect"
2020
"testing"
2121
"time"
22-
23-
"github.com/stretchr/testify/require"
2422
)
2523

2624
func testConn(t *testing.T, impl conn, addr string) {
@@ -55,7 +53,9 @@ func testConn(t *testing.T, impl conn, addr string) {
5553

5654
go func() {
5755
_, err := s.Write(bs)
58-
require.NoError(t, err)
56+
if err != nil {
57+
panic(err)
58+
}
5959
}()
6060
if err = c.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
6161
t.Fatal(err)
@@ -86,7 +86,9 @@ func testConn(t *testing.T, impl conn, addr string) {
8686
ch := make(chan *Packet, 1)
8787
go func() {
8888
err := s.SetReadDeadline(time.Now().Add(time.Second))
89-
require.NoError(t, err)
89+
if err != nil {
90+
panic(err)
91+
}
9092
var buf [1500]byte
9193
n, err := s.Read(buf[:])
9294
if err != nil {

go.mod

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/metal-stack/pixie
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/metal-stack/metal-api v0.26.3
@@ -10,9 +10,8 @@ require (
1010
github.com/spf13/cobra v1.8.0
1111
github.com/spf13/viper v1.18.2
1212
github.com/stretchr/testify v1.8.4
13-
go.uber.org/zap v1.26.0
14-
golang.org/x/crypto v0.18.0
15-
golang.org/x/net v0.20.0
13+
golang.org/x/crypto v0.19.0
14+
golang.org/x/net v0.21.0
1615
google.golang.org/grpc v1.61.0
1716
)
1817

@@ -39,10 +38,10 @@ require (
3938
github.com/spf13/pflag v1.0.5 // indirect
4039
github.com/subosito/gotenv v1.6.0 // indirect
4140
go.uber.org/multierr v1.11.0 // indirect
42-
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
43-
golang.org/x/sys v0.16.0 // indirect
41+
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect
42+
golang.org/x/sys v0.17.0 // indirect
4443
golang.org/x/text v0.14.0 // indirect
45-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
44+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240205150955-31a09d347014 // indirect
4645
google.golang.org/protobuf v1.32.0 // indirect
4746
gopkg.in/ini.v1 v1.67.0 // indirect
4847
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,25 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
7676
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7777
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
7878
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
79-
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
80-
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
8179
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
8280
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
83-
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
84-
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
8581
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
86-
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
87-
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
88-
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
89-
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
82+
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
83+
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
84+
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 h1:/RIbNt/Zr7rVhIkQhooTxCxFcdWLGIKnZA4IXNFSrvo=
85+
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
9086
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
91-
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
92-
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
87+
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
88+
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
9389
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
94-
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
95-
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
90+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
91+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
9692
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
9793
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
9894
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
9995
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
100-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe h1:bQnxqljG/wqi4NTXu2+DJ3n7APcEA882QZ1JvhQAq9o=
101-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s=
96+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240205150955-31a09d347014 h1:FSL3lRCkhaPFxqi0s9o+V4UI2WTzAVOvkgbd4kVV4Wg=
97+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240205150955-31a09d347014/go.mod h1:SaPjaZGWb0lPqs6Ittu0spdfrOArqji4ZdeP5IC/9N4=
10298
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
10399
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
104100
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=

pixiecore/booters.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"io"
23+
"log/slog"
2324
"net/http"
2425
"net/url"
2526
"os"
@@ -28,8 +29,6 @@ import (
2829
"text/template"
2930
"time"
3031

31-
"go.uber.org/zap"
32-
3332
v1 "github.com/metal-stack/metal-api/pkg/api/v1"
3433
"github.com/metal-stack/pixie/api"
3534
)
@@ -51,7 +50,7 @@ func APIBooter(url string, timeout time.Duration) (Booter, error) {
5150

5251
return ret, nil
5352
}
54-
func GRPCBooter(log *zap.SugaredLogger, client *GrpcClient, partition string, metalAPIConfig *api.MetalConfig) (Booter, error) {
53+
func GRPCBooter(log *slog.Logger, client *GrpcClient, partition string, metalAPIConfig *api.MetalConfig) (Booter, error) {
5554
ret := &grpcbooter{
5655
grpc: client,
5756
partition: partition,
@@ -61,7 +60,7 @@ func GRPCBooter(log *zap.SugaredLogger, client *GrpcClient, partition string, me
6160
if _, err := io.ReadFull(rand.Reader, ret.key[:]); err != nil {
6261
return nil, fmt.Errorf("failed to get randomness for signing key: %w", err)
6362
}
64-
log.Infow("starting grpc booter", "partition", partition)
63+
log.Info("starting grpc booter", "partition", partition)
6564
return ret, nil
6665
}
6766

@@ -76,12 +75,12 @@ type grpcbooter struct {
7675
grpc *GrpcClient
7776
config *api.MetalConfig
7877
partition string
79-
log *zap.SugaredLogger
78+
log *slog.Logger
8079
}
8180

8281
// BootSpec implements Booter
8382
func (g *grpcbooter) BootSpec(m Machine) (*Spec, error) {
84-
g.log.Infow("bootspec", "machine", m)
83+
g.log.Info("bootspec", "machine", m)
8584
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
8685
defer cancel()
8786

@@ -91,10 +90,10 @@ func (g *grpcbooter) BootSpec(m Machine) (*Spec, error) {
9190
req := &v1.BootServiceDhcpRequest{
9291
Uuid: string(m.GUID),
9392
}
94-
g.log.Infow("dhcp", "req", req)
93+
g.log.Info("dhcp", "req", req)
9594
_, err := g.grpc.BootService().Dhcp(ctx, req)
9695
if err != nil {
97-
g.log.Errorw("boot", "error", err)
96+
g.log.Error("boot", "error", err)
9897
return nil, err
9998
}
10099
r = rawSpec{}
@@ -104,13 +103,13 @@ func (g *grpcbooter) BootSpec(m Machine) (*Spec, error) {
104103
Mac: m.MAC.String(),
105104
PartitionId: g.partition,
106105
}
107-
g.log.Infow("boot", "req", req)
106+
g.log.Info("boot", "req", req)
108107
resp, err := g.grpc.BootService().Boot(ctx, req)
109108
if err != nil {
110-
g.log.Errorw("boot", "error", err)
109+
g.log.Error("boot", "error", err)
111110
return nil, err
112111
}
113-
g.log.Infow("boot", "resp", resp)
112+
g.log.Info("boot", "resp", resp)
114113

115114
cmdline := []string{resp.GetCmdline(), fmt.Sprintf("PIXIE_API_URL=%s", g.config.PixieAPIURL)}
116115
if g.config.Debug {
@@ -125,7 +124,7 @@ func (g *grpcbooter) BootSpec(m Machine) (*Spec, error) {
125124
}
126125

127126
spec, err := bootSpec(g.key, g.urlPrefix, r)
128-
g.log.Infow("bootspec", "return spec", spec)
127+
g.log.Info("bootspec", "return spec", spec)
129128
return spec, err
130129
}
131130

pixiecore/cli/cli.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ package cli // import "github.com/metal-stack/pixie/cli"
1717

1818
import (
1919
"fmt"
20+
"log/slog"
2021
"os"
2122

2223
"github.com/metal-stack/pixie/pixiecore"
2324
"github.com/spf13/cobra"
2425
"github.com/spf13/viper"
25-
"go.uber.org/zap"
26-
"go.uber.org/zap/zapcore"
2726
)
2827

29-
// Ipxe is the set of ipxe binaries for supported firmwares.
28+
// Ipxe is the set of ipxe binaries for supported firmware.
3029
//
3130
// Can be set externally before calling CLI(), and set/extended by
32-
// commandline processing in CLI().
31+
// command line processing in CLI().
3332
var Ipxe = map[pixiecore.Firmware][]byte{}
3433

3534
// CLI runs the Pixiecore commandline.
@@ -133,14 +132,10 @@ func serverFromFlags(cmd *cobra.Command) *pixiecore.Server {
133132
if httpPort <= 0 {
134133
fatalf("HTTP port must be >0")
135134
}
136-
log, err := getLogger(debug)
137-
if err != nil {
138-
fatalf("Error creating logging: %s", err)
139-
}
140135

141136
ret := &pixiecore.Server{
142137
Ipxe: map[pixiecore.Firmware][]byte{},
143-
Log: log,
138+
Log: getLogger(debug).WithGroup("dhcp"),
144139
HTTPPort: httpPort,
145140
HTTPStatusPort: httpStatusPort,
146141
MetricsPort: metricsPort,
@@ -170,18 +165,14 @@ func serverFromFlags(cmd *cobra.Command) *pixiecore.Server {
170165
return ret
171166
}
172167

173-
func getLogger(debug bool) (*zap.SugaredLogger, error) {
174-
cfg := zap.NewProductionConfig()
175-
level := zap.InfoLevel
176-
if debug {
177-
level = zap.DebugLevel
168+
func getLogger(debug bool) *slog.Logger {
169+
opts := &slog.HandlerOptions{
170+
Level: slog.LevelInfo,
171+
AddSource: debug,
178172
}
179-
cfg.Level = zap.NewAtomicLevelAt(level)
180-
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
181-
zlog, err := cfg.Build()
182-
if err != nil {
183-
return nil, err
173+
if debug {
174+
opts.Level = slog.LevelDebug
184175
}
185176

186-
return zlog.Sugar(), nil
177+
return slog.New(slog.NewJSONHandler(os.Stdout, opts))
187178
}

pixiecore/cli/ipv6apicmd.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ var ipv6ApiCmd = &cobra.Command{
3636
if err != nil {
3737
fatalf("Error reading flag: %s", err)
3838
}
39-
l, err := getLogger(debug)
40-
if err != nil {
41-
fatalf("Error creating logging: %s", err)
42-
}
43-
s.Log = l
39+
s.Log = getLogger(debug).WithGroup("dhcpv6")
4440

4541
if addr == "" {
4642
fatalf("Please specify address to bind to")

pixiecore/dhcp.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ func (s *Server) serveDHCP(conn *dhcp4.Conn) error {
3333
}
3434

3535
if err = s.isBootDHCP(pkt); err != nil {
36-
s.Log.Debugf("Ignoring packet from %s: %s", pkt.HardwareAddr, err)
36+
s.Log.Debug("Ignoring packet", "mac", pkt.HardwareAddr, "error", err)
3737
continue
3838
}
3939
mach, fwtype, err := s.validateDHCP(pkt)
4040
if err != nil {
41-
s.Log.Infof("Unusable packet from %s: %s", pkt.HardwareAddr, err)
41+
s.Log.Info("Unusable packet", "mac", pkt.HardwareAddr, "error", err)
4242
continue
4343
}
4444

45-
s.Log.Debugf("Got valid request to boot %s (%s, %s)", mach.MAC, mach.GUID, mach.Arch)
45+
s.Log.Debug("Got valid request to boot", "mac", mach.MAC, "guid", mach.GUID, "arch", mach.Arch)
4646

4747
spec, err := s.Booter.BootSpec(mach)
4848
if err != nil {
49-
s.Log.Infof("Couldn't get bootspec for %s: %s", pkt.HardwareAddr, err)
49+
s.Log.Info("Couldn't get bootspec", "mac", pkt.HardwareAddr, "error", err)
5050
continue
5151
}
5252
if spec == nil {
53-
s.Log.Debugf("No boot spec for %s, ignoring boot request", pkt.HardwareAddr)
53+
s.Log.Debug("No boot spec, ignoring boot request", "mac", pkt.HardwareAddr)
5454
s.machineEvent(pkt.HardwareAddr, machineStateIgnored, "Machine should not netboot")
5555
continue
5656
}
5757

58-
s.Log.Infof("Offering to boot %s", pkt.HardwareAddr)
58+
s.Log.Info("Offering to boot", "mac", pkt.HardwareAddr)
5959
if fwtype == FirmwarePixiecoreIpxe {
6060
s.machineEvent(pkt.HardwareAddr, machineStateProxyDHCPIpxe, "Offering to boot iPXE")
6161
} else {
@@ -65,18 +65,18 @@ func (s *Server) serveDHCP(conn *dhcp4.Conn) error {
6565
// Machine should be booted.
6666
serverIP, err := interfaceIP(intf)
6767
if err != nil {
68-
s.Log.Infof("Want to boot %s on %s, but couldn't get a source address: %s", pkt.HardwareAddr, intf.Name, err)
68+
s.Log.Info("Want to boot, but couldn't get a source address", "mac", pkt.HardwareAddr, "interface", intf.Name, "error", err)
6969
continue
7070
}
7171

7272
resp, err := s.offerDHCP(pkt, mach, serverIP, fwtype)
7373
if err != nil {
74-
s.Log.Infof("Failed to construct ProxyDHCP offer for %s: %s", pkt.HardwareAddr, err)
74+
s.Log.Info("Failed to construct ProxyDHCP offer", "mac", pkt.HardwareAddr, "error", err)
7575
continue
7676
}
7777

7878
if err = conn.SendDHCP(resp, intf); err != nil {
79-
s.Log.Infof("Failed to send ProxyDHCP offer for %s: %s", pkt.HardwareAddr, err)
79+
s.Log.Info("Failed to send ProxyDHCP offer", "mac", pkt.HardwareAddr, "error", err)
8080
continue
8181
}
8282
}

0 commit comments

Comments
 (0)