Skip to content

Commit 34b9f71

Browse files
committed
gcs: add endpoint configuration option
Add an Endpoint field to the GCS bucket configuration that allows specifying a custom GCS API endpoint. This is useful for testing with emulators or connecting to GCS-compatible storage services. The endpoint is passed through as a google.golang.org/api/option WithEndpoint option when creating the GCS client. Also updates README.md to document the new configuration field. Fixes: thanos-io/thanos#7862 Signed-off-by: Rohan Sood <56945243+rohansood10@users.noreply.github.com>
1 parent 4e5fd42 commit 34b9f71

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ config:
392392
disable_compression: false
393393
chunk_size_bytes: 0
394394
max_retries: 0
395+
endpoint: ""
395396
prefix: ""
396397
```
397398

providers/gcs/gcs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ type Config struct {
5959
// Overrides the default gcs storage client behavior if this value is greater than 0.
6060
// Set this to 1 to disable retries.
6161
MaxRetries int `yaml:"max_retries"`
62+
63+
// Endpoint specifies the GCS API endpoint to use.
64+
// This can be used to point to GCS regional endpoints or GCS-compatible storage backends.
65+
// See https://cloud.google.com/storage/docs/regional-endpoints for more details.
66+
Endpoint string `yaml:"endpoint"`
6267
}
6368

6469
// Bucket implements the store.Bucket and shipper.Bucket interfaces against GCS.
@@ -113,6 +118,10 @@ func NewBucketWithConfig(ctx context.Context, logger log.Logger, gc Config, comp
113118
option.WithUserAgent(fmt.Sprintf("thanos-%s/%s (%s)", component, version.Version, runtime.Version())),
114119
)
115120

121+
if gc.Endpoint != "" {
122+
opts = append(opts, option.WithEndpoint(gc.Endpoint))
123+
}
124+
116125
if !gc.UseGRPC {
117126
var err error
118127
opts, err = appendHttpOptions(gc, opts, wrapRoundtripper)

providers/gcs/gcs_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,55 @@ http_config:
159159
}
160160
}
161161

162+
func TestParseConfig_Endpoint(t *testing.T) {
163+
for _, tc := range []struct {
164+
name string
165+
input string
166+
assertions func(cfg Config)
167+
}{
168+
{
169+
name: "DefaultEndpoint",
170+
input: `bucket: abcd`,
171+
assertions: func(cfg Config) {
172+
testutil.Equals(t, "", cfg.Endpoint)
173+
},
174+
},
175+
{
176+
name: "CustomEndpoint",
177+
input: `bucket: abcd
178+
endpoint: https://storage.europe-west3.rep.googleapis.com`,
179+
assertions: func(cfg Config) {
180+
testutil.Equals(t, "https://storage.europe-west3.rep.googleapis.com", cfg.Endpoint)
181+
},
182+
},
183+
} {
184+
t.Run(tc.name, func(t *testing.T) {
185+
cfg, err := parseConfig([]byte(tc.input))
186+
testutil.Ok(t, err)
187+
tc.assertions(cfg)
188+
})
189+
}
190+
}
191+
192+
func TestNewBucketWithConfig_CustomEndpoint(t *testing.T) {
193+
svr, err := gcsemu.NewServer("127.0.0.1:0", gcsemu.Options{})
194+
testutil.Ok(t, err)
195+
defer svr.Close()
196+
197+
err = os.Setenv("STORAGE_EMULATOR_HOST", svr.Addr)
198+
testutil.Ok(t, err)
199+
200+
cfg := Config{
201+
Bucket: "test-bucket",
202+
Endpoint: "https://storage.europe-west3.rep.googleapis.com",
203+
noAuth: true,
204+
}
205+
206+
bkt, err := NewBucketWithConfig(context.Background(), log.NewNopLogger(), cfg, "test", nil)
207+
testutil.Ok(t, err)
208+
testutil.Assert(t, bkt != nil, "expected bucket to be created with custom endpoint")
209+
}
210+
162211
func TestNewBucketWithErrorRoundTripper(t *testing.T) {
163212
cfg := Config{
164213
Bucket: "test-bucket",

0 commit comments

Comments
 (0)