-
Notifications
You must be signed in to change notification settings - Fork 27
/
module_config.go
212 lines (179 loc) · 5.65 KB
/
module_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
package utils
import (
"fmt"
"strings"
"github.com/davecgh/go-spew/spew"
"sigs.k8s.io/yaml"
utils_checksum "github.com/flant/shell-operator/pkg/utils/checksum"
)
var (
ModuleEnabled = true
ModuleDisabled = false
)
type ModuleConfig struct {
ModuleName string
IsEnabled *bool
Values Values
IsUpdated bool
ModuleConfigKey string
ModuleEnabledKey string
RawConfig []string
}
// String returns description of ModuleConfig values.
func (mc *ModuleConfig) String() string {
return fmt.Sprintf("Module(Name=%s IsEnabled=%v IsUpdated=%v Values:\n%s)", mc.ModuleName, mc.IsEnabled, mc.IsUpdated, mc.Values.DebugString())
}
// GetEnabled returns string description of enabled status.
func (mc *ModuleConfig) GetEnabled() string {
if mc == nil {
return ""
}
switch {
case mc.IsEnabled == nil:
return "n/d"
case *mc.IsEnabled:
return "true"
default:
return "false"
}
}
func NewModuleConfig(moduleName string) *ModuleConfig {
return &ModuleConfig{
ModuleName: moduleName,
IsEnabled: nil,
Values: make(Values),
ModuleConfigKey: ModuleNameToValuesKey(moduleName),
ModuleEnabledKey: ModuleNameToValuesKey(moduleName) + "Enabled",
RawConfig: make([]string, 0),
}
}
func (mc *ModuleConfig) WithEnabled(v bool) *ModuleConfig {
if v {
mc.IsEnabled = &ModuleEnabled
} else {
mc.IsEnabled = &ModuleDisabled
}
return mc
}
func (mc *ModuleConfig) WithUpdated(v bool) *ModuleConfig {
mc.IsUpdated = v
return mc
}
func (mc *ModuleConfig) WithValues(values Values) *ModuleConfig {
mc.Values = values
return mc
}
// LoadFromValues loads module config from a map.
//
// Values for module in `values` map are addressed by a key.
// This key should be produced with ModuleNameToValuesKey.
func (mc *ModuleConfig) LoadFromValues(values Values) (*ModuleConfig, error) {
if moduleValuesData, hasModuleData := values[mc.ModuleConfigKey]; hasModuleData {
switch v := moduleValuesData.(type) {
case map[string]interface{}, []interface{}:
data := map[string]interface{}{mc.ModuleConfigKey: v}
values, err := NewValues(data)
if err != nil {
return nil, err
}
mc.Values = values
default:
return nil, fmt.Errorf("load '%s' values: module config should be array or map. Got: %s", mc.ModuleName, spew.Sdump(moduleValuesData))
}
}
if moduleEnabled, hasModuleEnabled := values[mc.ModuleEnabledKey]; hasModuleEnabled {
switch v := moduleEnabled.(type) {
case bool:
mc.WithEnabled(v)
default:
return nil, fmt.Errorf("load '%s' enable config: enabled value should be bool. Got: %#v", mc.ModuleName, moduleEnabled)
}
}
return mc, nil
}
// FromYaml loads module config from a yaml string.
//
// Example:
//
// simpleModule:
//
// param1: 10
// param2: 120
//
// simpleModuleEnabled: true
func (mc *ModuleConfig) FromYaml(yamlString []byte) (*ModuleConfig, error) {
values, err := NewValuesFromBytes(yamlString)
if err != nil {
return nil, fmt.Errorf("load module '%s' yaml config: %s\n%s", mc.ModuleName, err, string(yamlString))
}
mc.RawConfig = []string{string(yamlString)}
return mc.LoadFromValues(values)
}
// FromConfigMapData loads module config from a structure with string keys and yaml string values (ConfigMap)
//
// Example:
//
// simpleModule: |
// param1: 10
// param2: 120
// simpleModuleEnabled: "true"
// TODO "msg": "Kube config manager: cannot handle ConfigMap update: ConfigMap:
// bad yaml at key 'deployWithHooks':
// data is not compatible with JSON and YAML:
// error marshaling into JSON:
// json: unsupported type: map[interface {}]interface {}, data:
// (map[string]interface {}) (len=1) {\n (string) (len=15) \"deployWithHooks\": (map[interface {}]interface {}) (len=3) {\n (string) (len=6) \"param2\": (string) (len=11) \"srqweqweqwe\",\n (string) (len=8) \"paramArr\": ([]interface {}) (len=4 cap=4) {\n (string) (len=3) \"asd\",\n (string) (len=3) \"qwe\",\n (string) (len=6) \"sadasd\",\n (string) (len=5) \"salad\"\n },\n (string) (len=6) \"param1\": (int) 1\n }\n}\n",
func (mc *ModuleConfig) FromConfigMapData(configData map[string]string) (*ModuleConfig, error) {
// create Values with moduleNameKey and moduleEnabled keys
configValues := make(Values)
// if there is data for module, unmarshal it and put into configValues
valuesYaml, hasKey := configData[mc.ModuleConfigKey]
if hasKey {
var moduleValues interface{}
err := yaml.Unmarshal([]byte(valuesYaml), &moduleValues)
if err != nil {
return nil, fmt.Errorf("unmarshal yaml data in a module config key '%s': %v", mc.ModuleConfigKey, err)
}
configValues[mc.ModuleConfigKey] = moduleValues
mc.RawConfig = append(mc.RawConfig, valuesYaml)
}
// if there is enabled key, treat it as boolean
enabledString, hasKey := configData[mc.ModuleEnabledKey]
if hasKey {
var enabled bool
switch enabledString {
case "true":
enabled = true
case "false":
enabled = false
default:
return nil, fmt.Errorf("module enabled key '%s' should have a boolean value, got '%v'", mc.ModuleEnabledKey, enabledString)
}
configValues[mc.ModuleEnabledKey] = enabled
mc.RawConfig = append(mc.RawConfig, enabledString)
}
if len(configValues) == 0 {
return mc, nil
}
return mc.LoadFromValues(configValues)
}
func (mc *ModuleConfig) Checksum() string {
return utils_checksum.CalculateChecksum(mc.RawConfig...)
}
func ModuleEnabledValue(i interface{}) (*bool, error) {
switch v := i.(type) {
case string:
switch strings.ToLower(v) {
case "true":
return &ModuleEnabled, nil
case "false":
return &ModuleDisabled, nil
}
case bool:
if v {
return &ModuleEnabled, nil
}
return &ModuleDisabled, nil
}
return nil, fmt.Errorf("unsupported module enabled value: %v", i)
}