Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for opencontainers meta labels #1729

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/actions/mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

dockerTypes "github.com/docker/docker/api/types"

t "github.com/containrrr/watchtower/pkg/types"
)

Expand Down Expand Up @@ -66,6 +68,11 @@ func (client MockClient) RemoveImageByID(_ t.ImageID) error {
return nil
}

// GetImage is a mock method
func (client MockClient) GetImage(_ t.ImageID) (dockerTypes.ImageInspect, error) {
return dockerTypes.ImageInspect{}, nil
}

// GetContainer is a mock method
func (client MockClient) GetContainer(_ t.ContainerID) (t.Container, error) {
return client.TestData.Containers[0], nil
Expand Down
7 changes: 5 additions & 2 deletions internal/actions/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Update(client container.Client, params types.UpdateParams) (types.Report, e
staleCheckFailed := 0

for i, targetContainer := range containers {
stale, newestImage, err := client.IsContainerStale(targetContainer)
stale, newestImageID, err := client.IsContainerStale(targetContainer)
shouldUpdate := stale && !params.NoRestart && !params.MonitorOnly && !targetContainer.IsMonitorOnly()
if err == nil && shouldUpdate {
// Check to make sure we have all the necessary information for recreating the container
Expand All @@ -55,12 +55,15 @@ func Update(client container.Client, params types.UpdateParams) (types.Report, e
staleCheckFailed++
progress.AddSkipped(targetContainer, err)
} else {
progress.AddScanned(targetContainer, newestImage)
progress.AddScanned(targetContainer, newestImageID)
}
containers[i].SetStale(stale)

if stale {
staleCount++
if latestImage, err := client.GetImage(newestImageID); err == nil {
progress.UpdateLatestImage(targetContainer.ID(), latestImage)
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
ExecuteCommand(containerID t.ContainerID, command string, timeout int) (SkipUpdate bool, err error)
RemoveImageByID(t.ImageID) error
WarnOnHeadPullFailed(container t.Container) bool
GetImage(imageID t.ImageID) (types.ImageInspect, error)
}

// NewClient returns a new Client instance which can be used to interact with
Expand Down Expand Up @@ -397,6 +398,14 @@
return nil
}

func (client dockerClient) GetImage(id t.ImageID) (types.ImageInspect, error) {
imageInfo, _, err := client.api.ImageInspectWithRaw(context.Background(), string(id))
if err != nil {
return types.ImageInspect{}, err
}
return imageInfo, nil

Check warning on line 406 in pkg/container/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/container/client.go#L401-L406

Added lines #L401 - L406 were not covered by tests
}

func (client dockerClient) RemoveImageByID(id t.ImageID) error {
log.Infof("Removing image %s", id.ShortID())

Expand Down
14 changes: 13 additions & 1 deletion pkg/session/container_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ type ContainerStatus struct {
containerName string
imageName string
error
state State
state State
beforeMeta imageMeta
afterMeta imageMeta
}

// ID returns the container ID
Expand Down Expand Up @@ -80,3 +82,13 @@ func (u *ContainerStatus) State() string {
return "Unknown"
}
}

// Before returns the metadata for the image considered latest before the session
func (u *ContainerStatus) Before() wt.ImageMeta {
return u.beforeMeta
}

// After returns the metadata for the image considered latest after the session
func (u *ContainerStatus) After() wt.ImageMeta {
return u.afterMeta
}
58 changes: 58 additions & 0 deletions pkg/session/image_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package session

import "strings"

type imageMeta map[string]string

const openContainersPrefix = "org.opencontainers.image."

func imageMetaFromLabels(labels map[string]string) imageMeta {
im := make(imageMeta)
for key, value := range labels {
if strings.HasPrefix(key, openContainersPrefix) {
strippedKey := key[len(openContainersPrefix):]
im[strippedKey] = value
}
}
return im
}

func (im imageMeta) Authors() string {
return im["authors"]
}

func (im imageMeta) Created() string {
return im["created"]
}

func (im imageMeta) Description() string {
return im["description"]
}

func (im imageMeta) Documentation() string {
return im["documentation"]
}

func (im imageMeta) Licenses() string {
return im["licenses"]
}

func (im imageMeta) Revision() string {
return im["revision"]
}

func (im imageMeta) Source() string {
return im["source"]
}

func (im imageMeta) Title() string {
return im["title"]
}

func (im imageMeta) Url() string {
return im["url"]
}

func (im imageMeta) Version() string {
return im["version"]
}
22 changes: 20 additions & 2 deletions pkg/session/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ package session

import (
"github.com/containrrr/watchtower/pkg/types"
dockerTypes "github.com/docker/docker/api/types"
)

// Progress contains the current session container status
type Progress map[types.ContainerID]*ContainerStatus

// UpdateFromContainer sets various status fields from their corresponding container equivalents
func UpdateFromContainer(cont types.Container, newImage types.ImageID, state State) *ContainerStatus {

var beforeMeta imageMeta
if imageInfo := cont.ImageInfo(); imageInfo != nil && imageInfo.Config != nil {
beforeMeta = imageMetaFromLabels(imageInfo.Config.Labels)
} else {
beforeMeta = make(imageMeta)
}

return &ContainerStatus{
containerID: cont.ID(),
containerName: cont.Name(),
imageName: cont.ImageName(),
oldImage: cont.SafeImageID(),
newImage: newImage,
state: state,
beforeMeta: beforeMeta,
afterMeta: beforeMeta,
}
}

Expand All @@ -27,8 +38,9 @@ func (m Progress) AddSkipped(cont types.Container, err error) {
}

// AddScanned adds a container to the Progress with the state set as scanned
func (m Progress) AddScanned(cont types.Container, newImage types.ImageID) {
m.Add(UpdateFromContainer(cont, newImage, ScannedState))
func (m Progress) AddScanned(cont types.Container, newImageID types.ImageID) {
m.Add(UpdateFromContainer(cont, newImageID, ScannedState))

}

// UpdateFailed updates the containers passed, setting their state as failed with the supplied error
Expand All @@ -54,3 +66,9 @@ func (m Progress) MarkForUpdate(containerID types.ContainerID) {
func (m Progress) Report() types.Report {
return NewReport(m)
}

func (m Progress) UpdateLatestImage(containerID types.ContainerID, image dockerTypes.ImageInspect) {
if image.Config != nil {
m[containerID].afterMeta = imageMetaFromLabels(image.Config.Labels)
}
}
14 changes: 14 additions & 0 deletions pkg/types/image_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

type ImageMeta interface {
Authors() string
Created() string
Description() string
Documentation() string
Licenses() string
Revision() string
Source() string
Title() string
Url() string
Version() string
}
2 changes: 2 additions & 0 deletions pkg/types/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ type ContainerReport interface {
ImageName() string
Error() string
State() string
Before() ImageMeta
After() ImageMeta
}
Loading