-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathconfiguration.go
111 lines (91 loc) · 2.74 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
package v1alpha1
import (
"bytes"
"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
}
dynconfig, err := ParseDynconfig(rawYamlConfiguration)
if err == nil {
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, err
}
if config["hosts"] == nil {
hosts := generateHosts(cr)
config["hosts"] = hosts
}
return yaml.Marshal(config)
}
func ParseConfiguration(rawYamlConfiguration string) (schema.Configuration, error) {
configuration := schema.Configuration{}
dynconfig, err := ParseDynconfig(rawYamlConfiguration)
if err == nil {
config, err := yaml.Marshal(dynconfig.Config)
if err != nil {
return configuration, err
}
rawYamlConfiguration = string(config)
}
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
dec.KnownFields(false)
err = dec.Decode(&configuration)
return configuration, err
}
func ParseDynconfig(rawYamlConfiguration string) (schema.Dynconfig, error) {
dynconfig := schema.Dynconfig{}
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
dec.KnownFields(true)
err := dec.Decode(&dynconfig)
return dynconfig, err
}