Search in sources :

Example 46 with HasMetadata

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

the class ProcessTemplateLocallyTest method testProcessTemplateLocally.

@Test
public void testProcessTemplateLocally() throws Exception {
    Template template = assertParseExampleFile("template.json", Template.class);
    List<HasMetadata> objects = template.getObjects();
    assertNotEmpty("objects", objects);
    KubernetesList list = Templates.processTemplatesLocally(template, true);
    assertThat(list).describedAs("results").isNotNull();
    List<HasMetadata> items = list.getItems();
    assertThat(items).describedAs("items").isNotNull();
    ReplicationController rc = (ReplicationController) items.get(1);
    assertEquals("Template value not replaced: items[1].spec.template.spec.containers[0].env[0].value", "https://github.com/fabric8io/jenkins-pipeline-dsl.git", rc.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv().get(0).getValue());
    System.out.println("Created JSON: " + toJson(list));
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template) Test(org.junit.Test)

Example 47 with HasMetadata

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

the class Templates method combineTemplates.

public static Template combineTemplates(Template firstTemplate, Template template) {
    List<HasMetadata> objects = template.getObjects();
    if (objects != null) {
        for (HasMetadata object : objects) {
            addTemplateObject(firstTemplate, object);
        }
    }
    List<Parameter> parameters = firstTemplate.getParameters();
    if (parameters == null) {
        parameters = new ArrayList<>();
        firstTemplate.setParameters(parameters);
    }
    combineParameters(parameters, template.getParameters());
    String name = KubernetesHelper.getName(template);
    if (Strings.isNotBlank(name)) {
        // lets merge all the fabric8 annotations using the template id qualifier as a postfix
        Map<String, String> annotations = KubernetesHelper.getOrCreateAnnotations(firstTemplate);
        Map<String, String> otherAnnotations = KubernetesHelper.getOrCreateAnnotations(template);
        Set<Map.Entry<String, String>> entries = otherAnnotations.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!annotations.containsKey(key)) {
                annotations.put(key, value);
            }
        }
    }
    return firstTemplate;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Parameter(io.fabric8.openshift.api.model.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with HasMetadata

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

the class Templates method processTemplatesLocally.

/**
 * Lets locally process the templates so that we can process templates on any kubernetes environment
 */
public static KubernetesList processTemplatesLocally(Template entity, boolean failOnMissingParameterValue) throws IOException {
    List<HasMetadata> objects = null;
    if (entity != null) {
        objects = entity.getObjects();
        if (objects == null || objects.isEmpty()) {
            return null;
        }
    }
    List<Parameter> parameters = entity != null ? entity.getParameters() : null;
    if (parameters != null && !parameters.isEmpty()) {
        String json = "{\"kind\": \"List\", \"apiVersion\": \"" + KubernetesHelper.defaultApiVersion + "\",\n" + "  \"items\": " + KubernetesHelper.toJson(objects) + " }";
        // lets make a few passes in case there's expressions in values
        for (int i = 0; i < 5; i++) {
            for (Parameter parameter : parameters) {
                String name = parameter.getName();
                String regex = "${" + name + "}";
                String value = parameter.getValue();
                // TODO generate random strings for passwords etc!
                if (Strings.isNullOrBlank(value)) {
                    if (failOnMissingParameterValue) {
                        throw new IllegalArgumentException("No value available for parameter name: " + name);
                    } else {
                        value = "";
                    }
                }
                json = Strings.replaceAllWithoutRegex(json, regex, value);
            }
        }
        return OBJECT_MAPPER.readerFor(KubernetesList.class).readValue(json);
    } else {
        KubernetesList answer = new KubernetesList();
        answer.setItems(objects);
        return answer;
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Parameter(io.fabric8.openshift.api.model.Parameter) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList)

Example 49 with HasMetadata

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

the class Controller method applyEntity.

/**
 * Applies the given DTOs onto the Kubernetes master
 */
public void applyEntity(Object dto, String sourceName) throws Exception {
    if (dto instanceof Pod) {
        applyPod((Pod) dto, sourceName);
    } else if (dto instanceof ReplicationController) {
        applyReplicationController((ReplicationController) dto, sourceName);
    } else if (dto instanceof Service) {
        applyService((Service) dto, sourceName);
    } else if (dto instanceof Namespace) {
        applyNamespace((Namespace) dto);
    } else if (dto instanceof Route) {
        applyRoute((Route) dto, sourceName);
    } else if (dto instanceof BuildConfig) {
        applyBuildConfig((BuildConfig) dto, sourceName);
    } else if (dto instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) dto;
        OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
        if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.APPS)) {
            applyResource(resource, sourceName, openShiftClient.deploymentConfigs());
        } else {
            LOG.warn("Not connected to OpenShift cluster so cannot apply entity " + dto);
        }
    } else if (dto instanceof PolicyBinding) {
        applyPolicyBinding((PolicyBinding) dto, sourceName);
    } else if (dto instanceof RoleBinding) {
        applyRoleBinding((RoleBinding) dto, sourceName);
    } else if (dto instanceof Role) {
        Role resource = (Role) dto;
        OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
        if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.AUTHORIZATION)) {
            applyResource(resource, sourceName, openShiftClient.roles());
        } else {
            LOG.warn("Not connected to OpenShift cluster so cannot apply entity " + dto);
        }
    } else if (dto instanceof ImageStream) {
        applyImageStream((ImageStream) dto, sourceName);
    } else if (dto instanceof OAuthClient) {
        applyOAuthClient((OAuthClient) dto, sourceName);
    } else if (dto instanceof Template) {
        applyTemplate((Template) dto, sourceName);
    } else if (dto instanceof ServiceAccount) {
        applyServiceAccount((ServiceAccount) dto, sourceName);
    } else if (dto instanceof Secret) {
        applySecret((Secret) dto, sourceName);
    } else if (dto instanceof ConfigMap) {
        applyResource((ConfigMap) dto, sourceName, kubernetesClient.configMaps());
    } else if (dto instanceof DaemonSet) {
        applyResource((DaemonSet) dto, sourceName, kubernetesClient.extensions().daemonSets());
    } else if (dto instanceof Deployment) {
        applyResource((Deployment) dto, sourceName, kubernetesClient.extensions().deployments());
    } else if (dto instanceof ReplicaSet) {
        applyResource((ReplicaSet) dto, sourceName, kubernetesClient.extensions().replicaSets());
    } else if (dto instanceof StatefulSet) {
        applyResource((StatefulSet) dto, sourceName, kubernetesClient.apps().statefulSets());
    } else if (dto instanceof Ingress) {
        applyResource((Ingress) dto, sourceName, kubernetesClient.extensions().ingresses());
    } else if (dto instanceof PersistentVolumeClaim) {
        applyPersistentVolumeClaim((PersistentVolumeClaim) dto, sourceName);
    } else if (dto instanceof HasMetadata) {
        HasMetadata entity = (HasMetadata) dto;
        try {
            String namespace = getNamespace();
            String resourceNamespace = getNamespace(entity);
            if (Strings.isNotBlank(namespace) && Strings.isNullOrBlank(resourceNamespace)) {
                getOrCreateMetadata(entity).setNamespace(namespace);
            }
            LOG.info("Applying " + getKind(entity) + " " + getName(entity) + " from " + sourceName);
            kubernetesClient.resource(entity).inNamespace(namespace).createOrReplace();
        } catch (Exception e) {
            onApplyError("Failed to create " + getKind(entity) + " from " + sourceName + ". " + e, e);
        }
    } else {
        throw new IllegalArgumentException("Unknown entity type " + dto);
    }
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) OAuthClient(io.fabric8.openshift.api.model.OAuthClient) DoneableImageStream(io.fabric8.openshift.api.model.DoneableImageStream) ImageStream(io.fabric8.openshift.api.model.ImageStream) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Template(io.fabric8.openshift.api.model.Template) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) Route(io.fabric8.openshift.api.model.Route) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) Namespace(io.fabric8.kubernetes.api.model.Namespace) PolicyBinding(io.fabric8.openshift.api.model.PolicyBinding) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Role(io.fabric8.openshift.api.model.Role) Secret(io.fabric8.kubernetes.api.model.Secret) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 50 with HasMetadata

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

the class Controller method printSummary.

protected void printSummary(Object kubeResource) throws IOException {
    if (kubeResource != null) {
        LOG.debug("  " + kubeResource.getClass().getSimpleName() + " " + kubeResource);
    }
    if (kubeResource instanceof Template) {
        Template template = (Template) kubeResource;
        String id = getName(template);
        LOG.info("  Template " + id + " " + summaryText(template));
        printSummary(template.getObjects());
        return;
    }
    List<HasMetadata> list = toItemList(kubeResource);
    for (HasMetadata object : list) {
        if (object != null) {
            if (object == list) {
                LOG.debug("Ignoring recursive list " + list);
                continue;
            } else if (object instanceof List) {
                printSummary(object);
            } else {
                String kind = object.getClass().getSimpleName();
                String id = getObjectId(object);
                LOG.info("    " + kind + " " + id + " " + summaryText(object));
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesHelper.toItemList(io.fabric8.kubernetes.api.KubernetesHelper.toItemList) List(java.util.List) ArrayList(java.util.ArrayList) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)90 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)25 File (java.io.File)24 IOException (java.io.IOException)24 Template (io.fabric8.openshift.api.model.Template)19 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)18 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)18 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)17 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)14 Service (io.fabric8.kubernetes.api.model.Service)14 URL (java.net.URL)11 HashMap (java.util.HashMap)11 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)10 Map (java.util.Map)10 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9