Skip to content

Commit d1bf585

Browse files
authored
Add support for binary authorization in GKE (#1884)
* revendor container/v1beta1 * add support for binauthz in gke * update description
1 parent 92afc9c commit d1bf585

File tree

6 files changed

+487
-30
lines changed

6 files changed

+487
-30
lines changed

google/resource_container_cluster.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ func resourceContainerCluster() *schema.Resource {
196196
ForceNew: true,
197197
},
198198

199+
"enable_binary_authorization": {
200+
Type: schema.TypeBool,
201+
Optional: true,
202+
Default: false,
203+
},
204+
199205
"enable_kubernetes_alpha": {
200206
Type: schema.TypeBool,
201207
Optional: true,
@@ -650,6 +656,11 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
650656
cluster.ResourceLabels = m
651657
}
652658

659+
cluster.BinaryAuthorization = &containerBeta.BinaryAuthorization{
660+
Enabled: d.Get("enable_binary_authorization").(bool),
661+
ForceSendFields: []string{"Enabled"},
662+
}
663+
653664
req := &containerBeta.CreateClusterRequest{
654665
Cluster: cluster,
655666
}
@@ -750,14 +761,14 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
750761
d.Set("monitoring_service", cluster.MonitoringService)
751762
d.Set("network", cluster.NetworkConfig.Network)
752763
d.Set("subnetwork", cluster.NetworkConfig.Subnetwork)
764+
d.Set("enable_binary_authorization", cluster.BinaryAuthorization.Enabled)
753765
if err := d.Set("node_config", flattenNodeConfig(cluster.NodeConfig)); err != nil {
754766
return err
755767
}
756768
d.Set("project", project)
757769
if err := d.Set("addons_config", flattenClusterAddonsConfig(cluster.AddonsConfig)); err != nil {
758-
770+
return err
759771
}
760-
761772
nps, err := flattenClusterNodePools(d, config, cluster.NodePools)
762773
if err != nil {
763774
return err
@@ -911,6 +922,28 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
911922
}
912923
}
913924

925+
if d.HasChange("enable_binary_authorization") {
926+
enabled := d.Get("enable_binary_authorization").(bool)
927+
req := &containerBeta.UpdateClusterRequest{
928+
Update: &containerBeta.ClusterUpdate{
929+
DesiredBinaryAuthorization: &containerBeta.BinaryAuthorization{
930+
Enabled: enabled,
931+
ForceSendFields: []string{"Enabled"},
932+
},
933+
},
934+
}
935+
936+
updateF := updateFunc(req, "updating GKE binary authorization")
937+
// Call update serially.
938+
if err := lockedCall(lockKey, updateF); err != nil {
939+
return err
940+
}
941+
942+
log.Printf("[INFO] GKE cluster %s's binary authorization has been updated to %v", d.Id(), enabled)
943+
944+
d.SetPartial("enable_binary_authorization")
945+
}
946+
914947
if d.HasChange("maintenance_policy") {
915948
var req *containerBeta.SetMaintenancePolicyRequest
916949
if mp, ok := d.GetOk("maintenance_policy"); ok {

google/resource_container_cluster_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,38 @@ func TestAccContainerCluster_withResourceLabelsUpdate(t *testing.T) {
12611261
})
12621262
}
12631263

1264+
func TestAccContainerCluster_withBinaryAuthorization(t *testing.T) {
1265+
t.Parallel()
1266+
1267+
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))
1268+
1269+
resource.Test(t, resource.TestCase{
1270+
PreCheck: func() { testAccPreCheck(t) },
1271+
Providers: testAccProviders,
1272+
CheckDestroy: testAccCheckContainerClusterDestroy,
1273+
Steps: []resource.TestStep{
1274+
{
1275+
Config: testAccContainerCluster_withBinaryAuthorization(clusterName, true),
1276+
},
1277+
{
1278+
ResourceName: "google_container_cluster.with_binary_authorization",
1279+
ImportStateIdPrefix: "us-central1-a/",
1280+
ImportState: true,
1281+
ImportStateVerify: true,
1282+
},
1283+
{
1284+
Config: testAccContainerCluster_withBinaryAuthorization(clusterName, false),
1285+
},
1286+
{
1287+
ResourceName: "google_container_cluster.with_binary_authorization",
1288+
ImportStateIdPrefix: "us-central1-a/",
1289+
ImportState: true,
1290+
ImportStateVerify: true,
1291+
},
1292+
},
1293+
})
1294+
}
1295+
12641296
func testAccCheckContainerClusterDestroy(s *terraform.State) error {
12651297
config := testAccProvider.Meta().(*Config)
12661298

@@ -2420,3 +2452,15 @@ resource "google_container_cluster" "with_resource_labels" {
24202452
}
24212453
`, clusterName)
24222454
}
2455+
2456+
func testAccContainerCluster_withBinaryAuthorization(clusterName string, enabled bool) string {
2457+
return fmt.Sprintf(`
2458+
resource "google_container_cluster" "with_binary_authorization" {
2459+
name = "%s"
2460+
zone = "us-central1-a"
2461+
initial_node_count = 1
2462+
2463+
enable_binary_authorization = %v
2464+
}
2465+
`, clusterName, enabled)
2466+
}

0 commit comments

Comments
 (0)