Search in sources :

Example 96 with Image

use of io.fabric8.docker.api.model.Image in project fabric8-maven-plugin by fabric8io.

the class ImageStreamService method appendImageStreamResource.

/**
 * Save the images stream to a file
 * @param imageName name of the image for which the stream should be extracted
 * @param target file to store the image stream
 */
public void appendImageStreamResource(ImageName imageName, File target) throws MojoExecutionException {
    String tag = Strings.isNullOrBlank(imageName.getTag()) ? "latest" : imageName.getTag();
    try {
        ImageStream is = new ImageStreamBuilder().withNewMetadata().withName(imageName.getSimpleName()).endMetadata().withNewSpec().addNewTag().withName(tag).withNewFrom().withKind("ImageStreamImage").endFrom().endTag().endSpec().build();
        createOrUpdateImageStreamTag(client, imageName, is);
        File fullTargetFile = appendImageStreamToFile(is, target);
        log.info("ImageStream %s written to %s", imageName.getSimpleName(), fullTargetFile);
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Cannot write ImageStream descriptor for %s to %s : %s", imageName.getFullName(), target.getAbsoluteFile(), e.getMessage()), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) File(java.io.File) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 97 with Image

use of io.fabric8.docker.api.model.Image in project fabric8-maven-plugin by fabric8io.

the class DockerBuildService method build.

@Override
public void build(ImageConfiguration imageConfig) throws Fabric8ServiceException {
    io.fabric8.maven.docker.service.BuildService dockerBuildService = dockerServiceHub.getBuildService();
    io.fabric8.maven.docker.service.BuildService.BuildContext dockerBuildContext = config.getDockerBuildContext();
    try {
        dockerBuildService.buildImage(imageConfig, dockerBuildContext);
        // Assume we always want to tag
        dockerBuildService.tagImage(imageConfig.getName(), imageConfig);
    } catch (Exception ex) {
        throw new Fabric8ServiceException("Error while trying to build the image", ex);
    }
}
Also used : Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildService(io.fabric8.maven.core.service.BuildService) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException)

Example 98 with Image

use of io.fabric8.docker.api.model.Image in project fabric8-maven-plugin by fabric8io.

the class ContainerHandlerTest method getImageNameTest.

@Test
public void getImageNameTest() {
    ContainerHandler handler = new ContainerHandler(project, envVarHandler, probeHandler);
    // Image Configuration with name and without registry
    ImageConfiguration imageConfiguration2 = new ImageConfiguration.Builder().name("test").alias("test-app").buildConfig(buildImageConfiguration1).build();
    // Image Configuration without name and with registry
    ImageConfiguration imageConfiguration3 = new ImageConfiguration.Builder().alias("test-app").buildConfig(buildImageConfiguration1).registry("docker.io").build();
    // Image Configuration without name and registry
    ImageConfiguration imageConfiguration4 = new ImageConfiguration.Builder().alias("test-app").buildConfig(buildImageConfiguration1).build();
    images.clear();
    images.add(imageConfiguration1);
    images.add(imageConfiguration2);
    images.add(imageConfiguration3);
    images.add(imageConfiguration4);
    containers = handler.getContainers(config1, images);
    assertEquals("docker.io/test", containers.get(0).getImage());
    assertEquals("test", containers.get(1).getImage());
    assertNull(containers.get(2).getImage());
    assertNull(containers.get(3).getImage());
}
Also used : BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Test(org.junit.Test)

Example 99 with Image

use of io.fabric8.docker.api.model.Image in project fabric8-maven-plugin by fabric8io.

the class ImageStreamServiceTest method simple.

@Test
public void simple() throws IOException, MojoExecutionException {
    ImageStreamService service = new ImageStreamService(client, log);
    final ImageStream lookedUpIs = lookupImageStream("ab12cd");
    setupClientMock(lookedUpIs, "test");
    ImageName name = new ImageName("test:1.0");
    File target = File.createTempFile("ImageStreamServiceTest", ".yml");
    service.appendImageStreamResource(name, target);
    assertTrue(target.exists());
    Map result = readImageStreamDescriptor(target);
    Yaml yaml;
    System.out.println(result.toString());
    assertNotNull(result);
    List<Map> items = getItemsList(result);
    assertEquals(1, items.size());
    Map isRead = (Map<String, Object>) items.get(0);
    assertNotNull(isRead);
    assertEquals("ImageStream", isRead.get("kind"));
    Map spec = (Map<String, Object>) isRead.get("spec");
    assertNotNull(spec);
    List tags = (List) spec.get("tags");
    assertNotNull(tags);
    assertEquals(1, tags.size());
    Map tag = (Map) tags.get(0);
    Map from = (Map) tag.get("from");
    assertEquals("ImageStreamImage", from.get("kind"));
    assertEquals("test@ab12cd", from.get("name"));
    assertEquals("default", from.get("namespace"));
    // Add a second image stream
    ImageStream secondIs = lookupImageStream("secondIS");
    setupClientMock(secondIs, "second-test");
    ImageName name2 = new ImageName("second-test:1.0");
    service.appendImageStreamResource(name2, target);
    result = readImageStreamDescriptor(target);
    System.out.println(result.toString());
    items = getItemsList(result);
    assertEquals(2, items.size());
    Set<String> names = new HashSet<>(Arrays.asList("second-test", "test"));
    for (Map item : items) {
        assertTrue(names.remove(((Map) item.get("metadata")).get("name")));
    }
    assertTrue(names.isEmpty());
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) Yaml(org.yaml.snakeyaml.Yaml) Test(org.junit.Test)

Example 100 with Image

use of io.fabric8.docker.api.model.Image in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method checkTarPackage.

@Test
public void checkTarPackage() throws Exception {
    int nTries = 0;
    boolean bTestComplete = false;
    do {
        try {
            nTries++;
            BuildService.BuildServiceConfig config = defaultConfig.build();
            WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
            OpenShiftMockServer mockServer = collector.getMockServer();
            OpenShiftClient client = mockServer.createOpenShiftClient();
            final OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
            ImageConfiguration imageWithEnv = new ImageConfiguration.Builder(image).buildConfig(new BuildImageConfiguration.Builder(image.getBuildConfiguration()).env(Collections.singletonMap("FOO", "BAR")).build()).build();
            service.createBuildArchive(imageWithEnv);
            final List<ArchiverCustomizer> customizer = new LinkedList<>();
            new Verifications() {

                {
                    archiveService.createDockerBuildArchive(withInstanceOf(ImageConfiguration.class), withInstanceOf(MojoParameters.class), withCapture(customizer));
                    assertTrue(customizer.size() == 1);
                }
            };
            customizer.get(0).customize(tarArchiver);
            final List<File> file = new LinkedList<>();
            new Verifications() {

                {
                    String path;
                    tarArchiver.addFile(withCapture(file), path = withCapture());
                    assertEquals(".s2i/environment", path);
                }
            };
            assertEquals(1, file.size());
            List<String> lines;
            try (FileReader reader = new FileReader(file.get(0))) {
                lines = IOUtils.readLines(reader);
            }
            assertTrue(lines.contains("FOO=BAR"));
            bTestComplete = true;
        } catch (Fabric8ServiceException exception) {
            Throwable rootCause = getRootCause(exception);
            logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
            // Let's wait for a while, and then retry again
            if (rootCause != null && rootCause instanceof IOException) {
                continue;
            }
        }
    } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Also used : BuildService(io.fabric8.maven.core.service.BuildService) ArchiverCustomizer(io.fabric8.maven.docker.assembly.ArchiverCustomizer) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) IOException(java.io.IOException) Verifications(mockit.Verifications) LinkedList(java.util.LinkedList) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)35 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)28 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)20 IOException (java.io.IOException)16 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)15 File (java.io.File)13 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)12 ImageStream (io.fabric8.openshift.api.model.ImageStream)10 Arguments (io.fabric8.maven.docker.config.Arguments)8 Service (io.fabric8.kubernetes.api.model.Service)7 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)7 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)6 BuildService (io.fabric8.maven.core.service.BuildService)6 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)6 HashMap (java.util.HashMap)6 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)5 ImageName (io.fabric8.maven.docker.util.ImageName)5 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)5