This commit is contained in:
parent
70b8303fdb
commit
da69666389
2 changed files with 18 additions and 20 deletions
|
@ -191,6 +191,21 @@ class ControlPlane(ServalaModelMixin, models.Model):
|
|||
except Exception as 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):
|
||||
"""
|
||||
|
@ -465,27 +480,9 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
|||
base = "{self.organization.urls.instances}{self.name}/"
|
||||
|
||||
@classmethod
|
||||
def create_instance(cls, organization, context, created_by, spec_data):
|
||||
name = spec_data.get("spec.name")
|
||||
|
||||
def create_instance(cls, name, organization, context, created_by, spec_data):
|
||||
# Ensure the namespace exists
|
||||
namespace_name = 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
|
||||
context.control_plane.get_or_create_namespace(organization.namespace)
|
||||
|
||||
return cls.objects.create(
|
||||
name=name,
|
||||
|
|
|
@ -128,6 +128,7 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
|
|||
try:
|
||||
service_instance = ServiceInstance.create_instance(
|
||||
organization=self.organization,
|
||||
name=form.cleaned_data["name"],
|
||||
context=self.context_object,
|
||||
created_by=request.user,
|
||||
spec_data=form.get_nested_data(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue