config_reloader.go 7.7 KB

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