-
Notifications
You must be signed in to change notification settings - Fork 356
/
config.go
172 lines (154 loc) · 3.98 KB
/
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
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/alibaba/sealer/common"
v2 "github.com/alibaba/sealer/types/api/v2"
"gopkg.in/yaml.v3"
yaml2 "sigs.k8s.io/yaml"
"github.com/alibaba/sealer/logger"
v1 "github.com/alibaba/sealer/types/api/v1"
"github.com/alibaba/sealer/utils"
)
/*
config in Clusterfile:
apiVersion: sealer.aliyun.com/v1alpha1
kind: Config
metadata:
name: redis-config
spec:
path: etc/redis-config.yaml
data: |
redis-user: root
redis-passwd: xxx
Dump will dump the config to etc/redis-config.yaml file
*/
const (
Merge = "merge"
Overwrite = "overwrite"
)
type Interface interface {
// Dump Config in Clusterfile to the cluster rootfs disk
Dump(configs []v1.Config) error
}
type Dumper struct {
Configs []v1.Config
Cluster *v2.Cluster
}
func NewConfiguration(cluster *v2.Cluster) Interface {
return &Dumper{
Cluster: cluster,
}
}
func (c *Dumper) Dump(configs []v1.Config) error {
if configs == nil {
logger.Debug("clusterfile config is empty!")
return nil
}
/* configs, err := utils.DecodeConfigs(clusterfile)
if err != nil {
return fmt.Errorf("failed to dump config %v", err)
}*/
c.Configs = configs
if err := c.WriteFiles(); err != nil {
return fmt.Errorf("failed to write config files %v", err)
}
return nil
}
func (c *Dumper) WriteFiles() (err error) {
if c.Configs == nil {
logger.Debug("empty config found")
return nil
}
for _, config := range c.Configs {
configData := []byte(config.Spec.Data)
mountRoot := filepath.Join(common.DefaultClusterRootfsDir, c.Cluster.Name, "mount")
mountDirs, err := ioutil.ReadDir(mountRoot)
if err != nil {
return err
}
for _, f := range mountDirs {
if !f.IsDir() {
continue
}
configPath := filepath.Join(mountRoot, f.Name(), config.Spec.Path)
//only the YAML format is supported
if config.Spec.Strategy == Merge {
configData, err = getMergeConfigData(configPath, configData)
if err != nil {
return err
}
}
err = utils.WriteFile(configPath, configData)
if err != nil {
return fmt.Errorf("write config file failed %v", err)
}
}
}
return nil
}
//merge the contents of data into the path file
func getMergeConfigData(path string, data []byte) ([]byte, error) {
var configs [][]byte
context, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
mergeConfigMap := make(map[string]interface{})
err = yaml.Unmarshal(data, &mergeConfigMap)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal merge map: %v", err)
}
for _, rawCfgData := range bytes.Split(context, []byte("---\n")) {
configMap := make(map[string]interface{})
err = yaml.Unmarshal(rawCfgData, &configMap)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %v", err)
}
if len(configMap) == 0 {
continue
}
deepMerge(&configMap, &mergeConfigMap)
cfg, err := yaml2.Marshal(&configMap)
if err != nil {
return nil, err
}
configs = append(configs, cfg)
}
return bytes.Join(configs, []byte("\n---\n")), nil
}
func deepMerge(dst, src *map[string]interface{}) {
for srcK, srcV := range *src {
dstV, ok := (*dst)[srcK]
if !ok {
continue
}
dV, ok := dstV.(map[string]interface{})
// dstV is string type
if !ok {
(*dst)[srcK] = srcV
continue
}
sV, ok := srcV.(map[string]interface{})
if !ok {
continue
}
deepMerge(&dV, &sV)
(*dst)[srcK] = dV
}
}