Skip to content

Commit

Permalink
Improve kube-proxy and calico logging in Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <[email protected]>
  • Loading branch information
manuelbuil committed Jan 16, 2024
1 parent da9e1d5 commit fe65c43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
19 changes: 12 additions & 7 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,22 @@ 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)
}

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,
}
}
5 changes: 3 additions & 2 deletions pkg/pebinaryexecutor/pebinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 12 additions & 27 deletions pkg/windows/calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit fe65c43

Please sign in to comment.