Skip to content

Commit affbbda

Browse files
feat: print x/y progress to the stdout
print a counter to show progress while pushing images and embedded cluster artifacts.
1 parent 6ae026b commit affbbda

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

pkg/image/airgap.go

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func TagAndPushImagesFromBundle(airgapBundle string, options imagetypes.PushImag
172172
Registry: options.Registry,
173173
Tag: imageutil.SanitizeTag(fmt.Sprintf("%s-%s-%s", airgap.Spec.ChannelID, airgap.Spec.UpdateCursor, airgap.Spec.VersionLabel)),
174174
HTTPClient: orasretry.DefaultClient,
175+
LogForUI: options.LogForUI,
175176
}
176177
pushedArtifacts, err := PushEmbeddedClusterArtifacts(airgapBundle, pushEmbeddedArtifactsOpts)
177178
if err != nil {
@@ -226,6 +227,8 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option
226227
defer wc.Close()
227228
}
228229

230+
totalImages := len(imageInfos)
231+
var imageCounter int
229232
for imageID, imageInfo := range imageInfos {
230233
srcRef, err := tempRegistry.SrcRef(imageID)
231234
if err != nil {
@@ -280,6 +283,10 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option
280283
ReportWriter: reportWriter,
281284
},
282285
}
286+
imageCounter++
287+
if pushImageOpts.LogForUI {
288+
fmt.Printf("Pushing image %d/%d\n", imageCounter, totalImages)
289+
}
283290
if err := pushImage(pushImageOpts); err != nil {
284291
return errors.Wrapf(err, "failed to push image %s", imageID)
285292
}
@@ -699,6 +706,8 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
699706
}
700707
defer gzipReader.Close()
701708

709+
var artifacts []string
710+
702711
tarReader := tar.NewReader(gzipReader)
703712
pushedArtifacts := make([]string, 0)
704713
for {
@@ -718,52 +727,52 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
718727
continue
719728
}
720729

721-
if err := func() error {
722-
dstFilePath := filepath.Join(tmpDir, header.Name)
723-
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
724-
return errors.Wrap(err, "failed to create path")
725-
}
726-
defer os.RemoveAll(dstFilePath)
730+
dstFilePath := filepath.Join(tmpDir, header.Name)
731+
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
732+
return nil, errors.Wrap(err, "failed to create path")
733+
}
727734

728-
dstFile, err := os.Create(dstFilePath)
729-
if err != nil {
730-
return errors.Wrap(err, "failed to create file")
731-
}
732-
defer dstFile.Close()
735+
dstFile, err := os.Create(dstFilePath)
736+
if err != nil {
737+
return nil, errors.Wrap(err, "failed to create file")
738+
}
733739

734-
if _, err := io.Copy(dstFile, tarReader); err != nil {
735-
return errors.Wrap(err, "failed to copy file data")
736-
}
740+
if _, err := io.Copy(dstFile, tarReader); err != nil {
741+
dstFile.Close()
742+
return nil, errors.Wrap(err, "failed to copy file data")
743+
}
737744

738-
// push each file as an oci artifact to the registry
739-
name := filepath.Base(dstFilePath)
740-
repository := filepath.Join("embedded-cluster", imageutil.SanitizeRepo(name))
741-
artifactFile := imagetypes.OCIArtifactFile{
742-
Name: name,
743-
Path: dstFilePath,
744-
MediaType: EmbeddedClusterMediaType,
745-
}
745+
dstFile.Close()
746+
artifacts = append(artifacts, dstFilePath)
747+
}
746748

747-
pushOCIArtifactOpts := imagetypes.PushOCIArtifactOptions{
748-
Files: []imagetypes.OCIArtifactFile{artifactFile},
749-
ArtifactType: EmbeddedClusterArtifactType,
750-
Registry: opts.Registry,
751-
Repository: repository,
752-
Tag: opts.Tag,
753-
HTTPClient: opts.HTTPClient,
754-
}
749+
for i, dstFilePath := range artifacts {
750+
name := filepath.Base(dstFilePath)
751+
repository := filepath.Join("embedded-cluster", imageutil.SanitizeRepo(name))
752+
artifactFile := imagetypes.OCIArtifactFile{
753+
Name: name,
754+
Path: dstFilePath,
755+
MediaType: EmbeddedClusterMediaType,
756+
}
755757

756-
artifact := fmt.Sprintf("%s:%s", filepath.Join(opts.Registry.Endpoint, opts.Registry.Namespace, repository), opts.Tag)
757-
fmt.Printf("Pushing artifact %s\n", artifact)
758-
if err := pushOCIArtifact(pushOCIArtifactOpts); err != nil {
759-
return errors.Wrapf(err, "failed to push oci artifact %s", name)
760-
}
761-
pushedArtifacts = append(pushedArtifacts, artifact)
758+
pushOCIArtifactOpts := imagetypes.PushOCIArtifactOptions{
759+
Files: []imagetypes.OCIArtifactFile{artifactFile},
760+
ArtifactType: EmbeddedClusterArtifactType,
761+
Registry: opts.Registry,
762+
Repository: repository,
763+
Tag: opts.Tag,
764+
HTTPClient: opts.HTTPClient,
765+
}
762766

763-
return nil
764-
}(); err != nil {
765-
return nil, err
767+
if opts.LogForUI {
768+
fmt.Printf("Pushing artifact %d/%d\n", i+1, len(artifacts))
769+
}
770+
771+
artifact := fmt.Sprintf("%s:%s", filepath.Join(opts.Registry.Endpoint, opts.Registry.Namespace, repository), opts.Tag)
772+
if err := pushOCIArtifact(pushOCIArtifactOpts); err != nil {
773+
return nil, errors.Wrapf(err, "failed to push oci artifact %s", name)
766774
}
775+
pushedArtifacts = append(pushedArtifacts, artifact)
767776
}
768777

769778
return pushedArtifacts, nil

pkg/image/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type PushEmbeddedClusterArtifactsOptions struct {
8787
Registry dockerregistrytypes.RegistryOptions
8888
Tag string
8989
HTTPClient *http.Client
90+
LogForUI bool
9091
}
9192

9293
type PushOCIArtifactOptions struct {

0 commit comments

Comments
 (0)