Skip to content

Commit

Permalink
cluster networking fields
Browse files Browse the repository at this point in the history
  • Loading branch information
furkhat committed Dec 4, 2020
1 parent 2ca0b49 commit 5b9d085
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 25 deletions.
3 changes: 3 additions & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ The following arguments are supported:
* `audit_logging` - (Optional) Audit logging settings.
* `pod_security_policy` - (Optional) Pod security policies allow detailed authorization of pod creation and updates.
* `pod_node_selector` - (Optional) Configure PodNodeSelector admission plugin at the apiserver
* `services_cidr` - (Optional) Internal IP range for ClusterIP Services.
* `pods_cidr` - (Optional) Internal IP range for Pods.
* `domain_name` - (Optional) Cluster domain name.

### `cloud`

Expand Down
48 changes: 26 additions & 22 deletions metakube/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,31 @@ func resourceClusterCreate(d *schema.ResourceData, m interface{}) error {
return err
}

p.SetProjectID(pID)
p.SetDC(dc.Spec.Seed)
p.SetBody(&models.CreateClusterSpec{
clusterSpec := expandClusterSpec(d.Get("spec").([]interface{}), d.Get("dc_name").(string))
createClusterSpec := &models.CreateClusterSpec{
Cluster: &models.Cluster{
Name: d.Get("name").(string),
Spec: expandClusterSpec(d.Get("spec").([]interface{}), d.Get("dc_name").(string)),
Spec: clusterSpec,
Type: d.Get("type").(string),
Labels: getLabels(d),
Credential: d.Get("credential").(string),
},
})
}
if n := clusterSpec.ClusterNetwork; n != nil {
if n.DNSDomain != "" {
createClusterSpec.DNSDomain = n.DNSDomain
}
if v := clusterSpec.ClusterNetwork.Pods; v != nil && len(v.CIDRBlocks) == 1 {

This comment has been minimized.

Copy link
@phiphi282

phiphi282 Dec 10, 2020

Member

What should we do in case there are multiple CIDRBlocks in the cluster spec?
It is probably not expected that nothing happens then.

This comment has been minimized.

Copy link
@furkhat

furkhat Dec 11, 2020

Author

to be honest I don't understand why API has array of cidr blocks. I don't think it is possible to have more than one cidr block. I think I will return warning if the array has more than one item.

This comment has been minimized.

Copy link
@phiphi282

phiphi282 Dec 11, 2020

Member

I think that is fine 👍

createClusterSpec.PodsCIDR = v.CIDRBlocks[0]
}
if v := clusterSpec.ClusterNetwork.Services; v != nil && len(v.CIDRBlocks) == 1 {
createClusterSpec.ServicesCIDR = v.CIDRBlocks[0]
}
}

p.SetProjectID(pID)
p.SetDC(dc.Spec.Seed)
p.SetBody(createClusterSpec)

r, err := k.client.Project.CreateCluster(p, k.auth)
if err != nil {
Expand Down Expand Up @@ -459,10 +473,14 @@ func patchClusterFields(d *schema.ResourceData, k *metakubeProviderMeta) error {
p.SetDC(seedDC)
p.SetClusterID(clusterID)
name := d.Get("name").(string)
version := d.Get("spec.0.version").(string)
auditLogging := d.Get("spec.0.audit_logging").(bool)
labels := d.Get("labels")
p.SetPatch(newClusterPatch(name, version, auditLogging, labels))
clusterSpec := expandClusterSpec(d.Get("spec").([]interface{}), d.Get("dc_name").(string))

This comment has been minimized.

Copy link
@phiphi282

phiphi282 Dec 10, 2020

Member

It looks like the audit logging is missing here now.

This comment has been minimized.

Copy link
@furkhat

furkhat Dec 11, 2020

Author

what do you mean?
Audit logging is set in expandClusterSpec function.

This comment has been minimized.

Copy link
@phiphi282

phiphi282 Dec 11, 2020

Member

I was just a bit confused about why the auditLogging was removed here without any other change for auditLogging in this commit. :)

// p.SetPatch(newClusterPatch(name, version, auditLogging, labels))
p.SetPatch(map[string]interface{}{
"name": name,
"labels": labels,
"spec": clusterSpec,
})

err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
_, err := k.client.Project.PatchCluster(p, k.auth)
Expand Down Expand Up @@ -570,20 +588,6 @@ func waitClusterReady(k *metakubeProviderMeta, d *schema.ResourceData, projectID
})
}

func newClusterPatch(name, version string, auditLogging bool, labels interface{}) interface{} {
// TODO(furkhat): change to dedicated struct when API has it.
return map[string]interface{}{
"name": name,
"labels": labels,
"spec": map[string]interface{}{
"auditLogging": map[string]bool{
"enabled": auditLogging,
},
"version": version,
},
}
}

func resourceClusterDelete(d *schema.ResourceData, m interface{}) error {
k := m.(*metakubeProviderMeta)
projectID, seedDC, clusterID, err := metakubeClusterParseID(d.Id())
Expand Down
20 changes: 19 additions & 1 deletion metakube/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func TestAccMetaKubeCluster_Openstack_Basic(t *testing.T) {
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "labels.%", "0"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.#", "1"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.version", versionK8s17),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.domain_name", "foodomain.local"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.services_cidr", "10.240.16.0/18"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.pods_cidr", "172.25.0.0/18"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.cloud.#", "1"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.cloud.0.bringyourown.#", "0"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.cloud.0.aws.#", "0"),
Expand Down Expand Up @@ -130,6 +133,11 @@ func TestAccMetaKubeCluster_Openstack_Basic(t *testing.T) {
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "labels.test-key", "test-value"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.#", "1"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.version", versionK8s17),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.domain_name", "foodomain.local"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.services_cidr", "10.240.16.0/18"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.pods_cidr", "172.25.0.0/18"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.pod_node_selector", "true"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.pod_security_policy", "true"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.cloud.#", "1"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.cloud.0.bringyourown.#", "0"),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.cloud.0.aws.#", "0"),
Expand Down Expand Up @@ -270,8 +278,12 @@ func testAccCheckMetaKubeClusterOpenstackBasic(testName, username, password, ten
floating_ip_pool = "ext-net"
}
}
domain_name = "foodomain.local"
services_cidr = "10.240.16.0/18"
pods_cidr = "172.25.0.0/18"
}
}`
}
`

return fmt.Sprintf(config, testName, testName, nodeDC, version, tenant, username, password)
}
Expand Down Expand Up @@ -310,6 +322,12 @@ func testAccCheckMetaKubeClusterOpenstackBasic2(testName, username, password, te
# enable audit logging
audit_logging = true
pod_node_selector = true
pod_security_policy = true
domain_name = "foodomain.local"
services_cidr = "10.240.16.0/18"
pods_cidr = "172.25.0.0/18"
}
}`
return fmt.Sprintf(config, testName, testName, nodeDC, k8sVersion, tenant, username, password)
Expand Down
21 changes: 21 additions & 0 deletions metakube/schema_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ func clusterSpecFields() map[string]*schema.Schema {
Default: false,
Description: "Configure PodNodeSelector admission plugin at the apiserver",
},
"services_cidr": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "Internal IP range for ClusterIP Services",
},
"pods_cidr": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "Internal IP range for Pods",
},
"domain_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "Internal IP range for ClusterIP Pods",
},
}
}

Expand Down
37 changes: 37 additions & 0 deletions metakube/structure_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func flattenClusterSpec(values clusterPreserveValues, in *models.ClusterSpec) []

att["pod_node_selector"] = in.UsePodNodeSelectorAdmissionPlugin

if network := in.ClusterNetwork; network != nil {
if network.DNSDomain != "" {
att["domain_name"] = network.DNSDomain
}
if v := network.Pods; len(v.CIDRBlocks) > 0 && v.CIDRBlocks[0] != "" {
att["pods_cidr"] = v.CIDRBlocks[0]
}
if v := network.Services; len(v.CIDRBlocks) > 0 && v.CIDRBlocks[0] != "" {
att["services_cidr"] = v.CIDRBlocks[0]
}
}

if in.Cloud != nil {
att["cloud"] = flattenClusterCloudSpec(values, in.Cloud)
}
Expand Down Expand Up @@ -254,6 +266,31 @@ func expandClusterSpec(p []interface{}, dcName string) *models.ClusterSpec {
obj.UsePodNodeSelectorAdmissionPlugin = v.(bool)
}

if v, ok := in["services_cidr"]; ok {
if obj.ClusterNetwork == nil {
obj.ClusterNetwork = &models.ClusterNetworkingConfig{}
}
obj.ClusterNetwork.Services = &models.NetworkRanges{
CIDRBlocks: []string{v.(string)},
}
}

if v, ok := in["pods_cidr"]; ok {
if obj.ClusterNetwork == nil {
obj.ClusterNetwork = &models.ClusterNetworkingConfig{}
}
obj.ClusterNetwork.Pods = &models.NetworkRanges{
CIDRBlocks: []string{v.(string)},
}
}

if v, ok := in["domain_name"]; ok {
if obj.ClusterNetwork == nil {
obj.ClusterNetwork = &models.ClusterNetworkingConfig{}
}
obj.ClusterNetwork.DNSDomain = v.(string)
}

if v, ok := in["cloud"]; ok {
obj.Cloud = expandClusterCloudSpec(v.([]interface{}), dcName)
}
Expand Down
30 changes: 28 additions & 2 deletions metakube/structure_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@ func TestFlattenClusterSpec(t *testing.T) {
DatacenterName: "eu-west-1",
Bringyourown: map[string]interface{}{},
},
ClusterNetwork: &models.ClusterNetworkingConfig{
DNSDomain: "foocluster.local",
Services: &models.NetworkRanges{
CIDRBlocks: []string{"1.1.1.0/20"},
},
Pods: &models.NetworkRanges{
CIDRBlocks: []string{"2.2.0.0/16"},
},
},
},
[]interface{}{
map[string]interface{}{
"version": "1.18.8",
"audit_logging": false,
"pod_security_policy": false,
"pod_node_selector": false,
"services_cidr": "1.1.1.0/20",
"pods_cidr": "2.2.0.0/16",
"domain_name": "foocluster.local",
"cloud": []interface{}{
map[string]interface{}{
"bringyourown": []interface{}{map[string]interface{}{}},
Expand Down Expand Up @@ -321,6 +333,10 @@ func TestExpandClusterSpec(t *testing.T) {
"machine_networks": []interface{}{},
"audit_logging": false,
"pod_security_policy": true,
"pod_node_selector": true,
"services_cidr": "1.1.1.0/20",
"pods_cidr": "2.2.0.0/16",
"domain_name": "foocluster.local",
"cloud": []interface{}{
map[string]interface{}{
"bringyourown": []interface{}{
Expand All @@ -335,6 +351,16 @@ func TestExpandClusterSpec(t *testing.T) {
MachineNetworks: nil,
AuditLogging: &models.AuditLoggingSettings{},
UsePodSecurityPolicyAdmissionPlugin: true,
UsePodNodeSelectorAdmissionPlugin: true,
ClusterNetwork: &models.ClusterNetworkingConfig{
Services: &models.NetworkRanges{
CIDRBlocks: []string{"1.1.1.0/20"},
},
Pods: &models.NetworkRanges{
CIDRBlocks: []string{"2.2.0.0/16"},
},
DNSDomain: "foocluster.local",
},
Cloud: &models.CloudSpec{
DatacenterName: "eu-west-1",
Bringyourown: map[string]interface{}{},
Expand Down Expand Up @@ -494,7 +520,7 @@ func TestExpandAWSCloudSpec(t *testing.T) {
}
}

func TestExpandAzureCloudSpec(t *testing.T) {
func TestExpandOpenstackCloudSpec(t *testing.T) {
cases := []struct {
Input []interface{}
ExpectedOutput *models.OpenstackCloudSpec
Expand Down Expand Up @@ -538,7 +564,7 @@ func TestExpandAzureCloudSpec(t *testing.T) {
}
}

func TestExpandOpenstackCloudSpec(t *testing.T) {
func TestExpandAzureCloudSpec(t *testing.T) {
cases := []struct {
Input []interface{}
ExpectedOutput *models.AzureCloudSpec
Expand Down

0 comments on commit 5b9d085

Please sign in to comment.