Pārlūkot izejas kodu

Replace Kubecost env var names with neutral env vars, leave old env vars as backup.

Matt Bolt 1 gadu atpakaļ
vecāks
revīzija
d5ee037a66

+ 2 - 2
pkg/clustercache/clustercache.go

@@ -56,8 +56,8 @@ func NewKubernetesClusterCacheV1(client kubernetes.Interface) cc.ClusterCache {
 	batchClient := client.BatchV1().RESTClient()
 	pdbClient := client.PolicyV1().RESTClient()
 
-	kubecostNamespace := env.GetKubecostNamespace()
-	log.Infof("NAMESPACE: %s", kubecostNamespace)
+	installNamespace := env.GetInstallNamespace()
+	log.Infof("NAMESPACE: %s", installNamespace)
 
 	kcc := &KubernetesClusterCache{
 		client:                     client,

+ 2 - 2
pkg/cmd/agent/agent.go

@@ -76,7 +76,7 @@ func Execute(opts *AgentOpts) error {
 
 	// Create ConfigFileManager for synchronization of shared configuration
 	confManager := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
-		BucketStoreConfig: env.GetKubecostConfigBucket(),
+		BucketStoreConfig: env.GetConfigBucketFile(),
 		LocalConfigPath:   "/",
 	})
 
@@ -129,7 +129,7 @@ func Execute(opts *AgentOpts) error {
 	}
 
 	// Append the pricing config watcher
-	kubecostNamespace := env.GetKubecostNamespace()
+	kubecostNamespace := env.GetInstallNamespace()
 	configWatchers := watcher.NewConfigMapWatchers(k8sClient, kubecostNamespace)
 	configWatchers.AddWatcher(provider.ConfigWatcherFor(cloudProvider))
 	configWatchers.Watch()

+ 3 - 3
pkg/costmodel/allocation.go

@@ -330,7 +330,7 @@ func (cm *CostModel) computeAllocation(start, end time.Time, resolution time.Dur
 	resChNetInternetPricePerGiB := source.WithGroup(grp, ds.QueryNetInternetPricePerGiB(start, end))
 
 	var resChNodeLabels *source.QueryGroupFuture[source.NodeLabelsResult]
-	if env.GetAllocationNodeLabelsEnabled() {
+	if env.IsAllocationNodeLabelsEnabled() {
 		resChNodeLabels = source.WithGroup(grp, ds.QueryNodeLabels(start, end))
 	}
 
@@ -394,7 +394,7 @@ func (cm *CostModel) computeAllocation(start, end time.Time, resolution time.Dur
 	resNetInternetPricePerGiB, _ := resChNetInternetPricePerGiB.Await()
 
 	var resNodeLabels []*source.NodeLabelsResult
-	if env.GetAllocationNodeLabelsEnabled() {
+	if env.IsAllocationNodeLabelsEnabled() {
 		resNodeLabels, _ = resChNodeLabels.Await()
 	}
 	resNamespaceLabels, _ := resChNamespaceLabels.Await()
@@ -453,7 +453,7 @@ func (cm *CostModel) computeAllocation(start, end time.Time, resolution time.Dur
 	// (e.g. applyCPUCoresAllocated, etc.) -- otherwise, node labels will fail
 	// to correctly apply to the pods.
 	var nodeLabels map[nodeKey]map[string]string
-	if env.GetAllocationNodeLabelsEnabled() {
+	if env.IsAllocationNodeLabelsEnabled() {
 		nodeLabels = resToNodeLabels(resNodeLabels)
 	}
 	namespaceLabels := resToNamespaceLabels(resNamespaceLabels)

+ 2 - 2
pkg/costmodel/cluster.go

@@ -130,7 +130,7 @@ func ClusterDisks(dataSource source.OpenCostDataSource, cp models.Provider, star
 	resLocalStorageBytes := []*source.LocalStorageBytesResult{}
 	resLocalActiveMins := []*source.LocalStorageActiveMinutesResult{}
 
-	if env.GetAssetIncludeLocalDiskCost() {
+	if env.IsAssetIncludeLocalDiskCost() {
 		resChLocalStorageCost := source.WithGroup(grp, mq.QueryLocalStorageCost(start, end))
 		resChLocalStorageUsedCost := source.WithGroup(grp, mq.QueryLocalStorageUsedCost(start, end))
 		resChLocalStoreageUsedAvg := source.WithGroup(grp, mq.QueryLocalStorageUsedAvg(start, end))
@@ -405,7 +405,7 @@ func ClusterDisks(dataSource source.OpenCostDataSource, cp models.Provider, star
 		}
 	}
 
-	if !env.GetAssetIncludeLocalDiskCost() {
+	if !env.IsAssetIncludeLocalDiskCost() {
 		return filterOutLocalPVs(diskMap), nil
 	}
 

+ 2 - 2
pkg/costmodel/networkinsight.go

@@ -11,7 +11,7 @@ import (
 )
 
 func (cm *CostModel) ComputeNetworkInsights(start, end time.Time, resolution time.Duration) (*opencost.NetworkInsightSet, error) {
-	log.Debugf("Network Insight compute called on prometheus source for window  %s", opencost.NewClosedWindow(start, end).String())
+	log.Debugf("Network Insight compute called on CostModel for window  %s", opencost.NewClosedWindow(start, end).String())
 
 	// If the duration is short enough, compute the network insight directly
 	if end.Sub(start) <= cm.BatchDuration {
@@ -40,7 +40,7 @@ func (cm *CostModel) ComputeNetworkInsights(start, end time.Time, resolution tim
 		totalNis.Accumulate(nis, []opencost.NetworkInsightProperty{})
 		s = e
 	}
-	return totalNis, fmt.Errorf("unable to query prometheus for large duration")
+	return totalNis, fmt.Errorf("unable to query data source for large duration")
 }
 
 func (cm *CostModel) GetNetworkInsightSet(start, end time.Time) (*opencost.NetworkInsightSet, error) {

+ 4 - 37
pkg/costmodel/router.go

@@ -85,39 +85,6 @@ type Accesses struct {
 	settingsMutex       sync.Mutex
 }
 
-// FilterFunc is a filter that returns true iff the given CostData should be filtered out, and the environment that was used as the filter criteria, if it was an aggregate
-type FilterFunc func(*CostData) (bool, string)
-
-// FilterCostData allows through only CostData that matches all the given filter functions
-func FilterCostData(data map[string]*CostData, retains []FilterFunc, filters []FilterFunc) (map[string]*CostData, int, map[string]int) {
-	result := make(map[string]*CostData)
-	filteredEnvironments := make(map[string]int)
-	filteredContainers := 0
-DataLoop:
-	for key, datum := range data {
-		for _, rf := range retains {
-			if ok, _ := rf(datum); ok {
-				result[key] = datum
-				// if any retain function passes, the data is retained and move on
-				continue DataLoop
-			}
-		}
-		for _, ff := range filters {
-			if ok, environment := ff(datum); !ok {
-				if environment != "" {
-					filteredEnvironments[environment]++
-				}
-				filteredContainers++
-				// if any filter function check fails, move on to the next datum
-				continue DataLoop
-			}
-		}
-		result[key] = datum
-	}
-
-	return result, filteredContainers, filteredEnvironments
-}
-
 func filterFields(fields string, data map[string]*CostData) map[string]CostData {
 	fs := strings.Split(fields, ",")
 	fmap := make(map[string]bool)
@@ -379,7 +346,7 @@ func (a *Accesses) GetInstallNamespace(w http.ResponseWriter, r *http.Request, _
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Access-Control-Allow-Origin", "*")
 
-	ns := env.GetKubecostNamespace()
+	ns := env.GetInstallNamespace()
 	w.Write([]byte(ns))
 }
 
@@ -427,7 +394,7 @@ func (a *Accesses) GetInstallInfo(w http.ResponseWriter, r *http.Request, _ http
 }
 
 func GetKubecostContainers(kubeClientSet kubernetes.Interface) ([]ContainerInfo, error) {
-	pods, err := kubeClientSet.CoreV1().Pods(env.GetKubecostNamespace()).List(context.Background(), metav1.ListOptions{
+	pods, err := kubeClientSet.CoreV1().Pods(env.GetInstallNamespace()).List(context.Background(), metav1.ListOptions{
 		LabelSelector: "app=cost-analyzer",
 		FieldSelector: "status.phase=Running",
 		Limit:         1,
@@ -505,7 +472,7 @@ func Initialize(router *httprouter.Router, additionalConfigWatchers ...*watcher.
 
 	// Create ConfigFileManager for synchronization of shared configuration
 	confManager := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
-		BucketStoreConfig: env.GetKubecostConfigBucket(),
+		BucketStoreConfig: env.GetConfigBucketFile(),
 		LocalConfigPath:   "/",
 	})
 
@@ -556,7 +523,7 @@ func Initialize(router *httprouter.Router, additionalConfigWatchers ...*watcher.
 	}
 
 	// Append the pricing config watcher
-	kubecostNamespace := env.GetKubecostNamespace()
+	kubecostNamespace := env.GetInstallNamespace()
 
 	configWatchers := watcher.NewConfigMapWatchers(kubeClientset, kubecostNamespace, additionalConfigWatchers...)
 	configWatchers.AddWatcher(provider.ConfigWatcherFor(cloudProvider))

+ 36 - 54
pkg/env/costmodelenv.go

@@ -24,23 +24,21 @@ const (
 	AzureBillingAccountEnvVar            = "AZURE_BILLING_ACCOUNT"
 	AzureDownloadBillingDataToDiskEnvVar = "AZURE_DOWNLOAD_BILLING_DATA_TO_DISK"
 
-	ReleaseNameEnvVar              = "RELEASE_NAME"
-	KubecostNamespaceEnvVar        = "KUBECOST_NAMESPACE"
-	PodNameEnvVar                  = "POD_NAME"
-	ClusterIDEnvVar                = "CLUSTER_ID"
-	ClusterProfileEnvVar           = "CLUSTER_PROFILE"
-	RemoteEnabledEnvVar            = "REMOTE_WRITE_ENABLED"
-	RemotePWEnvVar                 = "REMOTE_WRITE_PASSWORD"
-	SQLAddressEnvVar               = "SQL_ADDRESS"
-	UseCSVProviderEnvVar           = "USE_CSV_PROVIDER"
-	UseCustomProviderEnvVar        = "USE_CUSTOM_PROVIDER"
-	CSVRegionEnvVar                = "CSV_REGION"
-	CSVEndpointEnvVar              = "CSV_ENDPOINT"
-	CSVPathEnvVar                  = "CSV_PATH"
-	ConfigPathEnvVar               = "CONFIG_PATH"
-	CloudProviderAPIKeyEnvVar      = "CLOUD_PROVIDER_API_KEY"
-	PromlessEnvVar                 = "PROMLESS"
-	DisableAggregateCostModelCache = "DISABLE_AGGREGATE_COST_MODEL_CACHE"
+	ReleaseNameEnvVar         = "RELEASE_NAME"
+	PodNameEnvVar             = "POD_NAME"
+	ClusterIDEnvVar           = "CLUSTER_ID"
+	ClusterProfileEnvVar      = "CLUSTER_PROFILE"
+	RemoteEnabledEnvVar       = "REMOTE_WRITE_ENABLED"
+	RemotePWEnvVar            = "REMOTE_WRITE_PASSWORD"
+	SQLAddressEnvVar          = "SQL_ADDRESS"
+	UseCSVProviderEnvVar      = "USE_CSV_PROVIDER"
+	UseCustomProviderEnvVar   = "USE_CUSTOM_PROVIDER"
+	CSVRegionEnvVar           = "CSV_REGION"
+	CSVEndpointEnvVar         = "CSV_ENDPOINT"
+	CSVPathEnvVar             = "CSV_PATH"
+	ConfigPathEnvVar          = "CONFIG_PATH"
+	CloudProviderAPIKeyEnvVar = "CLOUD_PROVIDER_API_KEY"
+	PromlessEnvVar            = "PROMLESS"
 
 	EmitPodAnnotationsMetricEnvVar       = "EMIT_POD_ANNOTATIONS_METRIC"
 	EmitNamespaceAnnotationsMetricEnvVar = "EMIT_NAMESPACE_ANNOTATIONS_METRIC"
@@ -62,16 +60,12 @@ const (
 
 	UTCOffsetEnvVar = "UTC_OFFSET"
 
-	CacheWarmingEnabledEnvVar    = "CACHE_WARMING_ENABLED"
-	ETLEnabledEnvVar             = "ETL_ENABLED"
-	ETLResolutionSeconds         = "ETL_RESOLUTION_SECONDS"
-	LegacyExternalAPIDisabledVar = "LEGACY_EXTERNAL_API_DISABLED"
+	ETLEnabledEnvVar     = "ETL_ENABLED"
+	ETLResolutionSeconds = "ETL_RESOLUTION_SECONDS"
 
-	PricingConfigmapName  = "PRICING_CONFIGMAP_NAME"
-	MetricsConfigmapName  = "METRICS_CONFIGMAP_NAME"
-	KubecostJobNameEnvVar = "KUBECOST_JOB_NAME"
+	PricingConfigmapName = "PRICING_CONFIGMAP_NAME"
+	MetricsConfigmapName = "METRICS_CONFIGMAP_NAME"
 
-	KubecostConfigBucketEnvVar    = "KUBECOST_CONFIG_BUCKET"
 	ClusterInfoFileEnabledEnvVar  = "CLUSTER_INFO_FILE_ENABLED"
 	ClusterCacheFileEnabledEnvVar = "CLUSTER_CACHE_FILE_ENABLED"
 
@@ -116,6 +110,13 @@ const (
 	CarbonEstimatesEnabledEnvVar = "CARBON_ESTIMATES_ENABLED"
 
 	UseCacheV1 = "USE_CACHE_V1"
+
+	InstallNamespaceEnvVar = "INSTALL_NAMESPACE"
+	ConfigBucketEnvVar     = "CONFIG_BUCKET"
+
+	// Deprecated
+	KubecostNamespaceEnvVar    = "KUBECOST_NAMESPACE"
+	KubecostConfigBucketEnvVar = "KUBECOST_CONFIG_BUCKET"
 )
 
 const DefaultConfigMountPath = "/var/configs"
@@ -150,10 +151,10 @@ func GetAPIPort() int {
 	return env.GetInt(APIPortEnvVar, 9003)
 }
 
-// GetKubecostConfigBucket returns a file location for a mounted bucket configuration which is used to store
-// a subset of kubecost configurations that require sharing via remote storage.
-func GetKubecostConfigBucket() string {
-	return env.Get(KubecostConfigBucketEnvVar, "")
+// GetConfigBucketFile returns a file location for a mounted bucket configuration which is used to store
+// a subset of configurations that require sharing via remote storage.
+func GetConfigBucketFile() string {
+	return env.Get(ConfigBucketEnvVar, env.Get(KubecostConfigBucketEnvVar, ""))
 }
 
 // IsClusterInfoFileEnabled returns true if the cluster info is read from a file or pulled from the local
@@ -265,10 +266,10 @@ func IsAzureDownloadBillingDataToDisk() bool {
 	return env.GetBool(AzureDownloadBillingDataToDiskEnvVar, true)
 }
 
-// GetKubecostNamespace returns the environment variable value for KubecostNamespaceEnvVar which
-// represents the namespace the cost model exists in.
-func GetKubecostNamespace() string {
-	return env.Get(KubecostNamespaceEnvVar, "kubecost")
+// GetInstallNamespace returns the environment variable value that is set for the kubernetes namespace
+// this service is installed in.
+func GetInstallNamespace() string {
+	return env.Get(InstallNamespaceEnvVar, env.Get(KubecostNamespaceEnvVar, "opencost"))
 }
 
 // GetPodName returns the name of the current running pod. If this environment variable is not set,
@@ -293,12 +294,6 @@ func IsKubeRbacProxyEnabled() bool {
 	return env.GetBool(KubeRbacProxyEnabled, false)
 }
 
-// IsAggregateCostModelCacheDisabled returns the environment variable value for DisableAggregateCostModelCache which
-// will inform the aggregator on whether to load cached data. Defaults to false
-func IsAggregateCostModelCacheDisabled() bool {
-	return env.GetBool(DisableAggregateCostModelCache, 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 {
@@ -411,15 +406,6 @@ func GetParsedUTCOffset() time.Duration {
 	return offset
 }
 
-// GetKubecostJobName returns the environment variable value for KubecostJobNameEnvVar
-func GetKubecostJobName() string {
-	return env.Get(KubecostJobNameEnvVar, "kubecost")
-}
-
-func IsCacheWarmingEnabled() bool {
-	return env.GetBool(CacheWarmingEnabledEnvVar, true)
-}
-
 func IsETLEnabled() bool {
 	return env.GetBool(ETLEnabledEnvVar, true)
 }
@@ -434,21 +420,17 @@ func GetETLResolution() time.Duration {
 	return secs * time.Second
 }
 
-func LegacyExternalCostsAPIDisabled() bool {
-	return env.GetBool(LegacyExternalAPIDisabledVar, false)
-}
-
 // IsIngestingPodUID returns the env variable from ingestPodUID, which alters the
 // contents of podKeys in Allocation
 func IsIngestingPodUID() bool {
 	return env.GetBool(IngestPodUIDEnvVar, false)
 }
 
-func GetAllocationNodeLabelsEnabled() bool {
+func IsAllocationNodeLabelsEnabled() bool {
 	return env.GetBool(AllocationNodeLabelsEnabled, true)
 }
 
-func GetAssetIncludeLocalDiskCost() bool {
+func IsAssetIncludeLocalDiskCost() bool {
 	return env.GetBool(AssetIncludeLocalDiskCostEnvVar, true)
 }
 

+ 0 - 37
pkg/env/costmodelenv_test.go

@@ -43,43 +43,6 @@ func TestGetAPIPort(t *testing.T) {
 
 }
 
-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_AGGREGATE_COST_MODEL_CACHE is set to false",
-			want: false,
-			pre: func() {
-				os.Setenv("DISABLE_AGGREGATE_COST_MODEL_CACHE", "false")
-			},
-		},
-		{
-			name: "Ensure the value is true when DISABLE_AGGREGATE_COST_MODEL_CACHE is set to true",
-			want: true,
-			pre: func() {
-				os.Setenv("DISABLE_AGGREGATE_COST_MODEL_CACHE", "true")
-			},
-		},
-	}
-	for _, tt := range tests {
-		if tt.pre != nil {
-			tt.pre()
-		}
-		t.Run(tt.name, func(t *testing.T) {
-			if got := IsAggregateCostModelCacheDisabled(); got != tt.want {
-				t.Errorf("IsAggregateCostModelCacheDisabled() = %v, want %v", got, tt.want)
-			}
-		})
-	}
-}
-
 func TestGetExportCSVMaxDays(t *testing.T) {
 	tests := []struct {
 		name string