Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove progressbar on image push #3110

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 6 additions & 7 deletions src/internal/packager/images/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/types"
)
Expand Down Expand Up @@ -98,18 +97,18 @@ func WithPushAuth(ri types.RegistryInfo) crane.Option {
return WithBasicAuth(ri.PushUsername, ri.PushPassword)
}

func createPushOpts(cfg PushConfig, pb *message.ProgressBar) []crane.Option {
func createPushOpts(cfg PushConfig) []crane.Option {
opts := CommonOpts(cfg.Arch)
opts = append(opts, WithPushAuth(cfg.RegInfo))

transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig.InsecureSkipVerify = config.CommonOptions.InsecureSkipTLSVerify
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport.TLSClientConfig.InsecureSkipVerify = config.CommonOptions.InsecureSkipTLSVerify
// TODO (@WSTARR) This is set to match the TLSHandshakeTimeout to potentially mitigate effects of https://github.com/zarf-dev/zarf/issues/1444
transport.ResponseHeaderTimeout = 10 * time.Second
defaultTransport.ResponseHeaderTimeout = 10 * time.Second

transportWithProgressBar := helpers.NewTransport(transport, pb)
transport := helpers.NewTransport(defaultTransport, nil)

opts = append(opts, crane.WithTransport(transportWithProgressBar))
opts = append(opts, crane.WithTransport(transport))

return opts
}
60 changes: 5 additions & 55 deletions src/internal/packager/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ package images

import (
"context"
"fmt"
"time"

"github.com/avast/retry-go/v4"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand All @@ -27,32 +25,22 @@ func Push(ctx context.Context, cfg PushConfig) error {
logs.Progress.SetOutput(&message.DebugWriter{})

toPush := map[transform.Image]v1.Image{}
var totalSize int64
// Build an image list from the references
for _, refInfo := range cfg.ImageList {
img, err := utils.LoadOCIImage(cfg.SourceDirectory, refInfo)
if err != nil {
return err
}
toPush[refInfo] = img
imgSize, err := calcImgSize(img)
if err != nil {
return err
}
totalSize += imgSize
}

// If this is not a no checksum image push we will be pushing two images (the second will go faster as it checks the same layers)
if !cfg.NoChecksum {
totalSize = totalSize * 2
}

var (
err error
tunnel *cluster.Tunnel
registryURL = cfg.RegInfo.Address
)

spinner := message.NewProgressSpinner("Pushing images to Zarf registry")
defer spinner.Stop()
err = retry.Do(func() error {
c, _ := cluster.NewCluster()
if c != nil {
Expand All @@ -64,16 +52,12 @@ func Push(ctx context.Context, cfg PushConfig) error {
defer tunnel.Close()
}
}

progress := message.NewProgressBar(totalSize, fmt.Sprintf("Pushing %d images", len(toPush)))
defer progress.Close()
pushOptions := createPushOpts(cfg, progress)
pushOptions := createPushOpts(cfg)

pushImage := func(img v1.Image, name string) error {
if tunnel != nil {
return tunnel.Wrap(func() error { return crane.Push(img, name, pushOptions...) })
}

return crane.Push(img, name, pushOptions...)
}

Expand All @@ -84,14 +68,7 @@ func Push(ctx context.Context, cfg PushConfig) error {
}
}()
for refInfo, img := range toPush {
refTruncated := helpers.Truncate(refInfo.Reference, 55, true)
progress.Updatef(fmt.Sprintf("Pushing %s", refTruncated))

size, err := calcImgSize(img)
if err != nil {
return err
}

spinner.Updatef("Pushing %s", refInfo.Reference)
// If this is not a no checksum image push it for use with the Zarf agent
if !cfg.NoChecksum {
offlineNameCRC, err := transform.ImageTransformHost(registryURL, refInfo.Reference)
Expand All @@ -102,8 +79,6 @@ func Push(ctx context.Context, cfg PushConfig) error {
if err = pushImage(img, offlineNameCRC); err != nil {
return err
}

totalSize -= size
}

// To allow for other non-zarf workloads to easily see the images upload a non-checksum version
Expand All @@ -113,43 +88,18 @@ func Push(ctx context.Context, cfg PushConfig) error {
return err
}

message.Debugf("push %s -> %s)", refInfo.Reference, offlineName)

if err = pushImage(img, offlineName); err != nil {
return err
}

pushed = append(pushed, refInfo)
totalSize -= size
}
progress.Successf("Pushed %d images", len(cfg.ImageList))
return nil
}, retry.Context(ctx), retry.Attempts(uint(cfg.Retries)), retry.Delay(500*time.Millisecond))
if err != nil {
return err
}
spinner.Success()
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func calcImgSize(img v1.Image) (int64, error) {
size, err := img.Size()
if err != nil {
return size, err
}

layers, err := img.Layers()
if err != nil {
return size, err
}

for _, layer := range layers {
ls, err := layer.Size()
if err != nil {
return size, err
}
size += ls
}

return size, nil
}
10 changes: 5 additions & 5 deletions src/internal/packager2/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func pushImagesToRegistry(ctx context.Context, c *cluster.Cluster, pkgLayout *la
return nil
}

transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig.InsecureSkipVerify = config.CommonOptions.InsecureSkipTLSVerify
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport.TLSClientConfig.InsecureSkipVerify = config.CommonOptions.InsecureSkipTLSVerify
// TODO (@WSTARR) This is set to match the TLSHandshakeTimeout to potentially mitigate effects of https://github.com/zarf-dev/zarf/issues/1444
transport.ResponseHeaderTimeout = 10 * time.Second
transportWithProgressBar := helpers.NewTransport(transport, nil)
defaultTransport.ResponseHeaderTimeout = 10 * time.Second
transport := helpers.NewTransport(defaultTransport, nil)

pushOptions := []crane.Option{
crane.WithPlatform(&v1.Platform{OS: "linux", Architecture: pkgLayout.Pkg.Build.Architecture}),
crane.WithTransport(transportWithProgressBar),
crane.WithTransport(transport),
crane.WithAuth(authn.FromConfig(authn.AuthConfig{
Username: regInfo.PushUsername,
Password: regInfo.PushPassword,
Expand Down