Skip to content

Commit 4acf2fb

Browse files
feat(google_container_cluster): support notification filter (#6508) (#12643)
Signed-off-by: toVersus <[email protected]> Signed-off-by: toVersus <[email protected]> Signed-off-by: Modular Magician <[email protected]> Signed-off-by: toVersus <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent a1a42b7 commit 4acf2fb

File tree

4 files changed

+173
-2
lines changed

4 files changed

+173
-2
lines changed

.changelog/6508.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: added `notification_config.pubsub.filter` field to `google_container_cluster`
3+
```

google/resource_container_cluster.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,25 @@ func resourceContainerCluster() *schema.Resource {
681681
Optional: true,
682682
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}.`,
683683
},
684+
"filter": {
685+
Type: schema.TypeList,
686+
Optional: true,
687+
MaxItems: 1,
688+
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`,
689+
Elem: &schema.Resource{
690+
Schema: map[string]*schema.Schema{
691+
"event_type": {
692+
Type: schema.TypeList,
693+
Required: true,
694+
Description: `Can be used to filter what notifications are sent. Valid values include include UPGRADE_AVAILABLE_EVENT, UPGRADE_EVENT and SECURITY_BULLETIN_EVENT`,
695+
Elem: &schema.Schema{
696+
Type: schema.TypeString,
697+
ValidateFunc: validation.StringInSlice([]string{"UPGRADE_AVAILABLE_EVENT", "UPGRADE_EVENT", "SECURITY_BULLETIN_EVENT"}, false),
698+
},
699+
},
700+
},
701+
},
702+
},
684703
},
685704
},
686705
},
@@ -3138,12 +3157,22 @@ func expandNotificationConfig(configured interface{}) *container.NotificationCon
31383157
if len(v.([]interface{})) > 0 {
31393158
pubsub := notificationConfig["pubsub"].([]interface{})[0].(map[string]interface{})
31403159

3141-
return &container.NotificationConfig{
3160+
nc := &container.NotificationConfig{
31423161
Pubsub: &container.PubSub{
31433162
Enabled: pubsub["enabled"].(bool),
31443163
Topic: pubsub["topic"].(string),
31453164
},
31463165
}
3166+
3167+
if vv, ok := pubsub["filter"]; ok && len(vv.([]interface{})) > 0 {
3168+
filter := vv.([]interface{})[0].(map[string]interface{})
3169+
eventType := filter["event_type"].([]interface{})
3170+
nc.Pubsub.Filter = &container.Filter{
3171+
EventType: convertStringArr(eventType),
3172+
}
3173+
}
3174+
3175+
return nc
31473176
}
31483177
}
31493178

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

3498+
if c.Pubsub.Filter != nil {
3499+
filter := []map[string]interface{}{}
3500+
if len(c.Pubsub.Filter.EventType) > 0 {
3501+
filter = append(filter, map[string]interface{}{
3502+
"event_type": c.Pubsub.Filter.EventType,
3503+
})
3504+
}
3505+
3506+
return []map[string]interface{}{
3507+
{
3508+
"pubsub": []map[string]interface{}{
3509+
{
3510+
"enabled": c.Pubsub.Enabled,
3511+
"topic": c.Pubsub.Topic,
3512+
"filter": filter,
3513+
},
3514+
},
3515+
},
3516+
}
3517+
}
3518+
34693519
return []map[string]interface{}{
34703520
{
34713521
"pubsub": []map[string]interface{}{

google/resource_container_cluster_test.go

+113-1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,46 @@ func TestAccContainerCluster_withNotificationConfig(t *testing.T) {
244244
})
245245
}
246246

247+
func TestAccContainerCluster_withFilteredNotificationConfig(t *testing.T) {
248+
t.Parallel()
249+
250+
clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
251+
topic := fmt.Sprintf("tf-test-topic-%s", randString(t, 10))
252+
newTopic := fmt.Sprintf("tf-test-topic-%s", randString(t, 10))
253+
254+
vcrTest(t, resource.TestCase{
255+
PreCheck: func() { testAccPreCheck(t) },
256+
Providers: testAccProviders,
257+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
258+
Steps: []resource.TestStep{
259+
{
260+
Config: testAccContainerCluster_withFilteredNotificationConfig(clusterName, topic),
261+
},
262+
{
263+
ResourceName: "google_container_cluster.filtered_notification_config",
264+
ImportState: true,
265+
ImportStateVerify: true,
266+
},
267+
{
268+
Config: testAccContainerCluster_withFilteredNotificationConfigUpdate(clusterName, newTopic),
269+
},
270+
{
271+
ResourceName: "google_container_cluster.filtered_notification_config",
272+
ImportState: true,
273+
ImportStateVerify: true,
274+
},
275+
{
276+
Config: testAccContainerCluster_disableFilteredNotificationConfig(clusterName, newTopic),
277+
},
278+
{
279+
ResourceName: "google_container_cluster.filtered_notification_config",
280+
ImportState: true,
281+
ImportStateVerify: true,
282+
},
283+
},
284+
})
285+
}
286+
247287
func TestAccContainerCluster_withConfidentialNodes(t *testing.T) {
248288
t.Parallel()
249289

@@ -2767,7 +2807,7 @@ resource "google_container_cluster" "notification_config" {
27672807
notification_config {
27682808
pubsub {
27692809
enabled = true
2770-
topic = google_pubsub_topic.%s.id
2810+
topic = google_pubsub_topic.%s.id
27712811
}
27722812
}
27732813
}
@@ -2789,6 +2829,78 @@ resource "google_container_cluster" "notification_config" {
27892829
`, clusterName)
27902830
}
27912831

2832+
func testAccContainerCluster_withFilteredNotificationConfig(clusterName string, topic string) string {
2833+
2834+
return fmt.Sprintf(`
2835+
2836+
resource "google_pubsub_topic" "%s" {
2837+
name = "%s"
2838+
}
2839+
2840+
resource "google_container_cluster" "filtered_notification_config" {
2841+
name = "%s"
2842+
location = "us-central1-a"
2843+
initial_node_count = 3
2844+
notification_config {
2845+
pubsub {
2846+
enabled = true
2847+
topic = google_pubsub_topic.%s.id
2848+
filter {
2849+
event_type = ["UPGRADE_EVENT", "SECURITY_BULLETIN_EVENT"]
2850+
}
2851+
}
2852+
}
2853+
}
2854+
`, topic, topic, clusterName, topic)
2855+
}
2856+
2857+
func testAccContainerCluster_withFilteredNotificationConfigUpdate(clusterName string, topic string) string {
2858+
2859+
return fmt.Sprintf(`
2860+
2861+
resource "google_pubsub_topic" "%s" {
2862+
name = "%s"
2863+
}
2864+
2865+
resource "google_container_cluster" "filtered_notification_config" {
2866+
name = "%s"
2867+
location = "us-central1-a"
2868+
initial_node_count = 3
2869+
notification_config {
2870+
pubsub {
2871+
enabled = true
2872+
topic = google_pubsub_topic.%s.id
2873+
filter {
2874+
event_type = ["UPGRADE_AVAILABLE_EVENT"]
2875+
}
2876+
}
2877+
}
2878+
}
2879+
`, topic, topic, clusterName, topic)
2880+
}
2881+
2882+
func testAccContainerCluster_disableFilteredNotificationConfig(clusterName string, topic string) string {
2883+
2884+
return fmt.Sprintf(`
2885+
2886+
resource "google_pubsub_topic" "%s" {
2887+
name = "%s"
2888+
}
2889+
2890+
resource "google_container_cluster" "filtered_notification_config" {
2891+
name = "%s"
2892+
location = "us-central1-a"
2893+
initial_node_count = 3
2894+
notification_config {
2895+
pubsub {
2896+
enabled = true
2897+
topic = google_pubsub_topic.%s.id
2898+
}
2899+
}
2900+
}
2901+
`, topic, topic, clusterName, topic)
2902+
}
2903+
27922904
func testAccContainerCluster_withConfidentialNodes(clusterName string, npName string) string {
27932905
return fmt.Sprintf(`
27942906
resource "google_container_cluster" "confidential_nodes" {

website/docs/r/container_cluster.html.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ The `pubsub` block supports:
886886

887887
* `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}`.
888888

889+
* `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).
890+
889891
```hcl
890892
notification_config {
891893
pubsub {
@@ -895,6 +897,10 @@ notification_config {
895897
}
896898
```
897899

900+
<a name="nested_notification_filter"></a> The `filter` block supports:
901+
902+
* `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.
903+
898904
<a name="nested_confidential_nodes"></a> The `confidential_nodes` block supports:
899905

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

0 commit comments

Comments
 (0)