From 698f1766b4988e14671f4ce9ea3247dc4dca695c Mon Sep 17 00:00:00 2001
From: kaiyou <dev@kaiyou.fr>
Date: Mon, 31 Oct 2022 00:31:25 +0100
Subject: [PATCH] Bind zap to klog

---
 cmd/hepto/config.go       | 3 ++-
 pkg/cluster/cluster.go    | 2 ++
 pkg/cluster/config.go     | 3 +++
 pkg/cluster/kubeconfig.go | 2 +-
 pkg/cluster/services.go   | 1 +
 pkg/cluster/vpn.go        | 5 +++--
 6 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/cmd/hepto/config.go b/cmd/hepto/config.go
index 6555500..bd00792 100644
--- a/cmd/hepto/config.go
+++ b/cmd/hepto/config.go
@@ -73,13 +73,14 @@ func (c Config) dataPath(subPath string) string {
 
 func (c *Config) Complete() error {
   // Initialize logging
-  logger, err := zap.NewProduction() 
+  logger, err := zap.NewDevelopment()
   if err != nil {
     return err
   }
   c.Logger = zapr.NewLogger(logger)
   c.Container.Logger = c.Logger
   c.Cluster.Logger = c.Logger
+  c.Cluster.ZapLogger = logger
   // Setup paths and container settings
 	c.Container.Root = c.dataPath("root")
 	c.Container.Data = c.dataPath("containers")
diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go
index 552c775..96e7f7f 100644
--- a/pkg/cluster/cluster.go
+++ b/pkg/cluster/cluster.go
@@ -9,6 +9,7 @@ import (
 	"forge.tedomum.net/acides/hepto/hepto/pkg/pki"
 	"forge.tedomum.net/acides/hepto/hepto/pkg/sml"
 	"forge.tedomum.net/acides/hepto/hepto/pkg/wg"
+	"k8s.io/klog/v2"
 )
 
 type Cluster struct {
@@ -44,6 +45,7 @@ func (c *Cluster) Run() {
 	c.initCerts()
 	c.initEnv()
 	c.ml.Meta.Role = string(c.node.Role)
+  klog.SetLogger(c.settings.Logger)
 	// Start waiting for events
 	events := c.ml.Events()
 	err := c.ml.Start()
diff --git a/pkg/cluster/config.go b/pkg/cluster/config.go
index a9f8a65..d19ddb5 100644
--- a/pkg/cluster/config.go
+++ b/pkg/cluster/config.go
@@ -5,11 +5,14 @@ import (
 	"net"
 
 	"github.com/go-logr/logr"
+	"go.uber.org/zap"
 )
 
 type ClusterSettings struct {
   // Logger interface
   Logger logr.Logger
+  // Concrete zap logger for etcd
+  ZapLogger *zap.Logger
 	// Cluster name, should be locally unique
 	Name string
 	// Cluster key, must be shared across nodes
diff --git a/pkg/cluster/kubeconfig.go b/pkg/cluster/kubeconfig.go
index 88f8c83..b93825f 100644
--- a/pkg/cluster/kubeconfig.go
+++ b/pkg/cluster/kubeconfig.go
@@ -23,7 +23,7 @@ func (c *Cluster) WriteConfig(config WriteableConfig, dest string) {
     os.Exit(1)
   }
   defer file.Close()
-  err = t.Execute(file, c)
+  err = t.Execute(file, config)
   if err != nil {
     c.settings.Logger.Error(err, "could not generate config")
     os.Exit(1)
diff --git a/pkg/cluster/services.go b/pkg/cluster/services.go
index a54fc5c..62080fc 100644
--- a/pkg/cluster/services.go
+++ b/pkg/cluster/services.go
@@ -45,6 +45,7 @@ func (c *Cluster) updateServices() {
 func (c *Cluster) startEtcd() {
 	etcdConfig := embed.NewConfig()
 	etcdConfig.Dir = "/etcd"
+  etcdConfig.ZapLoggerBuilder = embed.NewZapLoggerBuilder(c.settings.ZapLogger)
 	service, err := wrappers.ETCd(c.serviceCtx, etcdConfig)
 	if err != nil {
     c.settings.Logger.Error(err, "failed to start etcd")
diff --git a/pkg/cluster/vpn.go b/pkg/cluster/vpn.go
index 998de64..6f0499b 100644
--- a/pkg/cluster/vpn.go
+++ b/pkg/cluster/vpn.go
@@ -2,7 +2,8 @@ package cluster
 
 import (
 	"net"
-  "os"
+	"os"
+	"strconv"
 
 	"forge.tedomum.net/acides/hepto/hepto/pkg/wg"
 )
@@ -39,6 +40,6 @@ func (c *Cluster) updateVPN() {
 		}
 		peers = append(peers, peer)
 	}
-	c.settings.Logger.Info("updating VPN mesh", peers, len(peers), "mtu", c.networking.MTU)
+	c.settings.Logger.Info("updating VPN mesh", "peers", strconv.Itoa(len(peers)), "mtu", strconv.Itoa(c.networking.MTU))
 	c.vpn.Update(peers, c.networking.MTU)
 }
-- 
GitLab