diff --git a/pkg/controller/prometheus.go b/pkg/controller/prometheus.go index 1b231992d84..67f9d9c0afb 100644 --- a/pkg/controller/prometheus.go +++ b/pkg/controller/prometheus.go @@ -17,6 +17,7 @@ package controller import ( "context" "errors" + "reflect" promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" promv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1" @@ -161,20 +162,20 @@ func (c *Controller) checkAndCreatePrometheusAddlConfig(ctx context.Context, ten return err } } else { - var scrapeConfigs []configmaps.ScrapeConfig + var scrapeConfigs, exceptedScrapeConfigs []configmaps.ScrapeConfig err := yaml.Unmarshal(secret.Data[miniov2.PrometheusAddlScrapeConfigKey], &scrapeConfigs) if err != nil { return err } - // Check if the scrape config is already present - hasScrapeConfig := false + // get other scrape configs for _, sc := range scrapeConfigs { - if sc.JobName == tenant.PrometheusOperatorAddlConfigJobName() { - hasScrapeConfig = true - break + if sc.JobName != tenant.PrometheusOperatorAddlConfigJobName() { + exceptedScrapeConfigs = append(exceptedScrapeConfigs, sc) } } - if !hasScrapeConfig { + // add new scrape configs + exceptedScrapeConfigs = append(exceptedScrapeConfigs, promCfg.ScrapeConfigs...) + if !reflect.DeepEqual(scrapeConfigs, exceptedScrapeConfigs) { klog.Infof("Adding MinIO tenant Prometheus scrape config") scrapeConfigs = append(scrapeConfigs, promCfg.ScrapeConfigs...) scrapeCfgYaml, err := yaml.Marshal(scrapeConfigs) @@ -224,27 +225,20 @@ func (c *Controller) deletePrometheusAddlConfig(ctx context.Context, tenant *min return err } - var scrapeConfigs []configmaps.ScrapeConfig + var scrapeConfigs, exceptedScrapeConfigs []configmaps.ScrapeConfig err = yaml.Unmarshal(secret.Data[miniov2.PrometheusAddlScrapeConfigKey], &scrapeConfigs) if err != nil { return err } - // Check if the scrape config is present - hasScrapeConfig := false - scIndex := -1 - for i, sc := range scrapeConfigs { - if sc.JobName == tenant.PrometheusOperatorAddlConfigJobName() { - hasScrapeConfig = true - scIndex = i - break + for _, sc := range scrapeConfigs { + if sc.JobName != tenant.PrometheusOperatorAddlConfigJobName() { + exceptedScrapeConfigs = append(exceptedScrapeConfigs, sc) } } - if hasScrapeConfig { + if !reflect.DeepEqual(scrapeConfigs, exceptedScrapeConfigs) { klog.Infof("Deleting MinIO tenant Prometheus scrape config") - // Delete the config - newScrapeConfigs := append(scrapeConfigs[:scIndex], scrapeConfigs[scIndex+1:]...) // Update the secret - scrapeCfgYaml, err := yaml.Marshal(newScrapeConfigs) + scrapeCfgYaml, err := yaml.Marshal(exceptedScrapeConfigs) if err != nil { return err }