Skip to content

Commit

Permalink
fix: Copa hangs when InstallUpdates fails (#508)
Browse files Browse the repository at this point in the history
Signed-off-by: ashnamehrotra <ashnamehrotra@gmail.com>
Co-authored-by: Sertaç Özercan <852750+sozercan@users.noreply.github.com>
  • Loading branch information
ashnamehrotra and sozercan authored Feb 20, 2024
1 parent 5274edf commit 0b98f33
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Patch(ctx context.Context, timeout time.Duration, image, reportFile, patche

ch := make(chan error)
go func() {
ch <- patchWithContext(timeoutCtx, image, reportFile, patchedTag, workingFolder, scanner, format, output, ignoreError, bkOpts)
ch <- patchWithContext(timeoutCtx, ch, image, reportFile, patchedTag, workingFolder, scanner, format, output, ignoreError, bkOpts)
}()

select {
Expand All @@ -70,7 +70,7 @@ func removeIfNotDebug(workingFolder string) {
}
}

func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workingFolder, scanner, format, output string, ignoreError bool, bkOpts buildkit.Opts) error {
func patchWithContext(ctx context.Context, ch chan error, image, reportFile, patchedTag, workingFolder, scanner, format, output string, ignoreError bool, bkOpts buildkit.Opts) error {
imageName, err := reference.ParseNormalizedNamed(image)
if err != nil {
return err
Expand All @@ -81,7 +81,7 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin
}
taggedName, ok := imageName.(reference.Tagged)
if !ok {
err := errors.New("unexpected: TagNameOnly did create Tagged ref")
err := errors.New("unexpected: TagNameOnly did not create Tagged ref")
log.Error(err)
return err
}
Expand Down Expand Up @@ -156,31 +156,35 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin
return err
}

ch := make(chan *client.SolveStatus)
buildChannel := make(chan *client.SolveStatus)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
_, err := bkClient.Build(ctx, solveOpt, copaProduct, func(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
// Configure buildctl/client for use by package manager
config, err := buildkit.InitializeBuildkitConfig(ctx, c, imageName.String(), updates)
if err != nil {
ch <- err
return nil, err
}

// Create package manager helper
pkgmgr, err := pkgmgr.GetPackageManager(updates.Metadata.OS.Type, config, workingFolder)
if err != nil {
ch <- err
return nil, err
}

// Export the patched image state to Docker
// TODO: Add support for other output modes as buildctl does.
patchedImageState, errPkgs, err := pkgmgr.InstallUpdates(ctx, updates, ignoreError)
if err != nil {
ch <- err
return nil, err
}

def, err := patchedImageState.Marshal(ctx)
if err != nil {
ch <- err
return nil, err
}

Expand All @@ -191,6 +195,7 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin

res.AddMeta(exptypes.ExporterImageConfigKey, config.ConfigData)
if err != nil {
ch <- err
return nil, err
}

Expand All @@ -215,12 +220,13 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin
// vex document must contain at least one statement
if output != "" && len(validatedManifest.Updates) > 0 {
if err := vex.TryOutputVexDocument(validatedManifest, pkgmgr, patchedImageName, format, output); err != nil {
ch <- err
return nil, err
}
}

return res, nil
}, ch)
}, buildChannel)

return err
})
Expand All @@ -231,7 +237,7 @@ func patchWithContext(ctx context.Context, image, reportFile, patchedTag, workin
c = cn
}
// not using shared context to not disrupt display but let us finish reporting errors
_, err = progressui.DisplaySolveStatus(context.TODO(), c, os.Stdout, ch)
_, err = progressui.DisplaySolveStatus(context.TODO(), c, os.Stdout, buildChannel)
return err
})

Expand Down

0 comments on commit 0b98f33

Please sign in to comment.