You can copy an existing Managed Service for Apache Spark on GKE virtual cluster's configuration,
update the copied configuration, and then create a new Managed Service for Apache Spark on GKE
cluster using the updated configuration.

## Recreate and update a Managed Service for Apache Spark on GKE cluster

### gcloud

1. Set environment variables:

   ```
   CLUSTER=existing Managed Service for Apache Spark on GKE  cluster name \
     REGION=region
   ```

   <br />

2. Export the existing Managed Service for Apache Spark on GKE cluster configuration to a YAML file.

   ```
   gcloud dataproc clusters export $CLUSTER \
       --region=$REGION > "${CLUSTER}-config.yaml"
   ```

   <br />

3. Update the configuration.

   1. Remove the
      [`kubernetesNamespace`](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.clusters.md.txt#KubernetesClusterConfig.FIELDS.kubernetes_namespace)
      field. Removing this field is necessary to avoid a namespace conflict
      when you create the updated cluster.

      Sample `sed` command to remove the `kubernetesNamespace` field:

      ```
      sed -E "s/kubernetesNamespace: .+$//g" ${CLUSTER}-config.yaml
      ```

      <br />

   2. Make additional changes to update Managed Service for Apache Spark on GKE virtual cluster
      configuration settings, such as changing the Spark
      [componentVersion](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.clusters.md.txt#KubernetesSoftwareConfig.FIELDS.component_version).

4. [Delete the existing Managed Service for Apache Spark on GKE virtual cluster](https://docs.cloud.google.com/managed-spark/docs/guides/dpgke/gke-delete-cluster.md.txt) if you will create a cluster that
   has the same name as the cluster it is updating (if you are replacing the
   original cluster).

5. Wait for the previous delete operation to finish, and then import the
   updated cluster configuration to create a new Managed Service for Apache Spark on GKE
   virtual cluster with the updated config settings.

   ```
   gcloud dataproc clusters import $CLUSTER \
       --region=$REGION \
       --source="${CLUSTER}-config.yaml"
   ```

### API

1. Set environment variables:

   ```
   CLUSTER=existing Managed Service for Apache Spark on GKE  cluster name \
     REGION=region
   ```

   <br />

2. Export the existing Managed Service for Apache Spark on GKE cluster configuration to a YAML file.

   ```
   curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)"  "https://dataproc.googleapis.com/v1/projects/${PROJECT}/regions/${REGION}/clusters/${CLUSTER}?alt=json" > "${CLUSTER}-config.json"
   ```

   <br />

3. Update the configuration.

   1. Remove the
      [`kubernetesNamespace`](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.clusters.md.txt#KubernetesClusterConfig.FIELDS.kubernetes_namespace)
      field. Removal of this field is necessary to avoid a namespace conflict
      when you create the updated cluster.

      Sample `jq` command to remove `kubernetesNamespace` field:

      ```
      jq 'del(.virtualClusterConfig.kubernetesClusterConfig.kubernetesNamespace)'
      ```

      <br />

   2. Make additional changes to update Managed Service for Apache Spark on GKE virtual cluster
      configuration settings, such as changing the Spark
      [componentVersion](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.clusters.md.txt#KubernetesSoftwareConfig.FIELDS.component_version).

4. Delete the existing Managed Service for Apache Spark on GKE virtual cluster if you will create a cluster
   that has the same name as the cluster it is updating (if you are replacing the
   original cluster).

   ```
   curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" "https://dataproc.googleapis.com/v1/projects/${PROJECT}/regions/${REGION}/clusters/${CLUSTER}"
   ```

   <br />

5. Wait for the previous delete operation to finish, and
   then import the updated cluster configuration to create a new Managed Service for Apache Spark on GKE
   virtual cluster with the updated settings.

   ```
   curl -i -X POST  -H "Authorization: Bearer $(gcloud auth print-access-token)"  -H "Content-Type: application/json; charset=utf-8" -d "@${CLUSTER}-config.json" "https://dataproc.googleapis.com/v1/projects/${PROJECT}/regions/${REGION}/clusters?alt=json"
   ```

### Console

The Google Cloud console does not support recreating a Managed Service for Apache Spark on GKE
virtual cluster by importing an existing cluster's configuration.

<br />