Search in sources :

Example 21 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class KubernetesClientUtil method deleteEntities.

public static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, Logger log) {
    List<HasMetadata> list = new ArrayList<>(entities);
    // For OpenShift cluster, also delete s2i buildconfig
    OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
    if (openshiftClient != null) {
        for (HasMetadata entity : list) {
            if ("ImageStream".equals(getKind(entity))) {
                ImageName imageName = new ImageName(entity.getMetadata().getName());
                String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix);
                log.info("Deleting resource BuildConfig " + namespace + "/" + buildName);
                openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete();
            }
        }
    }
    // lets delete in reverse order
    Collections.reverse(list);
    for (HasMetadata entity : list) {
        log.info("Deleting resource " + getKind(entity) + " " + namespace + "/" + getName(entity));
        kubernetes.resource(entity).inNamespace(namespace).cascading(true).delete();
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 22 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class ClientToolsService method getKubeCtlExecutable.

public File getKubeCtlExecutable() {
    OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
    String command = openShiftClient != null ? "oc" : "kubectl";
    String missingCommandMessage;
    File file = ProcessUtil.findExecutable(log, command);
    if (file == null && command.equals("oc")) {
        file = ProcessUtil.findExecutable(log, command);
        missingCommandMessage = "commands oc or kubectl";
    } else {
        missingCommandMessage = "command " + command;
    }
    if (file == null) {
        throw new IllegalStateException("Could not find " + missingCommandMessage + ". Please install the necessary binaries and ensure they get added to your $PATH");
    }
    return file;
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) File(java.io.File)

Example 23 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method waitForOpenShiftBuildToComplete.

private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws MojoExecutionException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch logTerminateLatch = new CountDownLatch(1);
    final String buildName = KubernetesHelper.getName(build);
    final AtomicReference<Build> buildHolder = new AtomicReference<>();
    // Don't query for logs directly, Watch over the build pod:
    waitUntilPodIsReady(buildName + "-build", 20, log);
    log.info("Waiting for build " + buildName + " to complete...");
    try (LogWatch logWatch = client.pods().withName(buildName + "-build").watchLog()) {
        KubernetesClientUtil.printLogsAsync(logWatch, "Failed to tail build log", logTerminateLatch, log);
        Watcher<Build> buildWatcher = getBuildWatcher(latch, buildName, buildHolder);
        try (Watch watcher = client.builds().withName(buildName).watch(buildWatcher)) {
            // Check if the build is already finished to avoid waiting indefinitely
            Build lastBuild = client.builds().withName(buildName).get();
            String lastStatus = KubernetesResourceUtil.getBuildStatusPhase(lastBuild);
            if (Builds.isFinished(lastStatus)) {
                log.debug("Build %s is already finished", buildName);
                buildHolder.set(lastBuild);
                latch.countDown();
            }
            waitUntilBuildFinished(latch);
            logTerminateLatch.countDown();
            build = buildHolder.get();
            String status = KubernetesResourceUtil.getBuildStatusPhase(build);
            if (Builds.isFailed(status) || Builds.isCancelled(status)) {
                throw new MojoExecutionException("OpenShift Build " + buildName + ": " + KubernetesResourceUtil.getBuildStatusReason(build));
            }
            log.info("Build " + buildName + " " + status);
        }
    }
}
Also used : LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Build(io.fabric8.openshift.api.model.Build) Watch(io.fabric8.kubernetes.client.Watch) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method updateOrCreateBuildConfig.

private String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, ImageConfiguration imageConfig) {
    ImageName imageName = new ImageName(imageConfig.getName());
    String buildName = getS2IBuildName(config, imageName);
    String imageStreamName = getImageStreamName(imageName);
    String outputImageStreamTag = imageStreamName + ":" + (imageName.getTag() != null ? imageName.getTag() : "latest");
    BuildStrategy buildStrategyResource = createBuildStrategy(imageConfig, config.getOpenshiftBuildStrategy());
    BuildOutput buildOutput = new BuildOutputBuilder().withNewTo().withKind("ImageStreamTag").withName(outputImageStreamTag).endTo().build();
    // Fetch exsting build config
    BuildConfig buildConfig = client.buildConfigs().withName(buildName).get();
    if (buildConfig != null) {
        // lets verify the BC
        BuildConfigSpec spec = getBuildConfigSpec(buildConfig);
        validateSourceType(buildName, spec);
        if (config.getBuildRecreateMode().isBuildConfig()) {
            // Delete and recreate afresh
            client.buildConfigs().withName(buildName).delete();
            return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
        } else {
            // Update & return
            return updateBuildConfig(client, buildName, buildStrategyResource, buildOutput, spec);
        }
    } else {
        // Create afresh
        return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) BuildOutput(io.fabric8.openshift.api.model.BuildOutput) BuildStrategy(io.fabric8.openshift.api.model.BuildStrategy) OpenShiftBuildStrategy(io.fabric8.maven.core.config.OpenShiftBuildStrategy) BuildOutputBuilder(io.fabric8.openshift.api.model.BuildOutputBuilder) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) BuildConfigSpec(io.fabric8.openshift.api.model.BuildConfigSpec)

Example 25 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method logBuildBuildFailedDetails.

private void logBuildBuildFailedDetails(OpenShiftClient client, String buildName) {
    try {
        BuildConfig build = client.buildConfigs().withName(buildName).get();
        ObjectReference ref = build.getSpec().getStrategy().getSourceStrategy().getFrom();
        String kind = ref.getKind();
        String name = ref.getName();
        if ("DockerImage".equals(kind)) {
            log.error("Please, ensure that the Docker image '%s' exists and is accessible by OpenShift", name);
        } else if ("ImageStreamTag".equals(kind)) {
            String namespace = ref.getNamespace();
            String namespaceInfo = "current";
            String namespaceParams = "";
            if (namespace != null && !namespace.isEmpty()) {
                namespaceInfo = "'" + namespace + "'";
                namespaceParams = " -n " + namespace;
            }
            log.error("Please, ensure that the ImageStream Tag '%s' exists in the %s namespace (with 'oc get is%s')", name, namespaceInfo, namespaceParams);
        }
    } catch (Exception ex) {
        log.error("Unable to get detailed information from the BuildServiceConfig: " + ex.getMessage());
    }
}
Also used : BuildConfig(io.fabric8.openshift.api.model.BuildConfig) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)92 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)31 IOException (java.io.IOException)31 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)23 FileNotFoundException (java.io.FileNotFoundException)21 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)20 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)20 Test (org.junit.Test)17 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)16 JSONObject (org.json.JSONObject)16 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)15 Route (io.fabric8.openshift.api.model.Route)14 Service (io.fabric8.kubernetes.api.model.Service)12 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)12 API (org.wso2.carbon.apimgt.core.models.API)12 Controller (io.fabric8.kubernetes.api.Controller)11 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)9 BeforeTest (org.testng.annotations.BeforeTest)9