-
Notifications
You must be signed in to change notification settings - Fork 159
/
config_util.go
156 lines (131 loc) · 4.96 KB
/
config_util.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
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package controllerutil
import (
"context"
"encoding/json"
"reflect"
"github.com/StudioSol/set"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/configuration/util"
"github.com/apecloud/kubeblocks/pkg/configuration/validate"
"github.com/apecloud/kubeblocks/pkg/constant"
)
type Result struct {
Phase v1alpha1.ConfigurationPhase `json:"phase"`
Revision string `json:"revision"`
Policy string `json:"policy"`
ExecResult string `json:"execResult"`
SucceedCount int32 `json:"succeedCount"`
ExpectedCount int32 `json:"expectedCount"`
Retry bool `json:"retry"`
Failed bool `json:"failed"`
Message string `json:"message"`
}
// MergeAndValidateConfigs merges and validates configuration files
func MergeAndValidateConfigs(configConstraint v1alpha1.ConfigConstraintSpec, baseConfigs map[string]string, cmKey []string, updatedParams []core.ParamPairs) (map[string]string, error) {
var (
err error
fc = configConstraint.FormatterConfig
newCfg map[string]string
configOperator core.ConfigOperator
updatedKeys = util.NewSet()
)
cmKeySet := core.FromCMKeysSelector(cmKey)
configLoaderOption := core.CfgOption{
Type: core.CfgCmType,
Log: log.FromContext(context.TODO()),
CfgType: fc.Format,
ConfigResource: core.FromConfigData(baseConfigs, cmKeySet),
}
if configOperator, err = core.NewConfigLoader(configLoaderOption); err != nil {
return nil, err
}
// merge param to config file
for _, params := range updatedParams {
if err := configOperator.MergeFrom(params.UpdatedParams, core.NewCfgOptions(params.Key, core.WithFormatterConfig(fc))); err != nil {
return nil, err
}
updatedKeys.Add(params.Key)
}
if newCfg, err = configOperator.ToCfgContent(); err != nil {
return nil, core.WrapError(err, "failed to generate config file")
}
// The ToCfgContent interface returns the file contents of all keys, the configuration file is encoded and decoded into keys,
// the content may be different with the original file, such as comments, blank lines, etc,
// in order to minimize the impact on the original file, only update the changed part.
updatedCfg := fromUpdatedConfig(newCfg, updatedKeys)
if err = validate.NewConfigValidator(&configConstraint, validate.WithKeySelector(cmKey)).Validate(updatedCfg); err != nil {
return nil, core.WrapError(err, "failed to validate updated config")
}
return core.MergeUpdatedConfig(baseConfigs, updatedCfg), nil
}
// fromUpdatedConfig filters out changed file contents.
func fromUpdatedConfig(m map[string]string, sets *set.LinkedHashSetString) map[string]string {
if sets.Length() == 0 {
return map[string]string{}
}
r := make(map[string]string, sets.Length())
for key, v := range m {
if sets.InArray(key) {
r[key] = v
}
}
return r
}
// IsApplyConfigChanged checks if the configuration is changed
func IsApplyConfigChanged(configMap *corev1.ConfigMap, item v1alpha1.ConfigurationItemDetail) bool {
if configMap == nil {
return false
}
lastAppliedVersion, ok := configMap.Annotations[constant.ConfigAppliedVersionAnnotationKey]
if !ok {
return false
}
var target v1alpha1.ConfigurationItemDetail
if err := json.Unmarshal([]byte(lastAppliedVersion), &target); err != nil {
return false
}
return reflect.DeepEqual(target, item)
}
// IsRerender checks if the configuration template is changed
func IsRerender(configMap *corev1.ConfigMap, item v1alpha1.ConfigurationItemDetail) bool {
if configMap == nil {
return true
}
if item.Version == "" {
return false
}
version, ok := configMap.Annotations[constant.CMConfigurationTemplateVersion]
if !ok || version != item.Version {
return true
}
return false
}
// GetConfigSpecReconcilePhase gets the configuration phase
func GetConfigSpecReconcilePhase(configMap *corev1.ConfigMap,
item v1alpha1.ConfigurationItemDetail,
status *v1alpha1.ConfigurationItemDetailStatus) v1alpha1.ConfigurationPhase {
if status == nil || status.Phase == "" {
return v1alpha1.CCreatingPhase
}
if !IsApplyConfigChanged(configMap, item) {
return v1alpha1.CPendingPhase
}
return status.Phase
}