Skip to content

Commit

Permalink
Added confidential_nodes field to google_container_cluster (#4136) (#…
Browse files Browse the repository at this point in the history
…2632)

* Initial implementation of confidential_nodes field

* Made confidential_nodes optional and computed to match API behavior

* Added test of confidential nodes

* Marked confidential_nodes.enabled as forcing new

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Oct 23, 2020
1 parent e411252 commit b77f48f
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4136.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added `confidential_nodes` field to `google_container_cluster` resource
```
44 changes: 44 additions & 0 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,25 @@ func resourceContainerCluster() *schema.Resource {
},
},

"confidential_nodes": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Description: `Configuration for the confidential nodes feature, which makes nodes run on confidential VMs. Warning: This configuration can't be changed (or added/removed) after cluster creation without deleting and recreating the entire cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
Description: `Whether Confidential Nodes feature is enabled for all nodes in this cluster.`,
},
},
},
},

"master_auth": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1257,6 +1276,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
},
MasterAuth: expandMasterAuth(d.Get("master_auth")),
NotificationConfig: expandNotificationConfig(d.Get("notification_config")),
ConfidentialNodes: expandConfidentialNodes(d.Get("confidential_nodes")),
ResourceLabels: expandStringMap(d, "resource_labels"),
}

Expand Down Expand Up @@ -1580,6 +1600,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("notification_config", flattenNotificationConfig(cluster.NotificationConfig)); err != nil {
return err
}
if err := d.Set("confidential_nodes", flattenConfidentialNodes(cluster.ConfidentialNodes)); err != nil {
return err
}
if err := d.Set("enable_tpu", cluster.EnableTpu); err != nil {
return fmt.Errorf("Error setting enable_tpu: %s", err)
}
Expand Down Expand Up @@ -2946,6 +2969,17 @@ func expandNotificationConfig(configured interface{}) *containerBeta.Notificatio
}
}

func expandConfidentialNodes(configured interface{}) *containerBeta.ConfidentialNodes {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}
config := l[0].(map[string]interface{})
return &containerBeta.ConfidentialNodes{
Enabled: config["enabled"].(bool),
}
}

func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -3178,6 +3212,16 @@ func flattenNotificationConfig(c *containerBeta.NotificationConfig) []map[string
}
}

func flattenConfidentialNodes(c *containerBeta.ConfidentialNodes) []map[string]interface{} {
result := []map[string]interface{}{}
if c != nil {
result = append(result, map[string]interface{}{
"enabled": c.Enabled,
})
}
return result
}

func flattenNetworkPolicy(c *containerBeta.NetworkPolicy) []map[string]interface{} {
result := []map[string]interface{}{}
if c != nil {
Expand Down
97 changes: 97 additions & 0 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,48 @@ func TestAccContainerCluster_withNotificationConfig(t *testing.T) {
})
}

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

clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
npName := fmt.Sprintf("tf-test-cluster-nodepool-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withConfidentialNodes(clusterName, npName),
},
{
ResourceName: "google_container_cluster.confidential_nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
{
Config: testAccContainerCluster_disableConfidentialNodes(clusterName, npName),
},
{
ResourceName: "google_container_cluster.confidential_nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
{
Config: testAccContainerCluster_withConfidentialNodes(clusterName, npName),
},
{
ResourceName: "google_container_cluster.confidential_nodes",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
},
})
}

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

Expand Down Expand Up @@ -2351,6 +2393,61 @@ resource "google_container_cluster" "notification_config" {
}
`, clusterName)
}
func testAccContainerCluster_withConfidentialNodes(clusterName string, npName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "confidential_nodes" {
name = "%s"
location = "us-central1-a"
enable_shielded_nodes = true
// Minimum version for confidential node support.
// Can be removed once default version is greater or equal to this version.
min_master_version = "1.18.9-gke.1501"
release_channel {
channel = "RAPID"
}
node_pool {
name = "%s"
initial_node_count = 1
node_config {
machine_type = "n2d-standard-2"
}
}
confidential_nodes {
enabled = true
}
}
`, clusterName, npName)
}

func testAccContainerCluster_disableConfidentialNodes(clusterName string, npName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "confidential_nodes" {
name = "%s"
location = "us-central1-a"
enable_shielded_nodes = true
// Minimum version for confidential node support.
// Can be removed once default version is greater or equal to this version.
min_master_version = "1.18.9-gke.1501"
release_channel {
channel = "RAPID"
}
node_pool {
name = "%s"
initial_node_count = 1
node_config {
machine_type = "n2d-standard-2"
}
}
confidential_nodes {
enabled = false
}
}
`, clusterName, npName)
}

func testAccContainerCluster_withMasterAuth(clusterName string) string {
return fmt.Sprintf(`
Expand Down

0 comments on commit b77f48f

Please sign in to comment.