Skip to content

Commit

Permalink
Merge pull request #49 from tri-adam/opt-descriptors
Browse files Browse the repository at this point in the history
feat: add OptWriteWithSpareDescriptorCapacity
  • Loading branch information
tri-adam authored May 7, 2024
2 parents d8608d7 + 645cc8f commit e86295e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Binary file added pkg/sif/testdata/TestWrite/SpareDescriptor.golden
Binary file not shown.
34 changes: 32 additions & 2 deletions pkg/sif/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,46 @@ func numDescriptorsForIndex(ii v1.ImageIndex) (int64, error) {
return count + 1, nil
}

// writeOpts accumulates write options.
type writeOpts struct {
spareDescriptors int64
}

// WriteOpt are used to specify write options.
type WriteOpt func(*writeOpts) error

// OptWriteWithSpareDescriptorCapacity specifies that the SIF should be created with n spare
// descriptors.
func OptWriteWithSpareDescriptorCapacity(n int64) WriteOpt {
return func(wo *writeOpts) error {
wo.spareDescriptors = n
return nil
}
}

// Write constructs a SIF at path from an ImageIndex.
func Write(path string, ii v1.ImageIndex) error {
//
// By default, the SIF is created with the exact number of descriptors required to represent ii. To
// include spare descriptor capacity, consider using OptWriteWithSpareDescriptorCapacity.
func Write(path string, ii v1.ImageIndex, opts ...WriteOpt) error {
wo := writeOpts{
spareDescriptors: 0,
}

for _, opt := range opts {
if err := opt(&wo); err != nil {
return err
}
}

n, err := numDescriptorsForIndex(ii)
if err != nil {
return err
}

fi, err := sif.CreateContainerAtPath(path,
sif.OptCreateDeterministic(),
sif.OptCreateWithDescriptorCapacity(n),
sif.OptCreateWithDescriptorCapacity(n+wo.spareDescriptors),
)
if err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion pkg/sif/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestWrite(t *testing.T) {
tests := []struct {
name string
ii v1.ImageIndex
opts []sif.WriteOpt
}{
{
name: "DockerManifest",
Expand All @@ -35,12 +36,19 @@ func TestWrite(t *testing.T) {
name: "ManyLayers",
ii: corpus.ImageIndex(t, "many-layers"),
},
{
name: "SpareDescriptor",
ii: corpus.ImageIndex(t, "hello-world-docker-v2-manifest"),
opts: []sif.WriteOpt{
sif.OptWriteWithSpareDescriptorCapacity(1),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "image.sif")

if err := sif.Write(path, tt.ii); err != nil {
if err := sif.Write(path, tt.ii, tt.opts...); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit e86295e

Please sign in to comment.