Skip to content

Commit 584d087

Browse files
Add support for binauthz evaluation mode (#6101) (#12035)
Signed-off-by: Modular Magician <[email protected]>
1 parent 73cefe4 commit 584d087

5 files changed

+115
-11
lines changed

.changelog/6101.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: Added `binauthz_evaluation_mode` field to `resource_container_cluster`.
3+
```

google/data_source_google_container_cluster_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func TestAccContainerClusterDatasource_zonal(t *testing.T) {
2424
map[string]struct{}{
2525
"enable_autopilot": {},
2626
"enable_tpu": {},
27-
"enable_binary_authorization": {},
2827
"pod_security_policy_config.#": {},
2928
},
3029
),
@@ -51,7 +50,6 @@ func TestAccContainerClusterDatasource_regional(t *testing.T) {
5150
map[string]struct{}{
5251
"enable_autopilot": {},
5352
"enable_tpu": {},
54-
"enable_binary_authorization": {},
5553
"pod_security_policy_config.#": {},
5654
},
5755
),

google/resource_container_cluster.go

+96-8
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,38 @@ func resourceContainerCluster() *schema.Resource {
400400
},
401401

402402
"enable_binary_authorization": {
403-
Default: false,
404403
Type: schema.TypeBool,
405404
Optional: true,
405+
Default: false,
406+
Deprecated: "Deprecated in favor of binary_authorization.",
406407
Description: `Enable Binary Authorization for this cluster. If enabled, all container images will be validated by Google Binary Authorization.`,
407-
ConflictsWith: []string{"enable_autopilot"},
408+
ConflictsWith: []string{"enable_autopilot", "binary_authorization"},
409+
},
410+
"binary_authorization": {
411+
Type: schema.TypeList,
412+
Optional: true,
413+
DiffSuppressFunc: BinaryAuthorizationDiffSuppress,
414+
MaxItems: 1,
415+
Description: "Configuration options for the Binary Authorization feature.",
416+
ConflictsWith: []string{"enable_binary_authorization"},
417+
Elem: &schema.Resource{
418+
Schema: map[string]*schema.Schema{
419+
"enabled": {
420+
Type: schema.TypeBool,
421+
Optional: true,
422+
Deprecated: "Deprecated in favor of evaluation_mode.",
423+
Description: "Enable Binary Authorization for this cluster.",
424+
ConflictsWith: []string{"enable_autopilot", "binary_authorization.0.evaluation_mode"},
425+
},
426+
"evaluation_mode": {
427+
Type: schema.TypeString,
428+
Optional: true,
429+
ValidateFunc: validation.StringInSlice([]string{"DISABLED", "PROJECT_SINGLETON_POLICY_ENFORCE"}, false),
430+
Description: "Mode of operation for Binary Authorization policy evaluation.",
431+
ConflictsWith: []string{"binary_authorization.0.enabled"},
432+
},
433+
},
434+
},
408435
},
409436

410437
"enable_kubernetes_alpha": {
@@ -1299,10 +1326,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
12991326
EnableKubernetesAlpha: d.Get("enable_kubernetes_alpha").(bool),
13001327
IpAllocationPolicy: ipAllocationBlock,
13011328
Autoscaling: expandClusterAutoscaling(d.Get("cluster_autoscaling"), d),
1302-
BinaryAuthorization: &container.BinaryAuthorization{
1303-
Enabled: d.Get("enable_binary_authorization").(bool),
1304-
ForceSendFields: []string{"Enabled"},
1305-
},
1329+
BinaryAuthorization: expandBinaryAuthorization(d.Get("binary_authorization"), d.Get("enable_binary_authorization").(bool)),
13061330
Autopilot: &container.Autopilot{
13071331
Enabled: d.Get("enable_autopilot").(bool),
13081332
ForceSendFields: []string{"Enabled"},
@@ -1646,8 +1670,17 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
16461670
if err := d.Set("cluster_autoscaling", flattenClusterAutoscaling(cluster.Autoscaling)); err != nil {
16471671
return err
16481672
}
1649-
if err := d.Set("enable_binary_authorization", cluster.BinaryAuthorization != nil && cluster.BinaryAuthorization.Enabled); err != nil {
1650-
return fmt.Errorf("Error setting enable_binary_authorization: %s", err)
1673+
binauthz_enabled := d.Get("binary_authorization.0.enabled").(bool)
1674+
legacy_binauthz_enabled := d.Get("enable_binary_authorization").(bool)
1675+
if !binauthz_enabled {
1676+
if err := d.Set("enable_binary_authorization", cluster.BinaryAuthorization != nil && cluster.BinaryAuthorization.Enabled); err != nil {
1677+
return fmt.Errorf("Error setting enable_binary_authorization: %s", err)
1678+
}
1679+
}
1680+
if !legacy_binauthz_enabled {
1681+
if err := d.Set("binary_authorization", flattenBinaryAuthorization(cluster.BinaryAuthorization)); err != nil {
1682+
return err
1683+
}
16511684
}
16521685
if cluster.Autopilot != nil {
16531686
if err := d.Set("enable_autopilot", cluster.Autopilot.Enabled); err != nil {
@@ -1873,6 +1906,22 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
18731906
log.Printf("[INFO] GKE cluster %s's binary authorization has been updated to %v", d.Id(), enabled)
18741907
}
18751908

1909+
if d.HasChange("binary_authorization") {
1910+
req := &container.UpdateClusterRequest{
1911+
Update: &container.ClusterUpdate{
1912+
DesiredBinaryAuthorization: expandBinaryAuthorization(d.Get("binary_authorization"), d.Get("enable_binary_authorization").(bool)),
1913+
},
1914+
}
1915+
1916+
updateF := updateFunc(req, "updating GKE binary authorization")
1917+
// Call update serially.
1918+
if err := lockedCall(lockKey, updateF); err != nil {
1919+
return err
1920+
}
1921+
1922+
log.Printf("[INFO] GKE cluster %s's binary authorization has been updated to %v", d.Id(), req.Update.DesiredBinaryAuthorization)
1923+
}
1924+
18761925
if d.HasChange("enable_shielded_nodes") {
18771926
enabled := d.Get("enable_shielded_nodes").(bool)
18781927
req := &container.UpdateClusterRequest{
@@ -2981,6 +3030,21 @@ func expandNotificationConfig(configured interface{}) *container.NotificationCon
29813030
}
29823031
}
29833032

3033+
func expandBinaryAuthorization(configured interface{}, legacy_enabled bool) *container.BinaryAuthorization {
3034+
l := configured.([]interface{})
3035+
if len(l) == 0 || l[0] == nil {
3036+
return &container.BinaryAuthorization{
3037+
Enabled: legacy_enabled,
3038+
ForceSendFields: []string{"Enabled"},
3039+
}
3040+
}
3041+
config := l[0].(map[string]interface{})
3042+
return &container.BinaryAuthorization{
3043+
Enabled: config["enabled"].(bool),
3044+
EvaluationMode: config["evaluation_mode"].(string),
3045+
}
3046+
}
3047+
29843048
func expandConfidentialNodes(configured interface{}) *container.ConfidentialNodes {
29853049
l := configured.([]interface{})
29863050
if len(l) == 0 || l[0] == nil {
@@ -3247,6 +3311,18 @@ func flattenNotificationConfig(c *container.NotificationConfig) []map[string]int
32473311
},
32483312
}
32493313
}
3314+
3315+
func flattenBinaryAuthorization(c *container.BinaryAuthorization) []map[string]interface{} {
3316+
result := []map[string]interface{}{}
3317+
if c != nil {
3318+
result = append(result, map[string]interface{}{
3319+
"enabled": c.Enabled,
3320+
"evaluation_mode": c.EvaluationMode,
3321+
})
3322+
}
3323+
return result
3324+
}
3325+
32503326
func flattenConfidentialNodes(c *container.ConfidentialNodes) []map[string]interface{} {
32513327
result := []map[string]interface{}{}
32523328
if c != nil {
@@ -3859,3 +3935,15 @@ func containerClusterNetworkPolicyDiffSuppress(k, old, new string, r *schema.Res
38593935

38603936
return false
38613937
}
3938+
3939+
func BinaryAuthorizationDiffSuppress(k, old, new string, r *schema.ResourceData) bool {
3940+
// An empty config is equivalent to a config with enabled set to false.
3941+
if k == "binary_authorization.#" && old == "1" && new == "0" {
3942+
o, _ := r.GetChange("binary_authorization.0.enabled")
3943+
if !o.(bool) && !r.HasChange("binary_authorization.0.evaluation_mode") {
3944+
return true
3945+
}
3946+
}
3947+
3948+
return false
3949+
}

google/resource_container_cluster_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,9 @@ resource "google_container_cluster" "primary" {
23132313
enabled = true
23142314
}
23152315
2316+
binary_authorization {
2317+
evaluation_mode = "PROJECT_SINGLETON_POLICY_ENFORCE"
2318+
}
23162319
}
23172320
`, name)
23182321
}
@@ -2342,6 +2345,9 @@ resource "google_container_cluster" "primary" {
23422345
enabled = true
23432346
}
23442347
2348+
binary_authorization {
2349+
evaluation_mode = "PROJECT_SINGLETON_POLICY_ENFORCE"
2350+
}
23452351
}
23462352
`, name)
23472353
}

website/docs/r/container_cluster.html.markdown

+10-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ on the current needs of the cluster's workload. See the
136136
[guide to using Node Auto-Provisioning](https://cloud.google.com/kubernetes-engine/docs/how-to/node-auto-provisioning)
137137
for more details. Structure is [documented below](#nested_cluster_autoscaling).
138138

139+
* `binary_authorization` - (Optional) Configuration options for the Binary
140+
Authorization feature. Structure is [documented below](#nested_binary_authorization).
141+
139142
* `database_encryption` - (Optional)
140143
Structure is [documented below](#nested_database_encryption).
141144

@@ -146,8 +149,9 @@ per node in this cluster. This doesn't work on "routes-based" clusters, clusters
146149
that don't have IP Aliasing enabled. See the [official documentation](https://cloud.google.com/kubernetes-engine/docs/how-to/flexible-pod-cidr)
147150
for more information.
148151

149-
* `enable_binary_authorization` - (Optional) Enable Binary Authorization for this cluster.
152+
* `enable_binary_authorization` - (DEPRECATED) Enable Binary Authorization for this cluster.
150153
If enabled, all container images will be validated by Google Binary Authorization.
154+
Deprecated in favor of `binary_authorization`.
151155

152156
* `enable_kubernetes_alpha` - (Optional) Whether to enable Kubernetes Alpha features for
153157
this cluster. Note that when this option is enabled, the cluster cannot be upgraded
@@ -410,6 +414,11 @@ addons_config {
410414
}
411415
}
412416
```
417+
<a name="nested_binary_authorization"></a>The `binary_authorization` block supports:
418+
419+
* `enabled` - (DEPRECATED) Enable Binary Authorization for this cluster. Deprecated in favor of `evaluation_mode`.
420+
421+
* `evaluation_mode` - (Optional) Mode of operation for Binary Authorization policy evaluation.
413422

414423
<a name="nested_database_encryption"></a>The `database_encryption` block supports:
415424

0 commit comments

Comments
 (0)