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

fix: parallelize inventory verification #61

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 30 additions & 17 deletions pkg/fs/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/rs/zerolog/log"
"sigs.k8s.io/bom/pkg/spdx"
Expand Down Expand Up @@ -92,7 +94,10 @@ func Verify(input, inventory, missing string) error {

mdoc := bom.NewDocument("", "")
mdoc.Name = "missing-files-document"
mcount := 0

var mcount atomic.Uint64

var wg sync.WaitGroup

for _, entry := range inv.Entries {
mode, err := strconv.ParseInt(entry.Mode, 8, 32)
Expand All @@ -106,26 +111,34 @@ func Verify(input, inventory, missing string) error {
continue
}

if err := checkBOM(input, entry.Path); err != nil {
log.Error().Err(err).Str("path", entry.Path).Msg("inventory verify failed")
mcount++
sfile := spdx.NewFile()
sfile.SetEntity(
&spdx.Entity{
Name: entry.Path,
Checksum: map[string]string{"SHA256": strings.Split(entry.Checksum, ":")[1]},
},
)
wg.Add(1)

if err := mdoc.AddFile(sfile); err != nil {
log.Error().Err(err).Msg("unable to add file to package")
go func(entry Entry) {
defer wg.Done()

return err
if err := checkBOM(input, entry.Path); err != nil {
log.Error().Err(err).Str("path", entry.Path).Msg("inventory verify failed")
mcount.Add(1)
sfile := spdx.NewFile()
sfile.SetEntity(
&spdx.Entity{
Name: entry.Path,
Checksum: map[string]string{"SHA256": strings.Split(entry.Checksum, ":")[1]},
},
)

if err := mdoc.AddFile(sfile); err != nil {
log.Error().Err(err).Msg("unable to add file to package")

return
}
}
}
}(entry)
}

if mcount != 0 {
wg.Wait()

if mcount.Load() != 0 {
if missing != "" {
if err := bom.WriteDocument(mdoc, missing); err != nil {
log.Error().Err(err).Str("path", missing).Msg("unable to writing missing entries")
Expand All @@ -134,7 +147,7 @@ func Verify(input, inventory, missing string) error {
}
}

return fmt.Errorf("%w: %d entries missing", errors.ErrIncomplete, mcount)
return fmt.Errorf("%w: %d entries missing", errors.ErrIncomplete, mcount.Load())
}

return nil
Expand Down
Loading