Skip to content

Commit

Permalink
chore: Improve logging format consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Jun 11, 2024
1 parent 246913f commit b369e93
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 134 deletions.
38 changes: 19 additions & 19 deletions cmd/stunnerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ func main() {
log := st.GetLogger().NewLogger("stunnerd")

buildInfo := buildinfo.BuildInfo{Version: version, CommitHash: commitHash, BuildDate: buildDate}
log.Infof("starting stunnerd id %q, STUNner %s ", st.GetId(), buildInfo.String())
log.Infof("Starting stunnerd id %q, STUNner %s ", st.GetId(), buildInfo.String())

conf := make(chan *stnrv1.StunnerConfig, 1)
defer close(conf)

var cancelConfigLoader context.CancelFunc
if flag.NArg() == 1 {
log.Infof("starting %s with default configuration at TURN URI: %s",
log.Infof("Starting %s with default configuration at TURN URI: %s",
os.Args[0], flag.Arg(0))

c, err := stunner.NewDefaultConfig(flag.Arg(0))
if err != nil {
log.Errorf("could not load default STUNner config: %s", err.Error())
log.Errorf("Could not load default STUNner config: %s", err.Error())
os.Exit(1)
}

Expand All @@ -102,17 +102,17 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())

if configOrigin == "k8s" {
log.Info("discovering configuration from Kubernetes")
log.Info("Discovering configuration from Kubernetes")
cdsAddr, err := cdsclient.DiscoverK8sCDSServer(ctx, k8sConfigFlags, cdsConfigFlags,
st.GetLogger().NewLogger("cds-fwd"))
if err != nil {
log.Errorf("error searching for CDS server: %s", err.Error())
log.Errorf("Error searching for CDS server: %s", err.Error())
os.Exit(1)
}
configOrigin = cdsAddr.Addr
}

log.Infof("loading configuration from origin %q", configOrigin)
log.Infof("Loading configuration from origin %q", configOrigin)
c, err := st.LoadConfig(configOrigin)
if err != nil {
log.Error(err.Error())
Expand All @@ -123,7 +123,7 @@ func main() {
conf <- c

} else if *watch {
log.Info("bootstrapping with minimal config")
log.Info("Bootstrapping stunnerd with minimal config")
z := cdsclient.ZeroConfig(st.GetId())
conf <- z

Expand All @@ -132,19 +132,19 @@ func main() {
cancelConfigLoader = cancel

if configOrigin == "k8s" {
log.Info("discovering configuration from Kubernetes")
log.Info("Discovering configuration from Kubernetes")
cdsAddr, err := cdsclient.DiscoverK8sCDSServer(ctx, k8sConfigFlags, cdsConfigFlags,
st.GetLogger().NewLogger("cds-fwd"))
if err != nil {
log.Errorf("error searching for CDS server: %s", err.Error())
log.Errorf("Error searching for CDS server: %s", err.Error())
os.Exit(1)
}
configOrigin = cdsAddr.Addr
}

log.Infof("watching configuration at origin %q", configOrigin)
log.Infof("Watching configuration at origin %q", configOrigin)
if err := st.WatchConfig(ctx, configOrigin, conf); err != nil {
log.Errorf("could not run config watcher: %s", err.Error())
log.Errorf("Could not run config watcher: %s", err.Error())
os.Exit(1)
}
} else {
Expand All @@ -162,16 +162,16 @@ func main() {
for {
select {
case <-exit:
log.Info("normal exit on graceful shutdown")
log.Info("Normal exit on graceful shutdown")
os.Exit(0)

case <-sigterm:
log.Infof("performing a graceful shutdown with %d active connection(s)",
log.Infof("Commencing graceful shutdown with %d active connection(s)",
st.AllocationCount())
st.Shutdown()

if cancelConfigLoader != nil {
log.Info("canceling config loader")
log.Info("Canceling config loader")
cancelConfigLoader()
cancelConfigLoader = nil
}
Expand All @@ -188,22 +188,22 @@ func main() {
}()

case c := <-conf:
log.Infof("new configuration available: %q", c.String())
log.Infof("New configuration available: %q", c.String())

// command line loglevel overrides config
if *verbose || *level != "" {
c.Admin.LogLevel = logLevel
}

// we have working stunnerd: reconcile
log.Debug("initiating reconciliation")
log.Debug("Initiating reconciliation")
err := st.Reconcile(c)
log.Trace("reconciliation ready")
log.Trace("Reconciliation ready")
if err != nil {
if e, ok := err.(stnrv1.ErrRestarted); ok {
log.Debugf("reconciliation ready: %s", e.Error())
log.Debugf("Reconciliation ready: %s", e.Error())
} else {
log.Errorf("could not reconcile new configuration "+
log.Errorf("Could not reconcile new configuration "+
"(running configuration unchanged): %s", err.Error())
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/object/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "errors"

var (
// ErrRestartRequired indicates that an object needs to be restarted for reconciliation.
ErrRestartRequired = errors.New("restart required")
ErrRestartRequired = errors.New("Restart required")
)
2 changes: 1 addition & 1 deletion internal/resolver/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestMockResolver(t *testing.T) {
loggerFactory := logger.NewLoggerFactory(resolverTestLoglevel)
log := loggerFactory.NewLogger("resolver-test")

log.Debug("setting up the mock DNS")
log.Debug("Setting up the mock DNS")
mockDns := NewMockResolver(map[string]([]string){
"stunner.l7mp.io": []string{"1.2.3.4"},
"echo-server.l7mp.io": []string{"1.2.3.5"},
Expand Down
14 changes: 7 additions & 7 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func (r *dnsResolverImpl) Register(domain string) error {

// the resolver goroutine
func startResolver(e *serviceEntry, log logging.LeveledLogger) {
log.Infof("resolver thread starting for domain %q, DNS update interval: %v",
log.Infof("Resolver thread starting for domain %q, DNS update interval: %v",
e.domain, dnsUpdateInterval)

if err := doResolve(e); err != nil {
log.Debugf("initial resolution failed for domain %q: %s", e.domain, err.Error())
log.Debugf("Initial resolution failed for domain %q: %s", e.domain, err.Error())
}
log.Tracef("initial resolution ready for domain %q, found %d endpoints", e.domain,
log.Tracef("Initial resolution ready for domain %q, found %d endpoints", e.domain,
len(e.hostNames))

ticker := time.NewTicker(dnsUpdateInterval)
Expand All @@ -100,15 +100,15 @@ func startResolver(e *serviceEntry, log logging.LeveledLogger) {
for {
select {
case <-e.ctx.Done():
log.Debugf("resolver thread exiting for domain %q", e.domain)
log.Debugf("Resolver thread exiting for domain %q", e.domain)
return
case <-ticker.C:
log.Tracef("resolving for domain %q", e.domain)
log.Tracef("Resolving for domain %q", e.domain)
if err := doResolve(e); err != nil {
log.Debugf("resolution failed for domain %q: %s",
log.Debugf("Resolution failed for domain %q: %s",
e.domain, err.Error())
}
log.Tracef("periodic resolution ready for domain %q, found %d endpoints", e.domain,
log.Tracef("Periodic resolution ready for domain %q, found %d endpoints", e.domain,
len(e.hostNames))
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apis/v1/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

var (
ErrInvalidConf = errors.New("invalid configuration")
ErrNoSuchListener = errors.New("no such listener")
ErrNoSuchCluster = errors.New("no such cluster")
ErrInvalidConf = errors.New("Invalid configuration")
ErrNoSuchListener = errors.New("No such listener")
ErrNoSuchCluster = errors.New("No such cluster")
// ErrInvalidRoute = errors.New("invalid route")
)

Expand Down
34 changes: 17 additions & 17 deletions pkg/config/client/file_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (w *ConfigFileClient) Watch(ctx context.Context, ch chan<- *stnrv1.StunnerC
for {
// try to watch
if err := w.Poll(ctx, ch); err != nil {
w.log.Warnf("error loading config file %q: %s",
w.log.Warnf("Error loading config file %q: %s",
w.configFile, err.Error())
} else {
return
Expand Down Expand Up @@ -113,7 +113,7 @@ func (w *ConfigFileClient) Poll(ctx context.Context, ch chan<- *stnrv1.StunnerCo
return err
}

w.log.Debugf("initial config file successfully loaded from %q: %s", config, c.String())
w.log.Debugf("Initial config file successfully loaded from %q: %s", config, c.String())

ch <- c

Expand All @@ -128,43 +128,43 @@ func (w *ConfigFileClient) Poll(ctx context.Context, ch chan<- *stnrv1.StunnerCo

case e, ok := <-watcher.Events:
if !ok {
return errors.New("config watcher event handler received invalid event")
return errors.New("Config watcher received invalid event")
}

w.log.Debugf("received watcher event: %s", e.String())
w.log.Debugf("Received watch event: %s", e.String())

if e.Has(fsnotify.Remove) {
if err := watcher.Remove(config); err != nil {
w.log.Debugf("could not remove config file %q watcher: %s",
w.log.Debugf("Could not remove config file %q watcher: %s",
config, err.Error())
}

return fmt.Errorf("config file deleted %q, disabling watcher", e.Op.String())
return fmt.Errorf("Config file deleted %q, disabling watcher", e.Op.String())
}

if !e.Has(fsnotify.Write) {
w.log.Debugf("unhandled notify op on config file %q (ignoring): %s",
w.log.Debugf("Unhandled notify op on config file %q (ignoring): %s",
e.Name, e.Op.String())
continue
}

w.log.Debugf("loading configuration file: %s", config)
w.log.Debugf("Loading configuration file: %s", config)
c, err := w.Load()
if err != nil {
if errors.Is(err, errFileTruncated) {
w.log.Debugf("ignoring: %s", err.Error())
w.log.Debugf("Ignoring: %s", err.Error())
continue
}
return err
}

// suppress repeated events
if c.DeepEqual(&prev) {
w.log.Debugf("ignoring recurrent notify event for the same config file")
w.log.Debugf("Ignoring recurrent notify event for the same config file")
continue
}

w.log.Debugf("config file successfully loaded from %q: %s", config, c.String())
w.log.Debugf("Config file successfully loaded from %q: %s", config, c.String())

ch <- c

Expand All @@ -173,15 +173,15 @@ func (w *ConfigFileClient) Poll(ctx context.Context, ch chan<- *stnrv1.StunnerCo

case err, ok := <-watcher.Errors:
if !ok {
return errors.New("config watcher error handler received invalid error")
return errors.New("Config watcher error handler received invalid error")
}

if err := watcher.Remove(config); err != nil {
w.log.Debugf("could not remove config file %q watcher: %s",
w.log.Debugf("Could not remove config file %q watcher: %s",
config, err.Error())
}

return fmt.Errorf("watcher error, deactivating watcher: %w", err)
return fmt.Errorf("Config watcher error, deactivating: %w", err)
}
}
}
Expand All @@ -202,16 +202,16 @@ func (w *ConfigFileClient) tryWatchConfig(ctx context.Context) bool {
return false

case <-ticker.C:
w.log.Debugf("trying to read config file %q from periodic timer",
w.log.Debugf("Trying to read config file %q from periodic timer",
config)

// check if config file exists and it is readable
if _, err := os.Stat(config); errors.Is(err, os.ErrNotExist) {
w.log.Debugf("config file %q does not exist", config)
w.log.Debugf("Config file %q does not exist", config)

// report status in every 10th second
if time.Now().Second()%10 == 0 {
w.log.Warnf("waiting for config file %q", config)
w.log.Warnf("Waiting for config file %q", config)
}

continue
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/client/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func DiscoverK8sStunnerdPods(ctx context.Context, k8sFlags *cliopt.ConfigFlags,

p, err := d.portfwd(ctx, &pod, podFlags.Port)
if err != nil {
d.log.Errorf("failed to create port-forwarder to stunnerd pod %s/%s: %s",
d.log.Errorf("Failed to create port-forwarder to stunnerd pod %s/%s: %s",
pod.GetNamespace(), pod.GetName(), err.Error())
return
}
Expand Down Expand Up @@ -344,15 +344,15 @@ func DiscoverK8sAuthServer(ctx context.Context, k8sFlags *cliopt.ConfigFlags, au
LabelSelector: label,
})
if err != nil {
return PodInfo{}, fmt.Errorf("failed to query Kubernetes API server: %w", err)
return PodInfo{}, fmt.Errorf("Failed to query Kubernetes API server: %w", err)
}

if len(pods.Items) == 0 {
return PodInfo{}, fmt.Errorf("no authentication found")
}

if len(pods.Items) > 1 {
d.log.Infof("mulitple (%d) authentication service instances found, using the first one", len(pods.Items))
d.log.Infof("Mulitple (%d) authentication service instances found, using the first one", len(pods.Items))
}

return d.portfwd(ctx, &pods.Items[0], authFlags.Port)
Expand Down Expand Up @@ -387,12 +387,12 @@ func (d *PodConnector) portfwd(ctx context.Context, pod *corev1.Pod, port int) (
out, errOut := new(bytes.Buffer), new(bytes.Buffer)
fw, err := portforward.New(dialer, []string{remoteAddr}, stopChan, readyChan, out, errOut)
if err != nil {
return PodInfo{}, fmt.Errorf("error creating port-forwarder: %w", err)
return PodInfo{}, fmt.Errorf("Error creating port-forwarder: %w", err)
}

go func() {
if err := fw.ForwardPorts(); err != nil {
d.log.Errorf("error setting up port-forwarder: %s", err.Error())
d.log.Errorf("Error setting up port-forwarder: %s", err.Error())
os.Exit(1)
}
}()
Expand Down
Loading

0 comments on commit b369e93

Please sign in to comment.