Skip to content

Commit

Permalink
prints kube task logs to file
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Oct 12, 2023
1 parent 8c4d0cd commit 073bddf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
35 changes: 23 additions & 12 deletions pkg/client/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,31 +368,40 @@ func (client *DockerClient) ContainerLogs(opts *ContainerLogsOpts) error {
}
}

func (client *DockerClient) ContainerLogsStd(containerId string) error {

func (client *DockerClient) containerLogsStream(containerId string) (io.ReadCloser, error) {
outStream, err := client.docker.ContainerLogs(client.ctx, containerId, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
return errors.Wrap(err, "error container logs std")
return nil, errors.Wrap(err, "error container logs stream")
}
return outStream, nil
}

_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, outStream)
return err
func (client *DockerClient) ContainerLogsStd(containerId string) error {

outStream, err := client.containerLogsStream(containerId)
if err != nil {
return err
}
defer outStream.Close()

// blocks until the stream is finished
if _, err = stdcopy.StdCopy(os.Stdout, os.Stderr, outStream); err != nil {
return errors.Wrapf(err, "error container logs std copy")
}
return nil
}

func (client *DockerClient) ContainerLogsTee(containerId string, logFileName string) error {

outStream, err := client.docker.ContainerLogs(client.ctx, containerId, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
outStream, err := client.containerLogsStream(containerId)
if err != nil {
return errors.Wrap(err, "error container logs tee")
return err
}
defer outStream.Close()

logFile, err := util.OpenFile(logFileName)
if err != nil {
Expand All @@ -402,7 +411,9 @@ func (client *DockerClient) ContainerLogsTee(containerId string, logFileName str
//log.SetOutput(multiWriter)
defer logFile.Close()

_, err = stdcopy.StdCopy(multiWriter, multiWriter, outStream)
if _, err = stdcopy.StdCopy(multiWriter, multiWriter, outStream); err != nil {
return errors.Wrapf(err, "error container logs tee copy")
}
return err
}

Expand Down
43 changes: 36 additions & 7 deletions pkg/client/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/hckops/hckctl/pkg/util"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -465,24 +466,52 @@ func (client *KubeClient) PodExec(opts *PodExecOpts) error {
return nil
}

func (client *KubeClient) PodLog(opts *PodLogOpts) error {

stream, err := client.CoreApi().
func (client *KubeClient) podLogsStream(opts *PodLogsOpts) (io.ReadCloser, error) {
outStream, err := client.CoreApi().
Pods(opts.Namespace).
GetLogs(opts.PodName, &corev1.PodLogOptions{
Container: opts.PodId,
Follow: true,
}).
Stream(client.ctx)
if err != nil {
return errors.Wrapf(err, "error pod log stream")
return nil, errors.Wrapf(err, "error pod logs stream")
}
defer stream.Close()
return outStream, nil
}

func (client *KubeClient) PodLogsStd(opts *PodLogsOpts) error {

outStream, err := client.podLogsStream(opts)
if err != nil {
return err
}
defer outStream.Close()

// blocks until the stream is finished
_, err = io.Copy(os.Stdout, stream)
if _, err = io.Copy(os.Stdout, outStream); err != nil {
return errors.Wrapf(err, "error pod logs std copy")
}
return nil
}

func (client *KubeClient) PodLogsTee(opts *PodLogsOpts, logFileName string) error {

outStream, err := client.podLogsStream(opts)
if err != nil {
return errors.Wrapf(err, "error pod log copy")
return err
}
defer outStream.Close()

logFile, err := util.OpenFile(logFileName)
if err != nil {
return errors.Wrap(err, "error pod logs file")
}
multiWriter := io.MultiWriter(os.Stdout, logFile)
defer logFile.Close()

if _, err = io.Copy(multiWriter, outStream); err != nil {
return errors.Wrapf(err, "error pod logs tee copy")
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/kubernetes/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type PodExecOpts struct {
OnExecCallback func()
}

type PodLogOpts struct {
type PodLogsOpts struct {
Namespace string
PodName string
PodId string
Expand Down
5 changes: 2 additions & 3 deletions pkg/task/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ func (task *KubeTaskClient) runTask(opts *taskModel.RunOptions) error {
// stop loader
task.eventBus.Publish(newContainerWaitKubeLoaderEvent())

// TODO tee
logFileName := opts.GenerateLogFileName(taskModel.Kubernetes, podInfo.ContainerName)
logOpts := &kubernetes.PodLogOpts{
logOpts := &kubernetes.PodLogsOpts{
Namespace: namespace,
PodName: podInfo.PodName,
PodId: podInfo.ContainerName,
}
task.eventBus.Publish(newPodLogKubeEvent(logFileName))
if err := task.client.PodLog(logOpts); err != nil {
if err := task.client.PodLogsTee(logOpts, logFileName); err != nil {
return err
}

Expand Down

0 comments on commit 073bddf

Please sign in to comment.