/
overrides.go
79 lines (66 loc) · 2.66 KB
/
overrides.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
package overrides
import (
. "strings"
"github.com/kyma-project/kyma/components/application-operator/pkg/utils"
)
const (
overridesKey = "overrides"
overridePrefix = "override."
)
type OverridesData struct {
DomainName string `json:"domainName,omitempty"`
ApplicationGatewayImage string `json:"applicationGatewayImage,omitempty"`
ApplicationGatewayTestsImage string `json:"applicationGatewayTestsImage,omitempty"`
ApplicationConnectivityValidatorImage string `json:"applicationConnectivityValidatorImage,omitempty"`
Tenant string `json:"tenant,omitempty"`
Group string `json:"group,omitempty"`
LogLevel string `json:"logLevel,omitempty"`
LogFormat string `json:"logFormat,omitempty"`
GatewayOncePerNamespace bool `json:"deployGatewayOncePerNamespace,omitempty"`
StrictMode string `json:"strictMode,omitempty"`
PodSecurityPolicyEnabled bool `json:"podSecurityPolicyEnabled,omitempty"`
CentralApplicationConnectivityValidatorEnabled bool `json:"centralApplicationConnectivityValidatorEnabled,omitempty"`
}
func NewFlatOverridesMap(labels map[string]string) utils.StringMap {
overridesMap := make(utils.StringMap)
for key, value := range labels {
if HasPrefix(key, overridePrefix) {
overridesMap[removePrefix(key)] = value
}
}
return overridesMap
}
func NewExtractedOverridesMap(config utils.InterfaceMap) utils.StringMap {
previousOverrides := make(map[string]interface{})
if val, ok := config[overridesKey]; ok {
if casted, ok := val.(map[string]interface{}); ok {
previousOverrides = casted
}
}
return utils.NewStringMap(previousOverrides)
}
func MergeLabelOverrides(labels utils.StringMap, target map[string]interface{}) {
for key, value := range labels {
preKey := removePrefix(key)
if preKey != "" {
subMap := unwind(preKey, value)
utils.MergeMaps(target, subMap)
// add prop to override map for further reference
utils.MergeMaps(target, map[string]interface{}{overridesKey: subMap})
}
}
}
func removePrefix(key string) string {
preKey := TrimPrefix(key, overridePrefix)
preKey = Trim(preKey, utils.Separator)
return preKey
}
func unwind(key string, value string) map[string]interface{} {
index := IndexRune(key, utils.RuneSeparator)
if index == -1 {
return map[string]interface{}{key: value}
}
currentKey := key[:index]
subMap := unwind(key[index+1:], value)
return map[string]interface{}{currentKey: subMap}
}