Skip to content

Commit 7b604e0

Browse files
modular-magicianAvinash Kumar
andauthored
Added support for workload-vulnerability-scanning and workload-config-audit (#7310) (#5255)
* Added support for workload-vulnerability-scanning and workload-config-audit Fixes hashicorp/terraform-provider-google#12778 * Made google_container_cluster protect_config field optional * made container_cluster protect_config.workload_config.audit_mode required * Made fields inside protect_config as atleastOneOf in container_cluster resource. --------- Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Avinash Kumar <[email protected]>
1 parent a9ccc4c commit 7b604e0

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

.changelog/7310.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: added field `protect_config` to `google_container_cluster` (beta)
3+
```

google-beta/resource_container_cluster.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,48 @@ func ResourceContainerCluster() *schema.Resource {
10051005
},
10061006
},
10071007

1008+
"protect_config": {
1009+
Type: schema.TypeList,
1010+
Optional: true,
1011+
Computed: true,
1012+
MaxItems: 1,
1013+
Description: `The notification config for sending cluster upgrade notifications`,
1014+
Elem: &schema.Resource{
1015+
Schema: map[string]*schema.Schema{
1016+
"workload_config": {
1017+
Type: schema.TypeList,
1018+
Optional: true,
1019+
Computed: true,
1020+
MaxItems: 1,
1021+
Description: `WorkloadConfig defines the flags to enable or disable the workload configurations for the cluster.`,
1022+
Elem: &schema.Resource{
1023+
Schema: map[string]*schema.Schema{
1024+
"audit_mode": {
1025+
Type: schema.TypeString,
1026+
Required: true,
1027+
Description: `Mode defines how to audit the workload configs. Accepted values are MODE_UNSPECIFIED, DISABLED, BASIC.`,
1028+
},
1029+
},
1030+
},
1031+
AtLeastOneOf: []string{
1032+
"protect_config.0.workload_config",
1033+
"protect_config.0.workload_vulnerability_mode",
1034+
},
1035+
},
1036+
"workload_vulnerability_mode": {
1037+
Type: schema.TypeString,
1038+
Optional: true,
1039+
Computed: true,
1040+
Description: `WorkloadVulnerabilityMode defines mode to perform vulnerability scanning. Accepted values are WORKLOAD_VULNERABILITY_MODE_UNSPECIFIED, DISABLED, BASIC.`,
1041+
AtLeastOneOf: []string{
1042+
"protect_config.0.workload_config",
1043+
"protect_config.0.workload_vulnerability_mode",
1044+
},
1045+
},
1046+
},
1047+
},
1048+
},
1049+
10081050
"monitoring_config": {
10091051
Type: schema.TypeList,
10101052
Optional: true,
@@ -1933,6 +1975,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
19331975
ConfidentialNodes: expandConfidentialNodes(d.Get("confidential_nodes")),
19341976
ResourceLabels: expandStringMap(d, "resource_labels"),
19351977
NodePoolAutoConfig: expandNodePoolAutoConfig(d.Get("node_pool_auto_config")),
1978+
ProtectConfig: expandProtectConfig(d.Get("protect_config")),
19361979
CostManagementConfig: expandCostManagementConfig(d.Get("cost_management_config")),
19371980
}
19381981

@@ -2460,6 +2503,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
24602503
return err
24612504
}
24622505

2506+
if err := d.Set("protect_config", flattenProtectConfig(cluster.ProtectConfig)); err != nil {
2507+
return err
2508+
}
2509+
24632510
return nil
24642511
}
24652512

@@ -3570,6 +3617,20 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
35703617
return err
35713618
}
35723619

3620+
if d.HasChange("protect_config") {
3621+
req := &container.UpdateClusterRequest{
3622+
Update: &container.ClusterUpdate{
3623+
DesiredProtectConfig: expandProtectConfig(d.Get("protect_config")),
3624+
},
3625+
}
3626+
updateF := updateFunc(req, "updating GKE cluster master protect_config")
3627+
if err := lockedCall(lockKey, updateF); err != nil {
3628+
return err
3629+
}
3630+
3631+
log.Printf("[INFO] GKE cluster %s Protect Config has been updated to %#v", d.Id(), req.Update.DesiredProtectConfig)
3632+
}
3633+
35733634
return resourceContainerClusterRead(d, meta)
35743635
}
35753636

@@ -4131,6 +4192,56 @@ func expandAuthenticatorGroupsConfig(configured interface{}) *container.Authenti
41314192
return result
41324193
}
41334194

4195+
func expandProtectConfig(configured interface{}) *container.ProtectConfig {
4196+
l := configured.([]interface{})
4197+
if len(l) == 0 || l[0] == nil {
4198+
return nil
4199+
}
4200+
4201+
pc := &container.ProtectConfig{}
4202+
protectConfig := l[0].(map[string]interface{})
4203+
pc.WorkloadConfig = expandProtectConfigWorkloadConfig(protectConfig["workload_config"])
4204+
if v, ok := protectConfig["workload_vulnerability_mode"]; ok {
4205+
pc.WorkloadVulnerabilityMode = v.(string)
4206+
}
4207+
return pc
4208+
}
4209+
4210+
func expandProtectConfigWorkloadConfig(configured interface{}) *container.WorkloadConfig {
4211+
l := configured.([]interface{})
4212+
if len(l) == 0 || l[0] == nil {
4213+
return nil
4214+
}
4215+
workloadConfig := l[0].(map[string]interface{})
4216+
return &container.WorkloadConfig{
4217+
AuditMode: workloadConfig["audit_mode"].(string),
4218+
}
4219+
}
4220+
4221+
func flattenProtectConfig(pc *container.ProtectConfig) []map[string]interface{} {
4222+
if pc == nil {
4223+
return nil
4224+
}
4225+
4226+
result := make(map[string]interface{})
4227+
4228+
result["workload_config"] = flattenProtectConfigWorkloadConfig(pc.WorkloadConfig)
4229+
result["workload_vulnerability_mode"] = pc.WorkloadVulnerabilityMode
4230+
4231+
return []map[string]interface{}{result}
4232+
}
4233+
4234+
func flattenProtectConfigWorkloadConfig(wc *container.WorkloadConfig) []map[string]interface{} {
4235+
if wc == nil {
4236+
return nil
4237+
}
4238+
4239+
result := make(map[string]interface{})
4240+
result["audit_mode"] = wc.AuditMode
4241+
4242+
return []map[string]interface{}{result}
4243+
}
4244+
41344245
func expandNotificationConfig(configured interface{}) *container.NotificationConfig {
41354246
l := configured.([]interface{})
41364247
if len(l) == 0 || l[0] == nil {

google-beta/resource_container_cluster_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,6 +3477,38 @@ func TestAccContainerCluster_withTPUConfig(t *testing.T) {
34773477
})
34783478
}
34793479

3480+
func TestAccContainerCluster_withProtectConfig(t *testing.T) {
3481+
t.Parallel()
3482+
3483+
clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
3484+
3485+
vcrTest(t, resource.TestCase{
3486+
PreCheck: func() { testAccPreCheck(t) },
3487+
Providers: testAccProviders,
3488+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
3489+
Steps: []resource.TestStep{
3490+
{
3491+
Config: testAccContainerCluster_withProtectConfig(clusterName),
3492+
},
3493+
{
3494+
ResourceName: "google_container_cluster.primary",
3495+
ImportState: true,
3496+
ImportStateVerify: true,
3497+
ImportStateVerifyIgnore: []string{"min_master_version"},
3498+
},
3499+
{
3500+
Config: testAccContainerCluster_withProtectConfigUpdated(clusterName),
3501+
},
3502+
{
3503+
ResourceName: "google_container_cluster.primary",
3504+
ImportState: true,
3505+
ImportStateVerify: true,
3506+
ImportStateVerifyIgnore: []string{"min_master_version"},
3507+
},
3508+
},
3509+
})
3510+
}
3511+
34803512
func testAccContainerCluster_masterAuthorizedNetworksDisabled(t *testing.T, resource_name string) resource.TestCheckFunc {
34813513
return func(s *terraform.State) error {
34823514
rs, ok := s.RootModule().Resources[resource_name]
@@ -7262,6 +7294,40 @@ resource "google_container_cluster" "primary" {
72627294
}`, cluster, project, project)
72637295
}
72647296

7297+
func testAccContainerCluster_withProtectConfig(name string) string {
7298+
return fmt.Sprintf(`
7299+
resource "google_container_cluster" "primary" {
7300+
name = "%s"
7301+
location = "us-central1-a"
7302+
initial_node_count = 1
7303+
7304+
protect_config {
7305+
workload_config {
7306+
audit_mode = "BASIC"
7307+
}
7308+
workload_vulnerability_mode = "BASIC"
7309+
}
7310+
}
7311+
`, name)
7312+
}
7313+
7314+
func testAccContainerCluster_withProtectConfigUpdated(name string) string {
7315+
return fmt.Sprintf(`
7316+
resource "google_container_cluster" "primary" {
7317+
name = "%s"
7318+
location = "us-central1-a"
7319+
initial_node_count = 1
7320+
7321+
protect_config {
7322+
workload_config {
7323+
audit_mode = "DISABLED"
7324+
}
7325+
workload_vulnerability_mode = "DISABLED"
7326+
}
7327+
}
7328+
`, name)
7329+
}
7330+
72657331
func TestValidateNodePoolAutoConfig(t *testing.T) {
72667332
withTags := &container.NodePoolAutoConfig{
72677333
NetworkTags: &container.NetworkTags{

website/docs/r/container_cluster.html.markdown

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ subnetwork in which the cluster's instances are launched.
357357
* `gateway_api_config` - (Optional)
358358
Configuration for [GKE Gateway API controller](https://cloud.google.com/kubernetes-engine/docs/concepts/gateway-api). Structure is [documented below](#nested_gateway_api_config).
359359

360+
* `protect_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
361+
Enable/Disable Protect API features for the cluster. Structure is [documented below](#nested_protect_config).
362+
360363
<a name="nested_default_snat_status"></a>The `default_snat_status` block supports
361364

362365
* `disabled` - (Required) Whether the cluster disables default in-node sNAT rules. In-node sNAT rules will be disabled when defaultSnatStatus is disabled.When disabled is set to false, default IP masquerade rules will be applied to the nodes to prevent sNAT on cluster internal traffic
@@ -1141,6 +1144,16 @@ and all pods running on the nodes. Specified as a map from the key, such as
11411144

11421145
* `channel` - (Required) Which Gateway Api channel should be used. `CHANNEL_DISABLED` or `CHANNEL_STANDARD`.
11431146

1147+
<a name="nested_protect_config"></a>The `protect_config` block supports:
1148+
1149+
* `workload_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) WorkloadConfig defines which actions are enabled for a cluster's workload configurations. Structure is [documented below](#nested_workload_config)
1150+
1151+
* `workload_vulnerability_mode` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Sets which mode to use for Protect workload vulnerability scanning feature. Accepted values are WORKLOAD_VULNERABILITY_MODE_UNSPECIFIED, DISABLED, BASIC.
1152+
1153+
<a name="nested_workload_config"></a>The `protect_config.workload_config` block supports:
1154+
1155+
* `auditMode` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) WorkloadConfig defines the flags to enable or disable the workload configurations for the cluster. Accepted values are MODE_UNSPECIFIED, DISABLED, BASIC.
1156+
11441157
## Attributes Reference
11451158

11461159
In addition to the arguments listed above, the following computed attributes are

0 commit comments

Comments
 (0)