Skip to content

Commit 70d88dd

Browse files
committed
Move GSFetcher to internal/client/gcp package
1 parent 7ebf62c commit 70d88dd

File tree

3 files changed

+76
-52
lines changed

3 files changed

+76
-52
lines changed

internal/client/fetcher.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"path"
2525
"strings"
2626

27-
gcs "cloud.google.com/go/storage"
2827
"github.com/transparency-dev/tessera/api/layout"
2928
"k8s.io/klog/v2"
3029
)
@@ -124,56 +123,6 @@ func (f FileFetcher) ReadEntryBundle(_ context.Context, i uint64, p uint8) ([]by
124123
return os.ReadFile(path.Join(f.Root, ctEntriesPath(i, p)))
125124
}
126125

127-
// NewGSFetcher creates a new GSFetcher for the Google Cloud Storage bucket, using
128-
// the provided GCS client.
129-
//
130-
// bucket should not contain any slash.
131-
// c may be nil, in which case a new GCS client will be used.
132-
func NewGSFetcher(ctx context.Context, bucket string, c *gcs.Client) (*GSFetcher, error) {
133-
if c == nil {
134-
var err error
135-
c, err = gcs.NewClient(ctx, gcs.WithJSONReads())
136-
if err != nil {
137-
return nil, err
138-
}
139-
}
140-
return &GSFetcher{
141-
bucket: bucket,
142-
c: c,
143-
}, nil
144-
}
145-
146-
// GSFetcher knows how to fetch log artifacts from a Google Cloud Storage bucket.
147-
type GSFetcher struct {
148-
bucket string
149-
c *gcs.Client
150-
}
151-
152-
func (f GSFetcher) fetch(ctx context.Context, p string) ([]byte, error) {
153-
r, err := f.c.Bucket(f.bucket).Object(p).NewReader(ctx)
154-
if err != nil {
155-
return nil, fmt.Errorf("getObject: failed to create reader for object %q in bucket %q: %w", p, f.bucket, err)
156-
}
157-
158-
d, err := io.ReadAll(r)
159-
if err != nil {
160-
return nil, fmt.Errorf("failed to read %q: %v", p, err)
161-
}
162-
return d, r.Close()
163-
}
164-
165-
func (f GSFetcher) ReadCheckpoint(ctx context.Context) ([]byte, error) {
166-
return f.fetch(ctx, layout.CheckpointPath)
167-
}
168-
169-
func (f GSFetcher) ReadTile(ctx context.Context, l, i uint64, p uint8) ([]byte, error) {
170-
return f.fetch(ctx, layout.TilePath(l, i, p))
171-
}
172-
173-
func (f GSFetcher) ReadEntryBundle(ctx context.Context, i uint64, p uint8) ([]byte, error) {
174-
return f.fetch(ctx, ctEntriesPath(i, p))
175-
}
176-
177126
func ctEntriesPath(n uint64, p uint8) string {
178127
return fmt.Sprintf("tile/data/%s", layout.NWithSuffix(0, n, p))
179128
}

internal/client/gcp/fetcher.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2025 The Tessera authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package gcp
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"io"
21+
22+
gcs "cloud.google.com/go/storage"
23+
"github.com/transparency-dev/tessera/api/layout"
24+
)
25+
26+
// NewGSFetcher creates a new GSFetcher for the Google Cloud Storage bucket, using
27+
// the provided GCS client.
28+
//
29+
// bucket should not contain any slash.
30+
// c may be nil, in which case a new GCS client will be used.
31+
func NewGSFetcher(ctx context.Context, bucket string, c *gcs.Client) (*GSFetcher, error) {
32+
if c == nil {
33+
var err error
34+
c, err = gcs.NewClient(ctx, gcs.WithJSONReads())
35+
if err != nil {
36+
return nil, err
37+
}
38+
}
39+
return &GSFetcher{
40+
bucket: bucket,
41+
c: c,
42+
}, nil
43+
}
44+
45+
// GSFetcher knows how to fetch log artifacts from a Google Cloud Storage bucket.
46+
type GSFetcher struct {
47+
bucket string
48+
c *gcs.Client
49+
}
50+
51+
func (f GSFetcher) fetch(ctx context.Context, p string) ([]byte, error) {
52+
r, err := f.c.Bucket(f.bucket).Object(p).NewReader(ctx)
53+
if err != nil {
54+
return nil, fmt.Errorf("getObject: failed to create reader for object %q in bucket %q: %w", p, f.bucket, err)
55+
}
56+
57+
d, err := io.ReadAll(r)
58+
if err != nil {
59+
return nil, fmt.Errorf("failed to read %q: %v", p, err)
60+
}
61+
return d, r.Close()
62+
}
63+
64+
func (f GSFetcher) ReadCheckpoint(ctx context.Context) ([]byte, error) {
65+
return f.fetch(ctx, layout.CheckpointPath)
66+
}
67+
68+
func (f GSFetcher) ReadTile(ctx context.Context, l, i uint64, p uint8) ([]byte, error) {
69+
return f.fetch(ctx, layout.TilePath(l, i, p))
70+
}
71+
72+
func (f GSFetcher) ReadEntryBundle(ctx context.Context, i uint64, p uint8) ([]byte, error) {
73+
return f.fetch(ctx, fmt.Sprintf("tile/data/%s", layout.NWithSuffix(0, i, p)))
74+
}

internal/hammer/loadtest/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/transparency-dev/tesseract/internal/client"
32+
"github.com/transparency-dev/tesseract/internal/client/gcp"
3233
"github.com/transparency-dev/tesseract/internal/types/rfc6962"
3334
"github.com/transparency-dev/tesseract/internal/types/staticct"
3435
"k8s.io/klog/v2"
@@ -103,7 +104,7 @@ func newFetcher(ctx context.Context, root *url.URL, bearerToken string) fetcher
103104
case "file":
104105
return client.FileFetcher{Root: root.Path}
105106
case "gs":
106-
c, err := client.NewGSFetcher(ctx, root.Host, nil)
107+
c, err := gcp.NewGSFetcher(ctx, root.Host, nil)
107108
if err != nil {
108109
klog.Exitf("NewGSFetcher: %v", err)
109110
}

0 commit comments

Comments
 (0)