Merge pull request #3911 from ktock/passinfo

Enable to propagate necessary information to snapshotter during unpack
This commit is contained in:
Michael Crosby 2019-12-26 10:42:17 -05:00 committed by GitHub
commit 6b94b646b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 25 deletions

View File

@ -219,7 +219,7 @@ func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
inner := snapshots.Info{ inner := snapshots.Info{
Name: bkey, Name: bkey,
Labels: filterInheritedLabels(local.Labels), Labels: snapshots.FilterInheritedLabels(local.Labels),
} }
// NOTE: Perform this inside the transaction to reduce the // NOTE: Perform this inside the transaction to reduce the
@ -306,7 +306,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
bparent string bparent string
bkey string bkey string
bopts = []snapshots.Opt{ bopts = []snapshots.Opt{
snapshots.WithLabels(filterInheritedLabels(base.Labels)), snapshots.WithLabels(snapshots.FilterInheritedLabels(base.Labels)),
} }
) )
@ -586,7 +586,7 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
return err return err
} }
inheritedOpt := snapshots.WithLabels(filterInheritedLabels(base.Labels)) inheritedOpt := snapshots.WithLabels(snapshots.FilterInheritedLabels(base.Labels))
// NOTE: Backend snapshotters should commit fast and reliably to // NOTE: Backend snapshotters should commit fast and reliably to
// prevent metadata store locking and minimizing rollbacks. // prevent metadata store locking and minimizing rollbacks.
@ -939,20 +939,3 @@ func (s *snapshotter) pruneBranch(ctx context.Context, node *treeNode) error {
func (s *snapshotter) Close() error { func (s *snapshotter) Close() error {
return s.Snapshotter.Close() return s.Snapshotter.Close()
} }
// filterInheritedLabels filters the provided labels by removing any key which
// isn't a snapshot label. Snapshot labels have a prefix of "containerd.io/snapshot/"
// or are the "containerd.io/snapshot.ref" label.
func filterInheritedLabels(labels map[string]string) map[string]string {
if labels == nil {
return nil
}
filtered := make(map[string]string)
for k, v := range labels {
if k == labelSnapshotRef || strings.HasPrefix(k, inheritedLabelsPrefix) {
filtered[k] = v
}
}
return filtered
}

View File

@ -227,7 +227,7 @@ func TestFilterInheritedLabels(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
if actual := filterInheritedLabels(test.labels); !reflect.DeepEqual(actual, test.expected) { if actual := snapshots.FilterInheritedLabels(test.labels); !reflect.DeepEqual(actual, test.expected) {
t.Fatalf("expected %v but got %v", test.expected, actual) t.Fatalf("expected %v but got %v", test.expected, actual)
} }
} }

View File

@ -81,7 +81,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
if wrapper == nil { if wrapper == nil {
return unpackWrapper(h) return unpackWrapper(h)
} }
return wrapper(unpackWrapper(h)) return unpackWrapper(wrapper(h))
} }
} }

View File

@ -25,6 +25,11 @@ import (
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
) )
const (
inheritedLabelsPrefix = "containerd.io/snapshot/"
labelSnapshotRef = "containerd.io/snapshot.ref"
)
// Kind identifies the kind of snapshot. // Kind identifies the kind of snapshot.
type Kind uint8 type Kind uint8
@ -346,3 +351,20 @@ func WithLabels(labels map[string]string) Opt {
return nil return nil
} }
} }
// FilterInheritedLabels filters the provided labels by removing any key which
// isn't a snapshot label. Snapshot labels have a prefix of "containerd.io/snapshot/"
// or are the "containerd.io/snapshot.ref" label.
func FilterInheritedLabels(labels map[string]string) map[string]string {
if labels == nil {
return nil
}
filtered := make(map[string]string)
for k, v := range labels {
if k == labelSnapshotRef || strings.HasPrefix(k, inheritedLabelsPrefix) {
filtered[k] = v
}
}
return filtered
}

View File

@ -41,6 +41,10 @@ import (
"golang.org/x/sync/semaphore" "golang.org/x/sync/semaphore"
) )
const (
labelSnapshotRef = "containerd.io/snapshot.ref"
)
type unpacker struct { type unpacker struct {
updateCh chan ocispec.Descriptor updateCh chan ocispec.Descriptor
snapshotter string snapshotter string
@ -113,9 +117,13 @@ EachLayer:
return errors.Wrapf(err, "failed to stat snapshot %s", chainID) return errors.Wrapf(err, "failed to stat snapshot %s", chainID)
} }
labelOpt := snapshots.WithLabels(map[string]string{ // inherits annotations which are provided as snapshot labels.
"containerd.io/snapshot.ref": chainID, labels := snapshots.FilterInheritedLabels(desc.Annotations)
}) if labels == nil {
labels = make(map[string]string)
}
labels[labelSnapshotRef] = chainID
labelOpt := snapshots.WithLabels(labels)
var ( var (
key string key string