Skip to content

Commit 356bd5d

Browse files
committed
Fix spegel logging and startup sequence
Signed-off-by: Brad Davidson <[email protected]>
1 parent 6ab8b42 commit 356bd5d

File tree

4 files changed

+99
-14
lines changed

4 files changed

+99
-14
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ require (
9595
github.com/flannel-io/flannel v0.27.0
9696
github.com/fsnotify/fsnotify v1.7.0
9797
github.com/go-logr/logr v1.4.2
98-
github.com/go-logr/stdr v1.2.3-0.20220714215716-96bad1d688c5
9998
github.com/go-test/deep v1.0.7
10099
github.com/google/cadvisor v0.52.1
101100
github.com/google/go-containerregistry v0.20.2
@@ -264,6 +263,7 @@ require (
264263
github.com/go-errors/errors v1.4.2 // indirect
265264
github.com/go-ini/ini v1.67.0 // indirect
266265
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
266+
github.com/go-logr/stdr v1.2.3-0.20220714215716-96bad1d688c5 // indirect
267267
github.com/go-openapi/jsonpointer v0.21.1 // indirect
268268
github.com/go-openapi/jsonreference v0.21.0 // indirect
269269
github.com/go-openapi/swag v0.23.1 // indirect

pkg/agent/run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
106106
nodeConfig.AgentConfig.EnableIPv4 = enableIPv4
107107
nodeConfig.AgentConfig.EnableIPv6 = enableIPv6
108108

109+
if err := executor.Bootstrap(ctx, nodeConfig, cfg); err != nil {
110+
return err
111+
}
112+
109113
if nodeConfig.EmbeddedRegistry {
110114
if nodeConfig.Docker || nodeConfig.ContainerRuntimeEndpoint != "" {
111115
return errors.New("embedded registry mirror requires embedded containerd")
112116
}
113117

114-
if err := spegel.DefaultRegistry.Start(ctx, nodeConfig); err != nil {
118+
if err := spegel.DefaultRegistry.Start(ctx, nodeConfig, executor.CRIReadyChan()); err != nil {
115119
return pkgerrors.WithMessage(err, "failed to start embedded registry")
116120
}
117121
}
@@ -132,10 +136,6 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
132136
return err
133137
}
134138

135-
if err := executor.Bootstrap(ctx, nodeConfig, cfg); err != nil {
136-
return err
137-
}
138-
139139
if !nodeConfig.NoFlannel {
140140
if (nodeConfig.FlannelExternalIP) && (len(nodeConfig.AgentConfig.NodeExternalIPs) == 0) {
141141
logrus.Warnf("Server has flannel-external-ip flag set but this node does not set node-external-ip. Flannel will use internal address when connecting to this node.")

pkg/spegel/spegel.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"log"
98
"net"
109
"net/http"
1110
"net/url"
@@ -19,13 +18,12 @@ import (
1918
"github.com/k3s-io/k3s/pkg/clientaccess"
2019
"github.com/k3s-io/k3s/pkg/daemons/config"
2120
"github.com/k3s-io/k3s/pkg/server/auth"
21+
"github.com/k3s-io/k3s/pkg/util/logger"
2222
"github.com/k3s-io/k3s/pkg/version"
2323
"github.com/rancher/dynamiclistener/cert"
2424
"k8s.io/apimachinery/pkg/util/wait"
25-
"k8s.io/utils/ptr"
2625

2726
"github.com/go-logr/logr"
28-
"github.com/go-logr/stdr"
2927
"github.com/gorilla/mux"
3028
leveldb "github.com/ipfs/go-ds-leveldb"
3129
ipfslog "github.com/ipfs/go-log/v2"
@@ -108,7 +106,7 @@ func init() {
108106
}
109107

110108
// Start starts the embedded p2p router, and binds the registry API to an existing HTTP router.
111-
func (c *Config) Start(ctx context.Context, nodeConfig *config.Node) error {
109+
func (c *Config) Start(ctx context.Context, nodeConfig *config.Node, criReadyChan <-chan struct{}) error {
112110
localAddr := net.JoinHostPort(c.InternalAddress, c.RegistryPort)
113111
// distribute images for all configured mirrors. there doesn't need to be a
114112
// configured endpoint, just having a key for the registry will do.
@@ -135,12 +133,10 @@ func (c *Config) Start(ctx context.Context, nodeConfig *config.Node) error {
135133
c.ExternalAddress, c.RegistryPort, registries)
136134

137135
// set up the various logging logging frameworks
136+
ctx = logr.NewContext(ctx, logger.NewLogrusSink(nil).AsLogr().WithName("spegel"))
138137
level := ipfslog.LevelInfo
139138
if logrus.IsLevelEnabled(logrus.DebugLevel) {
140139
level = ipfslog.LevelDebug
141-
stdlog := log.New(logrus.StandardLogger().Writer(), "spegel ", log.LstdFlags)
142-
logger := stdr.NewWithOptions(stdlog, stdr.Options{Verbosity: ptr.To(7)})
143-
ctx = logr.NewContext(ctx, logger)
144140
}
145141
ipfslog.SetAllLoggers(level)
146142

@@ -232,7 +228,10 @@ func (c *Config) Start(ctx context.Context, nodeConfig *config.Node) error {
232228
}
233229

234230
// Track images available in containerd and publish via p2p router
235-
go state.Track(ctx, ociClient, router, resolveLatestTag)
231+
go func() {
232+
<-criReadyChan
233+
state.Track(ctx, ociClient, router, resolveLatestTag)
234+
}()
236235

237236
mRouter, err := c.Router(ctx, nodeConfig)
238237
if err != nil {

pkg/util/logger/logger.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package logger
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-logr/logr"
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
// implicit interface check
11+
var _ logr.LogSink = &logrusSink{}
12+
13+
// mapLevel maps logr log verbosities to logrus log levels
14+
// logr does not have "log levels", but Info prints at verbosity 0
15+
// while logrus's LevelInfo is unit32(4). This means:
16+
// * panic/fatal/warn are unused,
17+
// * 0 is info
18+
// * 1 is debug
19+
// * >=2 are trace
20+
func mapLevel(level int) logrus.Level {
21+
if level >= 2 {
22+
return logrus.TraceLevel
23+
}
24+
return logrus.Level(level + 4)
25+
}
26+
27+
// mapKV maps a list of keys and values to logrus Fields
28+
func mapKV(kvs []any) logrus.Fields {
29+
fields := logrus.Fields{}
30+
for i := 0; i < len(kvs); i += 2 {
31+
k := kvs[i].(string)
32+
if len(kvs) > i+1 {
33+
fields[k] = kvs[i+1]
34+
} else {
35+
fields[k] = ""
36+
}
37+
}
38+
return fields
39+
}
40+
41+
// LogrusSink wraps logrus the Logger/Entry types for use as a logr LogSink.
42+
type logrusSink struct {
43+
e *logrus.Entry
44+
ri logr.RuntimeInfo
45+
}
46+
47+
func NewLogrusSink(l *logrus.Logger) *logrusSink {
48+
if l == nil {
49+
l = logrus.StandardLogger()
50+
}
51+
return &logrusSink{e: logrus.NewEntry(l)}
52+
}
53+
54+
func (ls *logrusSink) AsLogr() logr.Logger {
55+
return logr.New(ls)
56+
}
57+
58+
func (ls *logrusSink) Init(ri logr.RuntimeInfo) {
59+
ls.ri = ri
60+
}
61+
62+
func (ls *logrusSink) Enabled(level int) bool {
63+
return ls.e.Logger.IsLevelEnabled(mapLevel(level))
64+
}
65+
66+
func (ls *logrusSink) Info(level int, msg string, kvs ...any) {
67+
ls.e.WithFields(mapKV(kvs)).Log(mapLevel(level), msg)
68+
}
69+
70+
func (ls *logrusSink) Error(err error, msg string, kvs ...any) {
71+
ls.e.WithError(err).WithFields(mapKV(kvs)).Error(msg)
72+
}
73+
74+
func (ls *logrusSink) WithValues(kvs ...any) logr.LogSink {
75+
return &logrusSink{
76+
e: ls.e.WithFields(mapKV(kvs)),
77+
ri: ls.ri,
78+
}
79+
}
80+
81+
func (ls *logrusSink) WithName(name string) logr.LogSink {
82+
if base, ok := ls.e.Data["logger"]; ok {
83+
name = fmt.Sprintf("%s/%s", base, name)
84+
}
85+
return ls.WithValues("logger", name)
86+
}

0 commit comments

Comments
 (0)