Skip to content

Commit

Permalink
Add support for dns_config to google_container_cluster (#4945) (#…
Browse files Browse the repository at this point in the history
…3606)

* Add support for dns_config to google_container_cluster

* Update mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown beta description

Co-authored-by: Sam Levenick <[email protected]>

Co-authored-by: Sam Levenick <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Sam Levenick <[email protected]>
  • Loading branch information
modular-magician and slevenick authored Sep 8, 2021
1 parent df0fb24 commit 2632899
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/4945.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added field `dns_config` to resource `google_container_cluster` (beta)
```
61 changes: 61 additions & 0 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,36 @@ func resourceContainerCluster() *schema.Resource {
},
},
},
"dns_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
Description: `Configuration for Cloud DNS for Kubernetes Engine.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_dns": {
Type: schema.TypeString,
Default: "PROVIDER_UNSPECIFIED",
ValidateFunc: validation.StringInSlice([]string{"PROVIDER_UNSPECIFIED", "PLATFORM_DEFAULT", "CLOUD_DNS"}, false),
Description: `Which in-cluster DNS provider should be used.`,
Optional: true,
},
"cluster_dns_scope": {
Type: schema.TypeString,
Default: "DNS_SCOPE_UNSPECIFIED",
ValidateFunc: validation.StringInSlice([]string{"DNS_SCOPE_UNSPECIFIED", "CLUSTER_SCOPE", "VPC_SCOPE"}, false),
Description: `The scope of access to cluster DNS records.`,
Optional: true,
},
"cluster_dns_domain": {
Type: schema.TypeString,
Description: `The suffix used for all cluster service records.`,
Optional: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -1358,6 +1388,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
DatapathProvider: d.Get("datapath_provider").(string),
PrivateIpv6GoogleAccess: d.Get("private_ipv6_google_access").(string),
EnableL4ilbSubsetting: d.Get("enable_l4_ilb_subsetting").(bool),
DnsConfig: expandDnsConfig(d.Get("dns_config")),
},
MasterAuth: expandMasterAuth(d.Get("master_auth")),
NotificationConfig: expandNotificationConfig(d.Get("notification_config")),
Expand Down Expand Up @@ -1799,6 +1830,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("resource_usage_export_config", flattenResourceUsageExportConfig(cluster.ResourceUsageExportConfig)); err != nil {
return err
}
if err := d.Set("dns_config", flattenDnsConfig(cluster.NetworkConfig.DnsConfig)); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -3392,6 +3426,20 @@ func expandResourceUsageExportConfig(configured interface{}) *containerBeta.Reso
return result
}

func expandDnsConfig(configured interface{}) *containerBeta.DNSConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}

config := l[0].(map[string]interface{})
return &containerBeta.DNSConfig{
ClusterDns: config["cluster_dns"].(string),
ClusterDnsScope: config["cluster_dns_scope"].(string),
ClusterDnsDomain: config["cluster_dns_domain"].(string),
}
}

func flattenNotificationConfig(c *containerBeta.NotificationConfig) []map[string]interface{} {
if c == nil {
return nil
Expand Down Expand Up @@ -3825,6 +3873,19 @@ func flattenDatabaseEncryption(c *containerBeta.DatabaseEncryption) []map[string
}
}

func flattenDnsConfig(c *containerBeta.DNSConfig) []map[string]interface{} {
if c == nil {
return nil
}
return []map[string]interface{}{
{
"cluster_dns": c.ClusterDns,
"cluster_dns_scope": c.ClusterDnsScope,
"cluster_dns_domain": c.ClusterDnsDomain,
},
}
}

func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)

Expand Down
37 changes: 37 additions & 0 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,28 @@ func TestAccContainerCluster_withIPv4Error(t *testing.T) {
})
}

func TestAccContainerCluster_withDNSConfig(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
domainName := fmt.Sprintf("tf-test-domain-%s", randString(t, 10))
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withDNSConfig(clusterName, "CLOUD_DNS", domainName, "CLUSTER_SCOPE"),
},
{
ResourceName: "google_container_cluster.with_dns_config",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccContainerCluster_masterAuthorizedNetworksDisabled(t *testing.T, resource_name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resource_name]
Expand Down Expand Up @@ -4521,3 +4543,18 @@ resource "google_container_cluster" "with_invalid_location" {
}
`, clusterName, location)
}

func testAccContainerCluster_withDNSConfig(clusterName string, clusterDns string, clusterDnsDomain string, clusterDnsScope string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_dns_config" {
name = "%s"
location = "us-central1-f"
initial_node_count = 1
dns_config {
cluster_dns = "%s"
cluster_dns_domain = "%s"
cluster_dns_scope = "%s"
}
}
`, clusterName, clusterDns, clusterDnsDomain, clusterDnsScope)
}
2 changes: 1 addition & 1 deletion google-beta/resource_gke_hub_feature_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ subnetwork in which the cluster's instances are launched.
* `default_snat_status` - (Optional)
[GKE SNAT](https://cloud.google.com/kubernetes-engine/docs/how-to/ip-masquerade-agent#how_ipmasq_works) DefaultSnatStatus contains the desired state of whether default sNAT should be disabled on the cluster, [API doc](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#networkconfig).

* `dns_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
Configuration for [Using Cloud DNS for GKE](https://cloud.google.com/kubernetes-engine/docs/how-to/cloud-dns). Structure is documented below.

The `default_snat_status` block supports

* `disabled` - (Required) Whether the cluster disables default in-node sNAT rules. In-node sNAT rules will be disabled when defaultSnatStatus is disabled.When disabled is set to false, default IP masquerade rules will be applied to the nodes to prevent sNAT on cluster internal traffic
Expand Down Expand Up @@ -887,6 +890,14 @@ The `vertical_pod_autoscaling` block supports:

* `enabled` (Required) - Enables vertical pod autoscaling

The `dns_config` block supports:

* `cluster_dns` - (Optional) Which in-cluster DNS provider should be used. `PROVIDER_UNSPECIFIED` (default) or `PLATFORM_DEFAULT` or `CLOUD_DNS`.

* `cluster_dns_scope` - (Optional) The scope of access to cluster DNS records. `DNS_SCOPE_UNSPECIFIED` (default) or `CLUSTER_SCOPE` or `VPC_SCOPE`.

* `cluster_dns_domain` - (Optional) The suffix used for all cluster service records.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down

0 comments on commit 2632899

Please sign in to comment.