Răsfoiți Sursa

feat(config): Expose DisableCache as an environment variable

This adds the ability to set the value for `disableCache` via an
environment variable `DISABLE_CACHE`. The default value is set to false.
This will influence whether ComputeAggregateCostModel will use
data if it's found in the cache.

Add unit tests to ensure the default value for `IsCacheDisabled` is
false and that it can be overidden.

- Relates to #1472

Signed-off-by: pokom <mark.poko@grafana.com>
pokom 3 ani în urmă
părinte
comite
d95572f408
3 a modificat fișierele cu 51 adăugiri și 1 ștergeri
  1. 1 1
      pkg/costmodel/aggregation.go
  2. 7 0
      pkg/env/costmodelenv.go
  3. 43 0
      pkg/env/costmodelenv_test.go

+ 1 - 1
pkg/costmodel/aggregation.go

@@ -1037,7 +1037,7 @@ func DefaultAggregateQueryOpts() *AggregateQueryOpts {
 		AllocateIdle:          false,
 		IncludeTimeSeries:     true,
 		IncludeEfficiency:     true,
-		DisableCache:          false,
+		DisableCache:          env.IsCacheDisabled(),
 		ClearCache:            false,
 		NoCache:               false,
 		NoExpireCache:         false,

+ 7 - 0
pkg/env/costmodelenv.go

@@ -32,6 +32,7 @@ const (
 	CSVPathEnvVar                  = "CSV_PATH"
 	ConfigPathEnvVar               = "CONFIG_PATH"
 	CloudProviderAPIKeyEnvVar      = "CLOUD_PROVIDER_API_KEY"
+	DisableCache                   = "DISABLE_CACHE"
 
 	EmitPodAnnotationsMetricEnvVar       = "EMIT_POD_ANNOTATIONS_METRIC"
 	EmitNamespaceAnnotationsMetricEnvVar = "EMIT_NAMESPACE_ANNOTATIONS_METRIC"
@@ -239,6 +240,12 @@ func GetInsecureSkipVerify() bool {
 	return GetBool(InsecureSkipVerify, false)
 }
 
+// IsCacheDisabled returns the environment variable value for DisableCache which
+// will inform the aggregator on whether to load cached data. Defaults to false
+func IsCacheDisabled() bool {
+	return GetBool(DisableCache, false)
+}
+
 // IsRemoteEnabled returns the environment variable value for RemoteEnabledEnvVar which represents whether
 // or not remote write is enabled for prometheus for use with SQL backed persistent storage.
 func IsRemoteEnabled() bool {

+ 43 - 0
pkg/env/costmodelenv_test.go

@@ -0,0 +1,43 @@
+package env
+
+import (
+	"os"
+	"testing"
+)
+
+func TestIsCacheDisabled(t *testing.T) {
+	tests := []struct {
+		name string
+		want bool
+		pre  func()
+	}{
+		{
+			name: "Ensure the default value is false",
+			want: false,
+		},
+		{
+			name: "Ensure the value is false when DISABLE_CACHE is set to valse",
+			want: false,
+			pre: func() {
+				os.Setenv("DISABLE_CACHE", "false")
+			},
+		},
+		{
+			name: "Ensure the value is false when DISABLE_CACHE is set to valse",
+			want: true,
+			pre: func() {
+				os.Setenv("DISABLE_CACHE", "true")
+			},
+		},
+	}
+	for _, tt := range tests {
+		if tt.pre != nil {
+			tt.pre()
+		}
+		t.Run(tt.name, func(t *testing.T) {
+			if got := IsCacheDisabled(); got != tt.want {
+				t.Errorf("IsCacheDisabled() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}