Skip to content

Commit b2a7039

Browse files
authored
Merge pull request containerd#3460 from apostasie/dev-3440-locking
Fix regression from containerd#3446
2 parents 2db8052 + 93b5999 commit b2a7039

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

pkg/cmd/container/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Commit(ctx context.Context, client *containerd.Client, rawRef string, req s
5757
if found.MatchCount > 1 {
5858
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
5959
}
60-
imageID, err := commit.Commit(ctx, client, found.Container, opts)
60+
imageID, err := commit.Commit(ctx, client, found.Container, opts, options.GOptions)
6161
if err != nil {
6262
return err
6363
}

pkg/cmd/container/remove.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,23 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
107107
return err
108108
}
109109

110+
// Get datastore
111+
dataStore, err := clientutil.DataStore(globalOptions.DataRoot, globalOptions.Address)
112+
if err != nil {
113+
return err
114+
}
115+
116+
// Ensure we do have a stateDir label
117+
stateDir := containerLabels[labels.StateDir]
118+
if stateDir == "" {
119+
stateDir, err = containerutil.ContainerStateDirPath(globalOptions.Namespace, dataStore, c.ID())
120+
if err != nil {
121+
return err
122+
}
123+
}
124+
110125
// Lock the container state
111-
lf, err := containerutil.Lock(ctx, c)
126+
lf, err := containerutil.Lock(stateDir)
112127
if err != nil {
113128
return err
114129
}
@@ -132,11 +147,7 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
132147
if err != nil {
133148
return err
134149
}
135-
// Get datastore
136-
dataStore, err := clientutil.DataStore(globalOptions.DataRoot, globalOptions.Address)
137-
if err != nil {
138-
return err
139-
}
150+
140151
// Get namestore
141152
nameStore, err := namestore.New(dataStore, containerNamespace)
142153
if err != nil {

pkg/containerutil/lock.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,12 @@
1717
package containerutil
1818

1919
import (
20-
"context"
21-
"errors"
2220
"path/filepath"
2321

24-
"github.com/containerd/containerd/v2/client"
25-
26-
"github.com/containerd/nerdctl/v2/pkg/labels"
2722
"github.com/containerd/nerdctl/v2/pkg/store"
2823
)
2924

30-
func Lock(ctx context.Context, c client.Container) (store.Store, error) {
31-
containerLabels, err := c.Labels(ctx)
32-
if err != nil {
33-
return nil, err
34-
}
35-
36-
stateDir := containerLabels[labels.StateDir]
37-
if stateDir == "" {
38-
return nil, errors.New("container is missing statedir label")
39-
}
40-
25+
func Lock(stateDir string) (store.Store, error) {
4126
stor, err := store.New(filepath.Join(stateDir, "oplock"), 0, 0)
4227
if err != nil {
4328
return nil, err

pkg/imgutil/commit/commit.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import (
4444
"github.com/containerd/log"
4545
"github.com/containerd/platforms"
4646

47+
"github.com/containerd/nerdctl/v2/pkg/api/types"
48+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
4749
"github.com/containerd/nerdctl/v2/pkg/containerutil"
4850
imgutil "github.com/containerd/nerdctl/v2/pkg/imgutil"
4951
"github.com/containerd/nerdctl/v2/pkg/labels"
@@ -66,8 +68,29 @@ var (
6668
emptyDigest = digest.Digest("")
6769
)
6870

69-
func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts) (digest.Digest, error) {
70-
lf, err := containerutil.Lock(ctx, container)
71+
func Commit(ctx context.Context, client *containerd.Client, container containerd.Container, opts *Opts, globalOptions types.GlobalCommandOptions) (digest.Digest, error) {
72+
// Get labels
73+
containerLabels, err := container.Labels(ctx)
74+
if err != nil {
75+
return emptyDigest, err
76+
}
77+
78+
// Get datastore
79+
dataStore, err := clientutil.DataStore(globalOptions.DataRoot, globalOptions.Address)
80+
if err != nil {
81+
return emptyDigest, err
82+
}
83+
84+
// Ensure we do have a stateDir label
85+
stateDir := containerLabels[labels.StateDir]
86+
if stateDir == "" {
87+
stateDir, err = containerutil.ContainerStateDirPath(globalOptions.Namespace, dataStore, container.ID())
88+
if err != nil {
89+
return emptyDigest, err
90+
}
91+
}
92+
93+
lf, err := containerutil.Lock(stateDir)
7194
if err != nil {
7295
return emptyDigest, err
7396
}

0 commit comments

Comments
 (0)