Compare commits
No commits in common. "4d13d71953f4c7a2f144094596c8dec1b8486601" and "a2ac202f265e8af45c5c7b6303ee652b7836ec7f" have entirely different histories.
4d13d71953
...
a2ac202f26
2 changed files with 35 additions and 60 deletions
|
@ -1,11 +1,9 @@
|
||||||
import json
|
|
||||||
|
|
||||||
import kubernetes
|
import kubernetes
|
||||||
import urlman
|
import urlman
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import IntegrityError, models
|
from django.db import models
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from encrypted_fields.fields import EncryptedJSONField
|
from encrypted_fields.fields import EncryptedJSONField
|
||||||
|
@ -494,59 +492,39 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
def create_instance(cls, name, organization, context, created_by, spec_data):
|
def create_instance(cls, name, organization, context, created_by, spec_data):
|
||||||
# Ensure the namespace exists
|
# Ensure the namespace exists
|
||||||
context.control_plane.get_or_create_namespace(organization.namespace)
|
context.control_plane.get_or_create_namespace(organization.namespace)
|
||||||
try:
|
|
||||||
instance = cls.objects.create(
|
|
||||||
name=name,
|
|
||||||
organization=organization,
|
|
||||||
created_by=created_by,
|
|
||||||
context=context,
|
|
||||||
)
|
|
||||||
except IntegrityError:
|
|
||||||
raise ValidationError(
|
|
||||||
_(
|
|
||||||
"An instance with this name already exists in this organization. Please choose a different name."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
group = context.service_definition.api_definition["group"]
|
||||||
group = context.service_definition.api_definition["group"]
|
version = context.service_definition.api_definition["version"]
|
||||||
version = context.service_definition.api_definition["version"]
|
kind = context.service_definition.api_definition["kind"]
|
||||||
kind = context.service_definition.api_definition["kind"]
|
create_data = {
|
||||||
create_data = {
|
"apiVersion": f"{group}/{version}",
|
||||||
"apiVersion": f"{group}/{version}",
|
"kind": kind,
|
||||||
"kind": kind,
|
"metadata": {
|
||||||
"metadata": {
|
"name": name,
|
||||||
"name": name,
|
"namespace": organization.namespace,
|
||||||
"namespace": organization.namespace,
|
},
|
||||||
},
|
"spec": spec_data or {},
|
||||||
"spec": spec_data or {},
|
}
|
||||||
}
|
if label := context.control_plane.required_label:
|
||||||
if label := context.control_plane.required_label:
|
create_data["metadata"]["labels"] = {settings.DEFAULT_LABEL_KEY: label}
|
||||||
create_data["metadata"]["labels"] = [
|
api_instance = client.CustomObjectsApi(
|
||||||
{settings.DEFAULT_LABEL_KEY: label}
|
context.control_plane.get_kubernetes_client()
|
||||||
]
|
)
|
||||||
api_instance = client.CustomObjectsApi(
|
plural = kind.lower()
|
||||||
context.control_plane.get_kubernetes_client()
|
if not plural.endswith("s"):
|
||||||
)
|
plural = f"{plural}s"
|
||||||
plural = kind.lower()
|
|
||||||
if not plural.endswith("s"):
|
|
||||||
plural = f"{plural}s"
|
|
||||||
|
|
||||||
api_instance.create_namespaced_custom_object(
|
api_instance.create_namespaced_custom_object(
|
||||||
group=group,
|
group=group,
|
||||||
version=version,
|
version=version,
|
||||||
namespace=organization.namespace,
|
namespace=organization.namespace,
|
||||||
plural=plural,
|
plural=plural,
|
||||||
body=create_data,
|
body=create_data,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
|
||||||
instance.delete()
|
return cls.objects.create(
|
||||||
if isinstance(e, ApiException):
|
name=name,
|
||||||
try:
|
organization=organization,
|
||||||
error_body = json.loads(e.body)
|
created_by=created_by,
|
||||||
reason = error_body.get("message", str(e))
|
context=context,
|
||||||
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,5 +1,4 @@
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.core.exceptions import ValidationError
|
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.views.generic import DetailView, ListView
|
from django.views.generic import DetailView, ListView
|
||||||
|
@ -135,8 +134,6 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
|
||||||
spec_data=form.get_nested_data().get("spec"),
|
spec_data=form.get_nested_data().get("spec"),
|
||||||
)
|
)
|
||||||
return redirect(service_instance.urls.base)
|
return redirect(service_instance.urls.base)
|
||||||
except ValidationError as e:
|
|
||||||
messages.error(self.request, e.message or str(e))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messages.error(self.request, str(e))
|
messages.error(self.request, str(e))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue