Skip to content

Commit 95ba09f

Browse files
committed
api: fix OCI hook ownership tracking.
Fix hook ownership tracking and add a proper test case for it. Ownership tracking for OCI hooks is supposed to be accumulative. The implementation tried to accumulate owners, but it was buggy. It tried to delete existing ownership by unclaiming which marks ownershipdeleted by the plugin instead of clearing it. This went unnoticed since we lacked any kind of proper test for hook ownership accumulation. It was only noticed thanks to containerd#263 which switches much of the ownership tracking code to generated from hand-written. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 2d1ee56 commit 95ba09f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

pkg/api/owners.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,7 @@ func (f *FieldOwners) ClaimMount(destination, plugin string) error {
475475
}
476476

477477
func (f *FieldOwners) ClaimHooks(plugin string) error {
478-
plugins := plugin
479-
480-
if current, ok := f.simpleOwner(Field_OciHooks.Key()); ok {
481-
f.clearSimple(Field_OciHooks.Key(), plugin)
482-
plugins = current + "," + plugin
483-
}
484-
485-
f.claimSimple(Field_OciHooks.Key(), plugins)
478+
f.accumulateSimple(Field_OciHooks.Key(), plugin)
486479
return nil
487480
}
488481

@@ -678,6 +671,14 @@ func (f *FieldOwners) ClearRdt(plugin string) {
678671
f.clearSimple(Field_RdtEnableMonitoring.Key(), plugin)
679672
}
680673

674+
func (f *FieldOwners) accumulateSimple(field int32, plugin string) {
675+
old, ok := f.simpleOwner(field)
676+
if ok {
677+
plugin = old + "," + plugin
678+
}
679+
f.Simple[field] = plugin
680+
}
681+
681682
func (f *FieldOwners) Conflict(field int32, plugin, other string, qualifiers ...string) error {
682683
return fmt.Errorf("plugins %q and %q both tried to set %s",
683684
plugin, other, qualify(field, qualifiers...))

pkg/api/owners_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,24 @@ func TestCompoundClaims(t *testing.T) {
113113

114114
require.Equal(t, api.Field_Annotations.String(), "Annotations", "annotation field name")
115115
}
116+
117+
func TestAccumulatingOwnership(t *testing.T) {
118+
o := api.NewOwningPlugins()
119+
120+
// claim OCI hooks of ctr0
121+
err := o.ClaimHooks("ctr0", "plugin0")
122+
require.NoError(t, err, "ctr0 OCI hooks by plugin0")
123+
124+
// claim OCI hooks of ctr0
125+
err = o.ClaimHooks("ctr0", "plugin1")
126+
require.NoError(t, err, "ctr0 OCI hooks by plugin1")
127+
128+
// claim OCI hooks of ctr0
129+
err = o.ClaimHooks("ctr0", "plugin2")
130+
require.NoError(t, err, "ctr0 OCI hooks by plugin2")
131+
132+
owners, ok := o.HooksOwner("ctr0")
133+
require.True(t, ok, "ctr0 has hooks owners")
134+
require.Equal(t, "plugin0,plugin1,plugin2", owners, "ctr0 hooks owners")
135+
136+
}

0 commit comments

Comments
 (0)