Search in sources :

Example 36 with HasMetadata

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

the class ResourceMojo method createTemplateWithObjects.

private static Template createTemplateWithObjects(KubernetesList kubernetesResources, Template template) {
    List<io.fabric8.openshift.api.model.Parameter> parameters = template.getParameters();
    List<HasMetadata> items = kubernetesResources.getItems();
    Template tempTemplate = null;
    if (parameters != null && parameters.size() > 0 && items != null && items.size() > 0) {
        tempTemplate = new Template();
        tempTemplate.setMetadata(template.getMetadata());
        tempTemplate.setParameters(parameters);
        tempTemplate.setObjects(items);
    }
    return tempTemplate;
}
Also used : Parameter(org.apache.maven.plugins.annotations.Parameter) Template(io.fabric8.openshift.api.model.Template)

Example 37 with HasMetadata

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

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

the class HelmMojo method findTemplate.

private Template findTemplate() throws MojoExecutionException {
    if (kubernetesTemplate != null && kubernetesTemplate.isFile()) {
        Object dto = null;
        try {
            dto = KubernetesHelper.loadYaml(kubernetesTemplate, KubernetesResource.class);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to load kubernetes YAML " + kubernetesTemplate + ". " + e, e);
        }
        if (dto instanceof Template) {
            return (Template) dto;
        }
        if (dto instanceof KubernetesList) {
            KubernetesList list = (KubernetesList) dto;
            List<HasMetadata> items = list.getItems();
            if (items != null) {
                for (HasMetadata item : items) {
                    if (item instanceof Template) {
                        return (Template) item;
                    }
                }
            }
        }
    }
    return null;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) IOException(java.io.IOException) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template)

Example 39 with HasMetadata

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

the class HelmMojo method copyTemplateResourcesToTemplatesDir.

private void copyTemplateResourcesToTemplatesDir(File templatesDir, Template template) throws MojoExecutionException {
    List<HasMetadata> objects = template.getObjects();
    if (objects != null) {
        for (HasMetadata object : objects) {
            String name = getNameWithSuffix(KubernetesHelper.getName(object), KubernetesHelper.getKind(object)) + ".yaml";
            File outFile = new File(templatesDir, name);
            try {
                saveYaml(object, outFile);
            } catch (IOException e) {
                throw new MojoExecutionException("Failed to save template " + outFile + ": " + e, e);
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) File(java.io.File)

Example 40 with HasMetadata

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

the class HelmMojo method findIconURL.

private String findIconURL() throws MojoExecutionException {
    String answer = null;
    if (kubernetesManifest != null && kubernetesManifest.isFile()) {
        Object dto = null;
        try {
            dto = KubernetesHelper.loadYaml(kubernetesManifest, KubernetesResource.class);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to load kubernetes YAML " + kubernetesManifest + ". " + e, e);
        }
        if (dto instanceof HasMetadata) {
            answer = KubernetesHelper.getOrCreateAnnotations((HasMetadata) dto).get(Annotations.Builds.ICON_URL);
        }
        if (Strings.isNullOrBlank(answer) && dto instanceof KubernetesList) {
            KubernetesList list = (KubernetesList) dto;
            List<HasMetadata> items = list.getItems();
            if (items != null) {
                for (HasMetadata item : items) {
                    answer = KubernetesHelper.getOrCreateAnnotations(item).get(Annotations.Builds.ICON_URL);
                    if (Strings.isNotBlank(answer)) {
                        break;
                    }
                }
            }
        }
    } else {
        getLog().warn("No kubernetes manifest file has been generated yet by the fabric8:resource goal at: " + kubernetesManifest);
    }
    return answer;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) IOException(java.io.IOException) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList)

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