Search in sources :

Example 26 with OpenShiftClient

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

the class ApplyMojo method createRoutes.

protected void createRoutes(Controller controller, Collection<HasMetadata> collection) {
    String routeDomainPostfix = this.routeDomain;
    Log log = getLog();
    String namespace = clusterAccess.getNamespace();
    // lets get the routes first to see if we should bother
    try {
        OpenShiftClient openshiftClient = controller.getOpenShiftClientOrNull();
        if (openshiftClient == null) {
            return;
        }
        RouteList routes = openshiftClient.routes().inNamespace(namespace).list();
        if (routes != null) {
            routes.getItems();
        }
    } catch (Exception e) {
        log.warn("Cannot load OpenShift Routes; maybe not connected to an OpenShift platform? " + e, e);
        return;
    }
    List<Route> routes = new ArrayList<>();
    for (Object object : collection) {
        if (object instanceof Service) {
            Service service = (Service) object;
            Route route = createRouteForService(routeDomainPostfix, namespace, service);
            if (route != null) {
                routes.add(route);
            }
        }
    }
    collection.addAll(routes);
}
Also used : Log(org.apache.maven.plugin.logging.Log) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) RouteList(io.fabric8.openshift.api.model.RouteList) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Route(io.fabric8.openshift.api.model.Route)

Example 27 with OpenShiftClient

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

the class DebugMojo method applyEntities.

@Override
protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    LabelSelector firstSelector = null;
    for (HasMetadata entity : entities) {
        String name = getName(entity);
        LabelSelector selector = null;
        if (entity instanceof Deployment) {
            Deployment resource = (Deployment) entity;
            DeploymentSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    kubernetes.extensions().deployments().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        } else if (entity instanceof ReplicaSet) {
            ReplicaSet resource = (ReplicaSet) entity;
            ReplicaSetSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    kubernetes.extensions().replicaSets().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        } else if (entity instanceof ReplicationController) {
            ReplicationController resource = (ReplicationController) entity;
            ReplicationControllerSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    kubernetes.replicationControllers().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        } else if (entity instanceof DeploymentConfig) {
            DeploymentConfig resource = (DeploymentConfig) entity;
            DeploymentConfigSpec spec = resource.getSpec();
            if (spec != null) {
                if (enableDebugging(entity, spec.getTemplate())) {
                    OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
                    if (openshiftClient == null) {
                        log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
                        continue;
                    }
                    openshiftClient.deploymentConfigs().inNamespace(namespace).withName(name).replace(resource);
                }
                selector = getPodLabelSelector(entity);
            }
        }
        if (selector != null) {
            firstSelector = selector;
        } else {
            controller.apply(entity, fileName);
        }
    }
    if (firstSelector != null) {
        Map<String, String> envVars = new TreeMap<>();
        envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG, "true");
        envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(this.debugSuspend));
        if (this.debugSuspendValue != null) {
            envVars.put(DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
        }
        String podName = waitForRunningPodWithEnvVar(kubernetes, namespace, firstSelector, envVars);
        portForward(controller, podName);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) KubernetesResourceUtil.getPodLabelSelector(io.fabric8.maven.core.util.KubernetesResourceUtil.getPodLabelSelector) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) TreeMap(java.util.TreeMap) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 28 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient 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 29 with OpenShiftClient

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

the class ImportMojo method ensureNamespaceExists.

private void ensureNamespaceExists(KubernetesClient kubernetes, String name) {
    name = convertToValidDnsLabel(name);
    // lets check namespace exists
    Namespace namespace = kubernetes.namespaces().withName(name).get();
    if (namespace == null) {
        Map<String, String> labels = new HashMap<>();
        labels.put("provider", "fabric8");
        labels.put("kind", "secrets");
        namespace = new NamespaceBuilder().withNewMetadata().withName(name).withLabels(labels).endMetadata().build();
        if (KubernetesHelper.isOpenShift(kubernetes)) {
            ProjectRequest projectRequest = new ProjectRequestBuilder().withMetadata(namespace.getMetadata()).build();
            OpenShiftClient openShiftClient = asOpenShiftClient(kubernetes);
            log.info("Creating ProjectRequest " + name + " with labels: " + labels);
            openShiftClient.projectrequests().create(projectRequest);
        } else {
            log.info("Creating Namespace " + name + " with labels: " + labels);
            kubernetes.namespaces().withName(name).create(namespace);
        }
    }
}
Also used : ProjectRequestBuilder(io.fabric8.openshift.api.model.ProjectRequestBuilder) HashMap(java.util.HashMap) ProjectRequest(io.fabric8.openshift.api.model.ProjectRequest) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Namespace(io.fabric8.kubernetes.api.model.Namespace) KubernetesHelper.getNamespace(io.fabric8.kubernetes.api.KubernetesHelper.getNamespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder)

Example 30 with OpenShiftClient

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

the class ImportMojo method findBuildConfigForGitRepo.

protected BuildConfig findBuildConfigForGitRepo(OpenShiftClient openShiftClient, String namespace, String gitRepoUrl, String gitRef) throws MojoExecutionException {
    BuildConfigList buildConfigList = openShiftClient.buildConfigs().inNamespace(namespace).list();
    if (buildConfigList != null) {
        List<BuildConfig> items = buildConfigList.getItems();
        if (items != null) {
            for (BuildConfig item : items) {
                BuildConfigSpec spec = item.getSpec();
                if (spec != null) {
                    BuildSource source = spec.getSource();
                    if (source != null) {
                        GitBuildSource git = source.getGit();
                        if (git != null) {
                            String uri = git.getUri();
                            String ref = git.getRef();
                            if (Objects.equal(gitRepoUrl, uri)) {
                                if (Strings.isNullOrBlank(gitRef) && Strings.isNullOrBlank(ref)) {
                                    return item;
                                }
                                if (Objects.equal(gitRef, ref)) {
                                    return item;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : BuildSource(io.fabric8.openshift.api.model.BuildSource) GitBuildSource(io.fabric8.openshift.api.model.GitBuildSource) BuildConfigHelper.createBuildConfig(io.fabric8.project.support.BuildConfigHelper.createBuildConfig) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) GitBuildSource(io.fabric8.openshift.api.model.GitBuildSource) BuildConfigSpec(io.fabric8.openshift.api.model.BuildConfigSpec) BuildConfigList(io.fabric8.openshift.api.model.BuildConfigList)

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