Successfully create an instance

This commit is contained in:
Tobias Kunze 2025-04-03 17:17:33 +02:00
parent a2c3695611
commit 08fada04e0
2 changed files with 29 additions and 3 deletions

View file

@ -6,7 +6,7 @@ from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from encrypted_fields.fields import EncryptedJSONField
from kubernetes import config
from kubernetes import client, config
from kubernetes.client.rest import ApiException
from servala.core.models.mixins import ServalaModelMixin
@ -137,7 +137,6 @@ class ControlPlane(ServalaModelMixin, models.Model):
"clusters": [
{
"cluster": {
"insecure-skip-tls-verify": True,
"certificate-authority-data": self.api_credentials[
"certificate-authority-data"
],
@ -484,6 +483,33 @@ class ServiceInstance(ServalaModelMixin, models.Model):
# Ensure the namespace exists
context.control_plane.get_or_create_namespace(organization.namespace)
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 {},
}
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,
)
return cls.objects.create(
name=name,
organization=organization,

View file

@ -131,7 +131,7 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
name=form.cleaned_data["name"],
context=self.context_object,
created_by=request.user,
spec_data=form.get_nested_data(),
spec_data=form.get_nested_data().get("spec"),
)
return redirect(service_instance.urls.base)
except Exception as e: