Skip to content

Commit 657643e

Browse files
authored
fix: fixes panic in agent with stopped containers (#3167)
1 parent b427760 commit 657643e

File tree

7 files changed

+45
-76
lines changed

7 files changed

+45
-76
lines changed

internal/agent/client.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ func (c *Client) StreamNewContainers(ctx context.Context, containers chan<- dock
240240
return rpcErrToErr(err)
241241
}
242242

243-
started := resp.Container.Started.AsTime()
244-
245243
containers <- docker.Container{
246244
ID: resp.Container.Id,
247245
Name: resp.Container.Name,
@@ -254,7 +252,7 @@ func (c *Client) StreamNewContainers(ctx context.Context, containers chan<- dock
254252
Health: resp.Container.Health,
255253
Host: resp.Container.Host,
256254
Tty: resp.Container.Tty,
257-
StartedAt: &started,
255+
StartedAt: resp.Container.Started.AsTime(),
258256
Command: resp.Container.Command,
259257
}
260258
}
@@ -277,12 +275,6 @@ func (c *Client) FindContainer(containerID string) (docker.Container, error) {
277275
})
278276
}
279277

280-
var startedAt *time.Time
281-
if response.Container.Started != nil {
282-
started := response.Container.Started.AsTime()
283-
startedAt = &started
284-
}
285-
286278
return docker.Container{
287279
ID: response.Container.Id,
288280
Name: response.Container.Name,
@@ -296,8 +288,8 @@ func (c *Client) FindContainer(containerID string) (docker.Container, error) {
296288
Host: response.Container.Host,
297289
Tty: response.Container.Tty,
298290
Command: response.Container.Command,
291+
StartedAt: response.Container.Started.AsTime(),
299292
Stats: utils.RingBufferFrom(300, stats),
300-
StartedAt: startedAt,
301293
}, nil
302294
}
303295

@@ -319,12 +311,6 @@ func (c *Client) ListContainers() ([]docker.Container, error) {
319311
})
320312
}
321313

322-
var startedAt *time.Time
323-
if container.Started != nil {
324-
started := container.Started.AsTime()
325-
startedAt = &started
326-
}
327-
328314
containers = append(containers, docker.Container{
329315
ID: container.Id,
330316
Name: container.Name,
@@ -337,9 +323,9 @@ func (c *Client) ListContainers() ([]docker.Container, error) {
337323
Health: container.Health,
338324
Host: container.Host,
339325
Tty: container.Tty,
340-
Stats: utils.RingBufferFrom(300, stats),
341326
Command: container.Command,
342-
StartedAt: startedAt,
327+
StartedAt: container.Started.AsTime(),
328+
Stats: utils.RingBufferFrom(300, stats),
343329
})
344330
}
345331

internal/agent/client_test.go

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,16 @@ func init() {
112112
})
113113

114114
client.On("FindContainer", "123456").Return(docker.Container{
115-
ID: "123456",
116-
Name: "test",
117-
Host: "localhost",
118-
Image: "test",
119-
ImageID: "test",
120-
StartedAt: &time.Time{},
121-
State: "running",
122-
Health: "healthy",
123-
Group: "test",
124-
Command: "test",
125-
Created: time.Time{},
126-
Tty: true,
115+
ID: "123456",
116+
Name: "test",
117+
Host: "localhost",
118+
Image: "test",
119+
ImageID: "test",
120+
State: "running",
121+
Health: "healthy",
122+
Group: "test",
123+
Command: "test",
124+
Tty: true,
127125
Labels: map[string]string{
128126
"test": "test",
129127
},
@@ -147,18 +145,16 @@ func TestFindContainer(t *testing.T) {
147145
container, _ := rpc.FindContainer("123456")
148146

149147
assert.Equal(t, container, docker.Container{
150-
ID: "123456",
151-
Name: "test",
152-
Host: "localhost",
153-
Image: "test",
154-
ImageID: "test",
155-
StartedAt: &time.Time{},
156-
State: "running",
157-
Health: "healthy",
158-
Group: "test",
159-
Command: "test",
160-
Created: time.Time{},
161-
Tty: true,
148+
ID: "123456",
149+
Name: "test",
150+
Host: "localhost",
151+
Image: "test",
152+
ImageID: "test",
153+
State: "running",
154+
Health: "healthy",
155+
Group: "test",
156+
Command: "test",
157+
Tty: true,
162158
Labels: map[string]string{
163159
"test": "test",
164160
},
@@ -176,18 +172,16 @@ func TestListContainers(t *testing.T) {
176172

177173
assert.Equal(t, containers, []docker.Container{
178174
{
179-
ID: "123456",
180-
Name: "test",
181-
Host: "localhost",
182-
Image: "test",
183-
ImageID: "test",
184-
StartedAt: &time.Time{},
185-
State: "running",
186-
Health: "healthy",
187-
Group: "test",
188-
Command: "test",
189-
Created: time.Time{},
190-
Tty: true,
175+
ID: "123456",
176+
Name: "test",
177+
Host: "localhost",
178+
Image: "test",
179+
ImageID: "test",
180+
State: "running",
181+
Health: "healthy",
182+
Group: "test",
183+
Command: "test",
184+
Tty: true,
191185
Labels: map[string]string{
192186
"test": "test",
193187
},

internal/agent/server.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (s *server) FindContainer(ctx context.Context, in *pb.FindContainerRequest)
189189
Tty: container.Tty,
190190
Labels: container.Labels,
191191
Group: container.Group,
192-
Started: timestamppb.New(*container.StartedAt),
192+
Started: timestamppb.New(container.StartedAt),
193193
},
194194
}, nil
195195
}
@@ -213,11 +213,6 @@ func (s *server) ListContainers(ctx context.Context, in *pb.ListContainersReques
213213
})
214214
}
215215

216-
var startedAt *timestamppb.Timestamp
217-
if container.StartedAt != nil {
218-
startedAt = timestamppb.New(*container.StartedAt)
219-
}
220-
221216
pbContainers = append(pbContainers, &pb.Container{
222217
Id: container.ID,
223218
Name: container.Name,
@@ -230,7 +225,7 @@ func (s *server) ListContainers(ctx context.Context, in *pb.ListContainersReques
230225
Tty: container.Tty,
231226
Labels: container.Labels,
232227
Group: container.Group,
233-
Started: startedAt,
228+
Started: timestamppb.New(container.StartedAt),
234229
Stats: pbStats,
235230
Command: container.Command,
236231
})
@@ -276,7 +271,7 @@ func (s *server) StreamContainerStarted(in *pb.StreamContainerStartedRequest, ou
276271
Tty: container.Tty,
277272
Labels: container.Labels,
278273
Group: container.Group,
279-
Started: timestamppb.New(*container.StartedAt),
274+
Started: timestamppb.New(container.StartedAt),
280275
},
281276
})
282277
case <-out.Context().Done():

internal/docker/client.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,11 @@ func newContainerFromJSON(c types.ContainerJSON, host string) Container {
405405
}
406406

407407
if startedAt, err := time.Parse(time.RFC3339Nano, c.State.StartedAt); err == nil {
408-
utc := startedAt.UTC()
409-
container.StartedAt = &utc
408+
container.StartedAt = startedAt.UTC()
410409
}
411410

412411
if createdAt, err := time.Parse(time.RFC3339Nano, c.Created); err == nil {
413-
utc := createdAt.UTC()
414-
container.Created = utc
412+
container.Created = createdAt.UTC()
415413
}
416414

417415
if c.State.Health != nil {

internal/docker/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Container struct {
1616
ImageID string `json:"imageId"`
1717
Command string `json:"command"`
1818
Created time.Time `json:"created"`
19-
StartedAt *time.Time `json:"startedAt,omitempty"`
19+
StartedAt time.Time `json:"startedAt,omitempty"`
2020
State string `json:"state"`
2121
Health string `json:"health,omitempty"`
2222
Host string `json:"host,omitempty"`

internal/web/logs.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,12 @@ func streamLogsForContainers(w http.ResponseWriter, r *http.Request, multiHostCl
206206
}
207207

208208
streamLogs := func(container docker.Container) {
209-
start := time.Time{}
210-
if container.StartedAt != nil {
211-
start = *container.StartedAt
212-
}
213209
containerService, err := multiHostClient.FindContainer(container.Host, container.ID)
214210
if err != nil {
215211
log.Errorf("error while finding container %v", err.Error())
216212
return
217213
}
218-
err = containerService.StreamLogs(r.Context(), start, stdTypes, logs)
214+
err = containerService.StreamLogs(r.Context(), container.StartedAt, stdTypes, logs)
219215
if err != nil {
220216
if errors.Is(err, io.EOF) {
221217
log.WithError(err).Debugf("stream closed for container %v", container.Name)

internal/web/logs_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Test_handler_streamLogs_happy(t *testing.T) {
3838

3939
now := time.Now()
4040

41-
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Tty: false, Host: "localhost", StartedAt: &now}, nil)
41+
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Tty: false, Host: "localhost", StartedAt: now}, nil)
4242
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, now, docker.STDALL).Return(io.NopCloser(bytes.NewReader(data)), nil).
4343
Run(func(args mock.Arguments) {
4444
go func() {
@@ -80,7 +80,7 @@ func Test_handler_streamLogs_happy_with_id(t *testing.T) {
8080

8181
started := time.Date(2020, time.May, 13, 18, 55, 37, 772853839, time.UTC)
8282

83-
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: &started}, nil)
83+
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: started}, nil)
8484
mockedClient.On("ContainerLogs", mock.Anything, mock.Anything, started, docker.STDALL).Return(io.NopCloser(bytes.NewReader(data)), nil).
8585
Run(func(args mock.Arguments) {
8686
go func() {
@@ -120,7 +120,7 @@ func Test_handler_streamLogs_happy_container_stopped(t *testing.T) {
120120

121121
started := time.Date(2020, time.May, 13, 18, 55, 37, 772853839, time.UTC)
122122
mockedClient := new(MockedClient)
123-
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: &started}, nil)
123+
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: started}, nil)
124124
mockedClient.On("ContainerLogs", mock.Anything, id, started, docker.STDALL).Return(io.NopCloser(strings.NewReader("")), io.EOF).
125125
Run(func(args mock.Arguments) {
126126
go func() {
@@ -189,7 +189,7 @@ func Test_handler_streamLogs_error_reading(t *testing.T) {
189189

190190
started := time.Date(2020, time.May, 13, 18, 55, 37, 772853839, time.UTC)
191191
mockedClient := new(MockedClient)
192-
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: &started}, nil)
192+
mockedClient.On("FindContainer", id).Return(docker.Container{ID: id, Host: "localhost", StartedAt: started}, nil)
193193
mockedClient.On("ContainerLogs", mock.Anything, id, started, docker.STDALL).Return(io.NopCloser(strings.NewReader("")), errors.New("test error")).
194194
Run(func(args mock.Arguments) {
195195
go func() {

0 commit comments

Comments
 (0)