diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bb7d754baa751630b25598e182a30f062e9b6ebd..40be386b981d8564ef04539edebb44dc6a389d39 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,7 +21,7 @@ include: - ci-templates/default.yml - ci-templates/build.yml rules: - - if: '$CI_COMMIT_REF_PROTECTED && $CI_COMMIT_REF_NAME != "main" && $CI_COMMIT_REF_NAME != "develop"' + - if: '$CI_COMMIT_REF_PROTECTED == "true" && $CI_COMMIT_REF_NAME != "main" && $CI_COMMIT_REF_NAME != "develop"' - project: osl/code/org.etsi.osl.main ref: develop @@ -29,7 +29,7 @@ include: - ci-templates/default.yml - ci-templates/build_unprotected.yml rules: - - if: '$CI_COMMIT_REF_NAME != "main" && $CI_COMMIT_REF_NAME != "develop" && !$CI_COMMIT_REF_PROTECTED' + - if: '$CI_COMMIT_REF_NAME != "main" && $CI_COMMIT_REF_NAME != "develop" && $CI_COMMIT_REF_PROTECTED == "false"' maven_build: extends: .maven_build diff --git a/Dockerfile b/Dockerfile index f44060eb99868f55b3768468447451422e6a00dc..697956ea665fdc66ad57e090aa484e0431089b02 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,5 +2,5 @@ FROM ibm-semeru-runtimes:open-17.0.7_7-jdk MAINTAINER openslice.io RUN mkdir /opt/shareclasses RUN mkdir -p /opt/openslice/lib/ -COPY target/org.etsi.osl.cridge-1.0.0-exec.jar /opt/openslice/lib/ -CMD ["java", "-Xshareclasses:cacheDir=/opt/shareclasses", "-jar", "/opt/openslice/lib/org.etsi.osl.cridge-1.0.0-exec.jar"] +COPY target/org.etsi.osl.cridge-1.1.0-SNAPSHOT-exec.jar /opt/openslice/lib/ +CMD ["java", "-Xshareclasses:cacheDir=/opt/shareclasses", "-jar", "/opt/openslice/lib/org.etsi.osl.cridge-1.1.0-SNAPSHOT-exec.jar"] diff --git a/pom.xml b/pom.xml index 76e6d7e936603df3a077bf7b37d4e722307488c7..b6db3f9c1e05edc5bfdea9d8b9c7333a3255a518 100644 --- a/pom.xml +++ b/pom.xml @@ -5,14 +5,20 @@ org.etsi.osl org.etsi.osl.main - 1.0.0 + 2024Q4-SNAPSHOT ../org.etsi.osl.main org.etsi.osl.cridge org.etsi.osl.cridge - https://osl.etsi.org + ${org.etsi.osl.cridge.version} + + + OpenSlice by ETSI + https://osl.etsi.org + + @@ -97,6 +103,12 @@ spring-boot-starter-actuator + + org.bouncycastle + bcpkix-jdk15on + 1.70 + + diff --git a/src/main/java/org/etsi/osl/cridge/CatalogClient.java b/src/main/java/org/etsi/osl/cridge/CatalogClient.java index 0527c112490fd42faf3432af76c9d13e867c2124..9500084021bc1ca52378fe60aef6a77c60f8ce22 100644 --- a/src/main/java/org/etsi/osl/cridge/CatalogClient.java +++ b/src/main/java/org/etsi/osl/cridge/CatalogClient.java @@ -36,8 +36,8 @@ public class CatalogClient extends RouteBuilder{ @Value("${CATALOG_GET_RESOURCESPEC_BY_ID}") private String CATALOG_GET_RESOURCESPEC_BY_ID = ""; - @Value("${CATALOG_GET_RESOURCESPEC_BY_ΝAME_CATEGORY}") - private String CATALOG_GET_RESOURCESPEC_BY_ΝAME_CATEGORY = ""; + @Value("${CATALOG_GET_RESOURCESPEC_BY_NAME_CATEGORY}") + private String CATALOG_GET_RESOURCESPEC_BY_NAME_CATEGORY = ""; @Value("${CATALOG_UPDADD_RESOURCESPEC}") @@ -87,7 +87,7 @@ public class CatalogClient extends RouteBuilder{ map.put( "acategory", aCategory); map.put( "aversion", aVersion); Object response = - template.requestBodyAndHeaders( CATALOG_GET_RESOURCESPEC_BY_ΝAME_CATEGORY, null, map); + template.requestBodyAndHeaders( CATALOG_GET_RESOURCESPEC_BY_NAME_CATEGORY, null, map); if ( !(response instanceof String)) { logger.error("Resource Specification object is wrong."); diff --git a/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java b/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java index 088cd982fdfd3af871c23c519db39fcd9824ace2..3f76dedb785d6049e2b7bf3fa2848552731081bd 100644 --- a/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java +++ b/src/main/java/org/etsi/osl/cridge/KubernetesClientResource.java @@ -51,13 +51,13 @@ public class KubernetesClientResource { @Autowired CatalogClient catalogClient; + private KubernetesContextDefinition kubernetesContextDefinition = null; private ResourceSpecification kubernetesCRDV1ResourceSpec = null; private ResourceSpecification kubernetesCRV1ResourceSpec = null; - private ResourceSpecification kubernetesSecretResourceSpec = null; /** @@ -108,10 +108,7 @@ public class KubernetesClientResource { KubernetesCRV1.OSL_KUBCRV1_RSPEC_CATEGORY, KubernetesCRV1.OSL_KUBCRV1_RSPEC_VERSION); - kubernetesSecretResourceSpec = catalogClient.retrieveResourceSpecByNameCategoryVersion( - KubernetesSecret.OSL_KUBSECRET_RSPEC_NAME, - KubernetesSecret.OSL_KUBSECRET_RSPEC_CATEGORY, - KubernetesSecret.OSL_KUBSECRET_RSPEC_VERSION); + } return kubernetesContextDefinition; @@ -341,62 +338,7 @@ public class KubernetesClientResource { } - KubernetesSecret KubernetesSecret2OpensliceResource(Secret secret) { - - String baseCRD = String.format( "%s@%s@%s@%s", - secret.getKind(), - secret.getApiVersion(), - kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster(), - kubernetesClient.getMasterUrl().toExternalForm()); - - KubernetesSecret kcrv = KubernetesSecret.builder() - .osl_KUBCRD_RSPEC_UUID( kubernetesSecretResourceSpec.getUuid() ) - .name( secret.getMetadata().getName() - + "@" - + secret.getMetadata().getNamespace() - + "@" - + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() - + "@" + - kubernetesClient.getMasterUrl().toExternalForm() ) - .version( secret.getApiVersion() ) - .currentContextCluster( kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() ) - .clusterMasterURL( kubernetesClient.getMasterUrl().toString() ) - .fullResourceName( "" ) - .namespace( Serialization.asJson( secret.getMetadata().getNamespace()) ) - .kind( secret.getKind() ) - .apiGroup( secret.getPlural() ) - .uID( secret.getMetadata().getUid() ) - .metadata(Serialization.asJson( secret.getMetadata()) ) //.metadata( gkr.getMetadata().toString() ) - .description( - String.format( "A secret in namespace %s on %s", secret.getMetadata().getNamespace(), baseCRD )) - .build(); - - - secret.getMetadata().getLabels().forEach((pk, pv) -> { - logger.debug("\t label: {} {} ", pk, pv); - kcrv.getProperties().put( pk , pv); - - }); - - - if (secret.getData() != null) - secret.getData().forEach((kPropName, vProVal) -> { - logger.debug("propName={} propValue={} ", kPropName, vProVal ); - kcrv.getData().put(kPropName, vProVal); - }); - if (secret.getStringData() != null) - secret.getStringData().forEach((kPropName, vProVal) -> { - logger.debug("propName={} propValue={} ", kPropName, vProVal ); - kcrv.getProperties().put(kPropName, vProVal); - }); - - kcrv.setDataObj( Serialization.asYaml( secret.getData() ) ); - kcrv.setYaml( Serialization.asYaml( secret ) ); - kcrv.setJson( Serialization.asJson( secret ) ); - - return kcrv; - } @@ -475,7 +417,9 @@ public class KubernetesClientResource { */ if ( this.watchersForNamespaces.get(nameSpacename) == null ) { - SharedIndexInformer result = createWatchersFornamespace( nameSpacename, headers ); + + //SharedIndexInformer result = createWatchersFornamespace( nameSpacename, headers ); + NamespaceWatcher result = NamespaceWatcher.getNew( kubernetesClient, catalogClient, nameSpacename, headers); this.watchersForNamespaces.put(nameSpacename, result); } @@ -498,87 +442,6 @@ public class KubernetesClientResource { } - /** - * if the equivalent namespace for the service order is successfully created the create - * related wathcers for secrets, services, configmaps, pods, etc - * 1) we need to create domainmodels for these - * 2) we add them as support resources to our initial resource - * @param nameSpacename - * @param headers - * @return - */ - private SharedIndexInformer createWatchersFornamespace(String nameSpacename, Map headers) { - //watcher for secrets - - SharedIndexInformer shixInformer = - this.getKubernetesClient() - .secrets().inNamespace(nameSpacename).inform(new ResourceEventHandler() { - - @Override - public void onAdd(Secret obj) { - logger.debug("Added Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", - obj.getKind(), - obj.getMetadata().getName(), - obj.getMetadata().getUid(), - obj.getMetadata().getNamespace()); - - headers.forEach(((hname, hval) ->{ - if (hval instanceof String s) { - if ( hname.contains("org.etsi.osl")) { - logger.debug("Header: {} = {} ", hname, s ); - if ( obj.getMetadata() == null ) { - obj.setMetadata( new ObjectMeta()); - obj.getMetadata().setLabels( new HashMap()); - } - obj.getMetadata().getLabels().put(hname, s); - } - } - })); - - updateKubernetesSecretResourceInOSLCatalog( obj ); - - - } - - @Override - public void onUpdate(Secret oldObj, Secret newObj) { - logger.debug("onUpdate Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", - newObj.getKind(), - newObj.getMetadata().getName(), - newObj.getMetadata().getUid(), - newObj.getMetadata().getNamespace()); - updateKubernetesSecretResourceInOSLCatalog( newObj ); - - } - - @Override - public void onDelete(Secret obj, boolean deletedFinalStateUnknown) { - logger.debug("onDelete Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", - obj.getKind(), - obj.getMetadata().getName(), - obj.getMetadata().getUid(), - obj.getMetadata().getNamespace()); - updateKubernetesSecretResourceInOSLCatalog( obj ); - - } - - - }, 30 * 1000L); // resync period (set 0 for no resync); - - shixInformer.start(); - - return shixInformer; - - } - - private void updateKubernetesSecretResourceInOSLCatalog(Secret resource) { - ResourceCreate rs = this - .KubernetesSecret2OpensliceResource( resource ) - .toResourceCreate(); - - catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); - - } diff --git a/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java b/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java new file mode 100644 index 0000000000000000000000000000000000000000..f47bfcea1d55d25ff1c587f554ae757804ff782e --- /dev/null +++ b/src/main/java/org/etsi/osl/cridge/NamespaceWatcher.java @@ -0,0 +1,516 @@ +package org.etsi.osl.cridge; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.etsi.osl.domain.model.kubernetes.KubernetesConfigMap; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.Secret; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.informers.ResourceEventHandler; +import io.fabric8.kubernetes.client.informers.SharedIndexInformer; +import io.fabric8.kubernetes.client.utils.Serialization; + +public class NamespaceWatcher { + + + private static final Logger logger = LoggerFactory.getLogger( "org.etsi.osl.cridge" ); + + private KubernetesClient kubernetesClient; + + private CatalogClient catalogClient; + + private String nameSpacename; + + private Map headers; + + private ResourceSpecification kubernetesSecretResourceSpec = null; + + private ResourceSpecification kubernetesServiceResourceSpec = null; + + private ResourceSpecification kubernetesConfigMapResourceSpec = null; + + List< SharedIndexInformer> informers = new ArrayList<>(); + + + public NamespaceWatcher(KubernetesClient kubernetesClient, CatalogClient catalogClient, String anameSpacename, Map aheaders) { + super(); + this.kubernetesClient = kubernetesClient; + this.catalogClient = catalogClient; + this.nameSpacename = anameSpacename; + this.headers = aheaders; + + kubernetesSecretResourceSpec = catalogClient.retrieveResourceSpecByNameCategoryVersion( + KubernetesSecret.OSL_KUBSECRET_RSPEC_NAME, + KubernetesSecret.OSL_KUBSECRET_RSPEC_CATEGORY, + KubernetesSecret.OSL_KUBSECRET_RSPEC_VERSION); + + kubernetesServiceResourceSpec = catalogClient.retrieveResourceSpecByNameCategoryVersion( + KubernetesService.OSL_KUBSERVICE_RSPEC_NAME, + KubernetesService.OSL_KUBSERVICE_RSPEC_CATEGORY, + KubernetesService.OSL_KUBSERVICE_RSPEC_VERSION); + + + kubernetesConfigMapResourceSpec = catalogClient.retrieveResourceSpecByNameCategoryVersion( + KubernetesConfigMap.OSL_KUBCMAP_RSPEC_NAME, + KubernetesConfigMap.OSL_KUBCMAP_RSPEC_CATEGORY, + KubernetesConfigMap.OSL_KUBCMAP_RSPEC_VERSION); + } + + public static NamespaceWatcher getNew(KubernetesClient kubernetesClient2, CatalogClient catalogClient2, String anameSpacename, Map aheaders) { + + NamespaceWatcher aNamespaceWatchers = new NamespaceWatcher(kubernetesClient2, catalogClient2, anameSpacename, aheaders); + + SharedIndexInformer rSecret = aNamespaceWatchers.createSecretWatchersFornamespace(anameSpacename, aheaders); + aNamespaceWatchers.informers.add( rSecret ); + + SharedIndexInformer rService = aNamespaceWatchers.createServiceWatcherssForNamespace(anameSpacename, aheaders); + aNamespaceWatchers.informers.add( rService ); + + SharedIndexInformer rCMap = aNamespaceWatchers.createConfigMapWatcherssForNamespace(anameSpacename, aheaders); + aNamespaceWatchers.informers.add( rService ); + + + return aNamespaceWatchers; + + } + + + + + + /** + * if the equivalent namespace for the service order is successfully created the create + * related wathcers for secrets, services, configmaps, pods, etc + * 1) we need to create domainmodels for these + * 2) we add them as support resources to our initial resource + * @param nameSpacename + * @param headers + * @return + */ + + + private SharedIndexInformer createSecretWatchersFornamespace(String nameSpacename, Map headers) { + //watcher for secrets + SharedIndexInformer shixInformer = this.kubernetesClient.secrets().inNamespace(nameSpacename).inform(new ResourceEventHandler() { + + @Override + public void onAdd(Secret obj) { + logger.debug("Added Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + obj.getKind(), + obj.getMetadata().getName(), + obj.getMetadata().getUid(), + obj.getMetadata().getNamespace()); + + headers.forEach(((hname, hval) ->{ + if (hval instanceof String s) { + if ( hname.contains("org.etsi.osl")) { + logger.debug("Header: {} = {} ", hname, s ); + if ( obj.getMetadata() == null ) { + obj.setMetadata( new ObjectMeta()); + obj.getMetadata().setLabels( new HashMap()); + } + obj.getMetadata().getLabels().put(hname, s); + } + } + })); + + updateKubernetesSecretResourceInOSLCatalog( obj ); + + + } + + @Override + public void onUpdate(Secret oldObj, Secret newObj) { + logger.debug("onUpdate Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + newObj.getKind(), + newObj.getMetadata().getName(), + newObj.getMetadata().getUid(), + newObj.getMetadata().getNamespace()); + updateKubernetesSecretResourceInOSLCatalog( newObj ); + + } + + @Override + public void onDelete(Secret obj, boolean deletedFinalStateUnknown) { + logger.debug("onDelete Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + obj.getKind(), + obj.getMetadata().getName(), + obj.getMetadata().getUid(), + obj.getMetadata().getNamespace()); + updateKubernetesSecretResourceInOSLCatalog( obj ); + + } + + + }, 30 * 1000L); // resync period (set 0 for no resync); + + shixInformer.start(); + + return shixInformer; + + } + + + + + private void updateKubernetesSecretResourceInOSLCatalog(Secret resource) { + ResourceCreate rs = this.KubernetesSecret2OpensliceResource( resource ).toResourceCreate(); + catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); + + } + + + public KubernetesSecret KubernetesSecret2OpensliceResource(Secret secret) { + + + String baseCRD = String.format( "%s@%s@%s@%s", + secret.getKind(), + secret.getApiVersion(), + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster(), + kubernetesClient.getMasterUrl().toExternalForm()); + + KubernetesSecret kcrv = KubernetesSecret.builder().osl_KUBCRD_RSPEC_UUID( kubernetesSecretResourceSpec.getUuid() ) + .name( secret.getMetadata().getName() + + "@" + + secret.getMetadata().getNamespace() + + "@" + + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() + + "@" + + kubernetesClient.getMasterUrl().toExternalForm() ) + .version( secret.getApiVersion() ) + .currentContextCluster( kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() ) + .clusterMasterURL( kubernetesClient.getMasterUrl().toString() ) + .fullResourceName( "" ) + .namespace( Serialization.asJson( secret.getMetadata().getNamespace()) ) + .kind( secret.getKind() ) + .apiGroup( secret.getPlural() ) + .uID( secret.getMetadata().getUid() ) + .metadata(Serialization.asJson( secret.getMetadata()) ) //.metadata( gkr.getMetadata().toString() ) + .description( + String.format( "A secret in namespace %s on %s", secret.getMetadata().getNamespace(), baseCRD )) + .build(); + + + secret.getMetadata().getLabels().forEach((pk, pv) -> { + logger.debug("\t label: {} {} ", pk, pv); + kcrv.getProperties().put( pk , pv); + + }); + + + if (secret.getData() != null) + secret.getData().forEach((kPropName, vProVal) -> { + logger.debug("propName={} propValue={} ", kPropName, vProVal ); + kcrv.getData().put(kPropName, vProVal); + }); + if (secret.getStringData() != null) + secret.getStringData().forEach((kPropName, vProVal) -> { + logger.debug("propName={} propValue={} ", kPropName, vProVal ); + kcrv.getProperties().put(kPropName, vProVal); + }); + + kcrv.setDataObj( Serialization.asYaml( secret.getData() ) ); + //kcrv.setYaml( Serialization.asYaml( secret ) ); + kcrv.setJson( Serialization.asJson( secret ) ); + + return kcrv; + } + + + + + + /******************* + * + * Services Watcher + * + *******************/ + private SharedIndexInformer createServiceWatcherssForNamespace(String nameSpacename, Map headers) { + //watcher for secrets + SharedIndexInformer shixInformer = this.kubernetesClient.services().inNamespace(nameSpacename).inform(new ResourceEventHandler() { + + @Override + public void onAdd(Service obj) { + logger.debug("Added Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + obj.getKind(), + obj.getMetadata().getName(), + obj.getMetadata().getUid(), + obj.getMetadata().getNamespace()); + + headers.forEach(((hname, hval) ->{ + if (hval instanceof String s) { + if ( hname.contains("org.etsi.osl")) { + logger.debug("Header: {} = {} ", hname, s ); + if ( obj.getMetadata() == null ) { + obj.setMetadata( new ObjectMeta()); + obj.getMetadata().setLabels( new HashMap()); + } + obj.getMetadata().getLabels().put(hname, s); + } + } + })); + + updateKubernetesServiceResourceInOSLCatalog( obj ); + + + } + + @Override + public void onUpdate(Service oldObj, Service newObj) { + logger.debug("onUpdate Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + newObj.getKind(), + newObj.getMetadata().getName(), + newObj.getMetadata().getUid(), + newObj.getMetadata().getNamespace()); + updateKubernetesServiceResourceInOSLCatalog( newObj ); + + } + + @Override + public void onDelete(Service obj, boolean deletedFinalStateUnknown) { + logger.debug("onDelete Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + obj.getKind(), + obj.getMetadata().getName(), + obj.getMetadata().getUid(), + obj.getMetadata().getNamespace()); + updateKubernetesServiceResourceInOSLCatalog( obj ); + + } + + + }, 30 * 1000L); // resync period (set 0 for no resync); + + shixInformer.start(); + + return shixInformer; + + } + + + + private void updateKubernetesServiceResourceInOSLCatalog(Service resource) { + ResourceCreate rs = this.KubernetesService2OpensliceResource( resource ).toResourceCreate(); + catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); + + } + + + KubernetesService KubernetesService2OpensliceResource(Service serv) { + + String baseCRD = String.format( "%s@%s@%s@%s", + serv.getKind(), + serv.getApiVersion(), + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster(), + kubernetesClient.getMasterUrl().toExternalForm()); + + KubernetesService kcrv = KubernetesService.builder().osl_KUBCRD_RSPEC_UUID( kubernetesServiceResourceSpec.getUuid() ) + .name( serv.getMetadata().getName() + + "@" + + serv.getMetadata().getNamespace() + + "@" + + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() + + "@" + + kubernetesClient.getMasterUrl().toExternalForm() ) + .version( serv.getApiVersion() ) + .currentContextCluster( kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() ) + .clusterMasterURL( kubernetesClient.getMasterUrl().toString() ) + .fullResourceName( "" ) + .namespace( Serialization.asJson( serv.getMetadata().getNamespace()) ) + .kind( serv.getKind() ) + .apiGroup( serv.getPlural() ) + .uID( serv.getMetadata().getUid() ) + .metadata(Serialization.asJson( serv.getMetadata()) ) //.metadata( gkr.getMetadata().toString() ) + .description( + String.format( "A service in namespace %s on %s", serv.getMetadata().getNamespace(), baseCRD )) + .build(); + + + serv.getMetadata().getLabels().forEach((pk, pv) -> { + logger.debug("\t label: {} {} ", pk, pv); + kcrv.getProperties().put( pk , pv); + + }); + + + kcrv.setSpecObj( Serialization.asYaml( serv.getSpec() ) ); + kcrv.setStatusObj( Serialization.asYaml( serv.getStatus() ) ); + //kcrv.setYaml( Serialization.asYaml( serv ) ); + kcrv.setJson( Serialization.asJson( serv ) ); + + // Convert YAML to Map + Map resultMap = yamlToMap( kcrv.getSpecObj() ); + kcrv.setSpec(resultMap); + + resultMap = yamlToMap( kcrv.getStatusObj() ); + kcrv.setStatus(resultMap); + + return kcrv; + } + + + public static Map yamlToMap(String yamlString) { + Yaml yaml = new Yaml(); + Map yamlMap = yaml.load(new StringReader(yamlString)); + + // Create a result map that will contain flattened key-value pairs + Map resultMap = new LinkedHashMap<>(); + + // Call the recursive method to flatten the map + flattenMap("", yamlMap, resultMap); + + return resultMap; + } + + // Recursive method to flatten the YAML structure + private static void flattenMap(String prefix, Map sourceMap, Map resultMap) { + for (Map.Entry entry : sourceMap.entrySet()) { + String key = prefix.isEmpty() ? entry.getKey() : prefix + "." + entry.getKey(); + Object value = entry.getValue(); + + if (value instanceof Map) { + // Recursively flatten nested maps + flattenMap(key, (Map) value, resultMap); + } else { + // Convert value to String and put it in the resultMap + resultMap.put(key, value.toString()); + } + } + } + + + + /******************* + * + * ConfigMap Watcher + * + *******************/ + private SharedIndexInformer createConfigMapWatcherssForNamespace(String nameSpacename, Map headers) { + //watcher for secrets + SharedIndexInformer shixInformer = this.kubernetesClient.configMaps().inNamespace(nameSpacename).inform(new ResourceEventHandler() { + + @Override + public void onAdd(ConfigMap obj) { + logger.debug("Added Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + obj.getKind(), + obj.getMetadata().getName(), + obj.getMetadata().getUid(), + obj.getMetadata().getNamespace()); + + headers.forEach(((hname, hval) ->{ + if (hval instanceof String s) { + if ( hname.contains("org.etsi.osl")) { + logger.debug("Header: {} = {} ", hname, s ); + if ( obj.getMetadata() == null ) { + obj.setMetadata( new ObjectMeta()); + obj.getMetadata().setLabels( new HashMap()); + } + obj.getMetadata().getLabels().put(hname, s); + } + } + })); + + updateKubernetesConfigMapResourceInOSLCatalog( obj ); + + + } + + @Override + public void onUpdate(ConfigMap oldObj, ConfigMap newObj) { + logger.debug("onUpdate Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + newObj.getKind(), + newObj.getMetadata().getName(), + newObj.getMetadata().getUid(), + newObj.getMetadata().getNamespace()); + updateKubernetesConfigMapResourceInOSLCatalog( newObj ); + + } + + @Override + public void onDelete(ConfigMap obj, boolean deletedFinalStateUnknown) { + logger.debug("onDelete Namespace watcher Resource Kind:{} Name:{} UID:{} Namespace:{}", + obj.getKind(), + obj.getMetadata().getName(), + obj.getMetadata().getUid(), + obj.getMetadata().getNamespace()); + updateKubernetesConfigMapResourceInOSLCatalog( obj ); + + } + + + }, 30 * 1000L); // resync period (set 0 for no resync); + + shixInformer.start(); + + return shixInformer; + + } + + private void updateKubernetesConfigMapResourceInOSLCatalog(ConfigMap resource) { + ResourceCreate rs = this.KubernetesConfigMap2OpensliceResource( resource ).toResourceCreate(); + catalogClient.createOrUpdateResourceByNameCategoryVersion( rs ); + + } + + KubernetesConfigMap KubernetesConfigMap2OpensliceResource(ConfigMap cmap) { + + + String baseCRD = String.format( "%s@%s@%s@%s", + cmap.getKind(), + cmap.getApiVersion(), + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster(), + kubernetesClient.getMasterUrl().toExternalForm()); + + KubernetesConfigMap kcrv = KubernetesConfigMap.builder().osl_KUBCRD_RSPEC_UUID( kubernetesConfigMapResourceSpec.getUuid() ) + .name( cmap.getMetadata().getName() + + "@" + + cmap.getMetadata().getNamespace() + + "@" + + kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() + + "@" + + kubernetesClient.getMasterUrl().toExternalForm() ) + .version( cmap.getApiVersion() ) + .currentContextCluster( kubernetesClient.getConfiguration().getCurrentContext().getContext().getCluster() ) + .clusterMasterURL( kubernetesClient.getMasterUrl().toString() ) + .fullResourceName( "" ) + .namespace( Serialization.asJson( cmap.getMetadata().getNamespace()) ) + .kind( cmap.getKind() ) + .apiGroup( cmap.getPlural() ) + .uID( cmap.getMetadata().getUid() ) + .metadata(Serialization.asJson( cmap.getMetadata()) ) //.metadata( gkr.getMetadata().toString() ) + .description( + String.format( "A configMap in namespace %s on %s", cmap.getMetadata().getNamespace(), baseCRD )) + .build(); + + + cmap.getMetadata().getLabels().forEach((pk, pv) -> { + logger.debug("\t label: {} {} ", pk, pv); + kcrv.getProperties().put( pk , pv); + + }); + + + kcrv.setDataObj( Serialization.asYaml( cmap.getData() ) ); + //kcrv.setYaml( Serialization.asYaml( cmap ) ); + kcrv.setJson( Serialization.asJson( cmap ) ); + + // Convert YAML to Map + Map resultMap = yamlToMap( kcrv.getDataObj() ); + kcrv.setData(resultMap); + + return kcrv; + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3dcfe7f5d3f21f9643a3f64f6c353611194ceb19..ff80f2bb9193f539e9ae1d97f7a0053c7ac0417d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,6 +12,7 @@ spring: max-connections: 100 packages: trust-all: true + autoconfigure.exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration server: port: 0 @@ -38,7 +39,7 @@ CATALOG_ADD_RESOURCESPEC: "jms:queue:CATALOG.ADD.RESOURCESPEC" CATALOG_UPD_RESOURCESPEC: "jms:queue:CATALOG.UPD.RESOURCESPEC" CATALOG_UPDADD_RESOURCESPEC: "jms:queue:CATALOG.UPDADD.RESOURCESPEC" CATALOG_GET_RESOURCESPEC_BY_ID: "jms:queue:CATALOG.GET.RESOURCESPEC_BY_ID" -CATALOG_GET_RESOURCESPEC_BY_ΝAME_CATEGORY: "jms:queue:CATALOG.GET.RESOURCESPEC_BY_ΝAME_CATEGORY" +CATALOG_GET_RESOURCESPEC_BY_NAME_CATEGORY: "jms:queue:CATALOG.GET.RESOURCESPEC_BY_NAME_CATEGORY" #CRD ACTIONS diff --git a/src/test/java/org/etsi/osl/cridge/CridgeIntegrationTest.java b/src/test/java/org/etsi/osl/cridge/CridgeIntegrationTest.java index 3ebb8aec06da6a8d761b1e158cbe040d0d2b8524..cffb9ae6a7fa630a84015d7710b05658ac1a44a6 100644 --- a/src/test/java/org/etsi/osl/cridge/CridgeIntegrationTest.java +++ b/src/test/java/org/etsi/osl/cridge/CridgeIntegrationTest.java @@ -41,11 +41,13 @@ import io.fabric8.kubernetes.api.model.NamedContextBuilder; import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.api.model.SecretBuilder; +import io.fabric8.kubernetes.api.model.ServiceList; import io.fabric8.kubernetes.api.model.WatchEvent; import io.fabric8.kubernetes.api.model.WatchEventBuilder; import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition; import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionList; import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionListBuilder; +import io.fabric8.kubernetes.api.model.apiextensions.v1.ConversionRequestFluent.V1beta1ServiceReferenceObjectsNested; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClientBuilder; @@ -66,7 +68,7 @@ import io.fabric8.kubernetes.client.utils.Serialization; "kubernetes.master = test_kube", "spring.activemq.brokerUrl = vm://embedded?broker.persistent=false,useShutdownHook=false", "CATALOG_GET_RESOURCESPEC_BY_ID = direct:get_rspec_byid", - "CATALOG_GET_RESOURCESPEC_BY_ΝAME_CATEGORY = direct:get_rspec_byNameCategory", + "CATALOG_GET_RESOURCESPEC_BY_NAME_CATEGORY = direct:get_rspec_byNameCategory", "CATALOG_UPDADD_RESOURCE = direct:updadd_resource",}) @@ -226,12 +228,61 @@ public class CridgeIntegrationTest { // server api expectations server.expect().get() .withPath("/api/v1/namespaces/orderid-xxx-xxx-xxx/secrets?resourceVersion=0") - .andReturn(HttpURLConnection.HTTP_OK, crdMyCalc).once(); - + .andReturn(HttpURLConnection.HTTP_OK, crdMyCalc).always(); + server.expect().get().withPath( "/api/v1/namespaces/orderid-xxx-xxx-xxx/secrets?allowWatchBookmarks=true&timeoutSeconds=600&watch=true") - .andReturn(HttpURLConnection.HTTP_OK, crdMyCalc).once(); + .andReturn(HttpURLConnection.HTTP_OK, crdMyCalc).always(); + server.expect().get().withPath( + "/api/v1/namespaces/orderid-xxx-xxx-xxx/services?allowWatchBookmarks=true&resourceVersion=12345&timeoutSeconds=600&watch=true") + .andReturn(HttpURLConnection.HTTP_OK, "{\r\n" + + " \"kind\": \"ServiceList\",\r\n" + + " \"apiVersion\": \"v1\",\r\n" + + " \"metadata\": {\r\n" + + " \"resourceVersion\": \"12345\"\r\n" + + " },\r\n" + + " \"items\": []\r\n" + + " }").always(); + + // server api expectations + server.expect().get() + .withPath("/api/v1/namespaces/orderid-xxx-xxx-xxx/services?resourceVersion=0") + .andReturn(HttpURLConnection.HTTP_OK , "{\r\n" + + " \"kind\": \"ServiceList\",\r\n" + + " \"apiVersion\": \"v1\",\r\n" + + " \"metadata\": {\r\n" + + " \"resourceVersion\": \"12345\"\r\n" + + " },\r\n" + + " \"items\": []\r\n" + + " }").always(); + + + + server.expect().get().withPath( + "/api/v1/namespaces/orderid-xxx-xxx-xxx/configmaps?allowWatchBookmarks=true&resourceVersion=12345&timeoutSeconds=600&watch=true") + .andReturn(HttpURLConnection.HTTP_OK, "{\r\n" + + " \"kind\": \"ConfigMapList\",\r\n" + + " \"apiVersion\": \"v1\",\r\n" + + " \"metadata\": {\r\n" + + " \"resourceVersion\": \"12345\"\r\n" + + " },\r\n" + + " \"items\": []\r\n" + + " }").always(); + + // server api expectations + server.expect().get() + .withPath("/api/v1/namespaces/orderid-xxx-xxx-xxx/configmaps?resourceVersion=0") + .andReturn(HttpURLConnection.HTTP_OK , "{\r\n" + + " \"kind\": \"ConfigMapList\",\r\n" + + " \"apiVersion\": \"v1\",\r\n" + + " \"metadata\": {\r\n" + + " \"resourceVersion\": \"12345\"\r\n" + + " },\r\n" + + " \"items\": []\r\n" + + " }").always(); + + server.expect().get() .withPath("/apis/stable.example.com/v1/namespaces/orderid-xxx-xxx-xxx/mycalculators") .andReturn(HttpURLConnection.HTTP_OK, crdMyCalc).once(); @@ -392,12 +443,10 @@ public class CridgeIntegrationTest { Secret secretCreated = routesPreparation.kubernetesClient.secrets().inNamespace("default") .resource(secret1).create(); + - KubernetesSecret value = - kubernetesClientResource.KubernetesSecret2OpensliceResource(secretCreated); - - assertEquals("Secret", value.getKind()); - assertEquals("v1", value.getVersion()); + assertEquals("Secret", secretCreated.getKind()); + assertEquals("v1", secretCreated.getApiVersion() ); }