-
Notifications
You must be signed in to change notification settings - Fork 175
/
config.go
331 lines (293 loc) · 11.4 KB
/
config.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package config
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/ghodss/yaml"
"github.com/magiconair/properties"
"github.com/pkg/errors"
scyllaversionedclient "github.com/scylladb/scylla-operator/pkg/client/scylla/clientset/versioned"
"github.com/scylladb/scylla-operator/pkg/naming"
"github.com/scylladb/scylla-operator/pkg/semver"
"github.com/scylladb/scylla-operator/pkg/sidecar/identity"
"github.com/scylladb/scylla-operator/pkg/util/cpuset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
)
const (
configDirScylla = "/etc/scylla"
configDirScyllaD = "/etc/scylla.d"
scyllaYAMLPath = configDirScylla + "/" + naming.ScyllaConfigName
scyllaYAMLConfigMapPath = naming.ScyllaConfigDirName + "/" + naming.ScyllaConfigName
scyllaIOPropertiesPath = configDirScyllaD + "/" + naming.ScyllaIOPropertiesName
scyllaRackDCPropertiesPath = configDirScylla + "/" + naming.ScyllaRackDCPropertiesName
scyllaRackDCPropertiesConfigMapPath = naming.ScyllaConfigDirName + "/" + naming.ScyllaRackDCPropertiesName
entrypointPath = "/docker-entrypoint.py"
)
var scyllaJMXPaths = []string{"/usr/lib/scylla/jmx/scylla-jmx", "/opt/scylladb/jmx/scylla-jmx"}
type ScyllaConfig struct {
member *identity.Member
kubeClient kubernetes.Interface
scyllaClient scyllaversionedclient.Interface
scyllaRackDCPropertiesPath string
scyllaRackDCPropertiesConfigMapPath string
cpuCount int
}
func NewScyllaConfig(m *identity.Member, kubeClient kubernetes.Interface, scyllaClient scyllaversionedclient.Interface, cpuCount int) *ScyllaConfig {
return &ScyllaConfig{
member: m,
kubeClient: kubeClient,
scyllaClient: scyllaClient,
scyllaRackDCPropertiesPath: scyllaRackDCPropertiesPath,
scyllaRackDCPropertiesConfigMapPath: scyllaRackDCPropertiesConfigMapPath,
cpuCount: cpuCount,
}
}
func (s *ScyllaConfig) Setup(ctx context.Context) (*exec.Cmd, error) {
var err error
var cmd *exec.Cmd
klog.Info("Setting up scylla.yaml")
if err = s.setupScyllaYAML(scyllaYAMLPath, scyllaYAMLConfigMapPath); err != nil {
return nil, errors.Wrap(err, "failed to setup scylla.yaml")
}
klog.Info("Setting up cassandra-rackdc.properties")
if err = s.setupRackDCProperties(); err != nil {
return nil, errors.Wrap(err, "failed to setup rackdc properties file")
}
klog.Info("Setting up entrypoint script")
if cmd, err = s.setupEntrypoint(ctx); err != nil {
return nil, errors.Wrap(err, "failed to setup entrypoint")
}
return cmd, nil
}
// setupScyllaYAML edits the default scylla.yaml file with our custom options.
// We only edit the options that are not available to configure via the
// entrypoint script flags. Those options are:
// - cluster_name
// - rpc_address
// - endpoint_snitch
func (s *ScyllaConfig) setupScyllaYAML(configFilePath, configMapPath string) error {
// Read default scylla.yaml
configFileBytes, err := ioutil.ReadFile(configFilePath)
if err != nil {
return errors.Wrap(err, "failed to open scylla.yaml")
}
// Read config map scylla.yaml
configMapBytes, err := ioutil.ReadFile(configMapPath)
if err != nil {
klog.InfoS("no scylla.yaml config map available")
}
// Custom options
var cfg = make(map[string]interface{})
m := s.member
cfg["cluster_name"] = m.Cluster
cfg["rpc_address"] = "0.0.0.0"
cfg["endpoint_snitch"] = "GossipingPropertyFileSnitch"
overrideBytes, err := yaml.Marshal(cfg)
if err != nil {
return errors.Wrap(err, "failed to parse override options for scylla.yaml")
}
overwrittenBytes, err := mergeYAMLs(configFileBytes, overrideBytes)
if err != nil {
return errors.Wrap(err, "failed to merge scylla yaml with operator pre-sets")
}
customConfigBytesBytes, err := mergeYAMLs(overwrittenBytes, configMapBytes)
if err != nil {
return errors.Wrap(err, "failed to merge overwritten scylla yaml with user config map")
}
// Write result to file
if err = ioutil.WriteFile(configFilePath, customConfigBytesBytes, os.ModePerm); err != nil {
return errors.Wrap(err, "error trying to write scylla.yaml")
}
return nil
}
func (s *ScyllaConfig) setupRackDCProperties() error {
suppliedProperties := loadProperties(s.scyllaRackDCPropertiesConfigMapPath)
rackDCProperties := createRackDCProperties(suppliedProperties, s.member.Datacenter, s.member.Rack)
f, err := os.Create(s.scyllaRackDCPropertiesPath)
if err != nil {
return errors.Wrap(err, "error trying to create cassandra-rackdc.properties")
}
if _, err := rackDCProperties.Write(f, properties.UTF8); err != nil {
return errors.Wrap(err, "error trying to write cassandra-rackdc.properties")
}
return nil
}
func createRackDCProperties(suppliedProperties *properties.Properties, dc, rack string) *properties.Properties {
suppliedProperties.DisableExpansion = true
rackDCProperties := properties.NewProperties()
rackDCProperties.DisableExpansion = true
rackDCProperties.Set("dc", dc)
rackDCProperties.Set("rack", rack)
rackDCProperties.Set("prefer_local", suppliedProperties.GetString("prefer_local", "false"))
if dcSuffix, ok := suppliedProperties.Get("dc_suffix"); ok {
rackDCProperties.Set("dc_suffix", dcSuffix)
}
return rackDCProperties
}
func loadProperties(fileName string) *properties.Properties {
l := &properties.Loader{Encoding: properties.UTF8}
p, err := l.LoadFile(scyllaRackDCPropertiesConfigMapPath)
if err != nil {
klog.InfoS("unable to read properties", "file", fileName)
return properties.NewProperties()
}
return p
}
var scyllaArgumentsRegexp = regexp.MustCompile(`--([^= ]+)(="[^"]+"|=\S+|\s+"[^"]+"|\s+[^\s-]+|\s+-?\d*\.?\d+[^\s-]+|)`)
func convertScyllaArguments(scyllaArguments string) map[string]string {
output := make(map[string]string)
for _, value := range scyllaArgumentsRegexp.FindAllStringSubmatch(scyllaArguments, -1) {
if value[2] == "" {
output[value[1]] = ""
} else if value[2][0] == '=' {
output[value[1]] = strings.TrimSpace(value[2][1:])
} else {
output[value[1]] = strings.TrimSpace(value[2])
}
}
return output
}
func appendScyllaArguments(ctx context.Context, s *ScyllaConfig, scyllaArgs string, scyllaFinalArgs map[string]*string) {
for argName, argValue := range convertScyllaArguments(scyllaArgs) {
if existing := scyllaFinalArgs[argName]; existing == nil {
scyllaFinalArgs[argName] = pointer.StringPtr(strings.TrimSpace(argValue))
} else {
klog.Infof("ScyllaArgs: argument '%s' is ignored, it is already in the list", argName)
}
}
}
func (s *ScyllaConfig) setupEntrypoint(ctx context.Context) (*exec.Cmd, error) {
m := s.member
// Get seeds
seed, err := m.GetSeed(ctx, s.kubeClient.CoreV1())
if err != nil {
return nil, errors.Wrap(err, "error getting seeds")
}
// Check if we need to run in developer mode
devMode := "0"
cluster, err := s.scyllaClient.ScyllaV1().ScyllaClusters(s.member.Namespace).Get(ctx, s.member.Cluster, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrap(err, "error getting cluster")
}
if cluster.Spec.DeveloperMode {
devMode = "1"
}
overprovisioned := "0"
if m.Overprovisioned {
overprovisioned = "1"
}
// Listen on all interfaces so users or a service mesh can use localhost.
listenAddress := "0.0.0.0"
args := map[string]*string{
"listen-address": &listenAddress,
"broadcast-address": &m.StaticIP,
"broadcast-rpc-address": &m.StaticIP,
"seeds": pointer.StringPtr(seed),
"developer-mode": &devMode,
"overprovisioned": &overprovisioned,
"smp": pointer.StringPtr(strconv.Itoa(s.cpuCount)),
}
if cluster.Spec.Alternator.Enabled() {
args["alternator-port"] = pointer.StringPtr(strconv.Itoa(int(cluster.Spec.Alternator.Port)))
if cluster.Spec.Alternator.WriteIsolation != "" {
args["alternator-write-isolation"] = pointer.StringPtr(cluster.Spec.Alternator.WriteIsolation)
}
}
// If node is being replaced
if addr, ok := m.ServiceLabels[naming.ReplaceLabel]; ok {
args["replace-address-first-boot"] = pointer.StringPtr(addr)
}
// See if we need to use cpu-pinning
// TODO: Add more checks to make sure this is valid.
// eg. parse the cpuset and check the number of cpus is the same as cpu limits
// Now we rely completely on the user to have the cpu policy correctly
// configured in the kubelet, otherwise scylla will crash.
if cluster.Spec.CpuSet {
cpusAllowed, err := getCPUsAllowedList("/proc/1/status")
if err != nil {
return nil, errors.WithStack(err)
}
if err := s.validateCpuSet(ctx, cpusAllowed, s.cpuCount); err != nil {
return nil, errors.WithStack(err)
}
args["cpuset"] = &cpusAllowed
}
version := semver.NewScyllaVersion(cluster.Spec.Version)
klog.InfoS("Scylla version detected", "version", version)
if len(cluster.Spec.ScyllaArgs) > 0 {
if !version.SupportFeatureUnsafe(semver.ScyllaVersionThatSupportsArgs) {
klog.InfoS("This scylla version does not support ScyllaArgs. ScyllaArgs is ignored", "version", cluster.Spec.Version)
} else {
appendScyllaArguments(ctx, s, cluster.Spec.ScyllaArgs, args)
}
}
if _, err := os.Stat(scyllaIOPropertiesPath); err == nil && version.SupportFeatureSafe(semver.ScyllaVersionThatSupportsDisablingIOTuning) {
klog.InfoS("Scylla IO properties are already set, skipping io tuning")
ioSetup := "0"
args["io-setup"] = &ioSetup
}
var argsList []string
for key, value := range args {
if value == nil {
argsList = append(argsList, fmt.Sprintf("--%s", key))
} else {
argsList = append(argsList, fmt.Sprintf("--%s=%s", key, *value))
}
}
scyllaCmd := exec.Command(entrypointPath, argsList...)
scyllaCmd.Stderr = os.Stderr
scyllaCmd.Stdout = os.Stdout
klog.InfoS("Scylla entrypoint", "Command", scyllaCmd)
return scyllaCmd, nil
}
func (s *ScyllaConfig) validateCpuSet(ctx context.Context, cpusAllowed string, shards int) error {
cpuSet, err := cpuset.Parse(cpusAllowed)
if err != nil {
return err
}
if cpuSet.Size() != shards {
klog.InfoS("suboptimal shard and cpuset config, shard count (config: 'CPU') and cpuset size should match for optimal performance",
"shards", shards, "cpuset", cpuSet.String())
}
return nil
}
// mergeYAMLs merges two arbitrary YAML structures at the top level.
func mergeYAMLs(initialYAML, overrideYAML []byte) ([]byte, error) {
var initial, override map[string]interface{}
if err := yaml.Unmarshal(initialYAML, &initial); err != nil {
return nil, errors.WithStack(err)
}
if err := yaml.Unmarshal(overrideYAML, &override); err != nil {
return nil, errors.WithStack(err)
}
if initial == nil {
initial = make(map[string]interface{})
}
// Overwrite the values onto initial
for k, v := range override {
initial[k] = v
}
return yaml.Marshal(initial)
}
func getCPUsAllowedList(procFile string) (string, error) {
statusFile, err := ioutil.ReadFile(procFile)
if err != nil {
return "", errors.Wrapf(err, "error reading proc status file '%s'", procFile)
}
procStatus := string(statusFile[:])
startIndex := strings.Index(procStatus, "Cpus_allowed_list:")
if err != nil {
return "", fmt.Errorf("failed to get process status: %s", err.Error())
}
endIndex := startIndex + strings.Index(procStatus[startIndex:], "\n")
cpusAllowed := strings.TrimSpace(procStatus[startIndex+len("Cpus_allowed_list:") : endIndex])
return cpusAllowed, nil
}