Skip to content

Commit 8bb5213

Browse files
Merge pull request #3055 from ehotinger/ehotinger/flow-opts
Allow opts to flow to the backend snapshotter during snapshot creation.
2 parents 4355a2a + 75f1838 commit 8bb5213

3 files changed

Lines changed: 75 additions & 7 deletions

File tree

metadata/snapshot.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import (
3434
bolt "go.etcd.io/bbolt"
3535
)
3636

37+
const (
38+
inheritedLabelsPrefix = "containerd.io/snapshot/"
39+
)
40+
3741
type snapshotter struct {
3842
snapshots.Snapshotter
3943
name string
@@ -338,12 +342,14 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
338342
return err
339343
}
340344

345+
inheritedOpt := snapshots.WithLabels(filterInheritedLabels(base.Labels))
346+
341347
// TODO: Consider doing this outside of transaction to lessen
342348
// metadata lock time
343349
if readonly {
344-
m, err = s.Snapshotter.View(ctx, bkey, bparent)
350+
m, err = s.Snapshotter.View(ctx, bkey, bparent, inheritedOpt)
345351
} else {
346-
m, err = s.Snapshotter.Prepare(ctx, bkey, bparent)
352+
m, err = s.Snapshotter.Prepare(ctx, bkey, bparent, inheritedOpt)
347353
}
348354
return err
349355
}); err != nil {
@@ -445,9 +451,11 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
445451
return err
446452
}
447453

454+
inheritedOpt := snapshots.WithLabels(filterInheritedLabels(base.Labels))
455+
448456
// TODO: Consider doing this outside of transaction to lessen
449457
// metadata lock time
450-
return s.Snapshotter.Commit(ctx, nameKey, bkey)
458+
return s.Snapshotter.Commit(ctx, nameKey, bkey, inheritedOpt)
451459
})
452460

453461
}
@@ -761,3 +769,19 @@ func (s *snapshotter) pruneBranch(ctx context.Context, node *treeNode) error {
761769
func (s *snapshotter) Close() error {
762770
return s.Snapshotter.Close()
763771
}
772+
773+
// filterInheritedLabels filters the provided labels by removing any key which doesn't have
774+
// a prefix of "containerd.io/snapshot/".
775+
func filterInheritedLabels(labels map[string]string) map[string]string {
776+
if labels == nil {
777+
return nil
778+
}
779+
780+
filtered := make(map[string]string)
781+
for k, v := range labels {
782+
if strings.HasPrefix(k, inheritedLabelsPrefix) {
783+
filtered[k] = v
784+
}
785+
}
786+
return filtered
787+
}

metadata/snapshot_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"os"
2222
"path/filepath"
23+
"reflect"
2324
"runtime"
2425
"testing"
2526

@@ -63,3 +64,41 @@ func TestMetadata(t *testing.T) {
6364
testutil.RequiresRoot(t)
6465
testsuite.SnapshotterSuite(t, "Metadata", newTestSnapshotter)
6566
}
67+
68+
func TestFilterInheritedLabels(t *testing.T) {
69+
tests := []struct {
70+
labels map[string]string
71+
expected map[string]string
72+
}{
73+
{
74+
nil,
75+
nil,
76+
},
77+
{
78+
map[string]string{},
79+
map[string]string{},
80+
},
81+
{
82+
map[string]string{"": ""},
83+
map[string]string{},
84+
},
85+
{
86+
map[string]string{"foo": "bar"},
87+
map[string]string{},
88+
},
89+
{
90+
map[string]string{inheritedLabelsPrefix + "foo": "bar"},
91+
map[string]string{inheritedLabelsPrefix + "foo": "bar"},
92+
},
93+
{
94+
map[string]string{inheritedLabelsPrefix + "foo": "bar", "qux": "qaz"},
95+
map[string]string{inheritedLabelsPrefix + "foo": "bar"},
96+
},
97+
}
98+
99+
for _, test := range tests {
100+
if actual := filterInheritedLabels(test.labels); !reflect.DeepEqual(actual, test.expected) {
101+
t.Fatalf("expected %v but got %v", test.expected, actual)
102+
}
103+
}
104+
}

snapshots/snapshotter.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ func (k *Kind) UnmarshalJSON(b []byte) error {
8686
// Info provides information about a particular snapshot.
8787
// JSON marshallability is supported for interactive with tools like ctr,
8888
type Info struct {
89-
Kind Kind // active or committed snapshot
90-
Name string // name or key of snapshot
91-
Parent string `json:",omitempty"` // name of parent snapshot
92-
Labels map[string]string `json:",omitempty"` // Labels for snapshot
89+
Kind Kind // active or committed snapshot
90+
Name string // name or key of snapshot
91+
Parent string `json:",omitempty"` // name of parent snapshot
92+
93+
// Labels for a snapshot.
94+
//
95+
// Note: only labels prefixed with `containerd.io/snapshot/` will be inherited by the
96+
// snapshotter's `Prepare`, `View`, or `Commit` calls.
97+
Labels map[string]string `json:",omitempty"`
9398
Created time.Time `json:",omitempty"` // Created time
9499
Updated time.Time `json:",omitempty"` // Last update time
95100
}

0 commit comments

Comments
 (0)