From f990676168b727d6999d2ee6f84fcd6fe87c55b6 Mon Sep 17 00:00:00 2001 From: maskarb Date: Mon, 7 Aug 2023 13:00:22 -0400 Subject: [PATCH] address PR feedback --- collector/collector.go | 10 +++++----- collector/collector_test.go | 1 + collector/prometheus.go | 7 +++++-- controllers/kokumetricsconfig_controller_test.go | 5 ++++- scripts/txt_replace.py | 16 +++++++--------- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 88460b7aa..ad5656bfe 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -194,7 +194,7 @@ func GenerateReports(cr *metricscfgv1beta1.MetricsConfig, dirCfg *dirconfig.Dire // ################################################################################################################ log.Info("querying for node metrics") nodeResults := mappedResults{} - if err := c.getQueryRangeResults(nodeQueries, &nodeResults, 5); err != nil { + if err := c.getQueryRangeResults(nodeQueries, &nodeResults, MaxRetries); err != nil { return err } @@ -274,7 +274,7 @@ func generateCostManagementReports(log gologr.Logger, c *PrometheusCollector, di log.Info("querying for pod metrics") podResults := mappedResults{} - if err := c.getQueryRangeResults(podQueries, &podResults, 5); err != nil { + if err := c.getQueryRangeResults(podQueries, &podResults, MaxRetries); err != nil { return err } @@ -314,7 +314,7 @@ func generateCostManagementReports(log gologr.Logger, c *PrometheusCollector, di log.Info("querying for storage metrics") volResults := mappedResults{} - if err := c.getQueryRangeResults(volQueries, &volResults, 5); err != nil { + if err := c.getQueryRangeResults(volQueries, &volResults, MaxRetries); err != nil { return err } @@ -346,7 +346,7 @@ func generateCostManagementReports(log gologr.Logger, c *PrometheusCollector, di log.Info("querying for namespaces") namespaceResults := mappedResults{} - if err := c.getQueryRangeResults(namespaceQueries, &namespaceResults, 5); err != nil { + if err := c.getQueryRangeResults(namespaceQueries, &namespaceResults, MaxRetries); err != nil { return err } @@ -381,7 +381,7 @@ func generateResourceOpimizationReports(log gologr.Logger, c *PrometheusCollecto ts := c.TimeSeries.End log.Info(fmt.Sprintf("querying for resource-optimization for ts: %+v", ts)) rosResults := mappedResults{} - if err := c.getQueryResults(ts, resourceOptimizationQueries, &rosResults, 5); err != nil { + if err := c.getQueryResults(ts, resourceOptimizationQueries, &rosResults, MaxRetries); err != nil { return err } diff --git a/collector/collector_test.go b/collector/collector_test.go index ac3114979..9705f159b 100644 --- a/collector/collector_test.go +++ b/collector/collector_test.go @@ -270,6 +270,7 @@ func TestGenerateReportsNoCost(t *testing.T) { } func TestGenerateReportsQueryErrors(t *testing.T) { + MaxRetries = 1 mapResults := make(mappedMockPromResult) fakeCollector := &PrometheusCollector{ PromConn: mockPrometheusConnection{ diff --git a/collector/prometheus.go b/collector/prometheus.go index b2dc46c26..8c6f2813a 100644 --- a/collector/prometheus.go +++ b/collector/prometheus.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "io/ioutil" + "math" "path/filepath" "reflect" "time" @@ -27,6 +28,8 @@ const ( statusConfiguration ) +var MaxRetries int = 5 + var ( ps *metricscfgv1beta1.PrometheusSpec @@ -236,7 +239,7 @@ func (c *PrometheusCollector) getQueryRangeResults(queries *querys, results *map if len(queriesToRetry) > 0 { retries-- - waitTime := time.Duration(5-retries) * time.Second + waitTime := time.Duration(math.Pow(2, float64(MaxRetries-retries))) * time.Second log.Info(fmt.Sprintf("retrying failed queries after %s seconds", waitTime)) time.Sleep(waitTime) return c.getQueryRangeResults(&queriesToRetry, results, retries) @@ -276,7 +279,7 @@ func (c *PrometheusCollector) getQueryResults(ts time.Time, queries *querys, res if len(queriesToRetry) > 0 { retries-- - waitTime := time.Duration(5-retries) * time.Second + waitTime := time.Duration(math.Pow(2, float64(MaxRetries-retries))) * time.Second log.Info(fmt.Sprintf("retrying failed queries after %s seconds", waitTime)) time.Sleep(waitTime) return c.getQueryResults(ts, &queriesToRetry, results, retries) diff --git a/controllers/kokumetricsconfig_controller_test.go b/controllers/kokumetricsconfig_controller_test.go index a7556216b..b5d10a05c 100644 --- a/controllers/kokumetricsconfig_controller_test.go +++ b/controllers/kokumetricsconfig_controller_test.go @@ -268,7 +268,7 @@ func shutdown() { os.RemoveAll(testingDir) } -var _ = Describe("MetricsConfigController - CRD Handling", func() { +var _ = Describe("MetricsConfigController - CRD Handling", Ordered, func() { const timeout = time.Second * 60 const interval = time.Second * 1 @@ -288,6 +288,9 @@ var _ = Describe("MetricsConfigController - CRD Handling", func() { emptyDep1 := emptyDirDeployment.DeepCopy() emptyDep2 := emptyDirDeployment.DeepCopy() + // override MaxRetries to reduce testing time + collector.MaxRetries = 1 + BeforeEach(func() { GitCommit = "1234567" diff --git a/scripts/txt_replace.py b/scripts/txt_replace.py index 96a13c842..e5b970266 100755 --- a/scripts/txt_replace.py +++ b/scripts/txt_replace.py @@ -38,7 +38,7 @@ def replace(file_path, pattern, subst): remove(file_path) move(abs_path, file_path) -def fix_csv(version, previous_version, image_sha, namespace=""): +def fix_csv(version, previous_version, image_sha, namespace): # get the operator description from docs docs = open("docs/csv-description.md") @@ -72,17 +72,15 @@ def fix_dockerfile(version): replace(filename, k, v) if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Script for updating the appropriate fields of the CSV", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument("-n", "--namespace", help="namespace used for testing") + parser = argparse.ArgumentParser(description="Script for updating the appropriate fields of the CSV") + parser.add_argument("-n", "--namespace", help="namespace used for testing", default="") parser.add_argument("version", help="New version of the CSV") parser.add_argument("previous_version", help="Version of CSV being replaced") parser.add_argument("image_sha", help="The image sha of the compiled operator") args = parser.parse_args() - config = vars(args) - print(config) + print(vars(args)) - check_version(config["version"], config["previous_version"]) + check_version(args.version, args.previous_version) - fix_csv(**config) - fix_dockerfile(config["version"]) + fix_csv(args.version, args.previous_version, args.image_sha, args.namespace) + fix_dockerfile(args.version)