From fe65c43a0dc639f52d0c4e7f73b62434eff771c0 Mon Sep 17 00:00:00 2001 From: Manuel Buil Date: Thu, 11 Jan 2024 16:00:49 +0100 Subject: [PATCH] Improve kube-proxy and calico logging in Windows Signed-off-by: Manuel Buil --- pkg/logging/logging.go | 19 ++++++++++------ pkg/pebinaryexecutor/pebinary.go | 5 ++-- pkg/windows/calico.go | 39 ++++++++++---------------------- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go index 740b2a523d..0b76bbbb15 100644 --- a/pkg/logging/logging.go +++ b/pkg/logging/logging.go @@ -74,13 +74,7 @@ func ExtractFromArgs(args []string) ([]string, io.Writer) { return extraArgs, io.Discard } - logger := &lumberjack.Logger{ - Filename: filename, - MaxSize: int(maxSize), - MaxBackups: 3, - MaxAge: 28, - Compress: true, - } + logger := GetLogger(filename, int(maxSize)) if alsoToStderr { return extraArgs, io.MultiWriter(os.Stderr, logger) @@ -88,3 +82,14 @@ func ExtractFromArgs(args []string) ([]string, io.Writer) { return extraArgs, logger } + +// GetLogger returns a new io.Writer that writes to the specified file +func GetLogger(filename string, maxSize int) io.Writer { + return &lumberjack.Logger{ + Filename: filename, + MaxSize: int(maxSize), + MaxBackups: 3, + MaxAge: 28, + Compress: true, + } +} diff --git a/pkg/pebinaryexecutor/pebinary.go b/pkg/pebinaryexecutor/pebinary.go index dc746cd520..4665954b98 100644 --- a/pkg/pebinaryexecutor/pebinary.go +++ b/pkg/pebinaryexecutor/pebinary.go @@ -220,10 +220,11 @@ func (p *PEBinaryConfig) KubeProxy(ctx context.Context, args []string) error { logrus.Infof("Running RKE2 kube-proxy %s", args) go func() { + outputFile := logging.GetLogger(filepath.Join(p.DataDir, "agent", "logs", "kube-proxy.log"), 50) for { cmd := exec.CommandContext(ctx, filepath.Join("c:\\", p.DataDir, "bin", "kube-proxy.exe"), args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = outputFile + cmd.Stderr = outputFile err := cmd.Run() logrus.Errorf("kube-proxy exited: %v", err) time.Sleep(5 * time.Second) diff --git a/pkg/windows/calico.go b/pkg/windows/calico.go index c395a70a7a..868622c7a0 100644 --- a/pkg/windows/calico.go +++ b/pkg/windows/calico.go @@ -17,6 +17,7 @@ import ( daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/version" "github.com/pkg/errors" + "github.com/rancher/rke2/pkg/logging" "github.com/sirupsen/logrus" opv1 "github.com/tigera/operator/api/v1" authv1 "k8s.io/api/authentication/v1" @@ -131,7 +132,6 @@ const ( CalicoSystemNamespace = "calico-system" CalicoChart = "rke2-calico" calicoNode = "calico-node" - calicoLogPath = "C:\\var\\log\\" ) // Setup creates the basic configuration required by the CNI. @@ -263,20 +263,18 @@ func (c *Calico) createKubeConfig(ctx context.Context, restConfig *rest.Config) // Start starts the CNI services on the Windows node. func (c *Calico) Start(ctx context.Context) error { - if err := os.MkdirAll(calicoLogPath, 0755); err != nil { - return fmt.Errorf("error creating %s directory: %v", calicoLogPath, err) - } + logPath := filepath.Join(c.DataDir, "agent", "logs") for { - if err := startCalico(ctx, c.CNICfg); err != nil { + if err := startCalico(ctx, c.CNICfg, logPath); err != nil { time.Sleep(5 * time.Second) logrus.Errorf("Calico exited: %v. Retrying", err) continue } break } - go startFelix(ctx, c.CNICfg) + go startFelix(ctx, c.CNICfg, logPath) if c.CNICfg.Mode == "windows-bgp" { - go startConfd(ctx, c.CNICfg) + go startConfd(ctx, c.CNICfg, logPath) } return nil @@ -392,13 +390,8 @@ func findCalicoInterface(nodeV4 *opv1.NodeAddressAutodetection) (IPAutoDetection return } -func startConfd(ctx context.Context, config *CalicoConfig) { - outputFile, err := os.Create(calicoLogPath + "confd.log") - if err != nil { - logrus.Fatalf("error creating confd.log: %v", err) - return - } - defer outputFile.Close() +func startConfd(ctx context.Context, config *CalicoConfig, logPath string) { + outputFile := logging.GetLogger(filepath.Join(logPath, "confd.log"), 50) specificEnvs := []string{ fmt.Sprintf("PATH=%s", os.Getenv("PATH")), @@ -419,13 +412,8 @@ func startConfd(ctx context.Context, config *CalicoConfig) { logrus.Error("Confd exited") } -func startFelix(ctx context.Context, config *CalicoConfig) { - outputFile, err := os.Create(calicoLogPath + "felix.log") - if err != nil { - logrus.Fatalf("error creating felix.log: %v", err) - return - } - defer outputFile.Close() +func startFelix(ctx context.Context, config *CalicoConfig, logPath string) { + outputFile := logging.GetLogger(filepath.Join(logPath, "felix.log"), 50) specificEnvs := []string{ fmt.Sprintf("FELIX_FELIXHOSTNAME=%s", config.Hostname), @@ -453,12 +441,9 @@ func startFelix(ctx context.Context, config *CalicoConfig) { logrus.Error("Felix exited") } -func startCalico(ctx context.Context, config *CalicoConfig) error { - outputFile, err := os.Create(calicoLogPath + "calico-node.log") - if err != nil { - return fmt.Errorf("error creating calico-node.log: %v", err) - } - defer outputFile.Close() +func startCalico(ctx context.Context, config *CalicoConfig, logPath string) error { + outputFile := logging.GetLogger(filepath.Join(logPath, "calico-node.log"), 50) + specificEnvs := []string{ fmt.Sprintf("CALICO_NODENAME_FILE=%s", config.NodeNameFile), fmt.Sprintf("CALICO_NETWORKING_BACKEND=%s", config.Mode),