Search in sources :

Example 46 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class ImageEnricherTest method checkEnrichDeploymentConfig.

@Test
public void checkEnrichDeploymentConfig() throws Exception {
    KubernetesListBuilder builder = new KubernetesListBuilder().addNewDeploymentConfigItem().endDeploymentConfigItem();
    imageEnricher.addMissingResources(builder);
    assertCorrectlyGeneratedResources(builder.build(), "DeploymentConfig");
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Test(org.junit.Test)

Example 47 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class MergeEnricher method adapt.

@Override
public void adapt(KubernetesListBuilder builder) {
    List<HasMetadata> items = notNullList(builder.getItems());
    Map<String, Map<String, HasMetadata>> kindMaps = new HashMap<>();
    List<HasMetadata> removeList = new ArrayList<>();
    for (HasMetadata item : items) {
        String kind = getKind(item);
        String name = KubernetesHelper.getName(item);
        Map<String, HasMetadata> map = kindMaps.get(kind);
        if (map == null) {
            map = new HashMap<>();
            kindMaps.put(kind, map);
        }
        HasMetadata old = map.get(name);
        if (old != null) {
            HasMetadata removeItem = mergeEntities(old, item);
            if (removeItem != null) {
                removeList.add(removeItem);
            }
        } else {
            map.put(name, item);
        }
    }
    items.removeAll(removeList);
    builder.withItems(items);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8 by fabric8io.

the class SessionListener method enhance.

private KubernetesList enhance(final Session session, Configuration configuration, KubernetesList kubernetesList) {
    if (configuration == null || configuration.getProperties() == null || !configuration.getProperties().containsKey(Constants.KUBERNETES_MODEL_PROCESSOR_CLASS)) {
        return kubernetesList;
    }
    String processorClassName = configuration.getProperties().get(Constants.KUBERNETES_MODEL_PROCESSOR_CLASS);
    try {
        final Object instance = SessionListener.class.getClassLoader().loadClass(processorClassName).newInstance();
        KubernetesListBuilder builder = new KubernetesListBuilder(kubernetesList);
        ((Visitable) builder).accept(new Visitor() {

            @Override
            public void visit(Object o) {
                for (Method m : findMethods(instance, o.getClass())) {
                    Named named = m.getAnnotation(Named.class);
                    if (named != null && !Strings.isNullOrBlank(named.value())) {
                        String objectName = o instanceof ObjectMeta ? getName((ObjectMeta) o) : getName((HasMetadata) o);
                        // If a name has been explicitly specified check if there is a match
                        if (!named.value().equals(objectName)) {
                            session.getLogger().warn("Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
                            return;
                        }
                    }
                    try {
                        m.invoke(instance, o);
                    } catch (IllegalAccessException e) {
                    } catch (InvocationTargetException e) {
                        session.getLogger().error("Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                    }
                }
            }
        });
        return builder.build();
    } catch (Exception e) {
        session.getLogger().warn("Failed to load processor class:" + processorClassName + ". Ignoring");
        return kubernetesList;
    }
}
Also used : Named(javax.inject.Named) Visitor(io.fabric8.kubernetes.api.builder.Visitor) Visitable(io.fabric8.kubernetes.api.builder.Visitable) Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiException(io.fabric8.utils.MultiException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 49 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8 by fabric8io.

the class TemplatesTest method testCombineResourcesIntoTemplate.

@Test
public void testCombineResourcesIntoTemplate() throws Exception {
    Service templateService = new ServiceBuilder().withNewMetadata().withName("templateService").endMetadata().build();
    KubernetesList list = new KubernetesListBuilder().addNewServiceItem().withNewMetadata().withName("service1").endMetadata().endServiceItem().addNewTemplateItem().addNewParameter().withName("PARAM1").withValue("ABC").endParameter().addToObjects(templateService).endTemplateItem().addNewServiceItem().withNewMetadata().withName("service2").endMetadata().endServiceItem().build();
    Object result = Templates.combineTemplates(list);
    System.out.println("Combined as " + KubernetesHelper.toJson(result));
    assertThat(result).isInstanceOf(Template.class);
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Service(io.fabric8.kubernetes.api.model.Service) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.junit.Test)

Aggregations

KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)30 Test (org.junit.Test)25 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)17 Expectations (mockit.Expectations)13 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 File (java.io.File)5 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 Secret (io.fabric8.kubernetes.api.model.Secret)3 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)3 IOException (java.io.IOException)3 Map (java.util.Map)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Visitable (io.fabric8.kubernetes.api.builder.Visitable)2 Visitor (io.fabric8.kubernetes.api.builder.Visitor)2 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)2