diff --git a/src/main/java/org/etsi/osl/cridge/CRRouteBuilder.java b/src/main/java/org/etsi/osl/cridge/CRRouteBuilder.java index 78ec06a93467a1a3af0ca0f050b753d36b043056..83237090c17a55f30ffd5864ea6db9c126dbabbd 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 3f76dedb785d6049e2b7bf3fa2848552731081bd..4546d74a4d2dff2708f3f5c3c8c9d193bb9e4631 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 f47bfcea1d55d25ff1c587f554ae757804ff782e..1e1ea08a88eb30f3d6704479fb774adfaa44cc4b 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(); + + } + }