Skip to content
Open
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
2 changes: 1 addition & 1 deletion cmd/crossplane/xpkg/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (c *batchCmd) pushWithRetry(logger logging.Logger, imgs []packageImage, s s
retryMsg := ""
for i := range tries {
logger.Info(fmt.Sprintf("Pushing xpkg to %s.%s", t, retryMsg))
err := pushImages(logger, imgs, t, nil)
err := pushImages(logger, nil, imgs, t, nil)
if err == nil {
break
}
Expand Down
28 changes: 19 additions & 9 deletions cmd/crossplane/xpkg/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"github.com/crossplane/crossplane-runtime/v2/pkg/xpkg/parser/examples"
"github.com/crossplane/crossplane-runtime/v2/pkg/xpkg/parser/yaml"

"github.com/crossplane/cli/v2/internal/terminal"

_ "embed"
)

Expand Down Expand Up @@ -164,14 +166,20 @@ func (c *buildCmd) GetOutputFileName(meta runtime.Object, hash v1.Hash) (string,
}

// Run executes the build command.
func (c *buildCmd) Run(logger logging.Logger) error {
func (c *buildCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error {
buildOpts, err := c.GetRuntimeBaseImageOpts()
if err != nil {
return errors.Wrap(err, errGetRuntimeBaseImageOpts)
}

img, meta, err := c.builder.Build(context.Background(), buildOpts...)
if err != nil {
var img v1.Image
var meta runtime.Object

if err := sp.WrapWithSuccessSpinner("Building package", func() error {
var err error
img, meta, err = c.builder.Build(context.Background(), buildOpts...)
return err
}); err != nil {
return errors.Wrap(err, errBuildPackage)
}

Expand All @@ -185,14 +193,16 @@ func (c *buildCmd) Run(logger logging.Logger) error {
return err
}

f, err := c.fs.Create(output)
if err != nil {
return errors.Wrap(err, errCreatePackage)
}
if err := sp.WrapWithSuccessSpinner("Writing package to disk", func() error {
f, err := c.fs.Create(output)
if err != nil {
return errors.Wrap(err, errCreatePackage)
}

defer func() { _ = f.Close() }()
defer func() { _ = f.Close() }()

if err := tarball.Write(nil, img, f); err != nil {
return tarball.Write(nil, img, f)
}); err != nil {
return err
}

Expand Down
70 changes: 49 additions & 21 deletions cmd/crossplane/xpkg/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
"github.com/crossplane/crossplane-runtime/v2/pkg/xpkg"

"github.com/crossplane/cli/v2/internal/terminal"

_ "embed"
)

Expand Down Expand Up @@ -85,7 +87,7 @@ func (c *pushCmd) AfterApply() error {
}

// Run runs the push cmd.
func (c *pushCmd) Run(logger logging.Logger) error {
func (c *pushCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter) error {
anns, err := parseAnnotations(c.OCIAnnotation)
if err != nil {
return errors.Wrap(err, errParseAnnotations)
Expand All @@ -110,15 +112,21 @@ func (c *pushCmd) Run(logger logging.Logger) error {

// load images from all the provided package files
images := make([]packageImage, 0, len(c.PackageFiles))
for _, p := range c.PackageFiles {
cleanPath := filepath.Clean(p)
if err := sp.WrapWithSuccessSpinner("Reading packages", func() error {
for _, p := range c.PackageFiles {
cleanPath := filepath.Clean(p)

img, err := tarball.ImageFromPath(cleanPath, nil)
if err != nil {
return err
img, err := tarball.ImageFromPath(cleanPath, nil)
if err != nil {
return err
}

images = append(images, packageImage{Image: img, Path: cleanPath})
}

images = append(images, packageImage{Image: img, Path: cleanPath})
return nil
}); err != nil {
return err
}

t := http.DefaultTransport.(*http.Transport).Clone() //nolint:forcetypeassert // http.DefaultTransport is always *http.Transport
Expand All @@ -133,7 +141,7 @@ func (c *pushCmd) Run(logger logging.Logger) error {
remote.WithTransport(t),
}

return pushImages(logger, images, c.Package, anns, options...)
return pushImages(logger, sp, images, c.Package, anns, options...)
}

// packageImage describes a package image that will be pushed.
Expand All @@ -147,7 +155,7 @@ type packageImage struct {
}

// pushImages pushes package images to the given URL using the provided options.
func pushImages(logger logging.Logger, images []packageImage, url string, annotations map[string]string, options ...remote.Option) error {
func pushImages(logger logging.Logger, sp terminal.SpinnerPrinter, images []packageImage, url string, annotations map[string]string, options ...remote.Option) error {
if len(options) == 0 {
options = []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
Expand All @@ -170,13 +178,20 @@ func pushImages(logger logging.Logger, images []packageImage, url string, annota

img = annotateImage(img, annotations)

if err := remote.Write(tag, img, options...); err != nil {
return errors.Wrapf(err, errFmtPushPackage, pi.Path)
}
push := func() error {
if err := remote.Write(tag, img, options...); err != nil {
return errors.Wrapf(err, errFmtPushPackage, pi.Path)
}

logger.Debug("Pushed package", "path", pi.Path, "ref", tag.String())
logger.Debug("Pushed package", "path", pi.Path, "ref", tag.String())

return nil
return nil
}

if sp != nil {
return sp.WrapWithSuccessSpinner("Pushing package", push)
}
return push()
}

// If there's more than one package file we'll write (push) them all by
Expand Down Expand Up @@ -237,16 +252,29 @@ func pushImages(logger logging.Logger, images []packageImage, url string, annota
})
}

if err := g.Wait(); err != nil {
if sp != nil {
if err := sp.WrapWithSuccessSpinner("Pushing packages", func() error {
return g.Wait()
}); err != nil {
return err
}
} else if err := g.Wait(); err != nil {
return err
}

idx := annotateIndex(mutate.AppendManifests(empty.Index, adds...), annotations)
if err := remote.WriteIndex(tag, idx, options...); err != nil {
return errors.Wrapf(err, errFmtWriteIndex, len(adds))
}
pushIndex := func() error {
idx := annotateIndex(mutate.AppendManifests(empty.Index, adds...), annotations)
if err := remote.WriteIndex(tag, idx, options...); err != nil {
return errors.Wrapf(err, errFmtWriteIndex, len(adds))
}

logger.Debug("Wrote OCI index", "ref", tag.String(), "manifests", len(adds))
logger.Debug("Wrote OCI index", "ref", tag.String(), "manifests", len(adds))

return nil
return nil
}

if sp != nil {
return sp.WrapWithSuccessSpinner("Pushing package index", pushIndex)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
return pushIndex()
}