Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit a12288b

Browse files
authored
Merge pull request #331 from vdemeester/262-fix
Remove Container.name field…
2 parents bc76e8b + fafa3d8 commit a12288b

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

docker/container.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
type Container struct {
3030
// FIXME(vdemeester) Replace with ContainerClient with engine-api vendor update
3131
client client.APIClient
32-
name string
3332
id string
3433
container *types.ContainerJSON
3534
}
@@ -40,18 +39,17 @@ func CreateContainer(ctx context.Context, client client.APIClient, name string,
4039
if err != nil {
4140
return nil, err
4241
}
43-
return New(ctx, client, container.ID, name)
42+
return New(ctx, client, container.ID)
4443
}
4544

4645
// New creates a container struct with the specified client, id and name
47-
func New(ctx context.Context, client client.APIClient, id, name string) (*Container, error) {
46+
func New(ctx context.Context, client client.APIClient, id string) (*Container, error) {
4847
container, err := GetContainer(ctx, client, id)
4948
if err != nil {
5049
return nil, err
5150
}
5251
return &Container{
5352
client: client,
54-
name: name,
5553
id: id,
5654
container: container,
5755
}, nil
@@ -61,7 +59,6 @@ func New(ctx context.Context, client client.APIClient, id, name string) (*Contai
6159
func NewInspected(client client.APIClient, container *types.ContainerJSON) *Container {
6260
return &Container{
6361
client: client,
64-
name: container.Name,
6562
id: container.ID,
6663
container: container,
6764
}
@@ -290,9 +287,9 @@ func holdHijackedConnection(tty bool, inputStream io.ReadCloser, outputStream, e
290287

291288
// Start the specified container with the specified host config
292289
func (c *Container) Start(ctx context.Context) error {
293-
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "c.name": c.name}).Debug("Starting container")
290+
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "container.Name": c.container.Name}).Debug("Starting container")
294291
if err := c.client.ContainerStart(ctx, c.container.ID, types.ContainerStartOptions{}); err != nil {
295-
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "c.name": c.name}).Debug("Failed to start container")
292+
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "container.Name": c.container.Name}).Debug("Failed to start container")
296293
return err
297294
}
298295
return nil
@@ -305,7 +302,7 @@ func (c *Container) ID() (string, error) {
305302

306303
// Name returns the container name.
307304
func (c *Container) Name() string {
308-
return c.name
305+
return c.container.Name
309306
}
310307

311308
// Restart restarts the container if existing, does nothing otherwise.
@@ -327,7 +324,7 @@ func (c *Container) Log(ctx context.Context, l logger.Logger, follow bool) error
327324
Follow: follow,
328325
Tail: "all",
329326
}
330-
responseBody, err := c.client.ContainerLogs(ctx, c.name, options)
327+
responseBody, err := c.client.ContainerLogs(ctx, c.container.ID, options)
331328
if err != nil {
332329
return err
333330
}

docker/service.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ func (s *Service) collectContainers(ctx context.Context) ([]*Container, error) {
122122
result := []*Container{}
123123

124124
for _, container := range containers {
125-
// Compose add "/" before name, so Name[1] will store actaul name.
126-
name := strings.SplitAfter(container.Names[0], "/")
127-
c, err := New(ctx, client, container.ID, name[1])
125+
c, err := New(ctx, client, container.ID)
128126
if err != nil {
129127
return nil, err
130128
}
@@ -461,11 +459,11 @@ func (s *Service) recreateIfNeeded(ctx context.Context, c *Container, noRecreate
461459
}
462460

463461
func (s *Service) recreate(ctx context.Context, c *Container) (*Container, error) {
464-
name := c.name
462+
name := c.Name()
465463
newName := fmt.Sprintf("%s_%s", name, c.container.ID[:12])
466464
logrus.Debugf("Renaming %s => %s", name, newName)
467465
if err := c.Rename(ctx, newName); err != nil {
468-
logrus.Errorf("Failed to rename old container %s", c.name)
466+
logrus.Errorf("Failed to rename old container %s", c.Name())
469467
return nil, err
470468
}
471469
namer := NewSingleNamer(name)
@@ -475,24 +473,24 @@ func (s *Service) recreate(ctx context.Context, c *Container) (*Container, error
475473
}
476474
logrus.Debugf("Created replacement container %s", newContainer.container.ID)
477475
if err := c.Remove(ctx, false); err != nil {
478-
logrus.Errorf("Failed to remove old container %s", c.name)
476+
logrus.Errorf("Failed to remove old container %s", c.Name())
479477
return nil, err
480478
}
481-
logrus.Debugf("Removed old container %s %s", c.name, c.container.ID)
479+
logrus.Debugf("Removed old container %s %s", c.Name(), c.container.ID)
482480
return newContainer, nil
483481
}
484482

485483
// OutOfSync checks if the container is out of sync with the service definition.
486484
// It looks if the the service hash container label is the same as the computed one.
487485
func (s *Service) OutOfSync(ctx context.Context, c *Container) (bool, error) {
488486
if c.ImageConfig() != s.serviceConfig.Image {
489-
logrus.Debugf("Images for %s do not match %s!=%s", c.name, c.ImageConfig(), s.serviceConfig.Image)
487+
logrus.Debugf("Images for %s do not match %s!=%s", c.Name(), c.ImageConfig(), s.serviceConfig.Image)
490488
return true, nil
491489
}
492490

493491
expectedHash := config.GetServiceHash(s.name, s.Config())
494492
if c.Hash() != expectedHash {
495-
logrus.Debugf("Hashes for %s do not match %s!=%s", c.name, c.Hash(), expectedHash)
493+
logrus.Debugf("Hashes for %s do not match %s!=%s", c.Name(), c.Hash(), expectedHash)
496494
return true, nil
497495
}
498496

0 commit comments

Comments
 (0)