-
Notifications
You must be signed in to change notification settings - Fork 311
/
fileuploader.go
164 lines (143 loc) · 5.08 KB
/
fileuploader.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package fileuploader
import (
"context"
"fmt"
"sync"
"github.com/rudderlabs/rudder-server/config"
backendconfig "github.com/rudderlabs/rudder-server/config/backend-config"
"github.com/rudderlabs/rudder-server/services/filemanager"
)
type StorageSettings struct {
Bucket backendconfig.StorageBucket
Preferences backendconfig.StoragePreferences
}
// Provider is an interface that provides file managers and storage preferences for a given workspace.
type Provider interface {
// Gets a file manager for the given workspace.
GetFileManager(workspaceID string) (filemanager.FileManager, error)
// Gets the storage preferences for the given workspace.
GetStoragePreferences(workspaceID string) (backendconfig.StoragePreferences, error)
}
// NewProvider creates a new provider that updates its storage settings while backend configuration gets updated.
func NewProvider(ctx context.Context, config backendconfig.BackendConfig) Provider {
s := &provider{
init: make(chan struct{}),
storageSettings: make(map[string]StorageSettings),
}
go s.updateLoop(ctx, config)
return s
}
// NewStaticProvider creates a new provider that operates against a predefined storage settings.
// Useful for tests.
func NewStaticProvider(storageSettings map[string]StorageSettings) Provider {
s := &provider{
init: make(chan struct{}),
storageSettings: storageSettings,
}
close(s.init)
return s
}
// NewDefaultProvider creates a new provider that operates against the default storage settings populated from the env.
// Useful for tests that populate settings from env.
func NewDefaultProvider() Provider {
d := &defaultProvider{}
return d
}
type provider struct {
onceInit sync.Once
init chan struct{}
storageSettings map[string]StorageSettings
}
func (p *provider) GetFileManager(workspaceID string) (filemanager.FileManager, error) {
<-p.init
settings, ok := p.storageSettings[workspaceID]
if !ok {
return nil, fmt.Errorf("no storage settings found for workspace: %s", workspaceID)
}
return filemanager.DefaultFileManagerFactory.New(&filemanager.SettingsT{
Provider: settings.Bucket.Type,
Config: settings.Bucket.Config,
})
}
func (p *provider) GetStoragePreferences(workspaceID string) (backendconfig.StoragePreferences, error) {
<-p.init
var prefs backendconfig.StoragePreferences
settings, ok := p.storageSettings[workspaceID]
if !ok {
return prefs, fmt.Errorf("no storage settings found for workspace: %s", workspaceID)
}
return settings.Preferences, nil
}
// updateLoop uses backend config to retrieve & keep up-to-date the storage settings of all workspaces.
func (p *provider) updateLoop(ctx context.Context, backendConfig backendconfig.BackendConfig) {
ch := backendConfig.Subscribe(ctx, backendconfig.TopicBackendConfig)
settings := make(map[string]StorageSettings)
for ev := range ch {
configs := ev.Data.(map[string]backendconfig.ConfigT)
for workspaceId, c := range configs {
var bucket backendconfig.StorageBucket
var preferences backendconfig.StoragePreferences
if c.Settings.DataRetention.UseSelfStorage {
settings := c.Settings.DataRetention.StorageBucket
defaultBucket := getDefaultBucket(ctx, settings.Type)
bucket = overrideWithSettings(defaultBucket.Config, settings, workspaceId)
} else {
bucket = getDefaultBucket(ctx, config.GetString("JOBS_BACKUP_STORAGE_PROVIDER", "S3"))
}
// bucket type and configuration must not be empty
if bucket.Type != "" && len(bucket.Config) > 0 {
preferences = c.Settings.DataRetention.StoragePreferences
}
settings[workspaceId] = StorageSettings{
Bucket: bucket,
Preferences: preferences,
}
}
p.storageSettings = settings
p.onceInit.Do(func() {
close(p.init)
})
}
p.onceInit.Do(func() {
close(p.init)
})
}
type defaultProvider struct{}
func (*defaultProvider) GetFileManager(_ string) (filemanager.FileManager, error) {
defaultConfig := getDefaultBucket(context.Background(), config.GetString("JOBS_BACKUP_STORAGE_PROVIDER", "S3"))
return filemanager.DefaultFileManagerFactory.New(&filemanager.SettingsT{
Provider: defaultConfig.Type,
Config: defaultConfig.Config,
})
}
func (*defaultProvider) GetStoragePreferences(_ string) (backendconfig.StoragePreferences, error) {
return backendconfig.StoragePreferences{
ProcErrors: true,
GatewayDumps: true,
ProcErrorDumps: true,
RouterDumps: true,
BatchRouterDumps: true,
}, nil
}
func getDefaultBucket(ctx context.Context, provider string) backendconfig.StorageBucket {
return backendconfig.StorageBucket{
Type: provider,
Config: filemanager.GetProviderConfigFromEnv(ctx, provider),
}
}
func overrideWithSettings(defaultConfig map[string]interface{}, settings backendconfig.StorageBucket, workspaceID string) backendconfig.StorageBucket {
config := make(map[string]interface{})
for k, v := range defaultConfig {
config[k] = v
}
for k, v := range settings.Config {
config[k] = v
}
if _, ok := config["externalId"]; ok && settings.Type == "S3" {
config["externalId"] = workspaceID
}
return backendconfig.StorageBucket{
Type: settings.Type,
Config: config,
}
}