Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 5 additions & 57 deletions pkg/backend/build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io"
"os"
"path/filepath"
"sync"
"syscall"
"time"

Expand All @@ -39,7 +38,6 @@ import (
"github.com/modelpack/modctl/internal/cache"
buildconfig "github.com/modelpack/modctl/pkg/backend/build/config"
"github.com/modelpack/modctl/pkg/backend/build/hooks"
"github.com/modelpack/modctl/pkg/backend/build/interceptor"
pkgcodec "github.com/modelpack/modctl/pkg/codec"
"github.com/modelpack/modctl/pkg/storage"
)
Expand Down Expand Up @@ -109,12 +107,11 @@ func NewBuilder(outputType OutputType, store storage.Storage, repo, tag string,
}

return &abstractBuilder{
store: store,
repo: repo,
tag: tag,
strategy: strategy,
interceptor: cfg.interceptor,
cache: cache,
store: store,
repo: repo,
tag: tag,
strategy: strategy,
cache: cache,
}, nil
}

Expand All @@ -125,8 +122,6 @@ type abstractBuilder struct {
tag string
// strategy is the output strategy used to output the blob.
strategy OutputStrategy
// interceptor is the interceptor used to intercept the build process.
interceptor interceptor.Interceptor
// cache is the cache used to store the file digest.
cache cache.Cache
}
Expand Down Expand Up @@ -171,38 +166,11 @@ func (ab *abstractBuilder) BuildLayer(ctx context.Context, mediaType, workDir, p
return ocispec.Descriptor{}, fmt.Errorf("failed to compute digest and size: %w", err)
}

var (
wg sync.WaitGroup
itErr error
applyDesc interceptor.ApplyDescriptorFn
)
// Intercept the reader if needed.
if ab.interceptor != nil {
var itReader io.Reader
reader, itReader = splitReader(reader)

wg.Add(1)
go func() {
defer wg.Done()
applyDesc, itErr = ab.interceptor.Intercept(ctx, mediaType, relPath, codec.Type(), itReader)
}()
}

desc, err := ab.strategy.OutputLayer(ctx, mediaType, relPath, destPath, digest, size, reader, hooks)
if err != nil {
return desc, err
}

// Wait for the interceptor to finish.
wg.Wait()
if itErr != nil {
return desc, itErr
}

if applyDesc != nil {
applyDesc(&desc)
}

// Add file metadata to descriptor.
if err := addFileMetadata(&desc, path, relPath); err != nil {
return desc, err
Expand Down Expand Up @@ -409,26 +377,6 @@ func addFileMetadata(desc *ocispec.Descriptor, path, relPath string) error {
return nil
}

// splitReader splits the original reader into two readers.
func splitReader(original io.Reader) (io.Reader, io.Reader) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
multiWriter := io.MultiWriter(w1, w2)

go func() {
defer w1.Close()
defer w2.Close()

_, err := io.Copy(multiWriter, original)
if err != nil {
w1.CloseWithError(err)
w2.CloseWithError(err)
}
}()

return r1, r2
}

// getFileMetadata retrieves metadata for a file at the given path.
func getFileMetadata(path string) (modelspec.FileMetadata, error) {
var metadata modelspec.FileMetadata
Expand Down
17 changes: 0 additions & 17 deletions pkg/backend/build/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ package build
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -282,21 +280,6 @@ func TestBuilderSuite(t *testing.T) {
suite.Run(t, new(BuilderTestSuite))
}

func TestPipeReader(t *testing.T) {
r := strings.NewReader("some io.Reader stream to be read\n")
r1, r2 := splitReader(r)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_, err := io.Copy(os.Stdout, r2)
assert.NoError(t, err)
}()
_, err := io.Copy(os.Stdout, r1)
assert.NoError(t, err)
wg.Wait()
}

func createTempFile(t *testing.T, dir, pattern, content string) string {
t.Helper()
f, err := os.CreateTemp(dir, pattern)
Expand Down
15 changes: 2 additions & 13 deletions pkg/backend/build/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@

package build

import (
"github.com/modelpack/modctl/pkg/backend/build/interceptor"
)

type Option func(*config)

// config is the configuration for the building.
type config struct {
plainHTTP bool
insecure bool
interceptor interceptor.Interceptor
plainHTTP bool
insecure bool
}

func WithPlainHTTP(plainHTTP bool) Option {
Expand All @@ -40,9 +35,3 @@ func WithInsecure(insecure bool) Option {
c.insecure = insecure
}
}

func WithInterceptor(interceptor interceptor.Interceptor) Option {
return func(c *config) {
c.interceptor = interceptor
}
}
33 changes: 0 additions & 33 deletions pkg/backend/build/interceptor/interceptor.go

This file was deleted.