forked from ydb-platform/ydb-kubernetes-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.go
149 lines (120 loc) · 3.98 KB
/
configuration.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
package v1alpha1
import (
"bytes"
"errors"
"fmt"
"strconv"
"gopkg.in/yaml.v3"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/configuration/schema"
)
func generateHosts(cr *Storage) []schema.Host {
var hosts []schema.Host
for i := 0; i < int(cr.Spec.Nodes); i++ {
datacenter := "az-1"
if cr.Spec.Erasure == ErasureMirror3DC {
datacenter = fmt.Sprintf("az-%d", i%3)
}
hosts = append(hosts, schema.Host{
Host: fmt.Sprintf("%v-%d", cr.GetName(), i),
HostConfigID: 1, // TODO
NodeID: i + 1,
Port: InterconnectPort,
WalleLocation: schema.WalleLocation{
Body: 12340 + i,
DataCenter: datacenter,
Rack: strconv.Itoa(i),
},
})
}
if cr.Spec.NodeSets != nil {
hostIndex := 0
for _, nodeSetSpec := range cr.Spec.NodeSets {
for podIndex := 0; podIndex < int(nodeSetSpec.Nodes); podIndex++ {
podName := cr.GetName() + "-" + nodeSetSpec.Name + "-" + strconv.Itoa(podIndex)
hosts[hostIndex].Host = podName
hostIndex++
}
}
}
return hosts
}
func BuildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
config := make(map[string]interface{})
// If any kind of configuration exists on Database object, then
// it will be used to fully override storage object.
// This is a temporary solution that should go away when it would
// be possible to override Database configuration partially.
var rawYamlConfiguration string
if crDB != nil && crDB.Spec.Configuration != "" {
rawYamlConfiguration = crDB.Spec.Configuration
} else {
rawYamlConfiguration = cr.Spec.Configuration
}
success, dynConfig, err := ParseDynConfig(rawYamlConfiguration)
if success {
if err != nil {
return nil, fmt.Errorf("failed to parse dynconfig, error: %w", err)
}
if dynConfig.Config["hosts"] == nil {
hosts := generateHosts(cr)
dynConfig.Config["hosts"] = hosts
}
return yaml.Marshal(dynConfig)
}
err = yaml.Unmarshal([]byte(rawYamlConfiguration), &config)
if err != nil {
return nil, fmt.Errorf("failed to serialize YAML config, error: %w", err)
}
if config["hosts"] == nil {
hosts := generateHosts(cr)
config["hosts"] = hosts
}
return yaml.Marshal(config)
}
func ParseConfiguration(rawYamlConfiguration string) (schema.Configuration, error) {
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
dec.KnownFields(false)
var configuration schema.Configuration
err := dec.Decode(&configuration)
if err != nil {
return schema.Configuration{}, nil
}
return configuration, nil
}
func ParseDynConfig(rawYamlConfiguration string) (bool, schema.DynConfig, error) {
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
dec.KnownFields(true)
var dynConfig schema.DynConfig
err := dec.Decode(&dynConfig)
if err != nil {
return false, schema.DynConfig{}, fmt.Errorf("error unmarshal yaml to dynconfig: %w", err)
}
err = validateDynConfig(dynConfig)
if err != nil {
return true, dynConfig, fmt.Errorf("error validate dynconfig: %w", err)
}
return true, dynConfig, err
}
func validateDynConfig(dynConfig schema.DynConfig) error {
if _, exist := dynConfig.Config["yaml_config_enabled"]; !exist {
return errors.New("failed to find mandatory `yaml_config_enabled` field inside config")
}
if _, exist := dynConfig.Config["static_erasure"]; !exist {
return errors.New("failed to find mandatory `static_erasure` field inside config")
}
if _, exist := dynConfig.Config["host_configs"]; !exist {
return errors.New("failed to find mandatory `host_configs` field inside config")
}
if _, exist := dynConfig.Config["blob_storage_config"]; !exist {
return errors.New("failed to find mandatory `blob_storage_config` field inside config")
}
return nil
}
func GetConfigForCMS(dynConfig schema.DynConfig) ([]byte, error) {
delete(dynConfig.Config, "static_erasure")
delete(dynConfig.Config, "host_configs")
delete(dynConfig.Config, "nameservice_config")
delete(dynConfig.Config, "blob_storage_config")
delete(dynConfig.Config, "hosts")
return yaml.Marshal(dynConfig)
}