Skip to content

Commit e61590f

Browse files
abhipatnalaAvinash Patnala
andauthored
Add TF support for Enable K8s Tokens and Certs via Dns Endpoint (#15352)
Co-authored-by: Avinash Patnala <[email protected]>
1 parent 8af9f09 commit e61590f

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

mmv1/third_party/terraform/services/container/resource_container_cluster.go.tmpl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,16 @@ func ResourceContainerCluster() *schema.Resource {
19221922
Optional: true,
19231923
Description: `Controls whether user traffic is allowed over this endpoint. Note that GCP-managed services may still use the endpoint even if this is false.`,
19241924
},
1925+
"enable_k8s_tokens_via_dns": {
1926+
Type: schema.TypeBool,
1927+
Optional: true,
1928+
Description: `Controls whether the k8s token auth is allowed via dns.`,
1929+
},
1930+
"enable_k8s_certs_via_dns": {
1931+
Type: schema.TypeBool,
1932+
Optional: true,
1933+
Description: `Controls whether the k8s certs auth is allowed via dns.`,
1934+
},
19251935
},
19261936
},
19271937
},
@@ -6252,6 +6262,16 @@ func expandControlPlaneEndpointsConfig(d *schema.ResourceData) *container.Contro
62526262
dns.ForceSendFields = []string{"AllowExternalTraffic"}
62536263
}
62546264

6265+
if v := d.Get("control_plane_endpoints_config.0.dns_endpoint_config.0.enable_k8s_tokens_via_dns"); v != nil {
6266+
dns.EnableK8sTokensViaDns = v.(bool)
6267+
dns.ForceSendFields = []string{"EnableK8sTokensViaDns"}
6268+
}
6269+
6270+
if v := d.Get("control_plane_endpoints_config.0.dns_endpoint_config.0.enable_k8s_certs_via_dns"); v != nil {
6271+
dns.EnableK8sCertsViaDns = v.(bool)
6272+
dns.ForceSendFields = []string{"EnableK8sCertsViaDns"}
6273+
}
6274+
62556275
ip := &container.IPEndpointsConfig{
62566276
Enabled: true,
62576277
ForceSendFields: []string{"Enabled"},
@@ -7080,8 +7100,10 @@ func flattenDnsEndpointConfig(dns *container.DNSEndpointConfig) []map[string]int
70807100
}
70817101
return []map[string]interface{}{
70827102
{
7083-
"endpoint": dns.Endpoint,
7084-
"allow_external_traffic": dns.AllowExternalTraffic,
7103+
"endpoint": dns.Endpoint,
7104+
"allow_external_traffic": dns.AllowExternalTraffic,
7105+
"enable_k8s_tokens_via_dns": dns.EnableK8sTokensViaDns,
7106+
"enable_k8s_certs_via_dns": dns.EnableK8sCertsViaDns,
70857107
},
70867108
}
70877109
}

mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14045,6 +14045,122 @@ resource "google_container_cluster" "primary" {
1404514045
}`, name, networkName, subnetworkName, enabled)
1404614046
}
1404714047

14048+
func TestAccContainerCluster_withDnsEndpointAndEnableK8sTokensViaDns(t *testing.T) {
14049+
t.Parallel()
14050+
14051+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14052+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14053+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14054+
14055+
acctest.VcrTest(t, resource.TestCase{
14056+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14057+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14058+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14059+
Steps: []resource.TestStep{
14060+
{
14061+
Config: testAccContainerCluster_withDnsEndpointAndEnablek8sTokensViaDns(clusterName, networkName, subnetworkName, false),
14062+
Check: resource.ComposeAggregateTestCheckFunc(
14063+
resource.TestCheckResourceAttr("google_container_cluster.primary", "control_plane_endpoints_config.0.dns_endpoint_config.0.enable_k8s_tokens_via_dns", "false"),
14064+
),
14065+
},
14066+
{
14067+
ResourceName: "google_container_cluster.primary",
14068+
ImportState: true,
14069+
ImportStateVerify: true,
14070+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14071+
},
14072+
{
14073+
Config: testAccContainerCluster_withDnsEndpointAndEnablek8sTokensViaDns(clusterName, networkName, subnetworkName, true),
14074+
Check: resource.ComposeAggregateTestCheckFunc(
14075+
resource.TestCheckResourceAttr("google_container_cluster.primary", "control_plane_endpoints_config.0.dns_endpoint_config.0.enable_k8s_tokens_via_dns", "true"),
14076+
),
14077+
},
14078+
{
14079+
ResourceName: "google_container_cluster.primary",
14080+
ImportState: true,
14081+
ImportStateVerify: true,
14082+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14083+
},
14084+
},
14085+
})
14086+
}
14087+
14088+
func testAccContainerCluster_withDnsEndpointAndEnablek8sTokensViaDns(name, networkName, subnetworkName string, enabled bool) string {
14089+
return fmt.Sprintf(`
14090+
resource "google_container_cluster" "primary" {
14091+
name = "%s"
14092+
location = "us-central1-a"
14093+
initial_node_count = 1
14094+
network = "%s"
14095+
subnetwork = "%s"
14096+
deletion_protection = false
14097+
control_plane_endpoints_config {
14098+
dns_endpoint_config {
14099+
allow_external_traffic = true
14100+
enable_k8s_tokens_via_dns = %t
14101+
}
14102+
}
14103+
}`, name, networkName, subnetworkName, enabled)
14104+
}
14105+
14106+
func TestAccContainerCluster_withDnsEndpointAndEnableK8sCertsViaDns(t *testing.T) {
14107+
t.Parallel()
14108+
14109+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14110+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14111+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14112+
14113+
acctest.VcrTest(t, resource.TestCase{
14114+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14115+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14116+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14117+
Steps: []resource.TestStep{
14118+
{
14119+
Config: testAccContainerCluster_withDnsEndpointAndEnablek8sCertsViaDns(clusterName, networkName, subnetworkName, false),
14120+
Check: resource.ComposeAggregateTestCheckFunc(
14121+
resource.TestCheckResourceAttr("google_container_cluster.primary", "control_plane_endpoints_config.0.dns_endpoint_config.0.enable_k8s_certs_via_dns", "false"),
14122+
),
14123+
},
14124+
{
14125+
ResourceName: "google_container_cluster.primary",
14126+
ImportState: true,
14127+
ImportStateVerify: true,
14128+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14129+
},
14130+
{
14131+
Config: testAccContainerCluster_withDnsEndpointAndEnablek8sCertsViaDns(clusterName, networkName, subnetworkName, true),
14132+
Check: resource.ComposeAggregateTestCheckFunc(
14133+
resource.TestCheckResourceAttr("google_container_cluster.primary", "control_plane_endpoints_config.0.dns_endpoint_config.0.enable_k8s_certs_via_dns", "true"),
14134+
),
14135+
},
14136+
{
14137+
ResourceName: "google_container_cluster.primary",
14138+
ImportState: true,
14139+
ImportStateVerify: true,
14140+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14141+
},
14142+
},
14143+
})
14144+
}
14145+
14146+
func testAccContainerCluster_withDnsEndpointAndEnablek8sCertsViaDns(name, networkName, subnetworkName string, enabled bool) string {
14147+
return fmt.Sprintf(`
14148+
resource "google_container_cluster" "primary" {
14149+
name = "%s"
14150+
location = "us-central1-a"
14151+
initial_node_count = 1
14152+
network = "%s"
14153+
subnetwork = "%s"
14154+
deletion_protection = false
14155+
control_plane_endpoints_config {
14156+
dns_endpoint_config {
14157+
allow_external_traffic = true
14158+
enable_k8s_certs_via_dns = %t
14159+
}
14160+
}
14161+
}`, name, networkName, subnetworkName, enabled)
14162+
}
14163+
1404814164
func TestAccContainerCluster_withCgroupMode(t *testing.T) {
1404914165
t.Parallel()
1405014166

mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,10 @@ The `control_plane_endpoints_config.dns_endpoint_config` block supports:
13201320

13211321
* `allow_external_traffic` - (Optional) Controls whether user traffic is allowed over this endpoint. Note that GCP-managed services may still use the endpoint even if this is false.
13221322

1323+
* `enable_k8s_tokens_via_dns` - (Optional) Controls whether the k8s token auth is allowed via Dns.
1324+
1325+
* `enable_k8s_certs_via_dns` - (Optional) Controls whether the k8s certs auth is allowed via Dns.
1326+
13231327
The `control_plane_endpoints_config.ip_endpoints_config` block supports:
13241328

13251329
* `enabled` - (Optional) Controls whether to allow direct IP access. Defaults to `true`.

0 commit comments

Comments
 (0)