add namespace import
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s

This commit is contained in:
2026-06-19 15:58:38 +01:00
parent e74b659b45
commit aa0939be6d
7 changed files with 105 additions and 18 deletions
+22 -5
View File
@@ -1,8 +1,8 @@
from django.contrib import admin,messages
from django.utils.translation import ngettext
from .models import Machine_Data,Schedule_Day_Data,Schedule_Week_Data
from .models import Deployment_Data,Schedule_Day_Data,Schedule_Week_Data,Namespace_Data,namespace_cmm_data_object
from .views import import_namespace
from apps.kubernetes_connection.utils import scale_up,scale_down
# Register your models here.
DB = 'cloudmanager_db'
@@ -43,6 +43,18 @@ def create_entry(modeladmin, request, queryset):
print("simpsons_data_object.create(season=2, episode=3, url='google.com'', seen=True)")
#call function to trigger refresh of all deploy
@admin.action(description="create_entry")
def create_entry(modeladmin, request, queryset):
print("simpsons_data_object.create(season=2, episode=3, url='google.com'', seen=True)")
#call function to trigger refresh of all deploy
@admin.action(description="import namespace")
def import_namespace_admin(modeladmin, request, queryset):
import_namespace(namespace_cmm_data_object)
print("simpsons_data_object.create(season=2, episode=3, url='google.com'', seen=True)")
#call function to trigger refresh of all deploy
class ActiveAtHourFilter(admin.SimpleListFilter):
title = 'Active at hour'
parameter_name = 'active_at_hour'
@@ -74,8 +86,8 @@ class MultiDBModelAdmin(admin.ModelAdmin):
queryset.using(self.db).delete()
@admin.register(Machine_Data)
class MachineDataAdmin(MultiDBModelAdmin):
@admin.register(Deployment_Data)
class DeploymentDataAdmin(MultiDBModelAdmin):
list_display = ('name', 'namespace', 'schedule_week', 'schedule_day', 'replicas')
list_filter = (ActiveAtHourFilter,)
actions = [scale_up_deploy,scale_down_deploy]
@@ -92,4 +104,9 @@ class ScheduleWeekAdmin(MultiDBModelAdmin):
'name_schedule',
'state_Monday', 'state_Tuesday', 'state_Wednesday',
'state_Thursday', 'state_Friday', 'state_Saturday', 'state_Sunday',
)
)
@admin.register(Namespace_Data)
class NamespaceAdmin(MultiDBModelAdmin):
list_display = ('namespace',)
actions = [import_namespace_admin]
@@ -0,0 +1,23 @@
# Generated by Django 4.2.30 on 2026-06-19 14:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cloudmanager', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Namespace_Data',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('namespace', models.CharField(max_length=100)),
],
options={
'db_table': 'namespace',
},
),
]
@@ -0,0 +1,17 @@
# Generated by Django 4.2.30 on 2026-06-19 14:49
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('cloudmanager', '0002_namespace_data'),
]
operations = [
migrations.RenameModel(
old_name='Machine_Data',
new_name='Deployment_Data',
),
]
+9 -2
View File
@@ -47,7 +47,7 @@ class Schedule_Day_Data(models.Model):
def __str__(self):
return self.name_schedule
class Machine_Data(models.Model):
class Deployment_Data(models.Model):
name = models.CharField(max_length=100)
namespace = models.CharField(max_length=100)
schedule_week = models.ForeignKey(Schedule_Week_Data,null=True, blank=True, on_delete=models.SET_NULL)
@@ -55,4 +55,11 @@ class Machine_Data(models.Model):
replicas = models.IntegerField()
class Meta:
db_table = 'machine'
class Namespace_Data(models.Model):
namespace = models.CharField(max_length=100)
#deployments = models.ForeignKey(Schedule_Week_Data,null=True, blank=True, on_delete=models.SET_NULL)
class Meta:
db_table = 'namespace'
namespace_cmm_data_object = Namespace_Data.objects.using('cloudmanager_db')
+8 -1
View File
@@ -1,7 +1,9 @@
from django.http import HttpResponse
# IMPORT AT THE TOP HERE: This is completely safe!
from apps.cloudmanager.models import Machine_Data,Schedule_Week_Data
from apps.cloudmanager.models import Deployment_Data,Schedule_Week_Data
from apps.kubernetes_connection.utils import get_namespace
from .models import namespace_cmm_data_object
def coucou(request):
# 1. Prepare the data inside the function
@@ -23,3 +25,8 @@ def display(request):
print(data[0].namespace)
return HttpResponse(data)
def import_namespace(queryset):
namespaces_list = get_namespace()
for namespace in namespaces_list:
queryset.create(namespace=namespace)
@@ -14,6 +14,7 @@ import ruamel.yaml
import yaml
import urllib.request
def hello(request):
return HttpResponse('<h1>Hello k8s!</h1>')
@@ -43,15 +44,6 @@ def scale_down(request,namespace_name,deploy_name):
return render(request, 'dashboard/shutdown.html',
{'namespace': namespace_name, 'deployment': deploy_name, 'url': 'https://namespace.com'})
def Import_Deployments():
config.load_kube_config(config_file='/kubeconfig/config')
deployment = api.list_namespaced_deployment('servarr')
len(deployment.items) #list all deployment inside the namespace
test.items[0].spec.template.spec.containers[0].name # name the first deploy within the namespace
test.items[2].spec.template.spec.containers[0].name # name the third deploy within the namespace
return 0
def nodes(request):
config.load_kube_config(config_file='/code/kubeconfig')
v1 = client.CoreV1Api()
@@ -1,7 +1,9 @@
from kubernetes import client, config
def scale_up(namespace_name,deploy_name,replicas=1):
config.load_kube_config(config_file='/kubeconfig/config')
api = client.AppsV1Api()
deployment = api.read_namespaced_deployment(name=deploy_name, namespace=namespace_name)
deployment.spec.replicas = replicas
@@ -16,4 +18,26 @@ def scale_down(namespace_name,deploy_name):
deployment.spec.replicas = 0
resp = api.patch_namespaced_deployment(
name=deploy_name, namespace=namespace_name, body=deployment);
return 0
return 0
def import_Deployments():
config.load_kube_config(config_file='/kubeconfig/config')
api = client.AppsV1Api()
deployment = api.list_deployment_for_all_namespaces()
for i in range(1,len(deployment.items)):
print(deployment.items[i].spec.template.spec.containers[0].name)
len(deployment.items) #list all deployment inside the namespace
test.items[0].spec.template.spec.containers[0].name # name the first deploy within the namespace
test.items[2].spec.template.spec.containers[0].name # name the third deploy within the namespace
return 0
def get_namespace():
config.load_kube_config(config_file='/kubeconfig/config')
core = client.CoreV1Api()
namespace = core.list_namespace()
namespace_list = []
for i in range(1,len(namespace.items)):
print(namespace.items[i].metadata.name)
namespace_list.append(namespace.items[i].metadata.name)
return namespace_list