Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(google_container_cluster): support notification filter #6508

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,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 @@ -3789,12 +3808,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 @@ -4246,12 +4275,33 @@ 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{}{
{
"enabled": c.Pubsub.Enabled,
"topic": c.Pubsub.Topic,
"topic": c.Pubsub.Topic,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,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 @@ -3471,7 +3511,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 @@ -3493,6 +3533,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
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