Skip to content

Commit

Permalink
feat: tee node logs to pod logs
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari committed Oct 18, 2023
1 parent ae1e7d5 commit b0af94b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
25 changes: 21 additions & 4 deletions launcher/clabernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func StartClabernetes() {
),
)

containerlabLogger := logManager.MustRegisterAndGetLogger(
"containerlab",
clabernetesconstants.Info,
)

nodeLogger := logManager.MustRegisterAndGetLogger(
"node",
clabernetesconstants.Info,
)

ctx, _ := clabernetesutil.SignalHandledContext(clabernetesLogger.Criticalf)

clabernetesInstance = &clabernetes{
Expand All @@ -46,7 +56,9 @@ func StartClabernetes() {
clabernetesconstants.AppNameEnvVar,
clabernetesconstants.AppNameDefault,
),
logger: clabernetesLogger,
logger: clabernetesLogger,
containerlabLogger: containerlabLogger,
nodeLogger: nodeLogger,
}

clabernetesInstance.startup()
Expand All @@ -59,7 +71,9 @@ type clabernetes struct {

appName string

logger claberneteslogging.Instance
logger claberneteslogging.Instance
containerlabLogger claberneteslogging.Instance
nodeLogger claberneteslogging.Instance
}

func (c *clabernetes) startup() {
Expand Down Expand Up @@ -115,13 +129,16 @@ func (c *clabernetes) setup() {
func (c *clabernetes) launch() {
c.logger.Debug("launching containerlab...")

err := c.runClab()
err := c.runContainerlab()
if err != nil {
c.logger.Criticalf("failed launching containerlab, err: %s", err)

clabernetesutil.Panic(err.Error())
}

containerIDs := c.getContainerIDs()
c.tailContainerLogs(containerIDs)

c.logger.Info("containerlab started, setting up any required tunnels...")

tunnelBytes, err := os.ReadFile("tunnels.yaml")
Expand All @@ -141,7 +158,7 @@ func (c *clabernetes) launch() {
}

for _, tunnel := range tunnelObj {
err = c.runClabVxlanTools(
err = c.runContainerlabVxlanTools(
tunnel.LocalNodeName,
tunnel.LocalLinkName,
tunnel.RemoteName,
Expand Down
12 changes: 6 additions & 6 deletions launcher/containerlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
claberneteserrors "github.com/srl-labs/clabernetes/errors"
)

func (c *clabernetes) runClab() error {
clabLogFile, err := os.Create("clab.log")
func (c *clabernetes) runContainerlab() error {
containerlabLogFile, err := os.Create("containerlab.log")
if err != nil {
return err
}

clabOutWriter := io.MultiWriter(c.logger, clabLogFile)
containerlabOutWriter := io.MultiWriter(c.containerlabLogger, containerlabLogFile)

args := []string{
"deploy",
Expand All @@ -31,8 +31,8 @@ func (c *clabernetes) runClab() error {

cmd := exec.Command("containerlab", args...)

cmd.Stdout = clabOutWriter
cmd.Stderr = clabOutWriter
cmd.Stdout = containerlabOutWriter
cmd.Stderr = containerlabOutWriter

err = cmd.Run()
if err != nil {
Expand All @@ -42,7 +42,7 @@ func (c *clabernetes) runClab() error {
return nil
}

func (c *clabernetes) runClabVxlanTools(
func (c *clabernetes) runContainerlabVxlanTools(
localNodeName, cntLink, vxlanRemote string,
vxlanID int,
) error {
Expand Down
56 changes: 56 additions & 0 deletions launcher/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package launcher
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -103,3 +104,58 @@ func (c *clabernetes) startDocker() error {
attempts++
}
}

func (c *clabernetes) getContainerIDs() []string {
// return all the container ids running in the pod
psCmd := exec.Command("docker", "ps", "--quiet")

output, err := psCmd.Output()
if err != nil {
c.logger.Warnf(
"failed determining container ids will continue but will not log container output,"+
" err: %s",
err,
)

return nil
}

containerIDs := strings.Split(strings.TrimSpace(string(output)), "\n")

c.logger.Debugf("found container ids %q", containerIDs)

return containerIDs
}

func (c *clabernetes) tailContainerLogs(containerIDs []string) {
nodeLogFile, err := os.Create("node.log")
if err != nil {
c.logger.Warnf("failed creating node log file, err: %s", err)

return
}

nodeOutWriter := io.MultiWriter(c.nodeLogger, nodeLogFile)

for _, containerID := range containerIDs {
go func(containerID string, nodeOutWriter io.Writer) {
args := []string{
"logs",
"-f",
containerID,
}

cmd := exec.Command("docker", args...) //nolint:gosec

cmd.Stdout = nodeOutWriter
cmd.Stderr = nodeOutWriter

err = cmd.Run()
if err != nil {
c.logger.Warnf(
"tailing node logs for container id %q failed, err: %s", containerID, err,
)
}
}(containerID, nodeOutWriter)
}
}

0 comments on commit b0af94b

Please sign in to comment.