Add system namespace filtering

- Add configuration to exclude system namespaces by default
- Add UI checkbox to include system namespaces when needed
- Update API endpoints to accept include_system_namespaces parameter
- Update Kubernetes client to apply namespace filtering
- Update ConfigMap and deployment with new environment variables
- Fix Dockerfile to install dependencies globally
- Test functionality with both filtered and unfiltered results
This commit is contained in:
2025-09-25 17:39:33 -03:00
parent 3a6875a80e
commit 071ffefef7
7 changed files with 90 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ Configurações da aplicação
import os
from typing import List, Optional
from pydantic_settings import BaseSettings
from pydantic import Field
class Settings(BaseSettings):
"""Configurações da aplicação"""
@@ -31,6 +32,24 @@ class Settings(BaseSettings):
"openshift-sdn"
]
# Configurações de filtro de namespaces
include_system_namespaces: bool = Field(default=False, alias="INCLUDE_SYSTEM_NAMESPACES")
system_namespace_prefixes: List[str] = Field(
default=[
"kube-",
"openshift-",
"default",
"kube-system",
"kube-public",
"kube-node-lease"
],
alias="SYSTEM_NAMESPACE_PREFIXES"
)
class Config:
env_file = ".env"
case_sensitive = False
# Configurações de relatório
report_export_path: str = "/tmp/reports"

View File

@@ -44,7 +44,20 @@ class K8sClient:
logger.error(f"Erro ao inicializar cliente Kubernetes: {e}")
raise
async def get_all_pods(self) -> List[PodResource]:
def _is_system_namespace(self, namespace: str, include_system: bool = None) -> bool:
"""Verificar se um namespace é do sistema"""
# Usar parâmetro se fornecido, senão usar configuração global
should_include = include_system if include_system is not None else settings.include_system_namespaces
if should_include:
return False
for prefix in settings.system_namespace_prefixes:
if namespace.startswith(prefix):
return True
return False
async def get_all_pods(self, include_system_namespaces: bool = None) -> List[PodResource]:
"""Coletar informações de todos os pods do cluster"""
if not self.initialized:
raise RuntimeError("Cliente Kubernetes não inicializado")
@@ -56,6 +69,9 @@ class K8sClient:
pods = self.v1.list_pod_for_all_namespaces(watch=False)
for pod in pods.items:
# Filtrar namespaces do sistema
if self._is_system_namespace(pod.metadata.namespace, include_system_namespaces):
continue
pod_resource = PodResource(
name=pod.metadata.name,
namespace=pod.metadata.namespace,
@@ -102,6 +118,18 @@ class K8sClient:
if not self.initialized:
raise RuntimeError("Cliente Kubernetes não inicializado")
# Verificar se é namespace do sistema
if self._is_system_namespace(namespace):
logger.info(f"Namespace {namespace} é do sistema, retornando vazio")
return NamespaceResources(
name=namespace,
pods=[],
total_cpu_requests="0",
total_cpu_limits="0",
total_memory_requests="0",
total_memory_limits="0"
)
try:
# Listar pods do namespace
pods = self.v1.list_namespaced_pod(namespace=namespace)