Skip to content

Commit 04d66ef

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 9d84ee7 commit 04d66ef

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

pkg/image/airgap.go

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option
226226
defer wc.Close()
227227
}
228228

229+
totalImages := len(imageInfos)
230+
var imageCounter int
229231
for imageID, imageInfo := range imageInfos {
230232
srcRef, err := tempRegistry.SrcRef(imageID)
231233
if err != nil {
@@ -280,6 +282,8 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option
280282
ReportWriter: reportWriter,
281283
},
282284
}
285+
imageCounter++
286+
fmt.Printf("Pushing image %d/%d\n", imageCounter, totalImages)
283287
if err := pushImage(pushImageOpts); err != nil {
284288
return errors.Wrapf(err, "failed to push image %s", imageID)
285289
}
@@ -699,6 +703,8 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
699703
}
700704
defer gzipReader.Close()
701705

706+
var artifacts []string
707+
702708
tarReader := tar.NewReader(gzipReader)
703709
pushedArtifacts := make([]string, 0)
704710
for {
@@ -718,52 +724,49 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
718724
continue
719725
}
720726

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)
727+
dstFilePath := filepath.Join(tmpDir, header.Name)
728+
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
729+
return nil, errors.Wrap(err, "failed to create path")
730+
}
727731

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

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

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-
}
742+
dstFile.Close()
743+
artifacts = append(artifacts, dstFilePath)
744+
}
746745

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-
}
746+
for i, dstFilePath := range artifacts {
747+
name := filepath.Base(dstFilePath)
748+
repository := filepath.Join("embedded-cluster", imageutil.SanitizeRepo(name))
749+
artifactFile := imagetypes.OCIArtifactFile{
750+
Name: name,
751+
Path: dstFilePath,
752+
MediaType: EmbeddedClusterMediaType,
753+
}
755754

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)
755+
pushOCIArtifactOpts := imagetypes.PushOCIArtifactOptions{
756+
Files: []imagetypes.OCIArtifactFile{artifactFile},
757+
ArtifactType: EmbeddedClusterArtifactType,
758+
Registry: opts.Registry,
759+
Repository: repository,
760+
Tag: opts.Tag,
761+
HTTPClient: opts.HTTPClient,
762+
}
762763

763-
return nil
764-
}(); err != nil {
765-
return nil, err
764+
fmt.Printf("Pushing embedded cluster artifact %d/%d\n", i+1, len(artifacts))
765+
artifact := fmt.Sprintf("%s:%s", filepath.Join(opts.Registry.Endpoint, opts.Registry.Namespace, repository), opts.Tag)
766+
if err := pushOCIArtifact(pushOCIArtifactOpts); err != nil {
767+
return nil, errors.Wrapf(err, "failed to push oci artifact %s", name)
766768
}
769+
pushedArtifacts = append(pushedArtifacts, artifact)
767770
}
768771

769772
return pushedArtifacts, nil

0 commit comments

Comments
 (0)