github.com/verrazzano/verrazzano@v1.7.0/pkg/metricsutils/edit_scrapeconfig.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package metricsutils 5 6 import ( 7 "github.com/Jeffail/gabs/v2" 8 "github.com/verrazzano/verrazzano/pkg/constants" 9 "sigs.k8s.io/yaml" 10 ) 11 12 // EditScrapeJob edits a scrape config and adds or replaces the specified job with the new scrape 13 // config for that job. 14 func EditScrapeJob(scrapeConfigs *gabs.Container, editScrapeJobName string, newScrapeConfig *gabs.Container) (*gabs.Container, error) { 15 scrapeJobIndex := FindScrapeJob(scrapeConfigs, editScrapeJobName) 16 found := scrapeJobIndex >= 0 17 // found an existing scrape config, either remove it or replace it 18 if found { 19 if newScrapeConfig == nil || newScrapeConfig.Data() == nil { 20 scrapeConfigs.ArrayRemove(scrapeJobIndex) 21 } else { 22 scrapeConfigs.SetIndex(newScrapeConfig.Data(), scrapeJobIndex) 23 } 24 } 25 26 if !found && newScrapeConfig != nil { 27 // if we didn't find an existing scrape config and we are not removing it, append it to the existing scrape config 28 scrapeConfigs.ArrayAppend(newScrapeConfig.Data()) 29 } 30 31 return scrapeConfigs, nil 32 } 33 34 // EditScrapeJobInPrometheusConfig is similar to EditScrapeJob, but it takes a prometheus config 35 // as first parameter i.e. a container containing a "scrape_configs" key (or other key specified by the scrapeJobsKey parameter) 36 // This may appear to duplicate some of the code in EditScrapeJob, but it has to be in a separate 37 // implementation because of the way gabs.Container behaves when editing arrays embedded in a parent 38 // object versus directly editing a top level array element. In this case, the underlying parent 39 // object does not get updated correctly. 40 func EditScrapeJobInPrometheusConfig(promConfig *gabs.Container, scrapeJobsKey string, editScrapeJobName string, newScrapeConfig *gabs.Container) error { 41 scrapeConfigs := promConfig.Search(scrapeJobsKey) 42 scrapeJobIndex := FindScrapeJob(scrapeConfigs, editScrapeJobName) 43 // _, err := EditScrapeJob(scrapeConfigs, editScrapeJobName, newScrapeConfig) 44 // return err 45 found := scrapeJobIndex >= 0 46 // found an existing scrape config, either remove it or replace it 47 if found { 48 if newScrapeConfig == nil || newScrapeConfig.Data() == nil { 49 err := promConfig.ArrayRemove(scrapeJobIndex, scrapeJobsKey) 50 return err 51 } 52 _, err := scrapeConfigs.SetIndex(newScrapeConfig.Data(), scrapeJobIndex) 53 return err 54 } 55 56 if !found && newScrapeConfig != nil { 57 // if we didn't find an existing scrape config and we are not removing it, append it to the existing scrape config 58 return promConfig.ArrayAppend(newScrapeConfig.Data(), scrapeJobsKey) 59 } 60 61 return nil 62 } 63 64 // FindScrapeJob returns the index of the given job name in the scrapeConfigs list, -1 if not found. 65 func FindScrapeJob(scrapeConfigs *gabs.Container, jobNameToFind string) int { 66 for index, scrapeConfig := range scrapeConfigs.Children() { 67 scrapeJobName := scrapeConfig.Search(constants.PrometheusJobNameKey).Data() 68 if jobNameToFind == scrapeJobName { 69 return index 70 } 71 } 72 return -1 73 } 74 75 // ParseScrapeConfig returns an editable representation of the prometheus scrape configuration 76 func ParseScrapeConfig(scrapeConfigStr string) (*gabs.Container, error) { 77 scrapeConfigJSON, _ := yaml.YAMLToJSON([]byte(scrapeConfigStr)) 78 newScrapeConfig, err := gabs.ParseJSON(scrapeConfigJSON) 79 if err != nil { 80 return nil, err 81 } 82 return newScrapeConfig, nil 83 }