Service instantiation #31

Merged
rixx merged 37 commits from 24-service-instantiation into main 2025-04-04 10:57:29 +00:00
2 changed files with 18 additions and 20 deletions
Showing only changes of commit da69666389 - Show all commits

View file

@ -191,6 +191,21 @@ class ControlPlane(ServalaModelMixin, models.Model):
except Exception as e: except Exception as e:
return False, _("Connection error: {}").format(str(e)) return False, _("Connection error: {}").format(str(e))
def get_or_create_namespace(self, name):
api_instance = kubernetes.client.CoreV1Api(self.get_kubernetes_client())
try:
api_instance.read_namespace(name=name)
except kubernetes.client.ApiException as e:
if e.status == 404:
# Namespace does not exist, create it
body = kubernetes.client.V1Namespace(
metadata=kubernetes.client.V1ObjectMeta(name=name)
)
api_instance.create_namespace(body=body)
else:
# If there's another error, raise it
raise
class CloudProvider(ServalaModelMixin, models.Model): class CloudProvider(ServalaModelMixin, models.Model):
""" """
@ -465,27 +480,9 @@ class ServiceInstance(ServalaModelMixin, models.Model):
base = "{self.organization.urls.instances}{self.name}/" base = "{self.organization.urls.instances}{self.name}/"
@classmethod @classmethod
def create_instance(cls, organization, context, created_by, spec_data): def create_instance(cls, name, organization, context, created_by, spec_data):
name = spec_data.get("spec.name")
# Ensure the namespace exists # Ensure the namespace exists
namespace_name = organization.namespace context.control_plane.get_or_create_namespace(organization.namespace)
api_instance = kubernetes.client.CoreV1Api(
context.control_plane.get_kubernetes_client()
)
try:
api_instance.read_namespace(name=namespace_name)
except kubernetes.client.ApiException as e:
if e.status == 404:
# Namespace does not exist, create it
body = kubernetes.client.V1Namespace(
metadata=kubernetes.client.V1ObjectMeta(name=namespace_name)
)
api_instance.create_namespace(body=body)
else:
# If there's another error, raise it
raise
return cls.objects.create( return cls.objects.create(
name=name, name=name,

View file

@ -128,6 +128,7 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
try: try:
service_instance = ServiceInstance.create_instance( service_instance = ServiceInstance.create_instance(
organization=self.organization, organization=self.organization,
name=form.cleaned_data["name"],
context=self.context_object, context=self.context_object,
created_by=request.user, created_by=request.user,
spec_data=form.get_nested_data(), spec_data=form.get_nested_data(),