Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vsan fault domain support #1969

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 98 additions & 7 deletions vsphere/resource_vsphere_compute_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,26 @@ func resourceVSphereComputeCluster() *schema.Resource {
},
},
},
"fault_domains": {
Type: schema.TypeSet,
Optional: true,
Description: "The configuration for fault domain.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of fault domain.",
Optional: true,
},
"host_ids": {
Type: schema.TypeSet,
Description: "The managed object IDs of the hosts to put in the fault domain.",
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
vSphereTagAttributeKey: tagsSchema(),
customattribute.ConfigKey: customattribute.ConfigSchema(),
},
Expand Down Expand Up @@ -712,7 +732,11 @@ func resourceVSphereComputeClusterDelete(d *schema.ResourceData, meta interface{
}

version := viapi.ParseVersionFromClient(client)
spec := expandClusterConfigSpecEx(d, version)

spec, err := expandClusterConfigSpecEx(d, version, cluster)
if err != nil {
return err
}

if *spec.DasConfig.Enabled && *spec.DasConfig.AdmissionControlEnabled {
if v, ok := spec.DasConfig.AdmissionControlPolicy.(*types.ClusterFailoverHostAdmissionControlPolicy); ok {
Expand Down Expand Up @@ -948,7 +972,10 @@ func resourceVSphereComputeClusterApplyClusterConfiguration(
version := viapi.ParseVersionFromClient(client)

// Expand the cluster configuration.
spec := expandClusterConfigSpecEx(d, version)
spec, err := expandClusterConfigSpecEx(d, version, cluster)
if err != nil {
return err
}

// Note that the reconfigure for a cluster is the same as a standalone host,
// hence we send this to the computeresource helper's Reconfigure function.
Expand Down Expand Up @@ -1408,11 +1435,17 @@ func resourceVSphereComputeClusterFlattenData(

// expandClusterConfigSpecEx reads certain ResourceData keys and returns a
// ClusterConfigSpecEx.
func expandClusterConfigSpecEx(d *schema.ResourceData, version viapi.VSphereVersion) *types.ClusterConfigSpecEx {
func expandClusterConfigSpecEx(d *schema.ResourceData, version viapi.VSphereVersion, cluster *object.ClusterComputeResource) (*types.ClusterConfigSpecEx, error) {
props, err := clustercomputeresource.Properties(cluster)
if err != nil {
return nil, err
}

obj := &types.ClusterConfigSpecEx{
DasConfig: expandClusterDasConfigInfo(d, version),
DpmConfig: expandClusterDpmConfigInfo(d),
DrsConfig: expandClusterDrsConfigInfo(d, version),
DasConfig: expandClusterDasConfigInfo(d, version),
DpmConfig: expandClusterDpmConfigInfo(d),
DrsConfig: expandClusterDrsConfigInfo(d, version),
VsanHostConfigSpec: expandVsanHostConfig(d, props.ConfigurationEx.(*types.ClusterConfigInfoEx).VsanHostConfig),
}

if version.Newer(viapi.VSphereVersion{Product: version.Product, Major: 6, Minor: 5}) {
Expand All @@ -1421,7 +1454,61 @@ func expandClusterConfigSpecEx(d *schema.ResourceData, version viapi.VSphereVers
obj.ProactiveDrsConfig = expandClusterProactiveDrsConfigInfo(d)
}

return obj
return obj, nil
}

// expandVsanHostConfig reads current VsanHostConfigInfo and only update
// fault domain info so returns VsanHostConfigInfo as well.
func expandVsanHostConfig(d *schema.ResourceData, obj []types.VsanHostConfigInfo) []types.VsanHostConfigInfo {
zxinyu08 marked this conversation as resolved.
Show resolved Hide resolved
faultDomains := d.Get("fault_domains").(*schema.Set).List()
fdMap := make(map[string]string)
for _, faultDomain := range faultDomains {
fd := faultDomain.(map[string]interface{})
fdName := fd["name"].(string)
hosts := fd["host_ids"].(*schema.Set).List()
for _, host := range hosts {
fdMap[host.(string)] = fdName
}
}

result := make([]types.VsanHostConfigInfo, 0)
for _, hostConfig := range obj {
if fdName, ok := fdMap[hostConfig.HostSystem.Value]; ok {
hostConfig.FaultDomainInfo = &types.VsanHostFaultDomainInfo{
Name: fdName,
}
} else {
hostConfig.FaultDomainInfo = &types.VsanHostFaultDomainInfo{
Name: "",
}
}
result = append(result, hostConfig)
}

return result
}

func flattenClusterVsanHostConfigInfo(d *schema.ResourceData, obj []types.VsanHostConfigInfo) error {
faultDomains := make([]map[string]interface{}, 0)
fdMap := make(map[string]interface{})
for _, vsanHost := range obj {
if vsanHost.FaultDomainInfo != nil {
name := vsanHost.FaultDomainInfo.Name
if hostIds, ok := fdMap[name]; ok {
hostIds = append(hostIds.([]string), vsanHost.HostSystem.Value)
} else {
fdMap[name] = []string{vsanHost.HostSystem.Value}
}
}
}
for fdName, hostIds := range fdMap {
faultDomains = append(faultDomains, map[string]interface{}{
"name": fdName,
"host_ids": hostIds,
})
}

return d.Set("fault_domains", faultDomains)
}

func expandVsanPerfConfig(d *schema.ResourceData) (*vsantypes.VsanPerfsvcConfig, error) {
Expand Down Expand Up @@ -1738,6 +1825,10 @@ func flattenClusterConfigSpecEx(d *schema.ResourceData, obj *types.ClusterConfig
return err
}

if err := flattenClusterVsanHostConfigInfo(d, obj.VsanHostConfig); err != nil {
return err
}

if version.Newer(viapi.VSphereVersion{Product: version.Product, Major: 6, Minor: 5}) {
if err := flattenClusterInfraUpdateHaConfigInfo(d, obj.InfraUpdateHaConfig); err != nil {
return err
Expand Down
73 changes: 73 additions & 0 deletions vsphere/resource_vsphere_compute_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,52 @@ func TestAccResourceVSphereComputeCluster_vsanEsaEnabled(t *testing.T) {
})
}

func TestAccResourceVSphereComputeCluster_faultDomain(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
RunSweepers()
testAccPreCheck(t)
testAccResourceVSphereComputeClusterPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccResourceVSphereComputeClusterCheckExists(false),
Steps: []resource.TestStep{
{
Config: testAccResourceVSphereComputeClusterConfigFaultDomains(),
Check: resource.ComposeTestCheckFunc(
testAccResourceVSphereComputeClusterCheckExists(true),
resource.TestCheckTypeSetElemAttrPair(
"vsphere_compute_cluster.compute_cluster",
"fault_domains.*.host_ids.*",
"data.vsphere_host.roothost3",
"id",
),
resource.TestCheckTypeSetElemAttrPair(
"vsphere_compute_cluster.compute_cluster",
"fault_domains.*.host_ids.*",
"data.vsphere_host.roothost4",
"id",
),
resource.TestCheckTypeSetElemNestedAttrs(
"vsphere_compute_cluster.compute_cluster",
"fault_domains.*",
map[string]string{
"name": "fd1",
},
),
resource.TestCheckTypeSetElemNestedAttrs(
"vsphere_compute_cluster.compute_cluster",
"fault_domains.*",
map[string]string{
"name": "fd2",
},
),
),
},
},
})
}

func TestAccResourceVSphereComputeCluster_explicitFailoverHost(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -1043,6 +1089,33 @@ resource "vsphere_compute_cluster" "compute_cluster" {
)
}

func testAccResourceVSphereComputeClusterConfigFaultDomains() string {
return fmt.Sprintf(`
%s
resource "vsphere_compute_cluster" "compute_cluster" {
name = "testacc-compute-cluster"
datacenter_id = data.vsphere_datacenter.rootdc1.id
host_system_ids = [data.vsphere_host.roothost3.id, data.vsphere_host.roothost4.id]
vsan_enabled = true
fault_domains {
name = "fd1"
host_ids = [data.vsphere_host.roothost3.id]
}
fault_domains {
name = "fd2"
host_ids = [data.vsphere_host.roothost4.id]
}
force_evacuate_on_destroy = true
}
`,
testhelper.CombineConfigs(
testhelper.ConfigDataRootDC1(),
testhelper.ConfigDataRootHost3(),
testhelper.ConfigDataRootHost4(),
),
)
}

func testAccResourceVSphereComputeClusterConfigBasic() string {
return fmt.Sprintf(`
%s
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/compute_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ details, see the referenced link in the above paragraph.
group in the cluster.
* `cache` - The canonical name of the disk to use for vSAN cache.
* `storage` - An array of disk canonical names for vSAN storage.
* `fault_domains` - (Optional) Configurations of fault domain.
* `name` - The name of fault domain.
* `host_ids` - The managed object IDs of the hosts to put in the fault domain.

~> **NOTE:** You must disable vSphere HA before you enable vSAN on the cluster.
You can enable or re-enable vSphere HA after vSAN is configured.
Expand Down Expand Up @@ -520,6 +523,14 @@ resource "vsphere_compute_cluster" "compute_cluster" {
cache = data.vsphere_vmfs_disks.cache_disks[0]
storage = data.vsphere_vmfs_disks.storage_disks
}
fault_domains {
name = "fd1"
host_ids = [data.vsphere_host.faultdomain1_hosts.*.id]
}
fault_domains {
name = "fd2"
host_ids = [data.vsphere_host.faultdomain2_hosts.*.id]
}
}
```

Expand Down