Skip to content

Commit

Permalink
populate owner in workspace, remove namespace logic to reduce require…
Browse files Browse the repository at this point in the history
…ments on granted permissions
  • Loading branch information
achtsnits committed Dec 10, 2024
1 parent 90f9ff3 commit aa63b83
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
6 changes: 3 additions & 3 deletions workspace_api/tests/tests.http
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
@baseUrl = http://localhost:5000
#@baseUrl = https://workspace-api.apx.develop.eoepca.org

### create Workspace

POST {{baseUrl}}/workspaces/
Content-Type: application/json

{
"preferred_name": "joe223"
"preferred_name": "joe2"
}

### get Workspace Alice
Expand All @@ -16,6 +17,5 @@ Content-Type: application/json

### get Workspace Joe

GET {{baseUrl}}/workspaces/ws--joe
GET {{baseUrl}}/workspaces/ws-joe
Content-Type: application/json

43 changes: 2 additions & 41 deletions workspace_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ async def load_k8s_config():
k8s_config.load_incluster_config()


def namespace_exists(workspace_name) -> bool:
try:
k8s_client.CoreV1Api().read_namespace(name=workspace_name)
except kubernetes.client.rest.ApiException as e:
if e.status == HTTPStatus.NOT_FOUND:
return False
else:
raise
else:
return True


def fetch_secret(secret_name: str, namespace: str) -> Optional[k8s_client.V1Secret]:
try:
return cast(
Expand Down Expand Up @@ -77,22 +65,6 @@ async def create_workspace(
):
workspace_name = workspace_name_from_preferred_name(data.preferred_name)

# if namespace_exists(workspace_name):
# raise HTTPException(
# status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
# detail={"error": "Namespace already exists"},
# )

# k8s_client.CoreV1Api().create_namespace(
# k8s_client.V1Namespace(
# metadata=k8s_client.V1ObjectMeta(
# name=workspace_name,
# )
# )
# )

# return {"name": workspace_name}

dynamic_client = DynamicClient(kubernetes.client.ApiClient())
try:
dynamic_client.resources.get(api_version="epca.eo/v1beta1", kind="Workspace").get(name=workspace_name)
Expand All @@ -102,7 +74,6 @@ async def create_workspace(
)
except kubernetes.client.rest.ApiException as e:
if e.status == HTTPStatus.NOT_FOUND:
# Workspace doesn't exist, proceed with creation
pass
else:
raise
Expand All @@ -115,6 +86,7 @@ async def create_workspace(
"metadata": V1ObjectMeta(name=workspace_name),
"spec" : {
"subscription": "silver",
"owner": data.default_owner
}
}
print(f"creating {workspace_name} in {current_namespace()}")
Expand Down Expand Up @@ -202,9 +174,6 @@ class Workspace(BaseModel):

@app.get("/workspaces/{workspace_name}", response_model=Workspace)
async def get_workspace(workspace_name: str = workspace_path_type):
if not namespace_exists(workspace_name):
raise HTTPException(status_code=HTTPStatus.NOT_FOUND)

secret = fetch_secret(
secret_name=config.WORKSPACE_SECRET_NAME,
namespace=workspace_name,
Expand Down Expand Up @@ -247,14 +216,6 @@ def serialize_workspace(workspace_name: str, secret: k8s_client.V1Secret) -> Wor

@app.delete("/workspaces/{workspace_name}", status_code=HTTPStatus.NO_CONTENT)
async def delete_workspace(workspace_name: str = workspace_path_type):
# NOTE: name is validated via regex
# try:
# k8s_client.CoreV1Api().delete_namespace(workspace_name)
# except kubernetes.client.rest.ApiException as e:
# if e.status == HTTPStatus.NOT_FOUND:
# raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
# else:
# raise
try:
dynamic_client = DynamicClient(kubernetes.client.ApiClient())
dynamic_client.resources.get(api_version="epca.eo/v1beta1", kind="Workspace").delete(name=workspace_name, namespace=current_namespace())
Expand Down Expand Up @@ -292,4 +253,4 @@ def current_namespace() -> str:
try:
return open("/var/run/secrets/kubernetes.io/serviceaccount/namespace").read()
except FileNotFoundError:
return "rm"
return "workspace"

0 comments on commit aa63b83

Please sign in to comment.