This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Whitelist label ts using registry cache decorator
We did not take the inheritance of labels from base images into account while developing the timestamps from image labels feature. As a result, users started experiencing issues with automated image updates, due to the (moderately old) timestamp from the base image being picked up and prioritized over the timestamp from the registry. This commits adds the ability to decorate `ImageRepository` structures before they are returned from cache by `GetImageRepositoryMetadata`. The sole decorator for now is `TimestampLabelWhitelist`, which replaces the `CreatedAt` field on the image info with the timestamp specified in one of the labels if the canonical name of the image matches one of the glob patterns on the whitelist. As a result, the labels from images will only be used if explicitly instructed by the user, dealing with the problem described at the top of this comment.
- Loading branch information
Showing
4 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package cache | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/weaveworks/flux/image" | ||
) | ||
|
||
// mockStorage holds a fixed ImageRepository item. | ||
type mockStorage struct { | ||
Item ImageRepository | ||
} | ||
|
||
// GetKey will always return the same item from the storage, | ||
// and does not care about the key it receives. | ||
func (m *mockStorage) GetKey(k Keyer) ([]byte, time.Time, error) { | ||
b, err := json.Marshal(m.Item) | ||
if err != nil { | ||
return []byte{}, time.Time{}, err | ||
} | ||
return b, time.Time{}, nil | ||
} | ||
|
||
// appendImage adds an image to the mocked storage item. | ||
func (m *mockStorage) appendImage(i image.Info) { | ||
tag := i.ID.Tag | ||
|
||
m.Item.Images[tag] = i | ||
m.Item.Tags = append(m.Item.Tags, tag) | ||
} | ||
|
||
func mockReader() *mockStorage { | ||
return &mockStorage{ | ||
Item: ImageRepository{ | ||
RepositoryMetadata: image.RepositoryMetadata{ | ||
Tags: []string{}, | ||
Images: map[string]image.Info{}, | ||
}, | ||
LastUpdate: time.Now(), | ||
}, | ||
} | ||
} | ||
|
||
func Test_WhitelabelDecorator(t *testing.T) { | ||
r := mockReader() | ||
|
||
// Image with no timestamp label | ||
r.appendImage(mustMakeInfo("docker.io/fluxcd/flux:equal", time.Time{}, time.Now().UTC())) | ||
// Image with a timestamp label | ||
r.appendImage(mustMakeInfo("docker.io/fluxcd/flux:label", time.Now().Add(-10*time.Second).UTC(), time.Now().UTC())) | ||
// Image with the wrong canonical ref | ||
r.appendImage(mustMakeInfo("docker.io/a/different:canonical", time.Now().Add(-10*time.Second).UTC(), time.Now().UTC())) | ||
|
||
c := Cache{r, []Decorator{TimestampLabelWhitelist{"index.docker.io/fluxcd/*"}}} | ||
|
||
rm, err := c.GetImageRepositoryMetadata(image.Name{}) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, r.Item.Images["equal"].CreatedAt, rm.Images["equal"].CreatedAt) | ||
assert.Equal(t, r.Item.Images["label"].Labels.Created, rm.Images["label"].CreatedAt) | ||
assert.Equal(t, r.Item.Images["canonical"].CreatedAt, rm.Images["canonical"].CreatedAt) | ||
} | ||
|
||
func mustMakeInfo(ref string, label time.Time, created time.Time) image.Info { | ||
r, err := image.ParseRef(ref) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var labels image.Labels | ||
if !label.IsZero() { | ||
labels.Created = label | ||
} | ||
return image.Info{ID: r, Labels: labels, CreatedAt: created} | ||
} |