Skip to content

Commit

Permalink
fixes download
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 committed Feb 6, 2025
1 parent f131340 commit 9dcbd8e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
14 changes: 6 additions & 8 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,15 @@ func (k *K8sClient) ContainerEvents(ctx context.Context, ch chan<- container.Con
}

name := ""
if event.Type == "ADDED" {
if event.Type == "ADDED" && time.Now().Sub(pod.Status.StartTime.Time) < 2*time.Second {
name = "start"
} else if event.Type == "DELETED" {
name = "die"
} else if event.Type == "MODIFIED" {
if time.Now().Sub(pod.Status.StartTime.Time) < 5*time.Second {
name = "start"
} else {
log.Debug().Str("pod", pod.Name).Msg("No changes to pod to report")
continue
}
} else if event.Type == "MODIFIED" && time.Now().Sub(pod.Status.StartTime.Time) < 2*time.Second {
name = "start"
} else {
log.Debug().Str("pod", pod.Name).Msg("No changes to pod to report")
continue
}

log.Debug().Interface("event.Type", event.Type).Str("name", name).Interface("StartTime", pod.Status.StartTime).Msg("Sending container event")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/amir20/dozzle/internal/container"
"github.com/amir20/dozzle/internal/docker"
container_support "github.com/amir20/dozzle/internal/support/container"
"github.com/docker/docker/pkg/stdcopy"
"github.com/rs/zerolog/log"
)

type DockerClientService struct {
Expand All @@ -23,7 +25,29 @@ func NewDockerClientService(client *docker.DockerClient, filter container.Contai
}

func (d *DockerClientService) RawLogs(ctx context.Context, container container.Container, from time.Time, to time.Time, stdTypes container.StdType) (io.ReadCloser, error) {
return d.client.ContainerLogsBetweenDates(ctx, container.ID, from, to, stdTypes)
reader, err := d.client.ContainerLogsBetweenDates(ctx, container.ID, from, to, stdTypes)
if err != nil {
return nil, err
}

in, out := io.Pipe()

go func() {
if container.Tty {
if _, err := io.Copy(out, reader); err != nil {
log.Error().Err(err).Msgf("error copying logs for container %s", container.ID)
}
} else {
if _, err := stdcopy.StdCopy(out, out, reader); err != nil {
log.Error().Err(err).Msgf("error copying logs for container %s", container.ID)
}
}

out.Close()
}()

return in, nil

}

func (d *DockerClientService) LogsBetweenDates(ctx context.Context, c container.Container, from time.Time, to time.Time, stdTypes container.StdType) (<-chan *container.LogEvent, error) {
Expand Down
20 changes: 6 additions & 14 deletions internal/web/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/amir20/dozzle/internal/auth"
"github.com/amir20/dozzle/internal/container"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -90,19 +89,12 @@ func (h *handler) downloadLogs(w http.ResponseWriter, r *http.Request) {
return
}

// Copy logs directly to zip entry
if containerService.Container.Tty {
if _, err := io.Copy(f, reader); err != nil {
log.Error().Err(err).Msgf("error copying logs for container %s", id)
http.Error(w, fmt.Sprintf("error copying logs for container %s: %v", id, err), http.StatusInternalServerError)
return
}
} else {
if _, err := stdcopy.StdCopy(f, f, reader); err != nil {
log.Error().Err(err).Msgf("error copying logs for container %s", id)
http.Error(w, fmt.Sprintf("error copying logs for container %s: %v", id, err), http.StatusInternalServerError)
return
}
// Copy logs to zip file
_, err = io.Copy(f, reader)
if err != nil {
log.Error().Err(err).Msgf("error copying logs for container %s", id)
http.Error(w, fmt.Sprintf("error copying logs for container %s: %v", id, err), http.StatusInternalServerError)
return
}
}
}

0 comments on commit 9dcbd8e

Please sign in to comment.