Search in sources :

Example 36 with ImageName

use of io.fabric8.maven.docker.util.ImageName in project fabric8-maven-plugin by fabric8io.

the class DockerImageWatcher method updateImageName.

private void updateImageName(KubernetesClient kubernetes, String namespace, HasMetadata entity, String imagePrefix, String imageName) {
    String name = KubernetesHelper.getName(entity);
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                kubernetes.extensions().deployments().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                kubernetes.replicationControllers().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            if (updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
                OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
                if (openshiftClient == null) {
                    log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
                }
                openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name).replace(resource);
            }
        }
    }
}
Also used : DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 37 with ImageName

use of io.fabric8.maven.docker.util.ImageName in project ballerina by ballerina-lang.

the class TestUtils method deleteDockerImage.

public static void deleteDockerImage(String imageName) {
    if (!imageName.contains(":")) {
        imageName += ":" + Constants.IMAGE_VERSION_LATEST;
    }
    DockerClient client = new io.fabric8.docker.client.DefaultDockerClient();
    List<ImageDelete> imageDeleteList = client.image().withName(imageName).delete().force().andPrune(false);
    for (ImageDelete imageDelete : imageDeleteList) {
        imageDelete.getDeleted();
        imageDelete.getUntagged();
    }
}
Also used : ImageDelete(io.fabric8.docker.api.model.ImageDelete) DockerClient(io.fabric8.docker.client.DockerClient)

Example 38 with ImageName

use of io.fabric8.maven.docker.util.ImageName in project ballerina by ballerina-lang.

the class DefaultBallerinaDockerClient method buildImage.

/*
    Execute a Docker image build using Fabric8 DSL.
     */
private void buildImage(String dockerEnv, String imageName, Path tmpDir, String buildArgs) throws InterruptedException, IOException {
    DockerClient client = getDockerClient(dockerEnv);
    OutputHandle buildHandle = client.image().build().withRepositoryName(imageName).withNoCache().alwaysRemovingIntermediate().withBuildArgs(buildArgs).usingListener(new DockerBuilderEventListener()).fromFolder(tmpDir.toString());
    buildDone.await();
    buildHandle.close();
    client.close();
}
Also used : DockerClient(io.fabric8.docker.client.DockerClient) BallerinaDockerClient(org.ballerinalang.containers.docker.BallerinaDockerClient) OutputHandle(io.fabric8.docker.dsl.OutputHandle)

Example 39 with ImageName

use of io.fabric8.maven.docker.util.ImageName in project fabric8 by fabric8io.

the class SessionListener method hasRegistry.

/**
 * Checks to see if there's a registry name already provided in the image name
 *
 * Code influenced from <a href="https://github.com/rhuss/docker-maven-plugin/blob/master/src/main/java/org/jolokia/docker/maven/util/ImageName.java">docker-maven-plugin</a>
 * @param imageName
 * @return true if the image name contains a registry
 */
public static boolean hasRegistry(String imageName) {
    if (imageName == null) {
        throw new NullPointerException("Image name must not be null");
    }
    Pattern tagPattern = Pattern.compile("^(.+?)(?::([^:/]+))?$");
    Matcher matcher = tagPattern.matcher(imageName);
    if (!matcher.matches()) {
        throw new IllegalArgumentException(imageName + " is not a proper image name ([registry/][repo][:port]");
    }
    String rest = matcher.group(1);
    String[] parts = rest.split("\\s*/\\s*");
    String part = parts[0];
    return part.contains(".") || part.contains(":");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString)

Example 40 with ImageName

use of io.fabric8.maven.docker.util.ImageName 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)

Aggregations

ImageName (io.fabric8.maven.docker.util.ImageName)20 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)11 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)10 File (java.io.File)9 IOException (java.io.IOException)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)6 Container (io.fabric8.kubernetes.api.model.Container)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 CopyConfiguration (io.fabric8.maven.docker.config.CopyConfiguration)4 Container (io.fabric8.maven.docker.model.Container)4 Test (org.junit.Test)4 DockerClient (io.fabric8.docker.client.DockerClient)3 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)3 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)3 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)3 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)3 Util.readAsString (io.fabric8.arquillian.utils.Util.readAsString)2 Controller (io.fabric8.kubernetes.api.Controller)2