@@ -21,6 +21,7 @@ import (
2121 "encoding/json"
2222 "errors"
2323 "fmt"
24+ "strings"
2425
2526 "github.com/containerd/containerd/v2/core/containers"
2627 "github.com/containerd/containerd/v2/core/content"
@@ -52,6 +53,11 @@ type InfoConfig struct {
5253 Refresh bool
5354}
5455
56+ const (
57+ devboxAnnotationPrefix = "devbox.sealos.io/"
58+ devboxSnapshotLabelPrefix = "containerd.io/snapshot/devbox-"
59+ )
60+
5561// WithRuntime allows a user to specify the runtime name and additional options that should
5662// be used to create tasks for the container
5763func WithRuntime (name string , options interface {}) NewContainerOpts {
@@ -255,10 +261,15 @@ func withNewSnapshot(id string, i Image, readonly bool, opts ...snapshots.Opt) N
255261 return err
256262 }
257263
264+ startOpts , err := withDevboxSnapshotLabels (opts ... )
265+ if err != nil {
266+ return err
267+ }
268+
258269 if readonly {
259- _ , err = s .View (ctx , id , parent , opts ... )
270+ _ , err = s .View (ctx , id , parent , startOpts ... )
260271 } else {
261- _ , err = s .Prepare (ctx , id , parent , opts ... )
272+ _ , err = s .Prepare (ctx , id , parent , startOpts ... )
262273 }
263274 if err != nil {
264275 return err
@@ -269,6 +280,30 @@ func withNewSnapshot(id string, i Image, readonly bool, opts ...snapshots.Opt) N
269280 }
270281}
271282
283+ func withDevboxSnapshotLabels (opts ... snapshots.Opt ) ([]snapshots.Opt , error ) {
284+ base := snapshots.Info {}
285+ for _ , opt := range opts {
286+ if err := opt (& base ); err != nil {
287+ return nil , fmt .Errorf ("error applying snapshot option: %w" , err )
288+ }
289+ }
290+
291+ translated := make (map [string ]string )
292+ for label , value := range base .Labels {
293+ if strings .HasPrefix (label , devboxAnnotationPrefix ) {
294+ translated [devboxSnapshotLabelPrefix + label [len (devboxAnnotationPrefix ):]] = value
295+ }
296+ }
297+ if len (translated ) == 0 {
298+ return opts , nil
299+ }
300+
301+ startOpts := make ([]snapshots.Opt , 0 , len (opts )+ 1 )
302+ startOpts = append (startOpts , snapshots .WithLabels (translated ))
303+ startOpts = append (startOpts , opts ... )
304+ return startOpts , nil
305+ }
306+
272307// WithContainerExtension appends extension data to the container object.
273308// Use this to decorate the container object with additional data for the client
274309// integration.
0 commit comments