From 468cc4abc5e49ac116798bfc4fe10b498f9eb322 Mon Sep 17 00:00:00 2001 From: Christos Tranoris Date: Wed, 18 Dec 2024 09:27:08 +0200 Subject: [PATCH] fix for #19 --- .../org/etsi/osl/cridge/CRRouteBuilder.java | 8 +-- .../osl/cridge/KubernetesClientResource.java | 23 ++++++- .../org/etsi/osl/cridge/NamespaceWatcher.java | 61 +++++++++++++++---- 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/etsi/osl/cridge/CRRouteBuilder.java b/src/main/java/org/etsi/osl/cridge/CRRouteBuilder.java index 78ec06a..8323709 100644 --- a/src/main/java/org/etsi/osl/cridge/CRRouteBuilder.java +++ b/src/main/java/org/etsi/osl/cridge/CRRouteBuilder.java @@ -57,8 +57,8 @@ public class CRRouteBuilder extends RouteBuilder{ .bean( kubernetesClientResource, "patchCR(${headers}, ${body})") .convertBodyTo( String.class ); - // every 3600000 ms (60 minutes) - from( "timer://processUpdateResources?period=3600000" ) + // every 120000 ms (2 minutes) + from( "timer://processUpdateResources?period=120000" ) .log(LoggingLevel.INFO, log, " process nameSpacesTobeDeleted!") .to("log:DEBUG?showBody=true&showHeaders=true") .bean( CRRouteBuilder.class , "processNameSpacesTobeDeleted()"); @@ -72,8 +72,8 @@ public class CRRouteBuilder extends RouteBuilder{ Date currentTime = new Date(); //get current time long diffInMillies = Math.abs(currentTime .getTime() - datetime.getTime()); - long diff = TimeUnit.HOURS.convert(diffInMillies, TimeUnit.MILLISECONDS); - if (diff<=2) { //after 2 hours this namespace will be deleted + long diff = TimeUnit.MINUTES.convert(diffInMillies, TimeUnit.MILLISECONDS); + if (diff<=2) { //after 2 minutes this namespace will be deleted return; } diff --git a/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java b/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java index 3f76ded..4546d74 100644 --- a/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java +++ b/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java @@ -66,7 +66,7 @@ public class KubernetesClientResource { */ private ConcurrentHashMap nameSpacesTobeDeleted = new ConcurrentHashMap<>(); - private Map watchersForNamespaces = new HashMap<>(); + private Map watchersForNamespaces = new HashMap<>(); @@ -481,7 +481,13 @@ public class KubernetesClientResource { this.nameSpacesTobeDeleted.put(nameSpacename, new Date() ); + NamespaceWatcher watchersForNamespace = this.watchersForNamespaces.get(nameSpacename); + if ( watchersForNamespace!= null) { + watchersForNamespace.disableNamespace( nameSpacename ); + } + this.watchersForNamespaces.remove(nameSpacename); + }catch (Exception e) { e.printStackTrace(); @@ -519,8 +525,19 @@ public class KubernetesClientResource { } } })); - gkr.getMetadata().setNamespace( (String) headers.get("org.etsi.osl.namespace") ); //set default namespace the service Order ID if a namespace is not there - gkr.getMetadata().setName( (String) headers.get("org.etsi.osl.prefixName") ) ; + + //we must change and assign a name to the resource + if ( headers.get("org.etsi.osl.prefixId") !=null ) { + gkr.getMetadata().setName( (String) headers.get("org.etsi.osl.prefixName") ) ; + }else { + //it must start with an alpha character + gkr.getMetadata().setName( "cr" + ((String) headers.get("org.etsi.osl.resourceId")).substring(0, 8) ) ; + } + + + if ( gkr.getMetadata().getNamespace() == null || gkr.getMetadata().getNamespace().equals("") ) { + gkr.getMetadata().setNamespace( (String) headers.get("org.etsi.osl.serviceOrderId") ); //set default namespace the service Order ID if a namespace is not there + } logger.debug("{}", gkr.toString() ); diff --git a/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java b/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java index f47bfce..1e1ea08 100644 --- a/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java +++ b/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java @@ -11,6 +11,7 @@ import org.etsi.osl.domain.model.kubernetes.KubernetesSecret; import org.etsi.osl.domain.model.kubernetes.KubernetesService; import org.etsi.osl.tmf.rcm634.model.ResourceSpecification; import org.etsi.osl.tmf.ri639.model.ResourceCreate; +import org.etsi.osl.tmf.ri639.model.ResourceStatusType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; @@ -127,7 +128,7 @@ public class NamespaceWatcher { } })); - updateKubernetesSecretResourceInOSLCatalog( obj ); + updateKubernetesSecretResourceInOSLCatalog( obj, false ); } @@ -139,7 +140,7 @@ public class NamespaceWatcher { newObj.getMetadata().getName(), newObj.getMetadata().getUid(), newObj.getMetadata().getNamespace()); - updateKubernetesSecretResourceInOSLCatalog( newObj ); + updateKubernetesSecretResourceInOSLCatalog( newObj, false ); } @@ -150,7 +151,7 @@ public class NamespaceWatcher { obj.getMetadata().getName(), obj.getMetadata().getUid(), obj.getMetadata().getNamespace()); - updateKubernetesSecretResourceInOSLCatalog( obj ); + updateKubernetesSecretResourceInOSLCatalog( obj, true ); } @@ -166,8 +167,15 @@ public class NamespaceWatcher { - private void updateKubernetesSecretResourceInOSLCatalog(Secret resource) { + private void updateKubernetesSecretResourceInOSLCatalog(Secret resource, boolean toDelete) { +// if (this.informers.size() == 0) { +// logger.debug("Informers are 0. Will exit updateKubernetesSecretResourceInOSLCatalog"); +// return; +// } ResourceCreate rs = this.KubernetesSecret2OpensliceResource( resource ).toResourceCreate(); + if (toDelete) { + rs.setResourceStatus(ResourceStatusType.SUSPENDED); + } catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); } @@ -263,7 +271,7 @@ public class NamespaceWatcher { } })); - updateKubernetesServiceResourceInOSLCatalog( obj ); + updateKubernetesServiceResourceInOSLCatalog( obj, false ); } @@ -275,7 +283,7 @@ public class NamespaceWatcher { newObj.getMetadata().getName(), newObj.getMetadata().getUid(), newObj.getMetadata().getNamespace()); - updateKubernetesServiceResourceInOSLCatalog( newObj ); + updateKubernetesServiceResourceInOSLCatalog( newObj, false ); } @@ -286,7 +294,7 @@ public class NamespaceWatcher { obj.getMetadata().getName(), obj.getMetadata().getUid(), obj.getMetadata().getNamespace()); - updateKubernetesServiceResourceInOSLCatalog( obj ); + updateKubernetesServiceResourceInOSLCatalog( obj, true ); } @@ -301,8 +309,15 @@ public class NamespaceWatcher { - private void updateKubernetesServiceResourceInOSLCatalog(Service resource) { + private void updateKubernetesServiceResourceInOSLCatalog(Service resource, boolean toDelete) { +// if (this.informers.size() == 0) { +// logger.debug("Informers are 0. Will exit updateKubernetesServiceResourceInOSLCatalog"); +// return; +// } ResourceCreate rs = this.KubernetesService2OpensliceResource( resource ).toResourceCreate(); + if (toDelete) { + rs.setResourceStatus(ResourceStatusType.SUSPENDED); + } catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); } @@ -422,7 +437,7 @@ public class NamespaceWatcher { } })); - updateKubernetesConfigMapResourceInOSLCatalog( obj ); + updateKubernetesConfigMapResourceInOSLCatalog( obj, false ); } @@ -434,7 +449,7 @@ public class NamespaceWatcher { newObj.getMetadata().getName(), newObj.getMetadata().getUid(), newObj.getMetadata().getNamespace()); - updateKubernetesConfigMapResourceInOSLCatalog( newObj ); + updateKubernetesConfigMapResourceInOSLCatalog( newObj, false ); } @@ -445,7 +460,7 @@ public class NamespaceWatcher { obj.getMetadata().getName(), obj.getMetadata().getUid(), obj.getMetadata().getNamespace()); - updateKubernetesConfigMapResourceInOSLCatalog( obj ); + updateKubernetesConfigMapResourceInOSLCatalog( obj, true ); } @@ -458,8 +473,15 @@ public class NamespaceWatcher { } - private void updateKubernetesConfigMapResourceInOSLCatalog(ConfigMap resource) { + private void updateKubernetesConfigMapResourceInOSLCatalog(ConfigMap resource, boolean toDelete) { +// if (this.informers.size() == 0) { +// logger.debug("Informers are 0. Will exit updateKubernetesConfigMapResourceInOSLCatalog"); +// return; +// } ResourceCreate rs = this.KubernetesConfigMap2OpensliceResource( resource ).toResourceCreate(); + if (toDelete) { + rs.setResourceStatus(ResourceStatusType.SUSPENDED); + } catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); } @@ -513,4 +535,19 @@ public class NamespaceWatcher { return kcrv; } + + public void disableNamespace(String ns) { + this.kubernetesClient.configMaps().inNamespace(ns).delete(); + this.kubernetesClient.secrets().inNamespace(ns).delete(); + this.kubernetesClient.services().inNamespace(ns).delete(); + +// for (SharedIndexInformer sharedIndexInformer : informers) { +// sharedIndexInformer.stop(); +// sharedIndexInformer.close(); +// +// } +// this.informers.clear(); + + } + } -- GitLab