Improve display of kubernetes errors
All checks were successful
Tests / test (push) Successful in 23s
All checks were successful
Tests / test (push) Successful in 23s
This commit is contained in:
parent
fb5a6e9a42
commit
4d13d71953
2 changed files with 46 additions and 29 deletions
|
@ -1,3 +1,5 @@
|
|||
import json
|
||||
|
||||
import kubernetes
|
||||
import urlman
|
||||
from django.conf import settings
|
||||
|
@ -506,32 +508,45 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
|||
)
|
||||
)
|
||||
|
||||
group = context.service_definition.api_definition["group"]
|
||||
version = context.service_definition.api_definition["version"]
|
||||
kind = context.service_definition.api_definition["kind"]
|
||||
create_data = {
|
||||
"apiVersion": f"{group}/{version}",
|
||||
"kind": kind,
|
||||
"metadata": {
|
||||
"name": name,
|
||||
"namespace": organization.namespace,
|
||||
},
|
||||
"spec": spec_data or {},
|
||||
}
|
||||
if label := context.control_plane.required_label:
|
||||
create_data["metadata"]["labels"] = {settings.DEFAULT_LABEL_KEY: label}
|
||||
api_instance = client.CustomObjectsApi(
|
||||
context.control_plane.get_kubernetes_client()
|
||||
)
|
||||
plural = kind.lower()
|
||||
if not plural.endswith("s"):
|
||||
plural = f"{plural}s"
|
||||
try:
|
||||
group = context.service_definition.api_definition["group"]
|
||||
version = context.service_definition.api_definition["version"]
|
||||
kind = context.service_definition.api_definition["kind"]
|
||||
create_data = {
|
||||
"apiVersion": f"{group}/{version}",
|
||||
"kind": kind,
|
||||
"metadata": {
|
||||
"name": name,
|
||||
"namespace": organization.namespace,
|
||||
},
|
||||
"spec": spec_data or {},
|
||||
}
|
||||
if label := context.control_plane.required_label:
|
||||
create_data["metadata"]["labels"] = [
|
||||
{settings.DEFAULT_LABEL_KEY: label}
|
||||
]
|
||||
api_instance = client.CustomObjectsApi(
|
||||
context.control_plane.get_kubernetes_client()
|
||||
)
|
||||
plural = kind.lower()
|
||||
if not plural.endswith("s"):
|
||||
plural = f"{plural}s"
|
||||
|
||||
api_instance.create_namespaced_custom_object(
|
||||
group=group,
|
||||
version=version,
|
||||
namespace=organization.namespace,
|
||||
plural=plural,
|
||||
body=create_data,
|
||||
)
|
||||
api_instance.create_namespaced_custom_object(
|
||||
group=group,
|
||||
version=version,
|
||||
namespace=organization.namespace,
|
||||
plural=plural,
|
||||
body=create_data,
|
||||
)
|
||||
except Exception as e:
|
||||
instance.delete()
|
||||
if isinstance(e, ApiException):
|
||||
try:
|
||||
error_body = json.loads(e.body)
|
||||
reason = error_body.get("message", str(e))
|
||||
raise ValidationError(_("Kubernetes API error: {}").format(reason))
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError(_("Kubernetes API error: {}").format(str(e)))
|
||||
raise ValidationError(_("Error creating instance: {}").format(str(e)))
|
||||
return instance
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.contrib import messages
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.shortcuts import redirect
|
||||
from django.utils.functional import cached_property
|
||||
from django.views.generic import DetailView, ListView
|
||||
|
@ -134,9 +135,10 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
|
|||
spec_data=form.get_nested_data().get("spec"),
|
||||
)
|
||||
return redirect(service_instance.urls.base)
|
||||
except ValidationError as e:
|
||||
messages.error(self.request, e.message or str(e))
|
||||
except Exception as e:
|
||||
error_message = getattr(e, "message", None) or str(e)
|
||||
messages.error(self.request, error_message)
|
||||
messages.error(self.request, str(e))
|
||||
|
||||
# If the form is not valid or if the service creation failed, we render it again
|
||||
context["service_form"] = form
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue