Parcourir la source

skip custom pricing if empty string (#2578)

* default to 1.0 if custom pricing is empty string

Signed-off-by: nickcurie <ncurie@kubecost.com>

* comment + ignore empty string

Signed-off-by: nickcurie <ncurie@kubecost.com>

---------

Signed-off-by: nickcurie <ncurie@kubecost.com>
Nick Curie il y a 2 ans
Parent
commit
206866dde7
2 fichiers modifiés avec 12 ajouts et 7 suppressions
  1. 11 6
      pkg/cloud/models/models.go
  2. 1 1
      pkg/cloud/models/models_test.go

+ 11 - 6
pkg/cloud/models/models.go

@@ -249,13 +249,18 @@ func SetCustomPricingField(obj *CustomPricing, name string, value string) error
 	// from getting set here.
 	switch strings.ToLower(name) {
 	case "cpu", "gpu", "ram", "spotcpu", "spotgpu", "spotram", "storage", "zonenetworkegress", "regionnetworkegress", "internetnetworkegress":
-		// Validate that "value" represents a real floating point number, and
-		// set precision, bits, etc. Do not allow NaN.
-		val, err := sanitizeFloatString(value, false)
-		if err != nil {
-			return fmt.Errorf("invalid numeric value for field '%s': %s", name, value)
+		// If we are sent an empty string, ignore the key and don't change the value
+		if value == "" {
+			return nil
+		} else {
+			// Validate that "value" represents a real floating point number, and
+			// set precision, bits, etc. Do not allow NaN.
+			val, err := sanitizeFloatString(value, false)
+			if err != nil {
+				return fmt.Errorf("invalid numeric value for field '%s': %s", name, value)
+			}
+			value = val
 		}
-		value = val
 	default:
 	}
 

+ 1 - 1
pkg/cloud/models/models_test.go

@@ -51,7 +51,7 @@ func TestSetSetCustomPricingField(t *testing.T) {
 			fieldName:  "%s",
 			fieldValue: "",
 			expValue:   defaultValue, // assert that the default value is not mutated
-			expError:   fmt.Errorf("invalid numeric value for field"),
+			expError:   nil,
 		},
 	}