Skip to content

Commit b8ab9d8

Browse files
committed
Add and implement zonal buckets config option.
Signed-off-by: NickGoog <[email protected]>
1 parent a4a90fa commit b8ab9d8

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ config:
373373
bucket: ""
374374
service_account: ""
375375
use_grpc: false
376+
use_zonal_buckets: false
376377
grpc_conn_pool_size: 0
377378
http_config:
378379
idle_conn_timeout: 0s

providers/gcs/gcs.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"cloud.google.com/go/storage"
18+
"cloud.google.com/go/storage/experimental"
1819
"github.com/go-kit/log"
1920
"github.com/pkg/errors"
2021
"github.com/prometheus/common/version"
@@ -42,6 +43,10 @@ type Config struct {
4243
Bucket string `yaml:"bucket"`
4344
ServiceAccount string `yaml:"service_account"`
4445
UseGRPC bool `yaml:"use_grpc"`
46+
// Both upload and download paths will use zonal bucket gRPC APIs by default.
47+
// Zonal buckets are currently allowlisted; please contact your Google account
48+
// manager if interested.
49+
UseZonalBuckets bool `yaml:"use_zonal_buckets"`
4550
// GRPCConnPoolSize controls the size of the gRPC connection pool and should only be used
4651
// when direct path is not enabled.
4752
// See https://pkg.go.dev/cloud.google.com/go/storage#hdr-Experimental_gRPC_API for more details
@@ -164,8 +169,14 @@ func newBucket(ctx context.Context, logger log.Logger, gc Config, opts []option.
164169
opts = append(opts,
165170
option.WithGRPCConnectionPool(gc.GRPCConnPoolSize),
166171
)
172+
if gc.UseZonalBuckets {
173+
opts = append(opts, experimental.WithZonalBucketAPIs())
174+
}
167175
gcsClient, err = storage.NewGRPCClient(ctx, opts...)
168176
} else {
177+
if gc.UseZonalBuckets {
178+
return nil, errors.New("Zonal buckets require `use_grpc: true`")
179+
}
169180
gcsClient, err = storage.NewClient(ctx, opts...)
170181
}
171182
if err != nil {

providers/gcs/gcs_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,54 @@ func TestNewBucketWithConfig_ShouldCreateGRPC(t *testing.T) {
7474
testutil.Assert(t, bkt != nil, "expected bucket to be created")
7575
}
7676

77+
func TestNewBucketWithConfig_ShouldCreateZonalBucket(t *testing.T) {
78+
cfg := Config{
79+
Bucket: "test-bucket",
80+
ServiceAccount: "",
81+
UseGRPC: true,
82+
UseZonalBuckets: true,
83+
}
84+
85+
// Note, the emulator does not currently have any specific zonal bucket logic.
86+
svr, err := gcsemu.NewServer("127.0.0.1:0", gcsemu.Options{})
87+
testutil.Ok(t, err)
88+
err = os.Setenv("STORAGE_EMULATOR_HOST", svr.Addr)
89+
testutil.Ok(t, err)
90+
err = os.Setenv("GCS_EMULATOR_HOST", svr.Addr)
91+
testutil.Ok(t, err)
92+
err = os.Setenv("STORAGE_EMULATOR_HOST_GRPC", svr.Addr)
93+
testutil.Ok(t, err)
94+
95+
bkt, err := NewBucketWithConfig(context.Background(), log.NewNopLogger(), cfg, "test-bucket", nil)
96+
testutil.Ok(t, err)
97+
98+
// Check if the bucket is created.
99+
testutil.Assert(t, bkt != nil, "expected bucket to be created")
100+
}
101+
102+
func TestNewBucketWithConfig_ShouldNotCreateZonalBucketWithoutGRPC(t *testing.T) {
103+
cfg := Config{
104+
Bucket: "test-bucket",
105+
ServiceAccount: "",
106+
UseGRPC: false,
107+
UseZonalBuckets: true,
108+
}
109+
110+
// Note, the emulator does not currently have any specific zonal bucket logic.
111+
svr, err := gcsemu.NewServer("127.0.0.1:0", gcsemu.Options{})
112+
testutil.Ok(t, err)
113+
err = os.Setenv("STORAGE_EMULATOR_HOST", svr.Addr)
114+
testutil.Ok(t, err)
115+
err = os.Setenv("GCS_EMULATOR_HOST", svr.Addr)
116+
testutil.Ok(t, err)
117+
err = os.Setenv("STORAGE_EMULATOR_HOST_GRPC", svr.Addr)
118+
testutil.Ok(t, err)
119+
120+
bkt, err := NewBucketWithConfig(context.Background(), log.NewNopLogger(), cfg, "test-bucket", nil)
121+
testutil.Assert(t, bkt == nil, "did not expect bucket to be created")
122+
testutil.Equals(t, err.Error(), "Zonal buckets require `use_grpc: true`")
123+
}
124+
77125
func TestParseConfig_ChunkSize(t *testing.T) {
78126
for _, tc := range []struct {
79127
name string

0 commit comments

Comments
 (0)