From 85ea00f3ad3e52ef56e5664a43cfd78cdd1ede9d Mon Sep 17 00:00:00 2001 From: Amin Faez Date: Wed, 8 May 2024 21:03:14 +0200 Subject: [PATCH 1/3] Add label definitions that allow containers to override image name and id --- pkg/container/client.go | 6 ++++++ pkg/container/container.go | 2 +- pkg/container/metadata.go | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/container/client.go b/pkg/container/client.go index c6c37de..c05e855 100644 --- a/pkg/container/client.go +++ b/pkg/container/client.go @@ -332,6 +332,12 @@ func (client dockerClient) HasNewImage(ctx context.Context, container t.Containe currentImageID := t.ImageID(container.ContainerInfo().ContainerJSONBase.Image) imageName := container.ImageName() + imageIDSetByLabel, ok := container.ContainerInfo().Config.Labels[originalImageIDLabel] + if ok { + currentImageID = t.ImageID(imageIDSetByLabel) + log.Infof("Original image id for %s found: (%s)", imageName, currentImageID.ShortID()) + } + newImageInfo, _, err := client.api.ImageInspectWithRaw(ctx, imageName) if err != nil { return false, currentImageID, err diff --git a/pkg/container/container.go b/pkg/container/container.go index 10ed677..280ba80 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -103,7 +103,7 @@ func (c Container) SafeImageID() wt.ImageID { // "latest" tag is assumed. func (c Container) ImageName() string { // Compatibility w/ Zodiac deployments - imageName, ok := c.getLabelValue(zodiacLabel) + imageName, ok := c.getLabelValue(originalImageNameLabel) if !ok { imageName = c.containerInfo.Config.Image } diff --git a/pkg/container/metadata.go b/pkg/container/metadata.go index 8ac5f34..7a1ffca 100644 --- a/pkg/container/metadata.go +++ b/pkg/container/metadata.go @@ -10,6 +10,8 @@ const ( noPullLabel = "com.centurylinklabs.watchtower.no-pull" dependsOnLabel = "com.centurylinklabs.watchtower.depends-on" zodiacLabel = "com.centurylinklabs.zodiac.original-image" + originalImageNameLabel = "com.centurylinklabs.watchtower.original-image" + originalImageIDLabel = "com.centurylinklabs.watchtower.original-image-id" scope = "com.centurylinklabs.watchtower.scope" preCheckLabel = "com.centurylinklabs.watchtower.lifecycle.pre-check" postCheckLabel = "com.centurylinklabs.watchtower.lifecycle.post-check" From ebe3fafeef8144971b03ba66471c2bdfaa8877b4 Mon Sep 17 00:00:00 2001 From: Amin Faez Date: Wed, 8 May 2024 21:27:55 +0200 Subject: [PATCH 2/3] Add comments that describe original image info overwriting --- pkg/container/client.go | 6 ++++-- pkg/container/container.go | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/container/client.go b/pkg/container/client.go index c05e855..cb74b41 100644 --- a/pkg/container/client.go +++ b/pkg/container/client.go @@ -329,10 +329,12 @@ func (client dockerClient) IsContainerStale(container t.Container, params t.Upda } func (client dockerClient) HasNewImage(ctx context.Context, container t.Container) (hasNew bool, latestImage t.ImageID, err error) { - currentImageID := t.ImageID(container.ContainerInfo().ContainerJSONBase.Image) + container_info := container.ContainerInfo() + currentImageID := t.ImageID(container_info.ContainerJSONBase.Image) imageName := container.ImageName() - imageIDSetByLabel, ok := container.ContainerInfo().Config.Labels[originalImageIDLabel] + // If the original-image-id label is set, it overwrites the image id reported by docker + imageIDSetByLabel, ok := container_info.Config.Labels[originalImageIDLabel] if ok { currentImageID = t.ImageID(imageIDSetByLabel) log.Infof("Original image id for %s found: (%s)", imageName, currentImageID.ShortID()) diff --git a/pkg/container/container.go b/pkg/container/container.go index 280ba80..7b18bb2 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -103,7 +103,11 @@ func (c Container) SafeImageID() wt.ImageID { // "latest" tag is assumed. func (c Container) ImageName() string { // Compatibility w/ Zodiac deployments - imageName, ok := c.getLabelValue(originalImageNameLabel) + imageName, ok := c.getLabelValue(zodiacLabel) + // If the original-image-name label is set, it overwrites the image name reported by docker + if !ok { + imageName, ok = c.getLabelValue(originalImageNameLabel) + } if !ok { imageName = c.containerInfo.Config.Image } From 002308db4d1e59ce297586b0f5410f58c6d9727f Mon Sep 17 00:00:00 2001 From: Amin Faez Date: Thu, 9 May 2024 17:33:30 +0200 Subject: [PATCH 3/3] Change logging level of the message relating to overwritten original image id and name --- pkg/container/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/container/client.go b/pkg/container/client.go index cb74b41..dc83615 100644 --- a/pkg/container/client.go +++ b/pkg/container/client.go @@ -337,7 +337,7 @@ func (client dockerClient) HasNewImage(ctx context.Context, container t.Containe imageIDSetByLabel, ok := container_info.Config.Labels[originalImageIDLabel] if ok { currentImageID = t.ImageID(imageIDSetByLabel) - log.Infof("Original image id for %s found: (%s)", imageName, currentImageID.ShortID()) + log.Debugf("Original image id for %s found: (%s)", imageName, currentImageID.ShortID()) } newImageInfo, _, err := client.api.ImageInspectWithRaw(ctx, imageName)