Skip to content

Commit 8498cca

Browse files
authored
Fix conversion of big.Float to float64 in kubernetes_manifest (#1661)
* big.Float to float64 conversion accuracy indicator is not an error * More precise testing of float handling in manifest
1 parent f8e02e0 commit 8498cca

File tree

7 files changed

+20
-13
lines changed

7 files changed

+20
-13
lines changed

.github/workflows/check_examples.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ jobs:
2020
terraform_version:
2121
- "0.14.10"
2222
- "0.15.5"
23-
- "1.0.6"
23+
- "1.0.11"
24+
- "1.1.7"
2425
env:
2526
TF_X_KUBERNETES_MANIFEST_RESOURCE: 1
2627
KUBE_CONFIG_PATH: "~/.kube/config"

.github/workflows/manifest_acc.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ jobs:
2323
# and there is no image available for the latest v1.19.x
2424
# - v1.19.11@sha256:07db187ae84b4b7de440a73886f008cf903fcf5764ba8106a9fd5243d6f32729
2525
terraform_version:
26-
- 1.0.8
26+
- 1.1.7
27+
- 1.0.11
2728
- 0.15.5
2829
- 0.14.11
2930
steps:

manifest/payload/from_value.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ func FromTFValue(in tftypes.Value, th map[string]string, ap *tftypes.AttributePa
3838
}
3939
return inv, nil
4040
}
41-
fnv, acc := nv.Float64()
42-
if acc != big.Exact {
43-
return nil, ap.NewErrorf("[%s] inexact float approximation when converting number value", ap.String())
44-
}
41+
fnv, _ := nv.Float64()
4542
return fnv, err
4643
case in.Type().Is(tftypes.String):
4744
var sv string

manifest/payload/from_value_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
)
1010

1111
func TestFromTFValue(t *testing.T) {
12+
// this mimics how terraform-plugin-go decodes floats that terraform sends over msgpack without a precision marker
13+
fv, _, err := big.ParseFloat("98.765", 10, 512, big.ToNearestEven)
14+
if err != nil {
15+
t.Fatalf("cannot create test float value out of string: %s", err)
16+
}
1217
samples := map[string]struct {
1318
In tftypes.Value
1419
Th map[string]string
@@ -18,9 +23,13 @@ func TestFromTFValue(t *testing.T) {
1823
In: tftypes.NewValue(tftypes.String, "hello"),
1924
Out: "hello",
2025
},
21-
"float-primitive": {
22-
In: tftypes.NewValue(tftypes.Number, big.NewFloat(100.2)),
23-
Out: 100.2,
26+
"float-primitive-native-big": {
27+
In: tftypes.NewValue(tftypes.Number, big.NewFloat(98.765)),
28+
Out: float64(98.765),
29+
},
30+
"float-primitive-from-string": {
31+
In: tftypes.NewValue(tftypes.Number, fv),
32+
Out: float64(98.765),
2433
},
2534
"boolean-primitive": {
2635
In: tftypes.NewValue(tftypes.Bool, true),

manifest/test/acceptance/customresource_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package acceptance
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910
"strings"
1011
"testing"
@@ -86,6 +87,7 @@ func TestKubernetesManifest_CustomResource(t *testing.T) {
8687
"kubernetes_manifest.test.object.metadata.name": name,
8788
"kubernetes_manifest.test.object.metadata.namespace": namespace,
8889
"kubernetes_manifest.test.object.data": "this is a test",
90+
"kubernetes_manifest.test.object.refs": json.Number("98.765"),
8991
"kubernetes_manifest.test.object.stuff.0": map[string]interface{}{"foo": interface{}(nil)},
9092
"kubernetes_manifest.test.object.limits": map[string]interface{}{
9193
"foo": interface{}("bar"),

manifest/test/acceptance/testdata/CustomResource/custom_resource.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ resource "kubernetes_manifest" "test" {
99
name = var.name
1010
}
1111
data = "this is a test"
12+
refs = 98.765
1213
stuff = [
1314
{
1415
foo = null

manifest/test/acceptance/testdata/CustomResource/terraform.tfvars

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)