-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathgcs.go
More file actions
114 lines (94 loc) · 2.9 KB
/
gcs.go
File metadata and controls
114 lines (94 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package registry
import (
"context"
"errors"
"io"
"net/url"
"strings"
"time"
"cloud.google.com/go/storage"
"google.golang.org/protobuf/proto"
"github.com/feast-dev/feast/go/protos/feast/core"
)
// GCSObjectReader defines the interface for reading GCS objects to allow mocking in tests.
type GCSObjectReader interface {
GetObject(ctx context.Context, bucket string, object string) (io.ReadCloser, error)
DeleteObject(ctx context.Context, bucket string, object string) error
}
// GCSClient implements GCSObjectReader using the real GCS SDK.
type GCSClient struct {
client *storage.Client
}
func (g *GCSClient) GetObject(ctx context.Context, bucket string, object string) (io.ReadCloser, error) {
return g.client.Bucket(bucket).Object(object).NewReader(ctx)
}
func (g *GCSClient) DeleteObject(ctx context.Context, bucket string, object string) error {
return g.client.Bucket(bucket).Object(object).Delete(ctx)
}
// GCSRegistryStore is a GCS bucket-based implementation of the RegistryStore interface.
type GCSRegistryStore struct {
registryPath string
client GCSObjectReader
}
// NewGCSRegistryStore creates a GCSRegistryStore with the given configuration.
func NewGCSRegistryStore(config *RegistryConfig, repoPath string) *GCSRegistryStore {
var rs GCSRegistryStore
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, err := storage.NewClient(ctx)
if err != nil {
rs = GCSRegistryStore{
registryPath: config.Path,
}
} else {
rs = GCSRegistryStore{
registryPath: config.Path,
client: &GCSClient{client: client},
}
}
return &rs
}
// GetRegistryProto reads and parses the registry proto from the GCS bucket object.
func (g *GCSRegistryStore) GetRegistryProto() (*core.Registry, error) {
bucket, object, err := g.parseGCSPath()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
reader, err := g.client.GetObject(ctx, bucket, object)
if err != nil {
return nil, err
}
defer reader.Close()
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
registry := &core.Registry{}
if err := proto.Unmarshal(data, registry); err != nil {
return nil, err
}
return registry, nil
}
func (g *GCSRegistryStore) UpdateRegistryProto(rp *core.Registry) error {
return errors.New("not implemented in GCSRegistryStore")
}
func (g *GCSRegistryStore) Teardown() error {
bucket, object, err := g.parseGCSPath()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return g.client.DeleteObject(ctx, bucket, object)
}
func (g *GCSRegistryStore) parseGCSPath() (string, string, error) {
uri, err := url.Parse(g.registryPath)
if err != nil {
return "", "", errors.New("invalid GCS registry path format")
}
bucket := uri.Host
object := strings.TrimPrefix(uri.Path, "/")
return bucket, object, nil
}