GCP: accept context from the caller in all methods

Modify all relevant methods in the internal GCP library to accept
context from the caller.

Modify all places which call the internal GCP library methods to pass
the context.

Signed-off-by: Tomas Hozza <thozza@redhat.com>
This commit is contained in:
Tomas Hozza 2021-03-12 16:45:26 +01:00 committed by Tomas Hozza
parent 4c3a30f035
commit 6d51d285cf
5 changed files with 49 additions and 38 deletions

View file

@ -271,6 +271,8 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
osbuildJobResult.Success = true
osbuildJobResult.UploadStatus = "success"
case *target.GCPTargetOptions:
ctx := context.Background()
g, err := gcp.New(impl.GCPCreds)
if err != nil {
appendTargetError(osbuildJobResult, err)
@ -278,7 +280,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
}
log.Printf("[GCP] 🚀 Uploading image to: %s/%s", options.Bucket, options.Object)
_, err = g.StorageObjectUpload(path.Join(outputDirectory, options.Filename),
_, err = g.StorageObjectUpload(ctx, path.Join(outputDirectory, options.Filename),
options.Bucket, options.Object, map[string]string{gcp.MetadataKeyImageName: args.Targets[0].ImageName})
if err != nil {
appendTargetError(osbuildJobResult, err)
@ -286,7 +288,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
}
log.Printf("[GCP] 📥 Importing image into Compute Node as '%s'", args.Targets[0].ImageName)
imageBuild, importErr := g.ComputeImageImport(options.Bucket, options.Object, args.Targets[0].ImageName, options.Os, options.Region)
imageBuild, importErr := g.ComputeImageImport(ctx, options.Bucket, options.Object, args.Targets[0].ImageName, options.Os, options.Region)
if imageBuild != nil {
log.Printf("[GCP] 📜 Image import log URL: %s", imageBuild.LogUrl)
log.Printf("[GCP] 🎉 Image import finished with status: %s", imageBuild.Status)
@ -294,11 +296,11 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
// Cleanup storage before checking for errors
log.Printf("[GCP] 🧹 Deleting uploaded image file: %s/%s", options.Bucket, options.Object)
if err = g.StorageObjectDelete(options.Bucket, options.Object); err != nil {
if err = g.StorageObjectDelete(ctx, options.Bucket, options.Object); err != nil {
log.Printf("[GCP] Encountered error while deleting object: %v", err)
}
deleted, errs := g.StorageImageImportCleanup(args.Targets[0].ImageName)
deleted, errs := g.StorageImageImportCleanup(ctx, args.Targets[0].ImageName)
for _, d := range deleted {
log.Printf("[GCP] 🧹 Deleted image import job file '%s'", d)
}
@ -315,7 +317,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
if len(options.ShareWithAccounts) > 0 {
log.Printf("[GCP] 🔗 Sharing the image with: %+v", options.ShareWithAccounts)
err = g.ComputeImageShare(args.Targets[0].ImageName, options.ShareWithAccounts)
err = g.ComputeImageShare(ctx, args.Targets[0].ImageName, options.ShareWithAccounts)
if err != nil {
appendTargetError(osbuildJobResult, err)
return nil