Search in sources :

Example 1 with ImageStreamStatus

use of io.fabric8.openshift.api.model.ImageStreamStatus in project fabric8-maven-plugin by fabric8io.

the class ImageStreamService method findTagSha.

private String findTagSha(OpenShiftClient client, String imageStreamName, String namespace) throws MojoExecutionException {
    ImageStream currentImageStream = null;
    for (int i = 0; i < IMAGE_STREAM_TAG_RETRIES; i++) {
        if (i > 0) {
            log.info("Retrying to find tag on ImageStream %s", imageStreamName);
            try {
                Thread.sleep(IMAGE_STREAM_TAG_RETRY_TIMEOUT_IN_MILLIS);
            } catch (InterruptedException e) {
                log.debug("interrupted", e);
            }
        }
        currentImageStream = client.imageStreams().withName(imageStreamName).get();
        if (currentImageStream == null) {
            continue;
        }
        ImageStreamStatus status = currentImageStream.getStatus();
        if (status == null) {
            continue;
        }
        List<NamedTagEventList> tags = status.getTags();
        if (tags == null || tags.isEmpty()) {
            continue;
        }
        // Iterate all imagestream tags and get the latest one by 'created' attribute
        TagEvent latestTag = null;
        TAG_EVENT_LIST: for (NamedTagEventList list : tags) {
            List<TagEvent> items = list.getItems();
            if (items == null || items.isEmpty()) {
                continue TAG_EVENT_LIST;
            }
            for (TagEvent tag : items) {
                latestTag = latestTag == null ? tag : newerTag(tag, latestTag);
            }
        }
        if (latestTag != null && StringUtils.isNotBlank(latestTag.getImage())) {
            String image = latestTag.getImage();
            log.info("Found tag on ImageStream " + imageStreamName + " tag: " + image);
            return image;
        }
    }
    // No image found, even after several retries:
    if (currentImageStream == null) {
        throw new MojoExecutionException("Could not find a current ImageStream with name " + imageStreamName + " in namespace " + namespace);
    } else {
        throw new MojoExecutionException("Could not find a tag in the ImageStream " + imageStreamName);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) TagEvent(io.fabric8.openshift.api.model.TagEvent) ImageStream(io.fabric8.openshift.api.model.ImageStream) NamedTagEventList(io.fabric8.openshift.api.model.NamedTagEventList) ArrayList(java.util.ArrayList) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) NamedTagEventList(io.fabric8.openshift.api.model.NamedTagEventList) List(java.util.List) ImageStreamStatus(io.fabric8.openshift.api.model.ImageStreamStatus)

Example 2 with ImageStreamStatus

use of io.fabric8.openshift.api.model.ImageStreamStatus in project jkube by eclipse.

the class ImageStreamService method findTagSha.

private String findTagSha(OpenShiftClient client, String imageStreamName, String namespace) {
    ImageStream currentImageStream = null;
    for (int i = 0; i < IMAGE_STREAM_TAG_RETRIES; i++) {
        if (i > 0) {
            log.info("Retrying to find tag on ImageStream %s", imageStreamName);
            try {
                Thread.sleep(IMAGE_STREAM_TAG_RETRY_TIMEOUT_IN_MILLIS);
            } catch (InterruptedException e) {
                log.debug("interrupted", e);
                Thread.currentThread().interrupt();
            }
        }
        currentImageStream = client.imageStreams().inNamespace(namespace).withName(imageStreamName).get();
        if (currentImageStream == null) {
            continue;
        }
        ImageStreamStatus status = currentImageStream.getStatus();
        if (status == null) {
            continue;
        }
        List<NamedTagEventList> tags = status.getTags();
        if (tags == null || tags.isEmpty()) {
            continue;
        }
        // Iterate all imagestream tags and get the latest one by 'created' attribute
        TagEvent latestTag = null;
        TAG_EVENT_LIST: for (NamedTagEventList list : tags) {
            List<TagEvent> items = list.getItems();
            if (items == null || items.isEmpty()) {
                continue TAG_EVENT_LIST;
            }
            for (TagEvent tag : items) {
                latestTag = latestTag == null ? tag : newerTag(tag, latestTag);
            }
        }
        if (latestTag != null && StringUtils.isNotBlank(latestTag.getImage())) {
            String image = latestTag.getImage();
            log.info("Found tag on ImageStream " + imageStreamName + " tag: " + image);
            return image;
        }
    }
    // No image found, even after several retries:
    if (currentImageStream == null) {
        throw new IllegalStateException("Could not find a current ImageStream with name " + imageStreamName + " in namespace " + namespace);
    } else {
        throw new IllegalStateException("Could not find a tag in the ImageStream " + imageStreamName);
    }
}
Also used : TagEvent(io.fabric8.openshift.api.model.TagEvent) ImageStream(io.fabric8.openshift.api.model.ImageStream) NamedTagEventList(io.fabric8.openshift.api.model.NamedTagEventList) ArrayList(java.util.ArrayList) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) NamedTagEventList(io.fabric8.openshift.api.model.NamedTagEventList) List(java.util.List) ImageStreamStatus(io.fabric8.openshift.api.model.ImageStreamStatus)

Aggregations

KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)2 ImageStream (io.fabric8.openshift.api.model.ImageStream)2 ImageStreamStatus (io.fabric8.openshift.api.model.ImageStreamStatus)2 NamedTagEventList (io.fabric8.openshift.api.model.NamedTagEventList)2 TagEvent (io.fabric8.openshift.api.model.TagEvent)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1