Skip to content

Commit

Permalink
runtimes: enable log export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Jan 8, 2025
1 parent 15a83a4 commit d99a268
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
21 changes: 21 additions & 0 deletions pkg/runtimes/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,32 @@ func (d Docker) ID() string {
return "docker"
}

type commandInfo struct {
Command []string
FileName string
}

var roleBasedExportPath = map[k3d.Role][]string{
k3d.ServerRole: {"/var/log", "/var/lib/rancher/k3s/agent/containerd/containerd.log"},
k3d.AgentRole: {"/var/log", "/var/lib/rancher/k3s/agent/containerd/containerd.log"},
}

var roleBasedCommandsToExecute = map[k3d.Role][]commandInfo{
k3d.ServerRole: {
{Command: []string{"crictl", "images"}, FileName: "crictl-images.log"},
{Command: []string{"crictl", "ps", "-a"}, FileName: "crictl-ps.log"},
{Command: []string{"kubectl", "cluster-info"}, FileName: "cluster-info.log"},
{Command: []string{"kubectl", "version"}, FileName: "kubectl-version.log"},
{Command: []string{"kubectl", "get", "node", "-o", "yaml"}, FileName: "kubectl-get-node.yaml"},
{Command: []string{"ps", "-aef"}, FileName: "ps-aef.log"},
},
k3d.AgentRole: {
{Command: []string{"crictl", "images"}, FileName: "crictl-images.log"},
{Command: []string{"crictl", "ps", "-a"}, FileName: "crictl-ps.log"},
{Command: []string{"ps", "-aef"}, FileName: "ps-aef.log"},
},
}

// GetHost returns the docker daemon host
func (d Docker) GetHost() string {
// a) docker-machine
Expand Down
3 changes: 3 additions & 0 deletions pkg/runtimes/docker/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,5 +535,8 @@ func (d Docker) ExportLogsFromNode(ctx context.Context, node *k3d.Node, dstPath
return fmt.Errorf("failed to untar files from container '%s': %w", c.ID, err)
}
}
for _, command := range roleBasedCommandsToExecute[node.Role] {
d.executeCommandInContainer(ctx, docker, c.ID, command.Command, filepath.Join(destinationPath, command.FileName))
}
return nil
}
49 changes: 47 additions & 2 deletions pkg/runtimes/docker/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package docker

import (
"archive/tar"
"bufio"
"bytes"
"context"
"fmt"
Expand All @@ -37,6 +38,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
l "github.com/k3d-io/k3d/v5/pkg/logger"
Expand Down Expand Up @@ -181,6 +183,51 @@ func (d Docker) ReadFromNode(ctx context.Context, path string, node *k3d.Node) (
return reader, err
}

func (d Docker) executeCommandInContainer(ctx context.Context, cli client.APIClient, containerID string, command []string, outputFilePath string) error {
execConfig := container.ExecOptions{
Cmd: strslice.StrSlice(command),
AttachStdout: true,
AttachStderr: true,
Tty: false,
}

execIDResp, err := cli.ContainerExecCreate(ctx, containerID, execConfig)
if err != nil {
return fmt.Errorf("error creating exec instance: %v", err)
}

resp, err := cli.ContainerExecAttach(ctx, execIDResp.ID, container.ExecStartOptions{Tty: false})
if err != nil {
return fmt.Errorf("error attaching to exec instance: %v", err)
}
defer resp.Close()

outputFile, err := os.Create(outputFilePath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()

writer := bufio.NewWriter(outputFile)

var stdoutBuf bytes.Buffer

_, err = io.Copy(&stdoutBuf, resp.Reader)
if err != nil {
return fmt.Errorf("Error copying stdout: %v", err)
}

if _, err := writer.WriteString(stdoutBuf.String()); err != nil {
return fmt.Errorf("Error writing stdout to file: %v", err)
}

if err := writer.Flush(); err != nil {
return fmt.Errorf("Error flushing writer: %v", err)
}

return nil
}

// GetDockerClient returns a docker client
func GetDockerClient() (client.APIClient, error) {
dockerCli, err := command.NewDockerCli(command.WithStandardStreams())
Expand Down Expand Up @@ -258,7 +305,6 @@ func copyOriginalContent(cli client.APIClient, ctx context.Context, containerID,
return err
}
} else if header.Typeflag == tar.TypeDir {
// If the link target is a directory, recursively copy its contents
if err := untarReader(ctx, cli, containerID, reader, target); err != nil {
return err
}
Expand Down Expand Up @@ -293,7 +339,6 @@ func untarReader(ctx context.Context, cli client.APIClient, containerID string,
return err
}
case tar.TypeSymlink:
// Resolve the symlink and copy the original content
linkTarget := header.Linkname
if err := copyOriginalContent(cli, ctx, containerID, linkTarget, target); err != nil {
return err
Expand Down

0 comments on commit d99a268

Please sign in to comment.