Skip to content

Print the progress for container Image Pull during cluster-up #1425

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 48 additions & 8 deletions cluster-provision/gocli/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func PrintProgress(progressReader io.ReadCloser, writer *os.File) error {
scanner := bufio.NewScanner(progressReader)
for scanner.Scan() {
line := scanner.Text()
if err := checkForError(line); err != nil {
if err := parseAndCheckForError(line, &PullStatus{}); err != nil {
return err
}
clearLength := w - len(line)
Expand All @@ -285,20 +285,52 @@ func PrintProgress(progressReader io.ReadCloser, writer *os.File) error {
} else {
fmt.Fprint(writer, "Downloading ...")
scanner := bufio.NewScanner(progressReader)
// Map to store which state was last printed for each ctr image layer
lastReportedState := make(map[string]PullStatus)
for scanner.Scan() {
line := scanner.Text()
if err := checkForError(line); err != nil {
// Discard empty lines
if line == "" {
continue
}
// Parse the progress json message into PullStatus struct
pullStatus := &PullStatus{}
if err := parseAndCheckForError(line, pullStatus); err != nil {
return err
}
fmt.Print(".")

lastStatus, ok := lastReportedState[pullStatus.Id]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the pull status reporting logic into a helper function to reduce nesting and improve clarity of the code.

Consider extracting the pull‑status reporting logic into a helper function to reduce nesting and improve clarity. For example, you could create a function like this:

```go
func shouldReport(current, last PullStatus) bool {
	if current.Status != last.Status {
		return true
	}
	lastProgress := float64(last.ProgressDetail.Current) / float64(last.ProgressDetail.Total)
	currProgress := float64(current.ProgressDetail.Current) / float64(current.ProgressDetail.Total)
	return currProgress-lastProgress >= 0.1
}

Then your scanner loop in the non‑terminal branch can become:

scanner := bufio.NewScanner(progressReader)
// Map to store the last state printed for each container layer.
lastReportedState := make(map[string]PullStatus)
for scanner.Scan() {
	line := scanner.Text()
	if line == "" {
		continue
	}

	pullStatus := &PullStatus{}
	if err := parseAndCheckForError(line, pullStatus); err != nil {
		return err
	}

	toReport := false
	if last, ok := lastReportedState[pullStatus.Id]; ok {
		toReport = shouldReport(*pullStatus, last)
	} else {
		toReport = true
	}

	if toReport {
		fmt.Fprintf(writer, "%s\t%s\t%s\n", pullStatus.Id, pullStatus.Status, pullStatus.Progress)
		lastReportedState[pullStatus.Id] = *pullStatus
	}
}

This refactoring isolates the decision logic, reduces nested conditionals, and improves overall readability without changing the functionality.

toReport := false
if ok {
// This later was seen before
if pullStatus.Status != lastStatus.Status {
toReport = true
} else {
// Current status is same as last seen status
// This is true only for Downloading and Extracting states
lastProgress := float64(lastStatus.ProgressDetail.Current) / float64(lastStatus.ProgressDetail.Total)
currProgress := float64(pullStatus.ProgressDetail.Current) / float64(pullStatus.ProgressDetail.Total)
if currProgress-lastProgress >= 0.1 { // 10% progress
toReport = true
}
}
} else {
// If this layer hasn't been seen before, print its status
toReport = true
}

if toReport {
fmt.Fprintf(writer, "%s\t%s\t%s\n", pullStatus.Id, pullStatus.Status, pullStatus.Progress)
// Update the last reported status for this layer
lastReportedState[pullStatus.Id] = *pullStatus
}

}
fmt.Print("\n")
}
return nil
}

func checkForError(line string) error {
pullStatus := &PullStatus{}
func parseAndCheckForError(line string, pullStatus *PullStatus) error {
if line != "" {
err := json.Unmarshal([]byte(line), pullStatus)
if err != nil {
Expand All @@ -311,7 +343,15 @@ func checkForError(line string) error {
return nil
}

type PullProgressDetail struct {
Current int64 `json:"current"`
Total int64 `json:"total"`
}

type PullStatus struct {
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
Id string `json:"id,omitempty"`
Status string `json:"status,omitempty"`
ProgressDetail PullProgressDetail `json:"progressDetail,omitempty"`
Progress string `json:"progress,omitempty"`
Error string `json:"error,omitempty"`
}