Search in sources :

Example 31 with HasMetadata

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

the class AbstractResourceMojo method writeIndividualResources.

private static void writeIndividualResources(KubernetesList resources, File targetDir, ResourceFileType resourceFileType, Logger log, Boolean generateRoute) throws MojoExecutionException {
    for (HasMetadata item : resources.getItems()) {
        String name = KubernetesHelper.getName(item);
        if (Strings.isNullOrBlank(name)) {
            log.error("No name for generated item %s", item);
            continue;
        }
        String itemFile = KubernetesResourceUtil.getNameWithSuffix(name, item.getKind());
        if (!(item.getKind().equalsIgnoreCase("Route") && !generateRoute)) {
            File itemTarget = new File(targetDir, itemFile);
            writeResource(itemTarget, item, resourceFileType);
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File)

Example 32 with HasMetadata

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

the class AbstractResourceMojo method writeResourcesIndividualAndComposite.

public static File writeResourcesIndividualAndComposite(KubernetesList resources, File resourceFileBase, ResourceFileType resourceFileType, Logger log, Boolean generateRoute) throws MojoExecutionException {
    // Creating a new items list. This will be used to generate openshift.yml
    List<HasMetadata> newItemList = new ArrayList<>();
    if (!generateRoute) {
        // if flag is set false, this will remove the Route resource from resources list
        for (HasMetadata item : resources.getItems()) {
            if (item.getKind().equalsIgnoreCase("Route")) {
                continue;
            }
            newItemList.add(item);
        }
        // update the resource with new list
        resources.setItems(newItemList);
    }
    // entity is object which will be sent to writeResource for openshift.yml
    // if generateRoute is false, this will be set to resources with new list
    // otherwise it will be set to resources with old list.
    Object entity = resources;
    // if the list contains a single Template lets unwrap it
    // in resources already new or old as per condition is set.
    // no need to worry about this for dropping Route.
    Template template = getSingletonTemplate(resources);
    if (template != null) {
        entity = template;
    }
    File file = writeResource(resourceFileBase, entity, resourceFileType);
    // write separate files, one for each resource item
    // resources passed to writeIndividualResources is also new one.
    writeIndividualResources(resources, resourceFileBase, resourceFileType, log, generateRoute);
    return file;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayList(java.util.ArrayList) File(java.io.File) Template(io.fabric8.openshift.api.model.Template)

Example 33 with HasMetadata

use of io.fabric8.kubernetes.api.model.HasMetadata 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 34 with HasMetadata

use of io.fabric8.kubernetes.api.model.HasMetadata 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 35 with HasMetadata

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

the class ResourceMojo method extractAndRemoveTemplates.

private Template extractAndRemoveTemplates(List<HasMetadata> items) {
    Template extractedTemplate = null;
    for (HasMetadata item : new ArrayList<>(items)) {
        if (item instanceof Template && !KubernetesResourceUtil.isAppCatalogResource(item)) {
            Template template = (Template) item;
            if (extractedTemplate == null) {
                extractedTemplate = template;
            } else {
                extractedTemplate = Templates.combineTemplates(extractedTemplate, template);
            }
            items.remove(item);
        }
    }
    if (extractedTemplate != null) {
        extractedTemplate.setObjects(items);
    }
    return extractedTemplate;
}
Also used : ArrayList(java.util.ArrayList) Template(io.fabric8.openshift.api.model.Template)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)79 Test (org.junit.Test)25 File (java.io.File)21 ArrayList (java.util.ArrayList)21 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)16 IOException (java.io.IOException)16 Template (io.fabric8.openshift.api.model.Template)15 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)14 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)13 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)13 Service (io.fabric8.kubernetes.api.model.Service)13 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)13 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)11 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 HashMap (java.util.HashMap)9 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)8 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)8 Map (java.util.Map)8 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)7