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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
package config
import (
"errors"
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"gitlab.com/gitlab-org/gitaly/internal/config/auth"
"gitlab.com/gitlab-org/gitaly/internal/config/log"
"gitlab.com/gitlab-org/gitaly/internal/config/prometheus"
"gitlab.com/gitlab-org/gitaly/internal/config/sentry"
"gitlab.com/gitlab-org/gitaly/internal/praefect/models"
)
type Failover struct {
Enabled bool `toml:"enabled"`
ElectionStrategy string `toml:"election_strategy"`
}
// Config is a container for everything found in the TOML config file
type Config struct {
ListenAddr string `toml:"listen_addr"`
SocketPath string `toml:"socket_path"`
VirtualStorages []*VirtualStorage `toml:"virtual_storage"`
//TODO: Remove VirtualStorageName and Nodes once omnibus and gdk are updated with support for
// VirtualStorages
VirtualStorageName string `toml:"virtual_storage_name"`
Nodes []*models.Node `toml:"node"`
Logging log.Config `toml:"logging"`
Sentry sentry.Config `toml:"sentry"`
PrometheusListenAddr string `toml:"prometheus_listen_addr"`
Prometheus prometheus.Config `toml:"prometheus"`
Auth auth.Config `toml:"auth"`
DB `toml:"database"`
Failover Failover `toml:"failover"`
// Keep for legacy reasons: remove after Omnibus has switched
FailoverEnabled bool `toml:"failover_enabled"`
PostgresQueueEnabled bool `toml:"postgres_queue_enabled"`
}
// VirtualStorage represents a set of nodes for a storage
type VirtualStorage struct {
Name string `toml:"name"`
Nodes []*models.Node `toml:"node"`
}
// FromFile loads the config for the passed file path
func FromFile(filePath string) (Config, error) {
config := &Config{}
cfgFile, err := os.Open(filePath)
if err != nil {
return *config, err
}
defer cfgFile.Close()
_, err = toml.DecodeReader(cfgFile, config)
// TODO: Remove this after the virtual storages change is merged in omnibus
// and gdk. This is for backwards compatibility purposes only
if len(config.VirtualStorages) == 0 && config.VirtualStorageName != "" && len(config.Nodes) > 0 {
config.VirtualStorages = []*VirtualStorage{
&VirtualStorage{
Name: config.VirtualStorageName,
Nodes: config.Nodes,
},
}
config.VirtualStorageName = ""
config.Nodes = nil
}
// TODO: Remove this after failover_enabled has moved under a separate failover section. This is for
// backwards compatibility only
if config.FailoverEnabled {
config.Failover.Enabled = true
}
return *config, err
}
var (
errDuplicateStorage = errors.New("internal gitaly storages are not unique")
errGitalyWithoutAddr = errors.New("all gitaly nodes must have an address")
errGitalyWithoutStorage = errors.New("all gitaly nodes must have a storage")
errMoreThanOnePrimary = errors.New("only 1 node can be designated as a primary")
errNoGitalyServers = errors.New("no primary gitaly backends configured")
errNoListener = errors.New("no listen address or socket path configured")
errNoPrimaries = errors.New("no primaries designated")
errNoVirtualStorages = errors.New("no virtual storages configured")
errStorageAddressMismatch = errors.New("storages with the same name must have the same address")
errVirtualStoragesNotUnique = errors.New("virtual storages must have unique names")
)
// Validate establishes if the config is valid
func (c Config) Validate() error {
if c.ListenAddr == "" && c.SocketPath == "" {
return errNoListener
}
if len(c.VirtualStorages) == 0 {
return errNoVirtualStorages
}
allStorages := make(map[string]string)
virtualStorages := make(map[string]struct{})
for _, virtualStorage := range c.VirtualStorages {
if _, ok := virtualStorages[virtualStorage.Name]; ok {
return errVirtualStoragesNotUnique
}
virtualStorages[virtualStorage.Name] = struct{}{}
storages := make(map[string]struct{})
var primaries int
for _, node := range virtualStorage.Nodes {
if node.DefaultPrimary {
primaries++
}
if primaries > 1 {
return fmt.Errorf("virtual storage %s: %v", virtualStorage.Name, errMoreThanOnePrimary)
}
if node.Storage == "" {
return errGitalyWithoutStorage
}
if node.Address == "" {
return errGitalyWithoutAddr
}
if _, found := storages[node.Storage]; found {
return errDuplicateStorage
}
if address, found := allStorages[node.Storage]; found {
if address != node.Address {
return errStorageAddressMismatch
}
} else {
allStorages[node.Storage] = node.Address
}
storages[node.Storage] = struct{}{}
}
if primaries == 0 {
return fmt.Errorf("virtual storage %s: %v", virtualStorage.Name, errNoPrimaries)
}
if len(storages) == 0 {
return fmt.Errorf("virtual storage %s: %v", virtualStorage.Name, errNoGitalyServers)
}
}
return nil
}
// NeedsSQL returns true if the driver for SQL needs to be initialized
func (c Config) NeedsSQL() bool {
return c.PostgresQueueEnabled || (c.Failover.Enabled && c.Failover.ElectionStrategy == "sql")
}
// DB holds Postgres client configuration data.
type DB struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
DBName string `toml:"dbname"`
SSLMode string `toml:"sslmode"`
SSLCert string `toml:"sslcert"`
SSLKey string `toml:"sslkey"`
SSLRootCert string `toml:"sslrootcert"`
}
// ToPQString returns a connection string that can be passed to github.com/lib/pq.
func (db DB) ToPQString() string {
var fields []string
if db.Port > 0 {
fields = append(fields, fmt.Sprintf("port=%d", db.Port))
}
for _, kv := range []struct{ key, value string }{
{"host", db.Host},
{"user", db.User},
{"password", db.Password},
{"dbname", db.DBName},
{"sslmode", db.SSLMode},
{"sslcert", db.SSLCert},
{"sslkey", db.SSLKey},
{"sslrootcert", db.SSLRootCert},
{"binary_parameters", "yes"},
} {
if len(kv.value) == 0 {
continue
}
kv.value = strings.ReplaceAll(kv.value, "'", `\'`)
kv.value = strings.ReplaceAll(kv.value, " ", `\ `)
fields = append(fields, kv.key+"="+kv.value)
}
return strings.Join(fields, " ")
}
|