Skip to content

Commit

Permalink
Add ProgressWriter to PullOptions
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Kochnev <[email protected]>
  • Loading branch information
marshall-lee committed Aug 18, 2022
1 parent ec9508e commit 3bf52aa
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 17 deletions.
5 changes: 5 additions & 0 deletions cmd/podman/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ func imagePull(cmd *cobra.Command, args []string) error {
pullOptions.Username = creds.Username
pullOptions.Password = creds.Password
}

if !pullOptions.Quiet {
pullOptions.Writer = os.Stderr
}

// Let's do all the remaining Yoga in the API to prevent us from
// scattering logic across (too) many parts of the code.
var errs utils.OutputErrors
Expand Down
13 changes: 8 additions & 5 deletions pkg/bindings/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -57,10 +56,14 @@ func Pull(ctx context.Context, rawImage string, options *PullOptions) ([]string,
return nil, response.Process(err)
}

// Historically pull writes status to stderr
stderr := io.Writer(os.Stderr)
var writer io.Writer
if options.GetQuiet() {
stderr = ioutil.Discard
writer = io.Discard
} else if progressWriter := options.GetProgressWriter(); progressWriter != nil {
writer = progressWriter
} else {
// Historically push writes status to stderr
writer = os.Stderr
}

dec := json.NewDecoder(response.Body)
Expand All @@ -84,7 +87,7 @@ func Pull(ctx context.Context, rawImage string, options *PullOptions) ([]string,

switch {
case report.Stream != "":
fmt.Fprint(stderr, report.Stream)
fmt.Fprint(writer, report.Stream)
case report.Error != "":
pullErrors = append(pullErrors, errors.New(report.Error))
case len(report.Images) > 0:
Expand Down
9 changes: 5 additions & 4 deletions pkg/bindings/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -58,12 +57,14 @@ func Push(ctx context.Context, source string, destination string, options *PushO
return response.Process(err)
}

// Historically push writes status to stderr
writer := io.Writer(os.Stderr)
var writer io.Writer
if options.GetQuiet() {
writer = ioutil.Discard
writer = io.Discard
} else if progressWriter := options.GetProgressWriter(); progressWriter != nil {
writer = progressWriter
} else {
// Historically push writes status to stderr
writer = os.Stderr
}

dec := json.NewDecoder(response.Body)
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindings/images/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ type PullOptions struct {
Policy *string
// Password for authenticating against the registry.
Password *string
// ProgressWriter is a writer where pull progress are sent.
ProgressWriter *io.Writer
// Quiet can be specified to suppress pull progress when pulling. Ignored
// for remote calls.
Quiet *bool
Expand Down
16 changes: 16 additions & 0 deletions pkg/bindings/images/types_pull_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/bindings/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,14 @@ func Push(ctx context.Context, name, destination string, options *images.PushOpt
return "", response.Process(err)
}

// Historically push writes status to stderr
writer := io.Writer(os.Stderr)
var writer io.Writer
if options.GetQuiet() {
writer = io.Discard
} else if progressWriter := options.GetProgressWriter(); progressWriter != nil {
writer = progressWriter
} else {
// Historically push writes status to stderr
writer = os.Stderr
}

dec := json.NewDecoder(response.Body)
Expand Down
24 changes: 22 additions & 2 deletions pkg/bindings/test/images_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package bindings_test

import (
"bytes"
"fmt"
"net/http"
"os"
"path/filepath"
"time"

podmanRegistry "github.com/containers/podman/v4/hack/podman-registry-go"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/bindings/containers"
"github.com/containers/podman/v4/pkg/bindings/images"
Expand Down Expand Up @@ -362,9 +365,14 @@ var _ = Describe("Podman images", func() {
It("Image Pull", func() {
rawImage := "docker.io/library/busybox:latest"

pulledImages, err := images.Pull(bt.conn, rawImage, nil)
var writer bytes.Buffer
pullOpts := new(images.PullOptions).WithProgressWriter(&writer)
pulledImages, err := images.Pull(bt.conn, rawImage, pullOpts)
Expect(err).NotTo(HaveOccurred())
Expect(len(pulledImages)).To(Equal(1))
output := writer.String()
Expect(output).To(ContainSubstring("Trying to pull "))
Expect(output).To(ContainSubstring("Getting image source signatures"))

exists, err := images.Exists(bt.conn, rawImage, nil)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -380,7 +388,19 @@ var _ = Describe("Podman images", func() {
})

It("Image Push", func() {
Skip("TODO: implement test for image push to registry")
registry, err := podmanRegistry.Start()
Expect(err).To(BeNil())

var writer bytes.Buffer
pushOpts := new(images.PushOptions).WithUsername(registry.User).WithPassword(registry.Password).WithSkipTLSVerify(true).WithProgressWriter(&writer).WithQuiet(false)
err = images.Push(bt.conn, alpine.name, fmt.Sprintf("localhost:%s/test:latest", registry.Port), pushOpts)
Expect(err).ToNot(HaveOccurred())

output := writer.String()
Expect(output).To(ContainSubstring("Copying blob "))
Expect(output).To(ContainSubstring("Copying config "))
Expect(output).To(ContainSubstring("Writing manifest to image destination"))
Expect(output).To(ContainSubstring("Storing signatures"))
})

It("Build no options", func() {
Expand Down
23 changes: 20 additions & 3 deletions pkg/bindings/test/manifests_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package bindings_test

import (
"bytes"
"fmt"
"net/http"
"time"

podmanRegistry "github.com/containers/podman/v4/hack/podman-registry-go"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/bindings/images"
"github.com/containers/podman/v4/pkg/bindings/manifests"
Expand All @@ -12,7 +15,7 @@ import (
"github.com/onsi/gomega/gexec"
)

var _ = Describe("podman manifest", func() {
var _ = Describe("Podman manifests", func() {
var (
bt *bindingTest
s *gexec.Session
Expand Down Expand Up @@ -172,7 +175,21 @@ var _ = Describe("podman manifest", func() {
Expect(list.Manifests[0].Platform.OS).To(Equal("foo"))
})

It("push manifest", func() {
Skip("TODO: implement test for manifest push to registry")
It("Manifest Push", func() {
registry, err := podmanRegistry.Start()
Expect(err).To(BeNil())

name := "quay.io/libpod/foobar:latest"
_, err = manifests.Create(bt.conn, name, []string{alpine.name}, nil)
Expect(err).ToNot(HaveOccurred())

var writer bytes.Buffer
pushOpts := new(images.PushOptions).WithUsername(registry.User).WithPassword(registry.Password).WithAll(true).WithSkipTLSVerify(true).WithProgressWriter(&writer).WithQuiet(false)
_, err = manifests.Push(bt.conn, name, fmt.Sprintf("localhost:%s/test:latest", registry.Port), pushOpts)
Expect(err).ToNot(HaveOccurred())

output := writer.String()
Expect(output).To(ContainSubstring("Writing manifest list to image destination"))
Expect(output).To(ContainSubstring("Storing list signatures"))
})
})
2 changes: 2 additions & 0 deletions pkg/domain/entities/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ type ImagePullOptions struct {
SkipTLSVerify types.OptionalBool
// PullPolicy whether to pull new image
PullPolicy config.PullPolicy
// Writer is used to display copy information including progress bars.
Writer io.Writer
}

// ImagePullReport is the response from pulling one or more images.
Expand Down
3 changes: 2 additions & 1 deletion pkg/domain/infra/abi/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ func (ir *ImageEngine) Pull(ctx context.Context, rawImage string, options entiti
pullOptions.Variant = options.Variant
pullOptions.SignaturePolicyPath = options.SignaturePolicy
pullOptions.InsecureSkipTLSVerify = options.SkipTLSVerify
pullOptions.Writer = options.Writer

if !options.Quiet {
if !options.Quiet && pullOptions.Writer == nil {
pullOptions.Writer = os.Stderr
}

Expand Down
1 change: 1 addition & 0 deletions pkg/domain/infra/tunnel/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (ir *ImageEngine) Pull(ctx context.Context, rawImage string, opts entities.
options.WithAllTags(opts.AllTags).WithAuthfile(opts.Authfile).WithArch(opts.Arch).WithOS(opts.OS)
options.WithVariant(opts.Variant).WithPassword(opts.Password)
options.WithQuiet(opts.Quiet).WithUsername(opts.Username).WithPolicy(opts.PullPolicy.String())
options.WithProgressWriter(opts.Writer)
if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
if s == types.OptionalBoolTrue {
options.WithSkipTLSVerify(true)
Expand Down
27 changes: 27 additions & 0 deletions test/e2e/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,33 @@ var _ = Describe("Podman manifest", func() {
Expect(foundZstdFile).To(BeTrue())
})

It("push progress", func() {
SkipIfRemote("manifest push to dir not supported in remote mode")

session := podmanTest.Podman([]string{"manifest", "create", "foo", imageList})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

dest := filepath.Join(podmanTest.TempDir, "pushed")
err := os.MkdirAll(dest, os.ModePerm)
Expect(err).To(BeNil())
defer func() {
os.RemoveAll(dest)
}()

session = podmanTest.Podman([]string{"push", "foo", "-q", "dir:" + dest})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.ErrorToString()).To(BeEmpty())

session = podmanTest.Podman([]string{"push", "foo", "dir:" + dest})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
output := session.ErrorToString()
Expect(output).To(ContainSubstring("Writing manifest list to image destination"))
Expect(output).To(ContainSubstring("Storing list signatures"))
})

It("authenticated push", func() {
registryOptions := &podmanRegistry.Options{
Image: "docker-archive:" + imageTarPath(REGISTRY_IMAGE),
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,18 @@ var _ = Describe("Podman pull", func() {
Expect(data[0]).To(HaveField("Os", runtime.GOOS))
Expect(data[0]).To(HaveField("Architecture", "arm64"))
})

It("podman pull progress", func() {
session := podmanTest.Podman([]string{"pull", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
output := session.ErrorToString()
Expect(output).To(ContainSubstring("Getting image source signatures"))
Expect(output).To(ContainSubstring("Copying blob "))

session = podmanTest.Podman([]string{"pull", "-q", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.ErrorToString()).To(BeEmpty())
})
})

0 comments on commit 3bf52aa

Please sign in to comment.