Skip to content

Commit

Permalink
[IMP] Added namespace prefix to application sets to be able to deploy…
Browse files Browse the repository at this point in the history
… to different namespaces.
  • Loading branch information
MrGigSolutions authored and tarteo committed May 14, 2024
1 parent 2d776ed commit a648e7f
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 6 deletions.
2 changes: 2 additions & 0 deletions argocd_deployer/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"version": "16.0.1.1.0",
"data": [
"data/ir_config_parameter_data.xml",
"data/application_namespace_prefix.xml",
"data/application_set_template.xml",
"data/application_set.xml",
"views/application_template_view.xml",
Expand All @@ -15,6 +16,7 @@
"views/application_value_view.xml",
"views/application_view.xml",
"views/application_set_view.xml",
"views/application_namespace_prefix_view.xml",
"templates/application_description.xml",
"security/ir.model.access.csv",
"menuitems.xml",
Expand Down
8 changes: 8 additions & 0 deletions argocd_deployer/data/application_namespace_prefix.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<odoo noupdate="1">
<record id="namespace_prefix_flavoured_odoo" model="argocd.application.namespace.prefix">
<field name="name">flavoured-odoo-</field>
</record>
<record id="namespace_prefix_application_set" model="argocd.application.namespace.prefix">
<field name="name">application-set-</field>
</record>
</odoo>
2 changes: 2 additions & 0 deletions argocd_deployer/data/application_set.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<field name="repository_directory">/home/tarteo/repo</field>
<field name="deployment_directory">application_sets</field>
<field name="template_id" ref="application_set_template_master"/>
<field name="namespace_prefix_id" ref="argocd_deployer.namespace_prefix_application_set"/>
<field name="is_master">True</field>
</record>
<record id="application_set_default" model="argocd.application.set">
Expand All @@ -20,6 +21,7 @@
<field name="domain_format">%(application_name)s.curq.k8s.onestein.eu</field>
<field name="subdomain_format">%(subdomain)s.%(application_name)s.curq.k8s.onestein.eu</field>
<field name="template_id" ref="application_set_template_default"/>
<field name="namespace_prefix_id" ref="argocd_deployer.namespace_prefix_flavoured_odoo"/>
<field name="is_master">False</field>
</record>
</odoo>
10 changes: 5 additions & 5 deletions argocd_deployer/data/application_set_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
- path: {{.config.deployment_directory}}/**/application_set.yaml
template:
metadata:
name: "application-set-{{ "{{ " }}.path.basename{{ " }}" }}"
name: "{{.application_set.namespace_prefix}}{{ "{{ " }}.path.basename{{ " }}" }}"
spec:
project: "default"
source:
Expand All @@ -26,7 +26,7 @@ spec:
path: "{{ "{{ " }}.path.filename{{ " }}" }}"
destination:
name: in-cluster
namespace: "application-set-{{ "{{ " }}.path.basename{{ " }}" }}"
namespace: "{{.application_set.namespace_prefix}}{{ "{{ " }}.path.basename{{ " }}" }}"
syncPolicy:
syncOptions:
- CreateNamespace=true
Expand All @@ -40,7 +40,7 @@ spec:
<field name="yaml">apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: "flavoured-odoo-{{.application_set.name}}"
name: "{{.application_set.namespace_prefix}}{{.application_set.name}}"
namespace: argocd
spec:
goTemplate: true
Expand All @@ -52,7 +52,7 @@ spec:
- path: {{.application_set.deployment_directory}}/**/config.yaml
template:
metadata:
name: "flavoured-odoo-{{ .path.basename }}"
name: "{{.application_set.namespace_prefix}}{{ .path.basename }}"
spec:
project: "default"
source:
Expand All @@ -64,7 +64,7 @@ spec:
{{ .helm }}
destination:
name: in-cluster
namespace: "flavoured-odoo-{{ .path.basename }}"
namespace: "{{.application_set.namespace_prefix}}{{ .path.basename }}"
syncPolicy:
syncOptions:
- CreateNamespace=true
Expand Down
7 changes: 7 additions & 0 deletions argocd_deployer/menuitems.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@
parent="config_menu"
sequence="30"
/>

<menuitem
id="application_namespace_prefix_menu"
action="application_namespace_prefix_action"
parent="config_menu"
sequence="40"
/>
</odoo>
1 change: 1 addition & 0 deletions argocd_deployer/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import application_tag
from . import application_tag_domain_override
from . import application_value
from . import application_namespace_prefix
31 changes: 31 additions & 0 deletions argocd_deployer/models/application_namespace_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import re

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ApplicationNamespacePrefix(models.Model):
_name = "argocd.application.namespace.prefix"
_description = "Application Namespace Prefix"

name = fields.Char(required=True)

_sql_constraints = [
("application_namespace_name_prefix_unique", "unique(name)", "Already exists"),
(
"app_namespace_prefix_unique",
"unique(name)",
"A namespace prefix with that name was already defined.",
),
]

@api.constrains("name")
def _constrain_name(self):
if not re.match(
"^[a-z0-9-]{1,100}$", self.name
): # lowercase a to z, 0 to 9 and - (dash) are allowed
raise ValidationError(
_(
"Only lowercase letters, numbers and dashes are allowed in the name (max 100 characters)."
)
)
3 changes: 3 additions & 0 deletions argocd_deployer/models/application_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ApplicationSet(models.Model):
help="The domain format used to build the domain for the deployment.",
default="-",
)
namespace_prefix_id = fields.Many2one("argocd.application.namespace.prefix")

_sql_constraints = [
("application_set_name_unique", "unique(name)", "Already exists"),
Expand Down Expand Up @@ -257,6 +258,8 @@ def _get_argocd_template(self):
"{{.application_set.branch}}": self.branch or "",
"{{.application_set.deployment_directory}}": self.deployment_directory
or "",
"{{.application_set.namespace_prefix}}": self.namespace_prefix_id.name
or "",
}
template_yaml = self.template_id.yaml
for key, value in replacements.items():
Expand Down
1 change: 1 addition & 0 deletions argocd_deployer/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import test_application
from . import test_application_set
from . import test_application_tag
from . import test_application_namespace_prefix
24 changes: 24 additions & 0 deletions argocd_deployer/tests/test_application_namespace_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase


class TestApplicationNamespacePrefix(TransactionCase):
def test_name(self):
"""Name may only contain lowercase letters, digits and underscores."""
with self.assertRaisesRegex(
ValidationError, "Only lowercase letters, numbers and dashes"
):
self.env["argocd.application.namespace.prefix"].create({"name": "Hello"})

with self.assertRaisesRegex(ValidationError, "max 100 characters"):
self.env["argocd.application.namespace.prefix"].create(
{
"name": "this-name-is-waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay-"
"toooooooooooooooooooooo-ridiculously-long-and should-"
"totally-not-be-allowed"
}
)

self.env["argocd.application.namespace.prefix"].create(
{"name": "hello-the-namespace"}
)
46 changes: 45 additions & 1 deletion argocd_deployer/tests/test_application_set.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import MagicMock, mock_open, patch

from odoo.exceptions import UserError
from odoo.exceptions import UserError, ValidationError
from odoo.tests import TransactionCase

APPLICATION_SET_PATCH = (
Expand All @@ -25,6 +25,8 @@ def setUpClass(cls):
revision: {{.config.branch}}
path: {{.config.deployment_directory}}
template-path: {{.path.path}}
destination:
namespace: {{.application_set.namespace_prefix}}{{.path.basename}}
""",
}
)
Expand All @@ -36,6 +38,9 @@ def setUpClass(cls):
"template_id": cls.application_set_template.id,
"repository_directory": "/home/test",
"deployment_directory": "instances",
"namespace_prefix_id": cls.env.ref(
"argocd_deployer.namespace_prefix_application_set"
).id,
}
)

Expand All @@ -53,8 +58,47 @@ def setUpClass(cls):
revision: {cls.master_application_set.branch}
path: {cls.master_application_set.deployment_directory}
template-path: {{{{.path.path}}}}
destination:
namespace: application-set-{{{{.path.basename}}}}
"""

def test_name(self):
"""Name may only contain lowercase letters, digits and underscores."""
params = {
"template_id": self.env.ref(
"argocd_deployer.application_set_template_default"
).id,
"repository_url": "[email protected]:odoo/odoo-no-exist.git",
"repository_directory": "/home/test",
}

with self.assertRaisesRegex(
ValidationError, "Only lowercase letters, numbers and dashes"
):
self.env["argocd.application.set"].create(
{
**params,
"name": "Hello",
}
)

with self.assertRaisesRegex(ValidationError, "max 100 characters"):
self.env["argocd.application.set"].create(
{
**params,
"name": "this-name-is-waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay-"
"toooooooooooooooooooooo-ridiculously-long-and should-"
"totally-not-be-allowed",
}
)

self.env["argocd.application.set"].create(
{
**params,
"name": "hello-the-namespace",
}
)

def test_get_master_repository_directory(self):
"""The master repository directory is stored in the config.
Check that it behaves."""
Expand Down
18 changes: 18 additions & 0 deletions argocd_deployer/views/application_namespace_prefix_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="application_namespace_prefix_tree" model="ir.ui.view">
<field name="model">argocd.application.namespace.prefix</field>
<field name="arch" type="xml">
<tree editable="bottom">
<field name="name" />
</tree>
</field>
</record>

<record id="application_namespace_prefix_action" model="ir.actions.act_window">
<field name="name">Namespace prefixes</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">argocd.application.namespace.prefix</field>
<field name="view_mode">tree</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions argocd_deployer/views/application_set_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<group name="group_applications" string="Applications">
<field name="domain_format"/>
<field name="subdomain_format"/>
<field name="namespace_prefix_id"/>
</group>
</group>
</sheet>
Expand Down

0 comments on commit a648e7f

Please sign in to comment.