Parcourir la source

Merge pull request #2434 from janlauber/fix-custom-provider

Enable Custom Pricing Provider in OpenCost for Public Cloud Kubernetes Clusters
Matt Ray il y a 2 ans
Parent
commit
318fe4ca06
2 fichiers modifiés avec 18 ajouts et 0 suppressions
  1. 11 0
      pkg/cloud/provider/provider.go
  2. 7 0
      pkg/env/costmodelenv.go

+ 11 - 0
pkg/cloud/provider/provider.go

@@ -278,6 +278,16 @@ func getClusterProperties(node *v1.Node) clusterProperties {
 		accountID:      "",
 		projectID:      "",
 	}
+
+	// Check for custom provider settings
+	if env.IsUseCustomProvider() {
+		// Use CSV provider if set
+		if env.IsUseCSVProvider() {
+			cp.provider = opencost.CSVProvider
+		}
+		return cp
+	}
+
 	// The second conditional is mainly if you're running opencost outside of GCE, say in a local environment.
 	if metadata.OnGCE() || strings.HasPrefix(providerID, "gce") {
 		cp.provider = opencost.GCPProvider
@@ -303,6 +313,7 @@ func getClusterProperties(node *v1.Node) clusterProperties {
 		cp.provider = opencost.OracleProvider
 		cp.configFileName = "oracle.json"
 	}
+	// Override provider to CSV if CSVProvider is used and custom provider is not set
 	if env.IsUseCSVProvider() {
 		cp.provider = opencost.CSVProvider
 	}

+ 7 - 0
pkg/env/costmodelenv.go

@@ -36,6 +36,7 @@ const (
 	RemotePWEnvVar                 = "REMOTE_WRITE_PASSWORD"
 	SQLAddressEnvVar               = "SQL_ADDRESS"
 	UseCSVProviderEnvVar           = "USE_CSV_PROVIDER"
+	UseCustomProviderEnvVar        = "USE_CUSTOM_PROVIDER"
 	CSVRegionEnvVar                = "CSV_REGION"
 	CSVEndpointEnvVar              = "CSV_ENDPOINT"
 	CSVPathEnvVar                  = "CSV_PATH"
@@ -409,6 +410,12 @@ func IsUseCSVProvider() bool {
 	return env.GetBool(UseCSVProviderEnvVar, false)
 }
 
+// IsUseCustomProvider returns the environment variable value for UseCustomProviderEnvVar which represents
+// whether or not the use of a custom cost provider is enabled.
+func IsUseCustomProvider() bool {
+	return env.GetBool(UseCustomProviderEnvVar, false)
+}
+
 // GetCSVRegion returns the environment variable value for CSVRegionEnvVar which represents the
 // region configured for a CSV provider.
 func GetCSVRegion() string {