@@ -400,11 +400,38 @@ func resourceContainerCluster() *schema.Resource {
400
400
},
401
401
402
402
"enable_binary_authorization" : {
403
- Default : false ,
404
403
Type : schema .TypeBool ,
405
404
Optional : true ,
405
+ Default : false ,
406
+ Deprecated : "Deprecated in favor of binary_authorization." ,
406
407
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
+ },
408
435
},
409
436
410
437
"enable_kubernetes_alpha" : {
@@ -1299,10 +1326,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
1299
1326
EnableKubernetesAlpha : d .Get ("enable_kubernetes_alpha" ).(bool ),
1300
1327
IpAllocationPolicy : ipAllocationBlock ,
1301
1328
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 )),
1306
1330
Autopilot : & container.Autopilot {
1307
1331
Enabled : d .Get ("enable_autopilot" ).(bool ),
1308
1332
ForceSendFields : []string {"Enabled" },
@@ -1646,8 +1670,17 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
1646
1670
if err := d .Set ("cluster_autoscaling" , flattenClusterAutoscaling (cluster .Autoscaling )); err != nil {
1647
1671
return err
1648
1672
}
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
+ }
1651
1684
}
1652
1685
if cluster .Autopilot != nil {
1653
1686
if err := d .Set ("enable_autopilot" , cluster .Autopilot .Enabled ); err != nil {
@@ -1873,6 +1906,22 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
1873
1906
log .Printf ("[INFO] GKE cluster %s's binary authorization has been updated to %v" , d .Id (), enabled )
1874
1907
}
1875
1908
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
+
1876
1925
if d .HasChange ("enable_shielded_nodes" ) {
1877
1926
enabled := d .Get ("enable_shielded_nodes" ).(bool )
1878
1927
req := & container.UpdateClusterRequest {
@@ -2981,6 +3030,21 @@ func expandNotificationConfig(configured interface{}) *container.NotificationCon
2981
3030
}
2982
3031
}
2983
3032
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
+
2984
3048
func expandConfidentialNodes (configured interface {}) * container.ConfidentialNodes {
2985
3049
l := configured .([]interface {})
2986
3050
if len (l ) == 0 || l [0 ] == nil {
@@ -3247,6 +3311,18 @@ func flattenNotificationConfig(c *container.NotificationConfig) []map[string]int
3247
3311
},
3248
3312
}
3249
3313
}
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
+
3250
3326
func flattenConfidentialNodes (c * container.ConfidentialNodes ) []map [string ]interface {} {
3251
3327
result := []map [string ]interface {}{}
3252
3328
if c != nil {
@@ -3859,3 +3935,15 @@ func containerClusterNetworkPolicyDiffSuppress(k, old, new string, r *schema.Res
3859
3935
3860
3936
return false
3861
3937
}
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
+ }
0 commit comments