Skip to content

Commit

Permalink
feat: print x/y progress to the stdout (#4545)
Browse files Browse the repository at this point in the history
* feat: print x/y progress to the stdout

print a counter to show progress while pushing images and embedded
cluster artifacts.

* chore: rephrasing user feedback
  • Loading branch information
ricardomaraschini authored Apr 11, 2024
1 parent 8685861 commit 1013d8e
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions pkg/image/airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option
defer wc.Close()
}

totalImages := len(imageInfos)
var imageCounter int
for imageID, imageInfo := range imageInfos {
srcRef, err := tempRegistry.SrcRef(imageID)
if err != nil {
Expand Down Expand Up @@ -280,6 +282,8 @@ func PushImagesFromTempRegistry(airgapRootDir string, imageList []string, option
ReportWriter: reportWriter,
},
}
imageCounter++
fmt.Printf("Pushing application images (%d/%d)\n", imageCounter, totalImages)
if err := pushImage(pushImageOpts); err != nil {
return errors.Wrapf(err, "failed to push image %s", imageID)
}
Expand Down Expand Up @@ -699,6 +703,8 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
}
defer gzipReader.Close()

var artifacts []string

tarReader := tar.NewReader(gzipReader)
pushedArtifacts := make([]string, 0)
for {
Expand All @@ -718,52 +724,49 @@ func PushEmbeddedClusterArtifacts(airgapBundle string, opts imagetypes.PushEmbed
continue
}

if err := func() error {
dstFilePath := filepath.Join(tmpDir, header.Name)
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
return errors.Wrap(err, "failed to create path")
}
defer os.RemoveAll(dstFilePath)
dstFilePath := filepath.Join(tmpDir, header.Name)
if err := os.MkdirAll(filepath.Dir(dstFilePath), 0755); err != nil {
return nil, errors.Wrap(err, "failed to create path")
}

dstFile, err := os.Create(dstFilePath)
if err != nil {
return errors.Wrap(err, "failed to create file")
}
defer dstFile.Close()
dstFile, err := os.Create(dstFilePath)
if err != nil {
return nil, errors.Wrap(err, "failed to create file")
}

if _, err := io.Copy(dstFile, tarReader); err != nil {
return errors.Wrap(err, "failed to copy file data")
}
if _, err := io.Copy(dstFile, tarReader); err != nil {
dstFile.Close()
return nil, errors.Wrap(err, "failed to copy file data")
}

// push each file as an oci artifact to the registry
name := filepath.Base(dstFilePath)
repository := filepath.Join("embedded-cluster", imageutil.SanitizeRepo(name))
artifactFile := imagetypes.OCIArtifactFile{
Name: name,
Path: dstFilePath,
MediaType: EmbeddedClusterMediaType,
}
dstFile.Close()
artifacts = append(artifacts, dstFilePath)
}

pushOCIArtifactOpts := imagetypes.PushOCIArtifactOptions{
Files: []imagetypes.OCIArtifactFile{artifactFile},
ArtifactType: EmbeddedClusterArtifactType,
Registry: opts.Registry,
Repository: repository,
Tag: opts.Tag,
HTTPClient: opts.HTTPClient,
}
for i, dstFilePath := range artifacts {
name := filepath.Base(dstFilePath)
repository := filepath.Join("embedded-cluster", imageutil.SanitizeRepo(name))
artifactFile := imagetypes.OCIArtifactFile{
Name: name,
Path: dstFilePath,
MediaType: EmbeddedClusterMediaType,
}

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

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

return pushedArtifacts, nil
Expand Down

0 comments on commit 1013d8e

Please sign in to comment.