Skip to content

Commit

Permalink
Fix cleanup functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovilla committed Aug 7, 2024
1 parent bdddbe2 commit 6338899
Showing 1 changed file with 56 additions and 41 deletions.
97 changes: 56 additions & 41 deletions src/_nebari/provider/cloud/google_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import os
from typing import List, Set

import google.api_core.exceptions
from google.auth import load_credentials_from_dict, load_credentials_from_file
from google.cloud import compute_v1, container_v1, iam_credentials_v1, storage
from google.cloud import compute_v1, container_v1, iam_admin_v1, storage

from _nebari.constants import GCP_ENV_DOCS
from _nebari.provider.cloud.commons import filter_by_highest_supported_k8s_version
Expand Down Expand Up @@ -53,85 +54,99 @@ def kubernetes_versions(region: str) -> List[str]:
return filter_by_highest_supported_k8s_version(supported_kubernetes_versions)


def cluster_exists(cluster_name: str, zone: str) -> bool:
def cluster_exists(cluster_name: str, region: str) -> bool:
"""Check if a GKE cluster exists."""
credentials, project_id = load_credentials()
client = container_v1.ClusterManagerClient(credentials=credentials)
request = container_v1.GetClusterRequest(credentials=credentials)
response = client.get_cluster(request=request, project_id=project_id, zone=zone)

return response is not None
try:
client.get_cluster(
name=f"projects/{project_id}/locations/{region}/clusters/{cluster_name}"
)
except google.api_core.exceptions.NotFound:
return False
return True


def bucket_exists(bucket_name: str, project_id: str) -> bool:
def bucket_exists(bucket_name: str) -> bool:
"""Check if a storage bucket exists."""
client = storage.Client(project=project_id)
bucket = client.get_bucket(bucket_name)
return bucket is not None
credentials, _ = load_credentials()
client = storage.Client(credentials=credentials)

try:
client.get_bucket(bucket_name)
except google.api_core.exceptions.NotFound:
return False
return True


def service_account_exists(service_account_name: str, project_id: str) -> bool:
def service_account_exists(service_account_name: str) -> bool:
"""Check if a service account exists."""
client = iam_credentials_v1.IAMCredentialsClient()
service_acc = client.service_account_path(project_id, service_account_name)
return service_acc is not None
credentials, project_id = load_credentials()
client = iam_admin_v1.IAMClient(credentials=credentials)

service_account_path = client.service_account_path(project_id, service_account_name)
try:
client.get_service_account(name=service_account_path)
except google.api_core.exceptions.NotFound:
return False
return True

def delete_cluster(cluster_name: str, project_id: str, region: str):
"""Delete a GKE cluster if it exists."""
check_credentials()

if not cluster_exists(cluster_name, project_id, region):
def delete_cluster(cluster_name: str, region: str):
"""Delete a GKE cluster if it exists."""
credentials, project_id = load_credentials()
if not cluster_exists(cluster_name, region):
print(
f"Cluster {cluster_name} does not exist in project {project_id}, region {region}. Exiting gracefully."
)
return

client = container_v1.ClusterManagerClient()
request = client.DeleteClusterRequest()
client = container_v1.ClusterManagerClient(credentials=credentials)
try:
client.delete_cluster(request=request)
except google.api_core.exceptions.GoogleAPICallError as e:
if e.status_code == 200:
print("Cluster deleted successfully!")
else:
print("error deleting cluster!")
client.delete_cluster(
name=f"projects/{project_id}/locations/{region}/clusters/{cluster_name}"
)
print(f"Successfully deleted cluster {cluster_name}.")
except google.api_core.exceptions.GoogleAPIError as e:
print(f"Failed to delete bucket {bucket_name}. Error: {e}")


def delete_storage_bucket(bucket_name: str, project_id: str):
def delete_storage_bucket(bucket_name: str):
"""Delete a storage bucket if it exists."""
check_credentials()
credentials, project_id = load_credentials()

if not bucket_exists(bucket_name, project_id):
if not bucket_exists(bucket_name):
print(
f"Bucket {bucket_name} does not exist in project {project_id}. Exiting gracefully."
)
return

client = storage.Client(project=project_id)
client = storage.Client(credentials=credentials)
bucket = client.get_bucket(bucket_name)
try:
bucket.delete()
bucket.delete(force=True)
print(f"Successfully deleted bucket {bucket_name}.")
except storage.exceptions.BucketNotFoundError as e:
except google.api_core.exceptions.GoogleAPIError as e:
print(f"Failed to delete bucket {bucket_name}. Error: {e}")


def delete_service_account(service_account_name: str, project_id: str):
def delete_service_account(service_account_name: str):
"""Delete a service account if it exists."""
check_credentials()
credentials, project_id = load_credentials()

if not service_account_exists(service_account_name, project_id):
if not service_account_exists(service_account_name):
print(
f"Service account {service_account_name} does not exist in project {project_id}. Exiting gracefully."
)
return
client = iam_credentials_v1.IAMCredentialsClient()
client.service_account_path(project_id, service_account_name)

client = iam_admin_v1.IAMClient(credentials=credentials)
service_account_path = client.service_account_path(project_id, service_account_name)
try:
client.delete_service_account(service_account_name)
client.delete_service_account(name=service_account_path)
print(f"Successfully deleted service account {service_account_name}.")
except iam_credentials_v1.exceptions.IamServiceAccountNotFoundError as e:
except google.api_core.exceptions.GoogleAPIError as e:
print(f"Failed to delete service account {service_account_name}. Error: {e}")


Expand All @@ -148,6 +163,6 @@ def gcp_cleanup(config: schema.Main):
f"{project_name}-{namespace}@{project_id}.iam.gserviceaccount.com"
)

delete_cluster(cluster_name, project_id, region)
delete_storage_bucket(bucket_name, project_id)
delete_service_account(service_account_name, project_id)
delete_cluster(cluster_name, region)
delete_storage_bucket(bucket_name)
delete_service_account(service_account_name)

0 comments on commit 6338899

Please sign in to comment.