1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 06:28:42 +01:00

fix: fixes panic in agent with stopped containers (#3167)

This commit is contained in:
Amir Raminfar
2024-07-30 19:31:14 -07:00
committed by GitHub
parent b4277603cf
commit 657643e6ce
7 changed files with 45 additions and 76 deletions

View File

@@ -240,8 +240,6 @@ func (c *Client) StreamNewContainers(ctx context.Context, containers chan<- dock
return rpcErrToErr(err)
}
started := resp.Container.Started.AsTime()
containers <- docker.Container{
ID: resp.Container.Id,
Name: resp.Container.Name,
@@ -254,7 +252,7 @@ func (c *Client) StreamNewContainers(ctx context.Context, containers chan<- dock
Health: resp.Container.Health,
Host: resp.Container.Host,
Tty: resp.Container.Tty,
StartedAt: &started,
StartedAt: resp.Container.Started.AsTime(),
Command: resp.Container.Command,
}
}
@@ -277,12 +275,6 @@ func (c *Client) FindContainer(containerID string) (docker.Container, error) {
})
}
var startedAt *time.Time
if response.Container.Started != nil {
started := response.Container.Started.AsTime()
startedAt = &started
}
return docker.Container{
ID: response.Container.Id,
Name: response.Container.Name,
@@ -296,8 +288,8 @@ func (c *Client) FindContainer(containerID string) (docker.Container, error) {
Host: response.Container.Host,
Tty: response.Container.Tty,
Command: response.Container.Command,
StartedAt: response.Container.Started.AsTime(),
Stats: utils.RingBufferFrom(300, stats),
StartedAt: startedAt,
}, nil
}
@@ -319,12 +311,6 @@ func (c *Client) ListContainers() ([]docker.Container, error) {
})
}
var startedAt *time.Time
if container.Started != nil {
started := container.Started.AsTime()
startedAt = &started
}
containers = append(containers, docker.Container{
ID: container.Id,
Name: container.Name,
@@ -337,9 +323,9 @@ func (c *Client) ListContainers() ([]docker.Container, error) {
Health: container.Health,
Host: container.Host,
Tty: container.Tty,
Stats: utils.RingBufferFrom(300, stats),
Command: container.Command,
StartedAt: startedAt,
StartedAt: container.Started.AsTime(),
Stats: utils.RingBufferFrom(300, stats),
})
}

View File

@@ -112,18 +112,16 @@ func init() {
})
client.On("FindContainer", "123456").Return(docker.Container{
ID: "123456",
Name: "test",
Host: "localhost",
Image: "test",
ImageID: "test",
StartedAt: &time.Time{},
State: "running",
Health: "healthy",
Group: "test",
Command: "test",
Created: time.Time{},
Tty: true,
ID: "123456",
Name: "test",
Host: "localhost",
Image: "test",
ImageID: "test",
State: "running",
Health: "healthy",
Group: "test",
Command: "test",
Tty: true,
Labels: map[string]string{
"test": "test",
},
@@ -147,18 +145,16 @@ func TestFindContainer(t *testing.T) {
container, _ := rpc.FindContainer("123456")
assert.Equal(t, container, docker.Container{
ID: "123456",
Name: "test",
Host: "localhost",
Image: "test",
ImageID: "test",
StartedAt: &time.Time{},
State: "running",
Health: "healthy",
Group: "test",
Command: "test",
Created: time.Time{},
Tty: true,
ID: "123456",
Name: "test",
Host: "localhost",
Image: "test",
ImageID: "test",
State: "running",
Health: "healthy",
Group: "test",
Command: "test",
Tty: true,
Labels: map[string]string{
"test": "test",
},
@@ -176,18 +172,16 @@ func TestListContainers(t *testing.T) {
assert.Equal(t, containers, []docker.Container{
{
ID: "123456",
Name: "test",
Host: "localhost",
Image: "test",
ImageID: "test",
StartedAt: &time.Time{},
State: "running",
Health: "healthy",
Group: "test",
Command: "test",
Created: time.Time{},
Tty: true,
ID: "123456",
Name: "test",
Host: "localhost",
Image: "test",
ImageID: "test",
State: "running",
Health: "healthy",
Group: "test",
Command: "test",
Tty: true,
Labels: map[string]string{
"test": "test",
},

View File

@@ -189,7 +189,7 @@ func (s *server) FindContainer(ctx context.Context, in *pb.FindContainerRequest)
Tty: container.Tty,
Labels: container.Labels,
Group: container.Group,
Started: timestamppb.New(*container.StartedAt),
Started: timestamppb.New(container.StartedAt),
},
}, nil
}
@@ -213,11 +213,6 @@ func (s *server) ListContainers(ctx context.Context, in *pb.ListContainersReques
})
}
var startedAt *timestamppb.Timestamp
if container.StartedAt != nil {
startedAt = timestamppb.New(*container.StartedAt)
}
pbContainers = append(pbContainers, &pb.Container{
Id: container.ID,
Name: container.Name,
@@ -230,7 +225,7 @@ func (s *server) ListContainers(ctx context.Context, in *pb.ListContainersReques
Tty: container.Tty,
Labels: container.Labels,
Group: container.Group,
Started: startedAt,
Started: timestamppb.New(container.StartedAt),
Stats: pbStats,
Command: container.Command,
})
@@ -276,7 +271,7 @@ func (s *server) StreamContainerStarted(in *pb.StreamContainerStartedRequest, ou
Tty: container.Tty,
Labels: container.Labels,
Group: container.Group,
Started: timestamppb.New(*container.StartedAt),
Started: timestamppb.New(container.StartedAt),
},
})
case <-out.Context().Done():

View File

@@ -405,13 +405,11 @@ func newContainerFromJSON(c types.ContainerJSON, host string) Container {
}
if startedAt, err := time.Parse(time.RFC3339Nano, c.State.StartedAt); err == nil {
utc := startedAt.UTC()
container.StartedAt = &utc
container.StartedAt = startedAt.UTC()
}
if createdAt, err := time.Parse(time.RFC3339Nano, c.Created); err == nil {
utc := createdAt.UTC()
container.Created = utc
container.Created = createdAt.UTC()
}
if c.State.Health != nil {

View File

@@ -16,7 +16,7 @@ type Container struct {
ImageID string `json:"imageId"`
Command string `json:"command"`
Created time.Time `json:"created"`
StartedAt *time.Time `json:"startedAt,omitempty"`
StartedAt time.Time `json:"startedAt,omitempty"`
State string `json:"state"`
Health string `json:"health,omitempty"`
Host string `json:"host,omitempty"`

View File

@@ -206,16 +206,12 @@ func streamLogsForContainers(w http.ResponseWriter, r *http.Request, multiHostCl
}
streamLogs := func(container docker.Container) {
start := time.Time{}
if container.StartedAt != nil {
start = *container.StartedAt
}
containerService, err := multiHostClient.FindContainer(container.Host, container.ID)
if err != nil {
log.Errorf("error while finding container %v", err.Error())
return
}
err = containerService.StreamLogs(r.Context(), start, stdTypes, logs)
err = containerService.StreamLogs(r.Context(), container.StartedAt, stdTypes, logs)
if err != nil {
if errors.Is(err, io.EOF) {
log.WithError(err).Debugf("stream closed for container %v", container.Name)

View File

@@ -38,7 +38,7 @@ func Test_handler_streamLogs_happy(t *testing.T) {
now := time.Now()
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Tty: false, Host: "localhost", StartedAt: &now}, nil)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Tty: false, Host: "localhost", StartedAt: now}, nil)
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, now, docker.STDALL).Return(io.NopCloser(bytes.NewReader(data)), nil).
Run(func(args mock.Arguments) {
go func() {
@@ -80,7 +80,7 @@ func Test_handler_streamLogs_happy_with_id(t *testing.T) {
started := time.Date(2020, time.May, 13, 18, 55, 37, 772853839, time.UTC)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: &started}, nil)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: started}, nil)
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, started, docker.STDALL).Return(io.NopCloser(bytes.NewReader(data)), nil).
Run(func(args mock.Arguments) {
go func() {
@@ -120,7 +120,7 @@ func Test_handler_streamLogs_happy_container_stopped(t *testing.T) {
started := time.Date(2020, time.May, 13, 18, 55, 37, 772853839, time.UTC)
mockedClient := new(MockedClient)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: &started}, nil)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: started}, nil)
mockedClient.On("ContainerLogs", mock.Anything, id, started, docker.STDALL).Return(io.NopCloser(strings.NewReader("")), io.EOF).
Run(func(args mock.Arguments) {
go func() {
@@ -189,7 +189,7 @@ func Test_handler_streamLogs_error_reading(t *testing.T) {
started := time.Date(2020, time.May, 13, 18, 55, 37, 772853839, time.UTC)
mockedClient := new(MockedClient)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: &started}, nil)
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: started}, nil)
mockedClient.On("ContainerLogs", mock.Anything, id, started, docker.STDALL).Return(io.NopCloser(strings.NewReader("")), errors.New("test error")).
Run(func(args mock.Arguments) {
go func() {