Extract and reuse kubernetes name validator
Some checks failed
Tests / test (push) Failing after 26s
Some checks failed
Tests / test (push) Failing after 26s
This commit is contained in:
parent
e37e126d9d
commit
555462a99e
3 changed files with 21 additions and 12 deletions
|
@ -1,7 +1,6 @@
|
||||||
import rules
|
import rules
|
||||||
import urlman
|
import urlman
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.validators import RegexValidator
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.utils.text import slugify
|
from django.utils.text import slugify
|
||||||
|
@ -10,6 +9,7 @@ from django_scopes import ScopedManager, scopes_disabled
|
||||||
|
|
||||||
from servala.core import rules as perms
|
from servala.core import rules as perms
|
||||||
from servala.core.models.mixins import ServalaModelMixin
|
from servala.core.models.mixins import ServalaModelMixin
|
||||||
|
from servala.core.validators import kubernetes_name_validator
|
||||||
|
|
||||||
|
|
||||||
class Organization(ServalaModelMixin, models.Model):
|
class Organization(ServalaModelMixin, models.Model):
|
||||||
|
@ -21,16 +21,7 @@ class Organization(ServalaModelMixin, models.Model):
|
||||||
help_text=_(
|
help_text=_(
|
||||||
"This namespace will be used for all Kubernetes resources. Cannot be changed after creation."
|
"This namespace will be used for all Kubernetes resources. Cannot be changed after creation."
|
||||||
),
|
),
|
||||||
validators=[
|
validators=[kubernetes_name_validator],
|
||||||
RegexValidator(
|
|
||||||
regex=r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
|
|
||||||
message=_(
|
|
||||||
'Namespace must consist of lowercase alphanumeric characters or "-", '
|
|
||||||
"must start and end with an alphanumeric character."
|
|
||||||
),
|
|
||||||
code="invalid_namespace",
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
billing_entity = models.ForeignKey(
|
billing_entity = models.ForeignKey(
|
||||||
|
|
|
@ -9,6 +9,7 @@ from kubernetes import config
|
||||||
from kubernetes.client.rest import ApiException
|
from kubernetes.client.rest import ApiException
|
||||||
|
|
||||||
from servala.core.models.mixins import ServalaModelMixin
|
from servala.core.models.mixins import ServalaModelMixin
|
||||||
|
from servala.core.validators import kubernetes_name_validator
|
||||||
|
|
||||||
|
|
||||||
class ServiceCategory(ServalaModelMixin, models.Model):
|
class ServiceCategory(ServalaModelMixin, models.Model):
|
||||||
|
@ -16,7 +17,9 @@ class ServiceCategory(ServalaModelMixin, models.Model):
|
||||||
Categories for services, e.g. "Databases", "Storage", "Compute".
|
Categories for services, e.g. "Databases", "Storage", "Compute".
|
||||||
"""
|
"""
|
||||||
|
|
||||||
name = models.CharField(max_length=100, verbose_name=_("Name"))
|
name = models.CharField(
|
||||||
|
max_length=100, verbose_name=_("Name"), validators=[kubernetes_name_validator]
|
||||||
|
)
|
||||||
description = models.TextField(blank=True, verbose_name=_("Description"))
|
description = models.TextField(blank=True, verbose_name=_("Description"))
|
||||||
logo = models.ImageField(
|
logo = models.ImageField(
|
||||||
upload_to="public/service_categories",
|
upload_to="public/service_categories",
|
||||||
|
@ -446,3 +449,6 @@ class ServiceInstance(ServalaModelMixin, models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("Service instance")
|
verbose_name = _("Service instance")
|
||||||
verbose_name_plural = _("Service instances")
|
verbose_name_plural = _("Service instances")
|
||||||
|
# Names are unique per de-facto namespace, which is defined by the
|
||||||
|
# Organization + ServiceDefinition (group, version) + the ControlPlane.
|
||||||
|
unique_together = [("name", "organization", "context")]
|
||||||
|
|
12
src/servala/core/validators.py
Normal file
12
src/servala/core/validators.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
from django.core.validators import RegexValidator
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
# Kubernetes resource name validator - follows the same rules as namespace names
|
||||||
|
kubernetes_name_validator = RegexValidator(
|
||||||
|
regex=r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
|
||||||
|
message=_(
|
||||||
|
'Name must consist of lowercase alphanumeric characters or "-", '
|
||||||
|
"must start and end with an alphanumeric character."
|
||||||
|
),
|
||||||
|
code="invalid_kubernetes_name",
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue