Skip to content

Commit

Permalink
added using credentials for gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivek Reddy committed Oct 13, 2024
1 parent dcc88b9 commit 07cafe7
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions test/testenv/gcputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -14,7 +15,9 @@ import (

"cloud.google.com/go/storage"
"github.com/google/uuid"
"golang.org/x/oauth2/google"
"google.golang.org/api/iterator"
"google.golang.org/api/option"

logf "sigs.k8s.io/controller-runtime/pkg/log"
)
Expand Down Expand Up @@ -51,12 +54,32 @@ type GCPClient struct {

// NewGCPClient initializes and returns a GCPClient
func NewGCPClient() (*GCPClient, error) {
var err error
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
logf.Log.Error(err, "Failed to create GCP Storage client")
return nil, err
var client *storage.Client
gcpCredentials := os.Getenv("GCP_SERVICE_ACCOUNT_KEY")

if len(gcpCredentials) == 0 {
client, err = storage.NewClient(ctx)
if err != nil {
logf.Log.Error(err, "Failed to create GCP Storage client")
return nil, err
}

} else {
var creds google.Credentials
err = json.Unmarshal([]byte(gcpCredentials), &creds)
if err != nil {
logf.Log.Error(err, "Secret key.json value is not parsable")
return nil, err
}
client, err = storage.NewClient(ctx, option.WithCredentials(&creds))
if err != nil {
logf.Log.Error(err, "Failed to create GCP Storage client")
return nil, err
}
}

return &GCPClient{
Client: client,
Ctx: ctx,
Expand Down

0 comments on commit 07cafe7

Please sign in to comment.