loadConfig loads all configuration files using the loader function then merges the yaml configuration files into one yaml document. and notifies listeners if successful.
(ctx context.Context)
| 201 | // loadConfig loads all configuration files using the loader function then merges the yaml configuration files into one yaml document. |
| 202 | // and notifies listeners if successful. |
| 203 | func (om *Manager) loadConfig(ctx context.Context) error { |
| 204 | rawData := make([][]byte, len(om.providers)) |
| 205 | hashes := map[string]string{} |
| 206 | |
| 207 | for i, p := range om.providers { |
| 208 | buf, err := p.Read(ctx) |
| 209 | if err != nil { |
| 210 | om.configLoadSuccess.Set(0) |
| 211 | return errors.Wrapf(err, "read %q", p.Name()) |
| 212 | } |
| 213 | |
| 214 | if om.cfg.Preprocessor != nil { |
| 215 | buf, err = om.cfg.Preprocessor(buf) |
| 216 | if err != nil { |
| 217 | om.configLoadSuccess.Set(0) |
| 218 | return errors.Wrapf(err, "preprocess %q", p.Name()) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | rawData[i] = buf |
| 223 | hashes[p.Name()] = fmt.Sprintf("%x", sha256.Sum256(buf)) |
| 224 | } |
| 225 | |
| 226 | // check if new hashes are the same as before |
| 227 | sameHashes := true |
| 228 | for name, h := range hashes { |
| 229 | if om.fileHashes[name] != h { |
| 230 | sameHashes = false |
| 231 | break |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if sameHashes { |
| 236 | // No need to rebuild runtime config. |
| 237 | om.configLoadSuccess.Set(1) |
| 238 | return nil |
| 239 | } |
| 240 | |
| 241 | mergedConfig := map[string]interface{}{} |
| 242 | for i, p := range om.providers { |
| 243 | data := rawData[i] |
| 244 | yamlFile, err := om.unmarshalMaybeGzipped(p.Name(), data) |
| 245 | if err != nil { |
| 246 | om.configLoadSuccess.Set(0) |
| 247 | return errors.Wrapf(err, "unmarshal %q", p.Name()) |
| 248 | } |
| 249 | mergedConfig, err = mergeConfigMaps(mergedConfig, yamlFile, "") |
| 250 | if err != nil { |
| 251 | om.configLoadSuccess.Set(0) |
| 252 | return errors.Wrapf(err, "can't merge %q on top of the previous providers", p.Name()) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | buf, err := yaml.Marshal(mergedConfig) |
| 257 | if err != nil { |
| 258 | om.configLoadSuccess.Set(0) |
| 259 | return errors.Wrap(err, "marshal file") |
| 260 | } |
no test coverage detected