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

calculate sha256 file checksum by streaming #2036

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

samt42
Copy link
Contributor

@samt42 samt42 commented Nov 3, 2023

What type of PR is this?

/kind feature
change file checksum method to stream calculation in order to run on limited memory machine.

What this PR does / why we need it:

When I initiate the Harbor registry using the kubekey command tool like this:

./kk init registry

It fails with error message "Killed". After debugging step by step, I found the root cause in file cmd/kk/pkg/files/file.go function sha256sum:

func (b *KubeBinary) Download() error {
	for i := 5; i > 0; i-- {
		...
		if err := b.SHA256Check(); err != nil {
		    ...
                }
	}
	return nil
}
// SHA256Check is used to hash checks on downloaded binary. (sha256)
func (b *KubeBinary) SHA256Check() error {
	output, err := sha256sum(b.Path())
        ...
	return nil
}

func sha256sum(path string) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()

	data, err := io.ReadAll(file)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%x", sha256.Sum256(data)), nil
}

In function sha256sum, it read all file content into memory to calculate file checksum. because of that, when running on machine with low memory(only 2GB), process be killed by system because of the memory exhausted.
So I changed it into stream calculation, like this:

func sha256sum(path string) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()

	hasher := sha256.New()
	if _, err := io.Copy(hasher, file); err != nil {
		return "", err
	}
	return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}

It works fine on my server machine.

@ks-ci-bot ks-ci-bot added kind/feature Categorizes issue or PR as related to a new feature. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 3, 2023
@samt42 samt42 marked this pull request as ready for review November 3, 2023 06:57
@ks-ci-bot ks-ci-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 3, 2023
@pixiake
Copy link
Collaborator

pixiake commented Nov 6, 2023

@samt42 Thanks for this contributions !
/lgtm
/approve

@ks-ci-bot ks-ci-bot added the lgtm Indicates that a PR is ready to be merged. label Nov 6, 2023
@ks-ci-bot
Copy link
Collaborator

LGTM label has been added.

Git tree hash: c2cf94e5655e4d0bcfbe0d305e37e70596cd35fb

@ks-ci-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pixiake, samt42

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ks-ci-bot ks-ci-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 6, 2023
@ks-ci-bot ks-ci-bot merged commit 79d1949 into kubesphere:master Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants