Search in sources :

Example 16 with Controller

use of io.fabric8.kubernetes.api.Controller in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method executeInternal.

public void executeInternal() throws MojoExecutionException, MojoFailureException {
    clusterAccess = new ClusterAccess(namespace);
    try {
        KubernetesClient kubernetes = clusterAccess.createDefaultClient(log);
        URL masterUrl = kubernetes.getMasterUrl();
        File manifest;
        if (KubernetesHelper.isOpenShift(kubernetes)) {
            manifest = openshiftManifest;
        } else {
            manifest = kubernetesManifest;
        }
        if (!Files.isFile(manifest)) {
            if (failOnNoKubernetesJson) {
                throw new MojoFailureException("No such generated manifest file: " + manifest);
            } else {
                log.warn("No such generated manifest file %s for this project so ignoring", manifest);
                return;
            }
        }
        String clusterKind = "Kubernetes";
        if (KubernetesHelper.isOpenShift(kubernetes)) {
            clusterKind = "OpenShift";
        }
        KubernetesResourceUtil.validateKubernetesMasterUrl(masterUrl);
        log.info("Using %s at %s in namespace %s with manifest %s ", clusterKind, masterUrl, clusterAccess.getNamespace(), manifest);
        Controller controller = createController();
        controller.setAllowCreate(createNewResources);
        controller.setServicesOnlyMode(servicesOnly);
        controller.setIgnoreServiceMode(ignoreServices);
        controller.setLogJsonDir(jsonLogDir);
        controller.setBasedir(getRootProjectFolder());
        controller.setIgnoreRunningOAuthClients(ignoreRunningOAuthClients);
        controller.setProcessTemplatesLocally(processTemplatesLocally);
        controller.setDeletePodsOnReplicationControllerUpdate(deletePodsOnReplicationControllerUpdate);
        controller.setRollingUpgrade(rollingUpgrades);
        controller.setRollingUpgradePreserveScale(isRollingUpgradePreserveScale());
        boolean openShift = KubernetesHelper.isOpenShift(kubernetes);
        if (openShift) {
            getLog().info("OpenShift platform detected");
        } else {
            disableOpenShiftFeatures(controller);
        }
        // lets check we have created the namespace
        String namespace = clusterAccess.getNamespace();
        controller.applyNamespace(namespace);
        controller.setNamespace(namespace);
        Set<HasMetadata> entities = KubernetesResourceUtil.loadResources(manifest);
        if (createExternalUrls) {
            if (controller.getOpenShiftClientOrNull() != null) {
                createRoutes(controller, entities);
            } else {
                createIngress(controller, kubernetes, entities);
            }
        }
        applyEntities(controller, kubernetes, namespace, manifest.getName(), entities);
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) URL(java.net.URL) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) File(java.io.File) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 17 with Controller

use of io.fabric8.kubernetes.api.Controller 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 18 with Controller

use of io.fabric8.kubernetes.api.Controller 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 19 with Controller

use of io.fabric8.kubernetes.api.Controller 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 20 with Controller

use of io.fabric8.kubernetes.api.Controller in project fabric8-maven-plugin by fabric8io.

the class EnricherManagerTest method createDefaultResources.

@Test
public void createDefaultResources() {
    new Expectations() {

        {
            context.getConfig();
            result = new ProcessorConfig(Arrays.asList("fmp-controller"), null, null);
            context.getImages();
            result = new ImageConfiguration.Builder().alias("img1").name("img1").build();
        }
    };
    EnricherManager manager = new EnricherManager(null, context);
    KubernetesListBuilder builder = new KubernetesListBuilder();
    manager.createDefaultResources(builder);
    assertTrue(builder.build().getItems().size() > 0);
}
Also used : Expectations(mockit.Expectations) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)16 Controller (io.fabric8.kubernetes.api.Controller)13 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)12 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)12 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)9 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)8 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)8 Async (io.vertx.ext.unit.Async)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)7 Service (io.fabric8.kubernetes.api.model.Service)6 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)5 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)5 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4