Search in sources :

Example 11 with ImageName

use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.

the class TriggersAnnotationEnricher method createAnnotation.

protected String createAnnotation(Visitable<?> builder) {
    final List<ImageChangeTrigger> triggerList = new ArrayList<>();
    builder.accept(new TypedVisitor<ContainerBuilder>() {

        @Override
        public void visit(ContainerBuilder cb) {
            Container container = cb.build();
            String containerName = container.getName();
            String containerImage = container.getImage();
            ImageName image = new ImageName(containerImage);
            if (isContainerAllowed(containerName) && image.getRegistry() == null && image.getUser() == null) {
                // Imagestreams used as trigger are in the same namespace
                String tag = image.getTag() != null ? image.getTag() : "latest";
                ImageChangeTrigger trigger = new ImageChangeTriggerBuilder().withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).endFrom().build();
                trigger.setAdditionalProperty("fieldPath", "spec.template.spec.containers[?(@.name==\"" + containerName + "\")].image");
                triggerList.add(trigger);
            }
        }
    });
    try {
        return ResourceUtil.toJson(triggerList);
    } catch (JsonProcessingException e) {
        getLog().error("Error while creating ImageStreamTag triggers for Kubernetes resources: %s", e);
        return "[]";
    }
}
Also used : ImageName(org.eclipse.jkube.kit.config.image.ImageName) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ImageChangeTriggerBuilder(io.fabric8.openshift.api.model.ImageChangeTriggerBuilder) ArrayList(java.util.ArrayList) ImageChangeTrigger(io.fabric8.openshift.api.model.ImageChangeTrigger) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 12 with ImageName

use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.

the class RegistryService method pushImage.

/**
 * Push a set of images to a registry
 *
 * @param imageConfig image to push (but only if they have a build configuration)
 * @param retries how often to retry
 * @param registryConfig a global registry configuration
 * @param skipTag flag to skip pushing tagged images
 * @throws IOException exception
 */
public void pushImage(ImageConfiguration imageConfig, int retries, RegistryConfig registryConfig, boolean skipTag) throws IOException {
    BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String name = imageConfig.getName();
    if (buildConfig != null) {
        String configuredRegistry = EnvUtil.firstRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
        AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
        long start = System.currentTimeMillis();
        docker.pushImage(name, authConfig, configuredRegistry, retries);
        log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
        if (!skipTag) {
            for (String tag : imageConfig.getBuildConfiguration().getTags()) {
                if (tag != null) {
                    docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
                }
            }
        }
    }
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageName(org.eclipse.jkube.kit.config.image.ImageName) AuthConfig(org.eclipse.jkube.kit.build.api.auth.AuthConfig)

Example 13 with ImageName

use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.

the class DockerAccessWithHcClient method pushImage.

@Override
public void pushImage(String image, AuthConfig authConfig, String registry, int retries) throws DockerAccessException {
    ImageName name = new ImageName(image);
    String pushUrl = urlBuilder.pushImage(name, registry);
    TemporaryImageHandler temporaryImageHandler = tagTemporaryImage(name, registry);
    DockerAccessException dae = null;
    try {
        doPushImage(pushUrl, createAuthHeader(authConfig), createPullOrPushResponseHandler(), HTTP_OK, retries);
    } catch (IOException e) {
        dae = new DockerAccessException(e, "Unable to push '%s'%s", image, (registry != null) ? " to registry '" + registry + "'" : "");
        throw dae;
    } finally {
        temporaryImageHandler.handle(dae);
    }
}
Also used : ImageName(org.eclipse.jkube.kit.config.image.ImageName) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) IOException(java.io.IOException)

Example 14 with ImageName

use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.

the class DockerAccessWithHcClient method tag.

@Override
public void tag(String sourceImage, String targetImage, boolean force) throws DockerAccessException {
    ImageName source = new ImageName(sourceImage);
    ImageName target = new ImageName(targetImage);
    try {
        String url = urlBuilder.tagContainer(source, target, force);
        delegate.post(url, HTTP_CREATED);
    } catch (IOException e) {
        throw new DockerAccessException(e, "Unable to add tag [%s] to image [%s]", targetImage, sourceImage, e);
    }
}
Also used : ImageName(org.eclipse.jkube.kit.config.image.ImageName) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) IOException(java.io.IOException)

Example 15 with ImageName

use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.

the class ImageStreamServiceTest method simple.

@Test
public void simple() throws Exception {
    ImageStreamService service = new ImageStreamService(client, "default", log);
    final ImageStream lookedUpIs = lookupImageStream("ab12cd");
    setupClientMock(lookedUpIs, "default", "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);
    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, "default", "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(org.eclipse.jkube.kit.config.image.ImageName) ImageStream(io.fabric8.openshift.api.model.ImageStream) List(java.util.List) File(java.io.File) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ImageName (org.eclipse.jkube.kit.config.image.ImageName)22 BuildServiceConfig (org.eclipse.jkube.kit.config.service.BuildServiceConfig)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 BuildOutput (io.fabric8.openshift.api.model.BuildOutput)4 OpenShiftBuildServiceUtils.createBuildOutput (org.eclipse.jkube.kit.config.service.openshift.OpenShiftBuildServiceUtils.createBuildOutput)4 AuthConfig (org.eclipse.jkube.kit.build.api.auth.AuthConfig)3 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 WatcherException (io.fabric8.kubernetes.client.WatcherException)2 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)2 BuildConfigSpec (io.fabric8.openshift.api.model.BuildConfigSpec)2 BuildStrategy (io.fabric8.openshift.api.model.BuildStrategy)2 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)2 File (java.io.File)2 Map (java.util.Map)2 DockerAccessException (org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException)2 AuthConfigFactory (org.eclipse.jkube.kit.build.service.docker.auth.AuthConfigFactory)2 OpenShiftBuildServiceUtils.createBuildStrategy (org.eclipse.jkube.kit.config.service.openshift.OpenShiftBuildServiceUtils.createBuildStrategy)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1