Ejemplo n.º 1
0
    def create(self, cluster_create: EKSCreate) -> None:
        cluster_data = EKSCreateAPIFilter(**cluster_create.dict())
        cluster = self.client.create_cluster(**cluster_data.dict())

        if cluster:
            count = 0
            cluster_status = self.cluster_status(cluster_data.name)
            while cluster_status != "ACTIVE":
                if count > self.RETRY_COUNT:
                    raise HTTPException(
                        status_code=400,
                        detail="Something doesn't seem right "
                        f"with cluster {cluster_data.name}",
                    )
                logger.info("waiting before trying to create node group again")
                time.sleep(60)
                cluster_status = self.cluster_status(cluster_data.name)
                count += 1

        for nodegroup in cluster_create.nodegroups:
            try:
                self.client.create_nodegroup(**nodegroup.dict())
            except Exception as e:
                logger.error(e)
        return
Ejemplo n.º 2
0
 def describe_cluster_nodegroup(
     self, cluster_name: str, nodegroup_name: str
 ) -> List[dict]:
     try:
         return self.client.describe_nodegroup(
             clusterName=cluster_name, nodegroupName=nodegroup_name
         )
     except Exception as e:
         logger.error(e)
         raise HTTPException(status_code=400, detail=str(e))
Ejemplo n.º 3
0
 def handle_google_request(self, req: Any) -> dict:
     try:
         res = req.execute()
         return dict(res)
     except google_api_client_errors.HttpError as e:
         content = e.content.decode("utf8")
         logger.error(content)
         err = GKEErrorResponse(**json.loads(content))
         raise HTTPException(status_code=err.error.code,
                             detail=err.error.message)
Ejemplo n.º 4
0
 def __init__(self) -> None:
     try:
         self.client = build("container", "v1beta1")
         self.cluster_client = self.client.projects().zones().clusters()
     except google_auth_exceptions.DefaultCredentialsError as e:
         logger.error(
             "Missing credentials for GKEApi. "
             "Make sure you've set your "
             "GOOGLE_APPLICATION_CREDENTIALS environment variable.\n"
             f"Full exception:\n{e}")
Ejemplo n.º 5
0
 def create(self, resource_group_name: str, cluster_create: AKSCreate) -> dict:
     try:
         self.make_begin_create_or_update_request(
             resource_group_name=resource_group_name, cluster_create=cluster_create
         )
         return {
             "message": f"AKS create request submitted for {cluster_create.name}"
         }
     except ResourceExistsError as e:
         logger.error(f"Create AKS cluster failed with: {e.message}")
         raise HTTPException(detail=e.message, status_code=400)
Ejemplo n.º 6
0
 def describe(self, resource_group_name: str, cluster_name: str) -> ManagedCluster:
     try:
         return self.container_client.managed_clusters.get(
             resource_group_name=resource_group_name,
             resource_name=cluster_name,
         )
     except ResourceNotFoundError as e:
         logger.error(
             f"Get AKS cluster {cluster_name} failed for resource "
             f"group {resource_group_name} with: {e.message}"
         )
         raise HTTPException(detail=e.message, status_code=400)
Ejemplo n.º 7
0
 def __init__(self, region: str):
     self.region = region
     try:
         self.client = boto3.client("eks", region_name=self.region)
     except Exception as e:
         logger.error("Exception: ", e)
         logger.error(
             "Missing credentials for EKSApi. ",
             "Make sure you've set your ",
             "AWS_PROFILE environment variable and have ",
             "specificed those credentials in ~/.aws/credentials.",
         )
Ejemplo n.º 8
0
 def begin_delete(self, resource_group_name: str, cluster_name: str) -> dict:
     try:
         self.make_begin_delete_request(
             resource_group_name=resource_group_name, cluster_name=cluster_name
         )
         return {
             "message": f"AKS delete request submitted for {cluster_name}",
         }
     except ResourceNotFoundError as e:
         logger.error(
             f"Delete AKS cluster {cluster_name} failed for resource "
             f"group {resource_group_name} with: {e.message}"
         )
         raise HTTPException(detail=e.message, status_code=400)
Ejemplo n.º 9
0
 def get(self, resource_group_name: str) -> List[dict]:
     try:
         return [
             cluster.as_dict()
             for cluster in self.make_get_request(
                 resource_group_name=resource_group_name
             )
         ]
     except ResourceNotFoundError as e:
         logger.error(
             "Get AKS clusters failed for resource "
             f"group {resource_group_name} with: {e.message}"
         )
         raise HTTPException(detail=e.message, status_code=400)
Ejemplo n.º 10
0
 def __init__(self) -> None:
     try:
         assert self.AZURE_SUBSCRIPTION_ID is not None
         assert self.AZURE_TENANT_ID is not None
         assert self.AZURE_CLIENT_ID is not None
         assert self.AZURE_CLIENT_SECRET is not None
         credential = ClientSecretCredential(
             tenant_id=self.AZURE_TENANT_ID,
             client_id=self.AZURE_CLIENT_ID,
             client_secret=self.AZURE_CLIENT_SECRET,
         )
         container_client = ContainerServiceClient(
             credential=credential,
             subscription_id=self.AZURE_SUBSCRIPTION_ID,
         )
         self.container_client = container_client
     except Exception as e:
         logger.error(
             "Failed to create ContainerServiceClient for the api server. "
             "Make sure you've set the AZURE_SUBSCRIPTION_ID AZURE_TENANT_ID "
             "AZURE_CLIENT_ID AZURE_CLIENT_SECRET environment variables.\n"
             f"Full exception:\n{e}"
         )
Ejemplo n.º 11
0
 def delete(self, cluster_name: str, nodegroup_names: List[str]) -> dict:
     for nodegroup_name in nodegroup_names:
         try:
             self.client.delete_nodegroup(
                 clusterName=cluster_name, nodegroupName=nodegroup_name
             )
         except Exception as e:
             response_attr = "response"
             if hasattr(e, response_attr):
                 logger.error(getattr(e, response_attr))
             else:
                 logger.error(
                     f"Logging exception that does not have response attr: {e}"
                 )
     count = 0
     while True:
         if count > self.RETRY_COUNT:
             raise HTTPException(
                 status_code=400,
                 detail="Timed out waiting for node groups to delete.",
             )
         try:
             self.client.delete_cluster(name=cluster_name)
             return {
                 "cluster_name": cluster_name,
                 "nodegroup_names": nodegroup_names,
             }
         except Exception as e:
             response_attr = "response"
             if hasattr(e, response_attr):
                 logger.error(getattr(e, response_attr))
             else:
                 logger.error(
                     f"Logging exception that does not have response attr: {e}"
                 )
         count += 1
         logger.info("waiting before trying to delete cluster again")
         time.sleep(60)
Ejemplo n.º 12
0
 def list_cluster_nodegroups(self, cluster_name: str) -> List[dict]:
     try:
         return self.client.list_nodegroups(clusterName=cluster_name)
     except Exception as e:
         logger.error(e)
         raise HTTPException(status_code=400, detail=str(e))
Ejemplo n.º 13
0
 def describe(self, cluster_name: str) -> dict:
     try:
         return self.client.describe_cluster(name=cluster_name)
     except Exception as e:
         logger.error(e)
         raise HTTPException(status_code=400, detail=str(e))
Ejemplo n.º 14
0
 def get(self) -> List[str]:
     try:
         return self.client.list_clusters().get("clusters", [])
     except Exception as e:
         logger.error(e)
         raise HTTPException(status_code=400, detail=str(e))