Skip to content

Commit a3fc6c2

Browse files
author
Adam Hughes
committed
feat: add deterministic options
Add OptCreateDeterministic, OptAddDeterministic, OptDeleteDeterministic, and OptSetDeterministic, which set header/descriptor fields that support deterministic creation/modification of images
1 parent 6854e72 commit a3fc6c2

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

pkg/sif/create.go

+44-7
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ func OptCreateWithLaunchScript(s string) CreateOpt {
144144
}
145145
}
146146

147+
// OptCreateDeterministic sets header/descriptor fields to values that support deterministic
148+
// creation of images.
149+
func OptCreateDeterministic() CreateOpt {
150+
return func(co *createOpts) error {
151+
co.id = uuid.Nil
152+
co.t = time.Unix(0, 0)
153+
return nil
154+
}
155+
}
156+
147157
// OptCreateWithID specifies id as the unique ID.
148158
func OptCreateWithID(id string) CreateOpt {
149159
return func(co *createOpts) error {
@@ -239,10 +249,10 @@ func createContainer(rw ReadWriter, co createOpts) (*FileImage, error) {
239249
// interface. To change this behavior, consider using OptCreateWithCloseOnUnload.
240250
//
241251
// By default, the image ID is set to a randomly generated value. To override this, consider using
242-
// OptCreateWithID.
252+
// OptCreateDeterministic or OptCreateWithID.
243253
//
244254
// By default, the image creation time is set to time.Now(). To override this, consider using
245-
// OptCreateWithTime.
255+
// OptCreateDeterministic or OptCreateWithTime.
246256
//
247257
// By default, the image will support a maximum of 48 descriptors. To change this, consider using
248258
// OptCreateWithDescriptorCapacity.
@@ -284,10 +294,10 @@ func CreateContainer(rw ReadWriter, opts ...CreateOpt) (*FileImage, error) {
284294
// are released.
285295
//
286296
// By default, the image ID is set to a randomly generated value. To override this, consider using
287-
// OptCreateWithID.
297+
// OptCreateDeterministic or OptCreateWithID.
288298
//
289299
// By default, the image creation time is set to time.Now(). To override this, consider using
290-
// OptCreateWithTime.
300+
// OptCreateDeterministic or OptCreateWithTime.
291301
//
292302
// By default, the image will support a maximum of 48 descriptors. To change this, consider using
293303
// OptCreateWithDescriptorCapacity.
@@ -364,6 +374,15 @@ type addOpts struct {
364374
// AddOpt are used to specify object add options.
365375
type AddOpt func(*addOpts) error
366376

377+
// OptAddDeterministic sets header/descriptor fields to values that support deterministic
378+
// modification of images.
379+
func OptAddDeterministic() AddOpt {
380+
return func(ao *addOpts) error {
381+
ao.t = time.Unix(0, 0)
382+
return nil
383+
}
384+
}
385+
367386
// OptAddWithTime specifies t as the image modification time.
368387
func OptAddWithTime(t time.Time) AddOpt {
369388
return func(ao *addOpts) error {
@@ -375,7 +394,7 @@ func OptAddWithTime(t time.Time) AddOpt {
375394
// AddObject adds a new data object and its descriptor into the specified SIF file.
376395
//
377396
// By default, the image modification time is set to the current time. To override this, consider
378-
// using OptAddWithTime.
397+
// using OptAddDeterministic or OptAddWithTime.
379398
func (f *FileImage) AddObject(di DescriptorInput, opts ...AddOpt) error {
380399
ao := addOpts{
381400
t: time.Now(),
@@ -463,6 +482,15 @@ func OptDeleteCompact(b bool) DeleteOpt {
463482
}
464483
}
465484

485+
// OptDeleteDeterministic sets header/descriptor fields to values that support deterministic
486+
// modification of images.
487+
func OptDeleteDeterministic() DeleteOpt {
488+
return func(do *deleteOpts) error {
489+
do.t = time.Unix(0, 0)
490+
return nil
491+
}
492+
}
493+
466494
// OptDeleteWithTime specifies t as the image modification time.
467495
func OptDeleteWithTime(t time.Time) DeleteOpt {
468496
return func(do *deleteOpts) error {
@@ -479,7 +507,7 @@ var errCompactNotImplemented = errors.New("compact not implemented for non-last
479507
// object deletion, use OptDeleteCompact.
480508
//
481509
// By default, the image modification time is set to time.Now(). To override this, consider using
482-
// OptDeleteWithTime.
510+
// OptDeleteDeterministic or OptDeleteWithTime.
483511
func (f *FileImage) DeleteObject(id uint32, opts ...DeleteOpt) error {
484512
do := deleteOpts{
485513
t: time.Now(),
@@ -544,6 +572,15 @@ type setOpts struct {
544572
// SetOpt are used to specify object set options.
545573
type SetOpt func(*setOpts) error
546574

575+
// OptSetDeterministic sets header/descriptor fields to values that support deterministic
576+
// modification of images.
577+
func OptSetDeterministic() SetOpt {
578+
return func(so *setOpts) error {
579+
so.t = time.Unix(0, 0)
580+
return nil
581+
}
582+
}
583+
547584
// OptSetWithTime specifies t as the image/object modification time.
548585
func OptSetWithTime(t time.Time) SetOpt {
549586
return func(so *setOpts) error {
@@ -560,7 +597,7 @@ var (
560597
// SetPrimPart sets the specified system partition to be the primary one.
561598
//
562599
// By default, the image/object modification times are set to time.Now(). To override this,
563-
// consider using OptSetWithTime.
600+
// consider using OptSetDeterministic or OptSetWithTime.
564601
func (f *FileImage) SetPrimPart(id uint32, opts ...SetOpt) error {
565602
so := setOpts{
566603
t: time.Now(),

0 commit comments

Comments
 (0)