Search in sources :

Example 96 with Builder

use of io.fabric8.maven.docker.config.CopyConfiguration.Builder in project fabric8-maven-plugin by fabric8io.

the class BaseGeneratorTest method defaultAddFrom.

@Test
public void defaultAddFrom() {
    Properties props = new Properties();
    for (boolean isOpenShift : new Boolean[] { false, true }) {
        for (boolean isRedHat : new Boolean[] { false, true }) {
            for (TestFromSelector selector : new TestFromSelector[] { null, new TestFromSelector(ctx, isRedHat) }) {
                for (String from : new String[] { null, "openshift/testfrom" }) {
                    setupContext(props, isOpenShift, from, null);
                    BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder();
                    BaseGenerator generator = createGenerator(selector);
                    generator.addFrom(builder);
                    BuildImageConfiguration config = builder.build();
                    if (isRedHat && isOpenShift && selector != null) {
                        if (from != null) {
                            assertEquals("testfrom:latest", config.getFrom());
                            assertFromExt(config.getFromExt(), "testfrom:latest", "openshift");
                        } else {
                            assertEquals("selectorIstagFromRedhat", config.getFrom());
                            assertFromExt(config.getFromExt(), selector.getIstagFrom(), "openshift");
                        }
                    } else {
                        if (from != null) {
                            assertEquals(config.getFrom(), from);
                            assertNull(config.getFromExt());
                        } else {
                            System.out.println(isRedHat + " " + isOpenShift);
                            assertNull(config.getFromExt());
                            assertEquals(config.getFrom(), selector != null ? (isOpenShift ? selector.getS2iBuildFrom() : selector.getDockerBuildFrom()) : null);
                        }
                    }
                }
            }
        }
    }
}
Also used : Properties(java.util.Properties) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) Test(org.junit.Test)

Example 97 with Builder

use of io.fabric8.maven.docker.config.CopyConfiguration.Builder in project fabric8-maven-plugin by fabric8io.

the class BaseGeneratorTest method addLatestTagIfSnapshot.

@Test
public void addLatestTagIfSnapshot() {
    new Expectations() {

        {
            ctx.getProject();
            result = project;
            project.getVersion();
            result = "1.2-SNAPSHOT";
        }
    };
    BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder();
    BaseGenerator generator = createGenerator(null);
    generator.addLatestTagIfSnapshot(builder);
    ;
    BuildImageConfiguration config = builder.build();
    List<String> tags = config.getTags();
    assertEquals(1, tags.size());
    assertTrue(tags.get(0).endsWith("latest"));
}
Also used : Expectations(mockit.Expectations) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) Test(org.junit.Test)

Example 98 with Builder

use of io.fabric8.maven.docker.config.CopyConfiguration.Builder in project fabric8-maven-plugin by fabric8io.

the class DeploymentOpenShiftConverter method convert.

@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
    Deployment resource = (Deployment) item;
    DeploymentConfigBuilder builder = new DeploymentConfigBuilder();
    builder.withMetadata(resource.getMetadata());
    DeploymentSpec spec = resource.getSpec();
    if (spec != null) {
        DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec();
        Integer replicas = spec.getReplicas();
        if (replicas != null) {
            specBuilder.withReplicas(replicas);
        }
        Integer revisionHistoryLimit = spec.getRevisionHistoryLimit();
        if (revisionHistoryLimit != null) {
            specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
        }
        LabelSelector selector = spec.getSelector();
        if (selector != null) {
            Map<String, String> matchLabels = selector.getMatchLabels();
            if (matchLabels != null && !matchLabels.isEmpty()) {
                specBuilder.withSelector(matchLabels);
            }
        }
        Map<String, String> containerToImageMap = new HashMap<>();
        PodTemplateSpec template = spec.getTemplate();
        if (template != null) {
            specBuilder.withTemplate(template);
            PodSpec podSpec = template.getSpec();
            notNull(podSpec, "No PodSpec for PodTemplate:" + template);
            List<Container> containers = podSpec.getContainers();
            notNull(podSpec, "No containers for PodTemplate.spec: " + template);
            for (Container container : containers) {
                validateContainer(container);
                containerToImageMap.put(container.getName(), container.getImage());
            }
        }
        DeploymentStrategy strategy = spec.getStrategy();
        String strategyType = null;
        if (strategy != null) {
            strategyType = strategy.getType();
        }
        if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
            if (Strings.isNullOrBlank(strategyType) || "Rolling".equals(strategyType)) {
                specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
            } else if ("Recreate".equals(strategyType)) {
                specBuilder.withNewStrategy().withType("Recreate").withNewRecreateParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRecreateParams().endStrategy();
            } else {
                specBuilder.withNewStrategy().withType(strategyType).endStrategy();
            }
        } else if (Strings.isNotBlank(strategyType)) {
            // TODO is there any values we can copy across?
            specBuilder.withNewStrategy().withType(strategyType).endStrategy();
        }
        // lets add a default trigger so that its triggered when we change its config
        if (enableAutomaticTrigger) {
            specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
        }
        // add a new image change trigger for the build stream
        if (containerToImageMap.size() != 0) {
            if (mode.equals(PlatformMode.openshift)) {
                for (Map.Entry<String, String> entry : containerToImageMap.entrySet()) {
                    String containerName = entry.getKey();
                    ImageName image = new ImageName(entry.getValue());
                    String tag = image.getTag() != null ? image.getTag() : "latest";
                    specBuilder.addNewTrigger().withType("ImageChange").withNewImageChangeParams().withAutomatic(enableAutomaticTrigger).withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).withNamespace(image.getUser()).endFrom().withContainerNames(containerName).endImageChangeParams().endTrigger();
                }
            }
            if (trimImageInContainerSpec) {
                /*
                         * In Openshift 3.7, update to container image is automatically triggering redeployments
                         * and those subsequent rollouts lead to RC complaining about a missing image reference.
                         *
                         *    See this : https://github.com/openshift/origin/issues/18406#issuecomment-364090247
                         *
                         * this the time it gets fixed. Do this:
                         * Since we're using ImageTrigger here, set container image to " ". If there is any config
                         * change never set to image else than " "; so doing oc apply/rollouts won't be creating
                         * re-deployments again and again.
                         *
                         */
                List<Container> containers = template.getSpec().getContainers();
                for (Integer nIndex = 0; nIndex < containers.size(); nIndex++) {
                    containers.get(nIndex).setImage(" ");
                }
                template.getSpec().setContainers(containers);
                specBuilder.withTemplate(template);
            }
        }
        specBuilder.endSpec();
    }
    return builder.build();
}
Also used : HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) DeploymentConfigFluent(io.fabric8.openshift.api.model.DeploymentConfigFluent) DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ImageName(io.fabric8.maven.docker.util.ImageName) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) HashMap(java.util.HashMap) Map(java.util.Map)

Example 99 with Builder

use of io.fabric8.maven.docker.config.CopyConfiguration.Builder in project fabric8-maven-plugin by fabric8io.

the class DeploymentConfigOpenShiftConverter method convert.

@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
    if (item instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) item;
        if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
            DeploymentConfigBuilder builder = new DeploymentConfigBuilder(resource);
            DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder;
            if (resource.getSpec() != null) {
                specBuilder = builder.editSpec();
            } else {
                specBuilder = builder.withNewSpec();
            }
            specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
            specBuilder.endSpec();
            return builder.build();
        }
    }
    return item;
}
Also used : DeploymentConfigFluent(io.fabric8.openshift.api.model.DeploymentConfigFluent) DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 100 with Builder

use of io.fabric8.maven.docker.config.CopyConfiguration.Builder in project fabric8-maven-plugin by fabric8io.

the class EnricherManagerTest method enrichSimple.

@Test
public void enrichSimple() {
    new Expectations() {

        {
            context.getConfig();
            result = new ProcessorConfig(Arrays.asList("fmp-project"), null, new HashMap<String, TreeMap>());
        }
    };
    EnricherManager manager = new EnricherManager(null, context);
    KubernetesListBuilder builder = new KubernetesListBuilder();
    builder.addNewReplicaSetItem().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withName("test").withImage("busybox").endContainer().endSpec().endTemplate().endSpec().endReplicaSetItem();
    manager.enrich(builder);
    KubernetesList list = builder.build();
    assertEquals(1, list.getItems().size());
    ReplicaSet pod = (ReplicaSet) list.getItems().get(0);
    ObjectMeta metadata = pod.getMetadata();
    assertNotNull(metadata);
    Map<String, String> labels = metadata.getLabels();
    assertNotNull(labels);
    assertEquals("fabric8", labels.get("provider"));
}
Also used : Expectations(mockit.Expectations) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)60 ProfileBuilder (io.fabric8.api.ProfileBuilder)34 Profile (io.fabric8.api.Profile)33 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)27 FabricService (io.fabric8.api.FabricService)24 InputStream (java.io.InputStream)21 Deployment (org.jboss.arquillian.container.test.api.Deployment)21 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)21 Asset (org.jboss.shrinkwrap.api.asset.Asset)21 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)21 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Logger (org.slf4j.Logger)19 CommandSupport (io.fabric8.itests.support.CommandSupport)18 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)17 ServiceLocator (io.fabric8.api.gravia.ServiceLocator)16 IOException (java.io.IOException)16 Action (org.apache.felix.gogo.commands.Action)16 AbstractCommand (org.apache.felix.gogo.commands.basic.AbstractCommand)16