config_reloader.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "regexp"
  7. "sort"
  8. "strings"
  9. "github.com/knadh/koanf/parsers/yaml"
  10. "github.com/knadh/koanf/providers/file"
  11. "github.com/knadh/koanf/v2"
  12. "github.com/prometheus/client_golang/prometheus"
  13. "github.com/prometheus/client_golang/prometheus/promauto"
  14. log "github.com/sirupsen/logrus"
  15. )
  16. var (
  17. metricConfigActionCount = promauto.NewGauge(prometheus.GaugeOpts{
  18. Name: "olivetin_config_action_count",
  19. Help: "The number of actions in the config file",
  20. })
  21. metricConfigReloadedCount = promauto.NewCounter(prometheus.CounterOpts{
  22. Name: "olivetin_config_reloaded_count",
  23. Help: "The number of times the config has been reloaded",
  24. })
  25. listeners []func()
  26. )
  27. func AddListener(l func()) {
  28. listeners = append(listeners, l)
  29. }
  30. func AppendSource(cfg *Config, k *koanf.Koanf, configPath string) {
  31. log.WithFields(log.Fields{
  32. "configPath": configPath,
  33. }).Info("Appending cfg source")
  34. loadIncludedConfigsFromDir(k, configPath)
  35. if !unmarshalRoot(k, cfg) {
  36. return
  37. }
  38. afterLoadFinalize(cfg, configPath)
  39. }
  40. func unmarshalRoot(k *koanf.Koanf, cfg *Config) bool {
  41. err := k.UnmarshalWithConf("", cfg, koanf.UnmarshalConf{
  42. Tag: "koanf",
  43. DecoderConfig: newDefaultUnmarshalDecoderConfig(),
  44. })
  45. if err != nil {
  46. log.Errorf("Error unmarshalling config: %v", err)
  47. return false
  48. }
  49. return true
  50. }
  51. func afterLoadFinalize(cfg *Config, configPath string) {
  52. metricConfigReloadedCount.Inc()
  53. metricConfigActionCount.Set(float64(len(cfg.Actions)))
  54. cfg.SetDir(filepath.Dir(configPath))
  55. cfg.Sanitize()
  56. for _, l := range listeners {
  57. l()
  58. }
  59. }
  60. // buildIncludePath constructs the full path to the include directory.
  61. func buildIncludePath(k *koanf.Koanf, baseConfigPath string) string {
  62. relativeIncludePath := k.String("include")
  63. return filepath.Join(filepath.Dir(baseConfigPath), relativeIncludePath)
  64. }
  65. // loadAndMergeYamlFiles loads and merges all YAML files from the include directory.
  66. func loadAndMergeYamlFiles(k *koanf.Koanf, includePath string, yamlFiles []string) {
  67. sort.Strings(yamlFiles)
  68. for _, filename := range yamlFiles {
  69. loadAndMergeIncludedFile(k, includePath, filename)
  70. }
  71. log.Infof("Finished loading %d included config file(s)", len(yamlFiles))
  72. }
  73. // loadIncludedConfigsFromDir loads configuration files from an include directory and merges them
  74. func loadIncludedConfigsFromDir(k *koanf.Koanf, baseConfigPath string) {
  75. relativeIncludePath := k.String("include")
  76. if relativeIncludePath == "" {
  77. return
  78. }
  79. includePath := buildIncludePath(k, baseConfigPath)
  80. log.WithFields(log.Fields{
  81. "includePath": includePath,
  82. }).Infof("Loading included configs from dir")
  83. yamlFiles, ok := listYamlFiles(includePath)
  84. if !ok || len(yamlFiles) == 0 {
  85. return
  86. }
  87. loadAndMergeYamlFiles(k, includePath, yamlFiles)
  88. }
  89. // validateIncludeDirectory checks if the given path exists and is a directory.
  90. func validateIncludeDirectory(includePath string) bool {
  91. dirInfo, err := os.Stat(includePath)
  92. if err != nil {
  93. log.Warnf("Include directory not found: %s", includePath)
  94. return false
  95. }
  96. if !dirInfo.IsDir() {
  97. log.Warnf("Include path is not a directory: %s", includePath)
  98. return false
  99. }
  100. return true
  101. }
  102. // isYamlFile checks if a filename has a YAML extension.
  103. func isYamlFile(name string) bool {
  104. return strings.HasSuffix(name, ".yml") || strings.HasSuffix(name, ".yaml")
  105. }
  106. // filterYamlFilesFromEntries extracts YAML file names from directory entries.
  107. func filterYamlFilesFromEntries(entries []os.DirEntry) []string {
  108. var yamlFiles []string
  109. for _, entry := range entries {
  110. if entry.IsDir() {
  111. continue
  112. }
  113. if isYamlFile(entry.Name()) {
  114. yamlFiles = append(yamlFiles, entry.Name())
  115. }
  116. }
  117. return yamlFiles
  118. }
  119. func listYamlFiles(includePath string) ([]string, bool) {
  120. if !validateIncludeDirectory(includePath) {
  121. return nil, false
  122. }
  123. entries, err := os.ReadDir(includePath)
  124. if err != nil {
  125. log.Errorf("Error reading include directory: %v", err)
  126. return nil, false
  127. }
  128. yamlFiles := filterYamlFilesFromEntries(entries)
  129. if len(yamlFiles) == 0 {
  130. log.Infof("No YAML files found in include directory: %s", includePath)
  131. }
  132. return yamlFiles, true
  133. }
  134. func loadAndMergeIncludedFile(k *koanf.Koanf, includePath, filename string) {
  135. filePath := filepath.Join(includePath, filename)
  136. if err := k.Load(file.Provider(filePath), yaml.Parser(), koanf.WithMergeFunc(mergeFunc)); err != nil {
  137. log.Errorf("Error loading included config file %s: %v", filePath, err)
  138. return
  139. }
  140. log.WithFields(log.Fields{
  141. "filePath": filePath,
  142. }).Info("Successfully loaded included config file")
  143. }
  144. // mergeActionsWhenBothExist merges actions when both src and dest have actions.
  145. func mergeActionsWhenBothExist(srcActions interface{}, destActions interface{}, dest map[string]interface{}) {
  146. srcSlice, ok1 := srcActions.([]interface{})
  147. destSlice, ok2 := destActions.([]interface{})
  148. if ok1 && ok2 {
  149. dest["actions"] = append(destSlice, srcSlice...)
  150. } else {
  151. dest["actions"] = srcActions
  152. }
  153. }
  154. // mergeActionsFromSource merges actions from source into destination.
  155. func mergeActionsFromSource(srcActions interface{}, dest map[string]interface{}) {
  156. if destActions, ok := dest["actions"]; ok {
  157. mergeActionsWhenBothExist(srcActions, destActions, dest)
  158. } else {
  159. dest["actions"] = srcActions
  160. }
  161. }
  162. // mergeDashboardsWhenBothExist merges dashboards when both src and dest have dashboards.
  163. func mergeDashboardsWhenBothExist(srcDashboards interface{}, destDashboards interface{}, dest map[string]interface{}) {
  164. srcSlice, ok1 := srcDashboards.([]interface{})
  165. destSlice, ok2 := destDashboards.([]interface{})
  166. if ok1 && ok2 {
  167. dest["dashboards"] = append(destSlice, srcSlice...)
  168. } else {
  169. dest["dashboards"] = srcDashboards
  170. }
  171. }
  172. // mergeDashboardsFromSource merges dashboards from source into destination.
  173. func mergeDashboardsFromSource(srcDashboards interface{}, dest map[string]interface{}) {
  174. if destDashboards, ok := dest["dashboards"]; ok {
  175. mergeDashboardsWhenBothExist(srcDashboards, destDashboards, dest)
  176. } else {
  177. dest["dashboards"] = srcDashboards
  178. }
  179. }
  180. // mergeEntitiesWhenBothExist merges entities when both src and dest have entities.
  181. func mergeEntitiesWhenBothExist(srcEntities interface{}, destEntities interface{}, dest map[string]interface{}) {
  182. srcSlice, ok1 := srcEntities.([]interface{})
  183. destSlice, ok2 := destEntities.([]interface{})
  184. if ok1 && ok2 {
  185. dest["entities"] = append(destSlice, srcSlice...)
  186. } else {
  187. dest["entities"] = srcEntities
  188. }
  189. }
  190. // mergeEntitiesFromSource merges entities from source into destination.
  191. func mergeEntitiesFromSource(srcEntities interface{}, dest map[string]interface{}) {
  192. if destEntities, ok := dest["entities"]; ok {
  193. mergeEntitiesWhenBothExist(srcEntities, destEntities, dest)
  194. } else {
  195. dest["entities"] = srcEntities
  196. }
  197. }
  198. func mergeFunc(src map[string]interface{}, dest map[string]interface{}) error {
  199. if srcActions, ok := src["actions"]; ok {
  200. mergeActionsFromSource(srcActions, dest)
  201. }
  202. if srcDashboards, ok := src["dashboards"]; ok {
  203. mergeDashboardsFromSource(srcDashboards, dest)
  204. }
  205. if srcEntities, ok := src["entities"]; ok {
  206. mergeEntitiesFromSource(srcEntities, dest)
  207. }
  208. return nil
  209. }
  210. var envRegex = regexp.MustCompile(`\${{ *?(\S+) *?}}`)
  211. func envDecodeHookFunc(from reflect.Type, to reflect.Type, data any) (any, error) {
  212. log.Debugf("envDecodeHookFunc called: from=%v, to=%v, data=%v", from, to, data)
  213. if from.Kind() != reflect.String {
  214. return data, nil
  215. }
  216. input := data.(string)
  217. log.Debugf("Processing string input: %q", input)
  218. output := envRegex.ReplaceAllStringFunc(input, func(match string) string {
  219. submatches := envRegex.FindStringSubmatch(match)
  220. key := submatches[1]
  221. val, set := os.LookupEnv(key)
  222. log.Debugf("Environment variable %q: set=%v, value=%q", key, set, val)
  223. if !set {
  224. log.Warnf("Config file references unset environment variable: \"%s\"", key)
  225. }
  226. return val
  227. })
  228. log.Debugf("Environment variable interpolation result: %q -> %q", input, output)
  229. return output, nil
  230. }