|
6 | 6 | "fmt" |
7 | 7 | "log" |
8 | 8 | "reflect" |
| 9 | + "sort" |
9 | 10 | "strings" |
10 | 11 | "time" |
11 | 12 |
|
@@ -651,28 +652,30 @@ func resourceNodeTemplate() *schema.Resource { |
651 | 652 | "Custom instances are only supported in GCP.", |
652 | 653 | }, |
653 | 654 | 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, |
658 | 660 | Elem: &schema.Resource{ |
659 | 661 | Schema: map[string]*schema.Schema{ |
660 | 662 | FieldNodeTemplateEnableTimeSharing: { |
661 | 663 | Type: schema.TypeBool, |
662 | 664 | Optional: true, |
663 | | - Default: false, |
| 665 | + Default: nil, |
664 | 666 | Description: "Enable/disable GPU time-sharing.", |
665 | 667 | }, |
666 | 668 | FieldNodeTemplateDefaultSharedClientsPerGpu: { |
667 | 669 | Type: schema.TypeInt, |
668 | 670 | Optional: true, |
669 | | - Default: 1, |
| 671 | + Default: nil, |
670 | 672 | Description: "Defines default number of shared clients per GPU.", |
671 | 673 | }, |
672 | 674 | 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, |
676 | 679 | Elem: &schema.Resource{ |
677 | 680 | Schema: map[string]*schema.Schema{ |
678 | 681 | FieldNodeTemplateSharedGpuName: { |
@@ -1705,3 +1708,37 @@ func toTemplateConstraintsNodeAffinity(o map[string]any) *sdk.NodetemplatesV1Tem |
1705 | 1708 |
|
1706 | 1709 | return &out |
1707 | 1710 | } |
| 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 | +} |
0 commit comments