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
Merged
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
27 changes: 9 additions & 18 deletions internal/xpkg/dep/cache/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ const (
crdNameFmt = "%s.yaml"
delim = "\n"

errFailedToCreateMeta = "failed to create meta file in entry"
errFailedToCreateImageMeta = "faile to create image meta entry"
errFailedToCreateCRD = "failed to create crd"
errNoObjectsToFlushToDisk = "no objects to flush"
errFailedToCreateMeta = "failed to create meta file in entry"
errFailedToCreateImageMeta = "failed to create image meta entry"
errFailedToCreateCacheEntryFmt = "failed to create a cache entry for the CRD at path %s"
errFailedToAppendCRDFmt = "failed to add CRD %q to package"
errNoObjectsToFlushToDisk = "no objects to flush"
)

// entry is the internal representation of the cache at a given directory
Expand Down Expand Up @@ -246,23 +247,13 @@ func (e *entry) writeObjects(objs []runtime.Object) (*flushstats, error) { // no
continue
}

f, err := e.fs.Create(filepath.Join(e.location(), fmt.Sprintf(crdNameFmt, name)))
if err != nil {
return stats, err
}
defer f.Close() // nolint:errcheck

fb, err := f.Write(yb)
if err != nil {
return stats, err
}

if fb == 0 {
return stats, errors.New(errFailedToCreateCRD)
entryLocation := filepath.Join(e.location(), fmt.Sprintf(crdNameFmt, name))
if err := afero.WriteFile(e.fs, entryLocation, yb, 0o600); err != nil {
return stats, errors.Wrapf(err, errFailedToCreateCacheEntryFmt, entryLocation)
}

if err := e.appendToPackageJSON(jb); err != nil {
return stats, errors.New(errFailedToCreateCRD)
return stats, errors.Wrapf(err, errFailedToAppendCRDFmt, name)
}

inc()
Expand Down
Loading