Skip to content

Commit

Permalink
support param masters in cce cluster (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 authored Feb 10, 2021
1 parent 8eab829 commit 6d1a719
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
58 changes: 58 additions & 0 deletions flexibleengine/resource_flexibleengine_cce_cluster_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flexibleengine
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -119,6 +120,23 @@ func resourceCCEClusterV3() *schema.Resource {
ForceNew: true,
ValidateFunc: validateIP,
},
"masters": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Computed: true,
MaxItems: 3,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"availability_zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -208,6 +226,31 @@ func resourceClusterExtendParamV3(d *schema.ResourceData) map[string]string {
return m
}

func resourceClusterMastersV3(d *schema.ResourceData) ([]clusters.MasterSpec, error) {
if v, ok := d.GetOk("masters"); ok {
flavorId := d.Get("flavor_id").(string)
mastersRaw := v.([]interface{})
if strings.Contains(flavorId, "s1") && len(mastersRaw) != 1 {
return nil, fmt.Errorf("Error creating HuaweiCloud Cluster: "+
"single-master cluster need 1 az for master node, but got %d", len(mastersRaw))
}
if strings.Contains(flavorId, "s2") && len(mastersRaw) != 3 {
return nil, fmt.Errorf("Error creating HuaweiCloud Cluster: "+
"high-availability cluster need 3 az for master nodes, but got %d", len(mastersRaw))
}
masters := make([]clusters.MasterSpec, len(mastersRaw))
for i, raw := range mastersRaw {
rawMap := raw.(map[string]interface{})
masters[i] = clusters.MasterSpec{
MasterAZ: rawMap["availability_zone"].(string),
}
}
return masters, nil
}

return nil, nil
}

func resourceCCEClusterV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cceClient, err := config.cceV3Client(GetRegion(d, config))
Expand Down Expand Up @@ -236,6 +279,12 @@ func resourceCCEClusterV3Create(d *schema.ResourceData, meta interface{}) error
}
}

masters, err := resourceClusterMastersV3(d)
if err != nil {
return err
}
spec.Masters = masters

createOpts := clusters.CreateOpts{
Kind: "Cluster",
ApiVersion: "v3",
Expand Down Expand Up @@ -333,6 +382,15 @@ func resourceCCEClusterV3Read(d *schema.ResourceData, meta interface{}) error {
}
d.Set("certificate_users", userList)

// Set masters
var masterList []map[string]interface{}
for _, masterObj := range n.Spec.Masters {
master := make(map[string]interface{})
master["availability_zone"] = masterObj.MasterAZ
masterList = append(masterList, master)
}
d.Set("masters", masterList)

return nil
}

Expand Down
8 changes: 4 additions & 4 deletions flexibleengine/resource_flexibleengine_cce_cluster_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func TestAccCCEClusterV3_basic(t *testing.T) {
resource.TestCheckResourceAttr(
"flexibleengine_cce_cluster_v3.cluster_1", "flavor_id", "cce.s1.small"),
resource.TestCheckResourceAttr(
"flexibleengine_cce_cluster_v3.cluster_1", "cluster_version", "v1.11.7"),
"flexibleengine_cce_cluster_v3.cluster_1", "cluster_version", "v1.11.7-r2"),
resource.TestCheckResourceAttr(
"flexibleengine_cce_cluster_v3.cluster_1", "container_network_type", "overlay_l2"),
resource.TestCheckResourceAttr(
"flexibleengine_cce_cluster_v3.cluster_1", "authentication_mode", "x509"),
"flexibleengine_cce_cluster_v3.cluster_1", "authentication_mode", "rbac"),
),
},
{
Expand Down Expand Up @@ -131,7 +131,7 @@ resource "flexibleengine_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type="VirtualMachine"
flavor_id="cce.s1.small"
cluster_version = "v1.11.7"
cluster_version = "v1.11.7-r2"
vpc_id="%s"
subnet_id="%s"
container_network_type="overlay_l2"
Expand All @@ -144,7 +144,7 @@ resource "flexibleengine_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type="VirtualMachine"
flavor_id="cce.s1.small"
cluster_version = "v1.11.7"
cluster_version = "v1.11.7-r2"
vpc_id="%s"
subnet_id="%s"
container_network_type="overlay_l2"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions website/docs/r/cce_cluster_v3.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ The following arguments are supported:

* `eip` - (Optional) EIP address of the cluster. Changing this parameter will create a new cluster resource.

* `masters` - (Optional, List, ForceNew) Advanced configuration of master nodes. Changing this creates a new cluster.


The `masters` block supports:

* `availability_zone` - (Optional, String, ForceNew) Specifies the availability zone of the master node. Changing this creates a new cluster.

## Attributes Reference

All above argument parameters can be exported as attribute parameters along with attribute reference.
Expand Down

0 comments on commit 6d1a719

Please sign in to comment.