Skip to content

Commit dbe98ff

Browse files
committed
fix: add DiffSuppressFunc to compare lists in GPU configuration
1 parent e429ea1 commit dbe98ff

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

castai/resource_node_template.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"reflect"
9+
"sort"
910
"strings"
1011
"time"
1112

@@ -651,28 +652,30 @@ func resourceNodeTemplate() *schema.Resource {
651652
"Custom instances are only supported in GCP.",
652653
},
653654
FieldNodeTemplateGpu: {
654-
Type: schema.TypeList,
655-
MaxItems: 1,
656-
Optional: true,
657-
Description: "GPU configuration.",
655+
Type: schema.TypeList,
656+
MaxItems: 1,
657+
Optional: true,
658+
Description: "GPU configuration.",
659+
DiffSuppressFunc: compareLists,
658660
Elem: &schema.Resource{
659661
Schema: map[string]*schema.Schema{
660662
FieldNodeTemplateEnableTimeSharing: {
661663
Type: schema.TypeBool,
662664
Optional: true,
663-
Default: false,
665+
Default: nil,
664666
Description: "Enable/disable GPU time-sharing.",
665667
},
666668
FieldNodeTemplateDefaultSharedClientsPerGpu: {
667669
Type: schema.TypeInt,
668670
Optional: true,
669-
Default: 1,
671+
Default: nil,
670672
Description: "Defines default number of shared clients per GPU.",
671673
},
672674
FieldNodeTemplateSharingConfiguration: {
673-
Type: schema.TypeList,
674-
Optional: true,
675-
Description: "Defines GPU sharing configurations for GPU devices.",
675+
Type: schema.TypeList,
676+
Optional: true,
677+
Description: "Defines GPU sharing configurations for GPU devices.",
678+
DiffSuppressFunc: compareLists,
676679
Elem: &schema.Resource{
677680
Schema: map[string]*schema.Schema{
678681
FieldNodeTemplateSharedGpuName: {
@@ -1705,3 +1708,37 @@ func toTemplateConstraintsNodeAffinity(o map[string]any) *sdk.NodetemplatesV1Tem
17051708

17061709
return &out
17071710
}
1711+
1712+
// compareLists compares state of two lists
1713+
// inspired by https://github.com/hashicorp/terraform-plugin-sdk/issues/477#issuecomment-1238807249
1714+
func compareLists(key, oldValue, newValue string, d *schema.ResourceData) bool {
1715+
// The key is a path not the list itself, e.g. "gpu.0"
1716+
lastDotIndex := strings.LastIndex(key, ".")
1717+
if lastDotIndex != -1 {
1718+
key = string(key[:lastDotIndex])
1719+
}
1720+
oldData, newData := d.GetChange(key)
1721+
if oldData == nil || newData == nil {
1722+
return false
1723+
}
1724+
1725+
oldArray, okOldArray := oldData.([]interface{})
1726+
newArray, okNewArray := newData.([]interface{})
1727+
if okOldArray && okNewArray {
1728+
if len(oldArray) != len(newArray) {
1729+
return false
1730+
}
1731+
oldItems := make([]string, len(oldArray))
1732+
newItems := make([]string, len(newArray))
1733+
for i, oldItem := range oldArray {
1734+
oldItems[i] = fmt.Sprint(oldItem)
1735+
}
1736+
for i, newItem := range newArray {
1737+
newItems[i] = fmt.Sprint(newItem)
1738+
}
1739+
sort.Strings(oldItems)
1740+
sort.Strings(newItems)
1741+
return reflect.DeepEqual(oldItems, newItems)
1742+
}
1743+
return false
1744+
}

castai/resource_node_template_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,6 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
682682
resource.TestCheckResourceAttr(resourceName, "constraints.0.resource_limits.#", "1"),
683683
resource.TestCheckResourceAttr(resourceName, "constraints.0.resource_limits.0.cpu_limit_enabled", "false"),
684684
resource.TestCheckResourceAttr(resourceName, "constraints.0.resource_limits.0.cpu_limit_max_cores", "0"),
685-
resource.TestCheckResourceAttr(resourceName, "gpu.0.default_shared_clients_per_gpu", "1"),
686-
resource.TestCheckResourceAttr(resourceName, "gpu.0.enable_time_sharing", "false"),
687685
),
688686
},
689687
{
@@ -808,16 +806,6 @@ func testAccNodeTemplateConfig(rName, clusterName string) string {
808806
key = "%[1]s-taint-key-4"
809807
}
810808
811-
gpu {
812-
default_shared_clients_per_gpu = 1
813-
enable_time_sharing = false
814-
815-
sharing_configuration {
816-
gpu_name = "L4"
817-
shared_clients_per_gpu = 8
818-
}
819-
}
820-
821809
constraints {
822810
fallback_restore_rate_seconds = 1800
823811
spot = true

castai/sdk/api.gen.go

Lines changed: 23 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)