From b54b8fa3abf29567392b4492b5000546acd781a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Mon, 26 Sep 2022 18:30:49 +0200 Subject: [PATCH] worker/gcp: allow setting Bucket in worker configuration Extend the worker's configuration to allow setting GCP Bucket to use when uploading images to GCP. The value from the configuration is used only if not provided in the TargetOptions of the job. In GCP, the region of the bucket does not limit importing of the image to a particular region. So it is completely possible to use a single Bucket to import images to any and all regions. Return an error in case no bucket name was set in the job nor in the worker configuration. --- cmd/osbuild-worker/config.go | 1 + cmd/osbuild-worker/jobimpl-osbuild.go | 22 ++++++++++++++++------ cmd/osbuild-worker/main.go | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cmd/osbuild-worker/config.go b/cmd/osbuild-worker/config.go index d68a02e3b..b968f687f 100644 --- a/cmd/osbuild-worker/config.go +++ b/cmd/osbuild-worker/config.go @@ -23,6 +23,7 @@ type kojiServerConfig struct { type gcpConfig struct { Credentials string `toml:"credentials"` + Bucket string `toml:"bucket"` } type azureConfig struct { diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index 20b2ebc69..adb920a18 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -32,7 +32,8 @@ import ( ) type GCPConfiguration struct { - Creds string + Creds string + Bucket string } type S3Configuration struct { @@ -579,9 +580,18 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { break } - logWithId.Infof("[GCP] ๐Ÿš€ Uploading image to: %s/%s", targetOptions.Bucket, targetOptions.Object) + bucket := targetOptions.Bucket + if bucket == "" { + bucket = impl.GCPConfig.Bucket + if bucket == "" { + targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, "No GCP bucket provided", nil) + break + } + } + + logWithId.Infof("[GCP] ๐Ÿš€ Uploading image to: %s/%s", bucket, targetOptions.Object) _, err = g.StorageObjectUpload(ctx, path.Join(outputDirectory, jobTarget.OsbuildArtifact.ExportName, jobTarget.OsbuildArtifact.ExportFilename), - targetOptions.Bucket, targetOptions.Object, map[string]string{gcp.MetadataKeyImageName: jobTarget.ImageName}) + bucket, targetOptions.Object, map[string]string{gcp.MetadataKeyImageName: jobTarget.ImageName}) if err != nil { targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error(), nil) break @@ -589,14 +599,14 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { logWithId.Infof("[GCP] ๐Ÿ“ฅ Importing image into Compute Engine as '%s'", jobTarget.ImageName) - _, importErr := g.ComputeImageInsert(ctx, targetOptions.Bucket, targetOptions.Object, jobTarget.ImageName, []string{targetOptions.Region}, gcp.GuestOsFeaturesByDistro(targetOptions.Os)) + _, importErr := g.ComputeImageInsert(ctx, bucket, targetOptions.Object, jobTarget.ImageName, []string{targetOptions.Region}, gcp.GuestOsFeaturesByDistro(targetOptions.Os)) if importErr == nil { logWithId.Infof("[GCP] ๐ŸŽ‰ Image import finished successfully") } // Cleanup storage before checking for errors - logWithId.Infof("[GCP] ๐Ÿงน Deleting uploaded image file: %s/%s", targetOptions.Bucket, targetOptions.Object) - if err = g.StorageObjectDelete(ctx, targetOptions.Bucket, targetOptions.Object); err != nil { + logWithId.Infof("[GCP] ๐Ÿงน Deleting uploaded image file: %s/%s", bucket, targetOptions.Object) + if err = g.StorageObjectDelete(ctx, bucket, targetOptions.Object); err != nil { logWithId.Errorf("[GCP] Encountered error while deleting object: %v", err) } diff --git a/cmd/osbuild-worker/main.go b/cmd/osbuild-worker/main.go index 46dac35d6..03e321c97 100644 --- a/cmd/osbuild-worker/main.go +++ b/cmd/osbuild-worker/main.go @@ -354,6 +354,7 @@ func main() { var gcpConfig GCPConfiguration if config.GCP != nil { gcpConfig.Creds = config.GCP.Credentials + gcpConfig.Bucket = config.GCP.Bucket } // If the credentials are not provided in the configuration, then the