Skip to content

Commit

Permalink
Add allow_net_admin field to google_container_cluster resource (Googl…
Browse files Browse the repository at this point in the history
…eCloudPlatform#8323)

* add net_admin field to container cluster for autopilot clusters

* add allow_net_admin tests

* tweak param name

* fix net_admin test format string

* fix syntax bug

* fix api field typo

* version safety and ImportStateVerifyIgnore for min_master_version in test

* comment change

* Add documentation and remove provider-side validation

* add update test
  • Loading branch information
jeperetz authored and NickElliot committed Jul 31, 2023
1 parent c88f561 commit d3a1e48
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,12 @@ func ResourceContainerCluster() *schema.Resource {
// ConflictsWith: many fields, see https://cloud.google.com/kubernetes-engine/docs/concepts/autopilot-overview#comparison. The conflict is only set one-way, on other fields w/ this field.
},

"allow_net_admin": {
Type: schema.TypeBool,
Optional: true,
Description: `Enable NET_ADMIN for this cluster.`,
},

"authenticator_groups_config": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -2047,6 +2053,13 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
return err
}

var workloadPolicyConfig *container.WorkloadPolicyConfig
if allowed := d.Get("allow_net_admin").(bool); allowed {
workloadPolicyConfig = &container.WorkloadPolicyConfig{
AllowNetAdmin: allowed,
}
}

cluster := &container.Cluster{
Name: clusterName,
InitialNodeCount: int64(d.Get("initial_node_count").(int)),
Expand All @@ -2072,6 +2085,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
BinaryAuthorization: expandBinaryAuthorization(d.Get("binary_authorization"), d.Get("enable_binary_authorization").(bool)),
Autopilot: &container.Autopilot{
Enabled: d.Get("enable_autopilot").(bool),
WorkloadPolicyConfig: workloadPolicyConfig,
ForceSendFields: []string{"Enabled"},
},
ReleaseChannel: expandReleaseChannel(d.Get("release_channel")),
Expand Down Expand Up @@ -2492,10 +2506,15 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
return err
}
}
if cluster.Autopilot != nil {
if err := d.Set("enable_autopilot", cluster.Autopilot.Enabled); err != nil {
if autopilot := cluster.Autopilot; autopilot != nil {
if err := d.Set("enable_autopilot", autopilot.Enabled); err != nil {
return fmt.Errorf("Error setting enable_autopilot: %s", err)
}
if autopilot.WorkloadPolicyConfig != nil {
if err := d.Set("allow_net_admin", autopilot.WorkloadPolicyConfig.AllowNetAdmin); err != nil {
return fmt.Errorf("Error setting allow_net_admin: %s", err)
}
}
}
if cluster.ShieldedNodes != nil {
if err := d.Set("enable_shielded_nodes", cluster.ShieldedNodes.Enabled); err != nil {
Expand Down Expand Up @@ -2749,6 +2768,25 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
log.Printf("[INFO] GKE cluster %s's cluster-wide autoscaling has been updated", d.Id())
}

if d.HasChange("allow_net_admin") {
allowed := d.Get("allow_net_admin").(bool)
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
DesiredAutopilotWorkloadPolicyConfig: &container.WorkloadPolicyConfig{
AllowNetAdmin: allowed,
},
},
}

updateF := updateFunc(req, "updating net admin for GKE autopilot workload policy config")
// Call update serially.
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
return err
}

log.Printf("[INFO] GKE cluster %s's autopilot workload policy config allow_net_admin has been set to %v", d.Id(), allowed)
}

if d.HasChange("enable_binary_authorization") {
enabled := d.Get("enable_binary_authorization").(bool)
req := &container.UpdateClusterRequest{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3686,6 +3686,46 @@ func TestAccContainerCluster_autopilot_minimal(t *testing.T) {
})
}

func TestAccContainerCluster_autopilot_net_admin(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_autopilot_net_admin(clusterName, true),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
{
Config: testAccContainerCluster_autopilot_net_admin(clusterName, false),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
{
Config: testAccContainerCluster_autopilot_net_admin(clusterName, true),
},
{
ResourceName: "google_container_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
},
})
}

func testAccContainerCluster_masterAuthorizedNetworksDisabled(t *testing.T, resource_name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resource_name]
Expand Down Expand Up @@ -7641,3 +7681,14 @@ resource "google_container_cluster" "primary" {
enable_autopilot = true
}`, name)
}

func testAccContainerCluster_autopilot_net_admin(name string, enabled bool) string {
return fmt.Sprintf(`
resource "google_container_cluster" "primary" {
name = "%s"
location = "us-central1"
enable_autopilot = true
allow_net_admin = %t
min_master_version = 1.27
}`, name, enabled)
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ preferred.
* `addons_config` - (Optional) The configuration for addons supported by GKE.
Structure is [documented below](#nested_addons_config).

* `allow_net_admin` - (Optional) Enable NET_ADMIN for the cluster. Defaults to
`false`. This field should only be enabled for Autopilot clusters (`enable_autopilot`
set to `true`).

* `cluster_ipv4_cidr` - (Optional) The IP address range of the Kubernetes pods
in this cluster in CIDR notation (e.g. `10.96.0.0/14`). Leave blank to have one
automatically chosen or specify a `/14` block in `10.0.0.0/8`. This field will
Expand Down

0 comments on commit d3a1e48

Please sign in to comment.