Skip to content

Commit

Permalink
Merge pull request #4827 from tonistiigi/moby-deps-cleanup
Browse files Browse the repository at this point in the history
Remove unneeded dependencies from moby/moby
  • Loading branch information
AkihiroSuda authored Apr 8, 2024
2 parents dc23e43 + eb942ea commit fb97e6d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 70 deletions.
9 changes: 4 additions & 5 deletions frontend/dockerfile/instructions/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package instructions
import (
"strings"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/moby/buildkit/frontend/dockerfile/parser"
dockerspec "github.com/moby/docker-image-spec/specs-go/v1"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -325,7 +324,7 @@ type ShellInlineFile struct {

// ShellDependantCmdLine represents a cmdline optionally prepended with the shell
type ShellDependantCmdLine struct {
CmdLine strslice.StrSlice
CmdLine []string
Files []ShellInlineFile
PrependShell bool
}
Expand Down Expand Up @@ -368,7 +367,7 @@ type CmdCommand struct {
// HEALTHCHECK <health-config>
type HealthCheckCommand struct {
withNameAndCode
Health *container.HealthConfig
Health *dockerspec.HealthcheckConfig
}

// EntrypointCommand sets the default entrypoint of the container to use the
Expand Down Expand Up @@ -479,7 +478,7 @@ func (c *ArgCommand) Expand(expander SingleWordExpander) error {
// SHELL bash -e -c
type ShellCommand struct {
withNameAndCode
Shell strslice.StrSlice
Shell []string
}

// Stage represents a bundled collection of commands.
Expand Down
27 changes: 13 additions & 14 deletions frontend/dockerfile/instructions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"strings"
"time"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/moby/buildkit/frontend/dockerfile/command"
"github.com/moby/buildkit/frontend/dockerfile/linter"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/util/suggest"
dockerspec "github.com/moby/docker-image-spec/specs-go/v1"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -475,12 +474,11 @@ func parseShellDependentCommand(req parseRequest, command string, emptyAsNil boo
}

args := handleJSONArgs(req.args, req.attributes)
cmd := strslice.StrSlice(args)
if emptyAsNil && len(cmd) == 0 {
cmd = nil
if emptyAsNil && len(args) == 0 {
args = nil
}
return ShellDependantCmdLine{
CmdLine: cmd,
CmdLine: args,
Files: files,
PrependShell: !req.attributes["json"],
}, nil
Expand Down Expand Up @@ -563,8 +561,10 @@ func parseOptInterval(f *Flag) (time.Duration, error) {
if d == 0 {
return 0, nil
}
if d < container.MinimumDuration {
return 0, errors.Errorf("Interval %#v cannot be less than %s", f.name, container.MinimumDuration)

const minimumDuration = time.Millisecond
if d < minimumDuration {
return 0, errors.Errorf("Interval %#v cannot be less than %s", f.name, minimumDuration)
}
return d, nil
}
Expand All @@ -582,12 +582,11 @@ func parseHealthcheck(req parseRequest) (*HealthCheckCommand, error) {
if len(args) != 0 {
return nil, errors.New("HEALTHCHECK NONE takes no arguments")
}
test := strslice.StrSlice{typ}
cmd.Health = &container.HealthConfig{
Test: test,
cmd.Health = &dockerspec.HealthcheckConfig{
Test: []string{typ},
}
} else {
healthcheck := container.HealthConfig{}
healthcheck := dockerspec.HealthcheckConfig{}

flInterval := req.flags.AddString("interval", "")
flTimeout := req.flags.AddString("timeout", "")
Expand All @@ -610,7 +609,7 @@ func parseHealthcheck(req parseRequest) (*HealthCheckCommand, error) {
typ = "CMD-SHELL"
}

healthcheck.Test = strslice.StrSlice(append([]string{typ}, cmdSlice...))
healthcheck.Test = append([]string{typ}, cmdSlice...)
default:
return nil, errors.Errorf("Unknown type %#v in HEALTHCHECK (try CMD)", typ)
}
Expand Down Expand Up @@ -774,7 +773,7 @@ func parseShell(req parseRequest) (*ShellCommand, error) {
// SHELL ["powershell", "-command"]

return &ShellCommand{
Shell: strslice.StrSlice(shellSlice),
Shell: shellSlice,
withNameAndCode: newWithNameAndCode(req),
}, nil
default:
Expand Down
15 changes: 7 additions & 8 deletions frontend/dockerfile/instructions/parse_heredoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types/strslice"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -179,30 +178,30 @@ func TestRunHeredoc(t *testing.T) {
cases := []struct {
dockerfile string
shell bool
command strslice.StrSlice
command []string
files []ShellInlineFile
}{
{
dockerfile: `RUN ["ls", "/"]`,
command: strslice.StrSlice{"ls", "/"},
command: []string{"ls", "/"},
shell: false,
},
{
dockerfile: `RUN ["<<EOF"]`,
command: strslice.StrSlice{"<<EOF"},
command: []string{"<<EOF"},
shell: false,
},
{
dockerfile: "RUN ls /",
command: strslice.StrSlice{"ls /"},
command: []string{"ls /"},
shell: true,
},
{
dockerfile: `RUN <<EOF
ls /
whoami
EOF`,
command: strslice.StrSlice{"<<EOF"},
command: []string{"<<EOF"},
files: []ShellInlineFile{
{
Name: "EOF",
Expand All @@ -216,7 +215,7 @@ EOF`,
print("hello")
print("world")
EOF`,
command: strslice.StrSlice{"<<'EOF' | python"},
command: []string{"<<'EOF' | python"},
files: []ShellInlineFile{
{
Name: "EOF",
Expand All @@ -231,7 +230,7 @@ print("world")
dockerfile: `RUN <<-EOF
echo test
EOF`,
command: strslice.StrSlice{"<<-EOF"},
command: []string{"<<-EOF"},
files: []ShellInlineFile{
{
Name: "EOF",
Expand Down
20 changes: 6 additions & 14 deletions solver/llbsolver/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/identity"
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
"github.com/moby/buildkit/util/iohelper"
"github.com/moby/buildkit/util/leaseutil"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -254,7 +255,7 @@ func (h *HistoryQueue) migrateBlobV2(ctx context.Context, id string, detectSkipL
return false, nil // allow skipping
}
defer ra.Close()
if err := content.Copy(ctx, w, &reader{ReaderAt: ra}, 0, dgst, content.WithLabels(labels)); err != nil {
if err := content.Copy(ctx, w, iohelper.ReadCloser(ra), 0, dgst, content.WithLabels(labels)); err != nil {
return false, err
}

Expand Down Expand Up @@ -460,9 +461,11 @@ func (h *HistoryQueue) Status(ctx context.Context, ref string, st chan<- *client
if err != nil {
return err
}
defer ra.Close()

brdr := bufio.NewReader(&reader{ReaderAt: ra})
rc := iohelper.ReadCloser(ra)
defer rc.Close()

brdr := bufio.NewReader(rc)

buf := make([]byte, 32*1024)

Expand Down Expand Up @@ -906,14 +909,3 @@ func (p *channel[T]) close() {
close(p.done)
})
}

type reader struct {
io.ReaderAt
pos int64
}

func (r *reader) Read(p []byte) (int, error) {
n, err := r.ReaderAt.ReadAt(p, r.pos)
r.pos += int64(len(p))
return n, err
}
18 changes: 2 additions & 16 deletions source/containerimage/ocilayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/moby/buildkit/session"
sessioncontent "github.com/moby/buildkit/session/content"
"github.com/moby/buildkit/util/imageutil"
"github.com/moby/buildkit/util/iohelper"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (r *ociLayoutResolver) Fetch(ctx context.Context, desc ocispecs.Descriptor)
if err != nil {
return err
}
rc = &readerAtWrapper{readerAt: readerAt}
rc = iohelper.ReadCloser(readerAt)
return nil
})
return rc, err
Expand Down Expand Up @@ -137,18 +138,3 @@ func (r *ociLayoutResolver) withCaller(ctx context.Context, f func(context.Conte
return f(ctx, caller)
})
}

// readerAtWrapper wraps a ReaderAt to give a Reader
type readerAtWrapper struct {
offset int64
readerAt content.ReaderAt
}

func (r *readerAtWrapper) Read(p []byte) (n int, err error) {
n, err = r.readerAt.ReadAt(p, r.offset)
r.offset += int64(n)
return
}
func (r *readerAtWrapper) Close() error {
return r.readerAt.Close()
}
2 changes: 1 addition & 1 deletion util/compression/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,5 @@ func decompress(ctx context.Context, cs content.Store, desc ocispecs.Descriptor)
return nil, err
}
}
return &iohelper.ReadCloser{ReadCloser: r, CloseFunc: ra.Close}, nil
return iohelper.WithCloser(r, ra.Close), nil
}
4 changes: 1 addition & 3 deletions util/compression/uncompressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/docker/docker/pkg/ioutils"
"github.com/moby/buildkit/util/iohelper"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand All @@ -22,8 +21,7 @@ func (c uncompressedType) Decompress(ctx context.Context, cs content.Store, desc
if err != nil {
return nil, err
}
rdr := io.NewSectionReader(ra, 0, ra.Size())
return ioutils.NewReadCloserWrapper(rdr, ra.Close), nil
return iohelper.ReadCloser(ra), nil
}

func (c uncompressedType) NeedsConversion(ctx context.Context, cs content.Store, desc ocispecs.Descriptor) (bool, error) {
Expand Down
45 changes: 36 additions & 9 deletions util/iohelper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ func (w *NopWriteCloser) Close() error {
return nil
}

type ReadCloser struct {
io.ReadCloser
CloseFunc func() error
type closeFunc func() error

func (c closeFunc) Close() error {
return c()
}

func (rc *ReadCloser) Close() error {
err1 := rc.ReadCloser.Close()
err2 := rc.CloseFunc()
if err1 != nil {
return errors.Wrapf(err1, "failed to close: %v", err2)
// WithCloser returns a ReadCloser with additional closer function.
func WithCloser(r io.ReadCloser, closer func() error) io.ReadCloser {
var f closeFunc = func() error {
err1 := r.Close()
err2 := closer()
if err1 != nil {
return errors.Wrapf(err1, "failed to close: %v", err2)
}
return err2
}
return &readCloser{
Reader: r,
Closer: f,
}
return err2
}

type WriteCloser struct {
Expand Down Expand Up @@ -61,3 +69,22 @@ func (c *Counter) Size() (n int64) {
c.mu.Unlock()
return
}

type ReaderAtCloser interface {
io.ReaderAt
io.Closer
Size() int64
}

// ReadCloser returns a ReadCloser from ReaderAtCloser.
func ReadCloser(in ReaderAtCloser) io.ReadCloser {
return &readCloser{
Reader: io.NewSectionReader(in, 0, in.Size()),
Closer: in,
}
}

type readCloser struct {
io.Reader
io.Closer
}

0 comments on commit fb97e6d

Please sign in to comment.