Skip to content

Commit

Permalink
feat(google_container_cluster): support notification filter (#6508) (#…
Browse files Browse the repository at this point in the history
…12643)

Signed-off-by: toVersus <toversus2357@gmail.com>

Signed-off-by: toVersus <toversus2357@gmail.com>
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: toVersus <toversus2357@gmail.com>
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 26, 2022
1 parent a1a42b7 commit 4acf2fb
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/6508.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added `notification_config.pubsub.filter` field to `google_container_cluster`
```
52 changes: 51 additions & 1 deletion google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,25 @@ func resourceContainerCluster() *schema.Resource {
Optional: true,
Description: `The pubsub topic to push upgrade notifications to. Must be in the same project as the cluster. Must be in the format: projects/{project}/topics/{topic}.`,
},
"filter": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `Allows filtering to one or more specific event types. If event types are present, those and only those event types will be transmitted to the cluster. Other types will be skipped. If no filter is specified, or no event types are present, all event types will be sent`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"event_type": {
Type: schema.TypeList,
Required: true,
Description: `Can be used to filter what notifications are sent. Valid values include include UPGRADE_AVAILABLE_EVENT, UPGRADE_EVENT and SECURITY_BULLETIN_EVENT`,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"UPGRADE_AVAILABLE_EVENT", "UPGRADE_EVENT", "SECURITY_BULLETIN_EVENT"}, false),
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -3138,12 +3157,22 @@ func expandNotificationConfig(configured interface{}) *container.NotificationCon
if len(v.([]interface{})) > 0 {
pubsub := notificationConfig["pubsub"].([]interface{})[0].(map[string]interface{})

return &container.NotificationConfig{
nc := &container.NotificationConfig{
Pubsub: &container.PubSub{
Enabled: pubsub["enabled"].(bool),
Topic: pubsub["topic"].(string),
},
}

if vv, ok := pubsub["filter"]; ok && len(vv.([]interface{})) > 0 {
filter := vv.([]interface{})[0].(map[string]interface{})
eventType := filter["event_type"].([]interface{})
nc.Pubsub.Filter = &container.Filter{
EventType: convertStringArr(eventType),
}
}

return nc
}
}

Expand Down Expand Up @@ -3466,6 +3495,27 @@ func flattenNotificationConfig(c *container.NotificationConfig) []map[string]int
return nil
}

if c.Pubsub.Filter != nil {
filter := []map[string]interface{}{}
if len(c.Pubsub.Filter.EventType) > 0 {
filter = append(filter, map[string]interface{}{
"event_type": c.Pubsub.Filter.EventType,
})
}

return []map[string]interface{}{
{
"pubsub": []map[string]interface{}{
{
"enabled": c.Pubsub.Enabled,
"topic": c.Pubsub.Topic,
"filter": filter,
},
},
},
}
}

return []map[string]interface{}{
{
"pubsub": []map[string]interface{}{
Expand Down
114 changes: 113 additions & 1 deletion google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,46 @@ func TestAccContainerCluster_withNotificationConfig(t *testing.T) {
})
}

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

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

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withFilteredNotificationConfig(clusterName, topic),
},
{
ResourceName: "google_container_cluster.filtered_notification_config",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_withFilteredNotificationConfigUpdate(clusterName, newTopic),
},
{
ResourceName: "google_container_cluster.filtered_notification_config",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_disableFilteredNotificationConfig(clusterName, newTopic),
},
{
ResourceName: "google_container_cluster.filtered_notification_config",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

Expand Down Expand Up @@ -2767,7 +2807,7 @@ resource "google_container_cluster" "notification_config" {
notification_config {
pubsub {
enabled = true
topic = google_pubsub_topic.%s.id
topic = google_pubsub_topic.%s.id
}
}
}
Expand All @@ -2789,6 +2829,78 @@ resource "google_container_cluster" "notification_config" {
`, clusterName)
}

func testAccContainerCluster_withFilteredNotificationConfig(clusterName string, topic string) string {

return fmt.Sprintf(`
resource "google_pubsub_topic" "%s" {
name = "%s"
}
resource "google_container_cluster" "filtered_notification_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 3
notification_config {
pubsub {
enabled = true
topic = google_pubsub_topic.%s.id
filter {
event_type = ["UPGRADE_EVENT", "SECURITY_BULLETIN_EVENT"]
}
}
}
}
`, topic, topic, clusterName, topic)
}

func testAccContainerCluster_withFilteredNotificationConfigUpdate(clusterName string, topic string) string {

return fmt.Sprintf(`
resource "google_pubsub_topic" "%s" {
name = "%s"
}
resource "google_container_cluster" "filtered_notification_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 3
notification_config {
pubsub {
enabled = true
topic = google_pubsub_topic.%s.id
filter {
event_type = ["UPGRADE_AVAILABLE_EVENT"]
}
}
}
}
`, topic, topic, clusterName, topic)
}

func testAccContainerCluster_disableFilteredNotificationConfig(clusterName string, topic string) string {

return fmt.Sprintf(`
resource "google_pubsub_topic" "%s" {
name = "%s"
}
resource "google_container_cluster" "filtered_notification_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 3
notification_config {
pubsub {
enabled = true
topic = google_pubsub_topic.%s.id
}
}
}
`, topic, topic, clusterName, topic)
}

func testAccContainerCluster_withConfidentialNodes(clusterName string, npName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "confidential_nodes" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ The `pubsub` block supports:

* `topic` (Optional) - The pubsub topic to push upgrade notifications to. Must be in the same project as the cluster. Must be in the format: `projects/{project}/topics/{topic}`.

* `filter` (Optional) - Choose what type of notifications you want to receive. If no filters are applied, you'll receive all notification types. Structure is [documented below](#nested_notification_filter).

```hcl
notification_config {
pubsub {
Expand All @@ -895,6 +897,10 @@ notification_config {
}
```

<a name="nested_notification_filter"></a> The `filter` block supports:

* `event_type` (Optional) - Can be used to filter what notifications are sent. Accepted values are `UPGRADE_AVAILABLE_EVENT`, `UPGRADE_EVENT` and `SECURITY_BULLETIN_EVENT`. See [Filtering notifications](https://cloud.google.com/kubernetes-engine/docs/concepts/cluster-notifications#filtering) for more details.

<a name="nested_confidential_nodes"></a> The `confidential_nodes` block supports:

* `enabled` (Required) - Enable Confidential Nodes for this cluster.
Expand Down

0 comments on commit 4acf2fb

Please sign in to comment.