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

Prevent deferred File.Close and wrap errors while putting CRDs into xpls provider package cache #343

Merged
merged 1 commit into from
Jul 12, 2023

Conversation

ulucinar
Copy link
Contributor

@ulucinar ulucinar commented Jul 10, 2023

Description of your changes

Removes the deferred File.Close call in cache.entry.writeObjects to decrease the # of simultaneously open files and wraps the error returned from cache.entry.appendToPackageJSON to provide more context.

I have:

  • Read and followed Upbound's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR, as appropriate.

How has this code been tested

Tested with Go 1.18.10 on a test configuration package with the following command:

/tmp/up xpkg dep xpkg.upbound.io/upbound/provider-gcp

(I've initially observed this issue with up@v.17.0, which uses Go 1.18)

if fb == 0 {
return stats, errors.New(errFailedToCreateCRD)
if err := os.WriteFile(filepath.Join(e.location(), fmt.Sprintf(crdNameFmt, name)), yb, 0o600); err != nil {
return stats, errors.Wrap(err, errFailedToCreateCRD)
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you double checked that the filename is part of the errors from os.WriteFile?

Copy link
Contributor Author

@ulucinar ulucinar Jul 11, 2023

Choose a reason for hiding this comment

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

Hi @sttts,
Thanks. I think the file being processed is not always part of the error message coming from os.WriteFile. In my case, for instance, it was just too many open files. Added the cache entry's path into the error's context.

}

if err := e.appendToPackageJSON(jb); err != nil {
return stats, errors.New(errFailedToCreateCRD)
return stats, errors.Wrap(err, errFailedToCreateCRD)
Copy link
Contributor

Choose a reason for hiding this comment

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

that error is failed to create crd. Can a user understand what it means?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I've replaced that error message as failed to create a cache entry for the CRD at path: %s. I did not include the CRD name in the error message as the filename of the cache entry already has that information.


if fb == 0 {
return stats, errors.New(errFailedToCreateCRD)
if err := os.WriteFile(filepath.Join(e.location(), fmt.Sprintf(crdNameFmt, name)), yb, 0o600); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I think os.Writefile is going to side step the use of afero.Fs here. We can fix that up by using afero's API xref https://github.com/spf13/afero#using-aferos-utility-functions. So something like this:

Suggested change
if err := os.WriteFile(filepath.Join(e.location(), fmt.Sprintf(crdNameFmt, name)), yb, 0o600); err != nil {
if err := afero.WriteFile(e.fs, filepath.Join(e.location(), fmt.Sprintf(crdNameFmt, name)), yb, 0o600); err != nil {

may work. Here's another example of doing that in this repo https://github.com/upbound/up/blob/main/internal/xpkg/build_test.go#L171.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @tnthornton,
Thanks, done.

@ulucinar ulucinar force-pushed the xpkg-dep-cache branch 2 times, most recently from 77cca22 to b7ffca2 Compare July 11, 2023 07:47
errNoObjectsToFlushToDisk = "no objects to flush"
errFailedToCreateMeta = "failed to create meta file in entry"
errFailedToCreateImageMeta = "failed to create image meta entry"
errFailedToCreateCacheEntry = "failed to create a cache entry for the CRD at path: %s"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I believe to have seen a Fmt suffix in most places if it is a format.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Yes, that's the convention. Thank you for pointing that out.

errFailedToCreateMeta = "failed to create meta file in entry"
errFailedToCreateImageMeta = "failed to create image meta entry"
errFailedToCreateCacheEntry = "failed to create a cache entry for the CRD at path: %s"
errFailedToAppendCRD = "failed to add the CRD to the package: %s"
Copy link
Contributor

Choose a reason for hiding this comment

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

this won't look good with errors.Wrapf: failed to add the CRD to the package: package-name: some error. Would rather use:

Suggested change
errFailedToAppendCRD = "failed to add the CRD to the package: %s"
errFailedToAppendCRD = "failed to add the CRD to package %q"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

…ls provider package cache

Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
Copy link
Member

@tnthornton tnthornton left a comment

Choose a reason for hiding this comment

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

Thanks @ulucinar ! My concern was addressed, so LGTM 👍

@sttts
Copy link
Contributor

sttts commented Jul 11, 2023

🚢

@ulucinar ulucinar changed the title Prevent deferred File.Close and wrap error while putting CRDs into xpls provider package cache Prevent deferred File.Close and wrap errors while putting CRDs into xpls provider package cache Jul 12, 2023
@ulucinar ulucinar merged commit 4dc7070 into upbound:main Jul 12, 2023
6 checks passed
@ulucinar ulucinar deleted the xpkg-dep-cache branch July 12, 2023 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants