11package environment
22
33import (
4+ "bufio"
45 "context"
56 "fmt"
67 "github.com/pkg/errors"
78 "github.com/rs/zerolog/log"
89 "helm.sh/helm/v3/pkg/chartutil"
9- "io/ioutil "
10+ "os "
1011 "path/filepath"
1112 "strconv"
1213 "strings"
@@ -214,21 +215,32 @@ func NewMockserverHelmChart() *HelmChart {
214215}
215216
216217// NewPrometheusManifest creates new k8s manifest for prometheus
217- func NewPrometheusManifest () * K8sManifest {
218- rulesFilePath := filepath .Join (tools .ProjectRoot , "/environment/templates/prometheus/rules/ocr.rules.yml" )
219- content , err := ioutil .ReadFile (rulesFilePath )
220- if err != nil {
221- return nil
218+ // It receives a map of strings to *os.File which it uses in the following way:
219+ // The string in the map is the template value that will be used the prometheus-config-map.yml file.
220+ // The *os.File contains the rules yaml file, before being added to the values map of the K8sManifest.
221+ // Every line of the file is appended 4 spaces, this is done so after the file is templated to the
222+ // prometheus-config-map.yml file, the yml will be formatted correctly.
223+ func NewPrometheusManifest (rules map [string ]* os.File ) * K8sManifest {
224+ vals := map [string ]interface {}{}
225+ for val , file := range rules {
226+ scanner := bufio .NewScanner (file )
227+ var txtlines []string
228+ for scanner .Scan () {
229+ txtlines = append (txtlines , scanner .Text ())
230+ }
231+ for index , line := range txtlines {
232+ txtlines [index ] = fmt .Sprintf (" %s" , line )
233+ }
234+ vals [val ] = strings .Join (txtlines , "\n " )
222235 }
236+
223237 return & K8sManifest {
224238 id : "prometheus" ,
225239 DeploymentFile : filepath .Join (tools .ProjectRoot , "/environment/templates/prometheus/prometheus-deployment.yml" ),
226240 ServiceFile : filepath .Join (tools .ProjectRoot , "/environment/templates/prometheus/prometheus-service.yml" ),
227241 ConfigMapFile : filepath .Join (tools .ProjectRoot , "/environment/templates/prometheus/prometheus-config-map.yml" ),
228242
229- values : map [string ]interface {}{
230- "ocrRulesYml" : string (content ),
231- },
243+ values : vals ,
232244 }
233245}
234246
@@ -546,14 +558,14 @@ func OtpeGroup() K8sEnvSpecInit {
546558}
547559
548560// PrometheusGroup contains manifests for prometheus
549- func PrometheusGroup () K8sEnvSpecInit {
561+ func PrometheusGroup (rules map [ string ] * os. File ) K8sEnvSpecInit {
550562 return func (config * config.NetworkConfig ) (string , K8sEnvSpecs ) {
551563 var specs K8sEnvSpecs
552564 prometheusDependencyGroup := & K8sManifestGroup {
553565 id : "PrometheusDependencyGroup" ,
554- manifests : []K8sEnvResource {NewPrometheusManifest ()},
566+ manifests : []K8sEnvResource {NewPrometheusManifest (rules )},
555567 }
556568 specs = append (specs , prometheusDependencyGroup )
557569 return "" , specs
558570 }
559- }
571+ }
0 commit comments