Search in sources :

Example 41 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class MergeResourceTest method testMergeDeploymentTemplateMetadata.

@Test
public void testMergeDeploymentTemplateMetadata() throws Exception {
    Deployment resource = new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").endContainer().endSpec().withNewMetadata().addToAnnotations("overwriteKey", "originalValue").addToAnnotations("unchangedKey", "shouldNotChange").addToAnnotations("unchangedBlankKey", "").addToAnnotations("deletedKey", "shouldBeDeleted").endMetadata().endTemplate().endSpec().build();
    Deployment override = new DeploymentBuilder().withNewMetadata().withName("cheese").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().addToEnv(new EnvVarBuilder().withName("ENV_FOO").withValue("FOO_VALUE").build()).endContainer().endSpec().withNewMetadata().addToAnnotations("overwriteKey", "newValue").addToAnnotations("deletedKey", "").endMetadata().endTemplate().endSpec().build();
    HasMetadata answer = KubernetesResourceUtil.mergeResources(resource, override, log, false);
    assertNotNull(answer);
    log.info("Override metadata on Deployment generated: " + KubernetesHelper.toYaml(answer));
    assertThat(answer).describedAs("mergeResult").isInstanceOf(Deployment.class);
    Deployment deployment = (Deployment) answer;
    Map<String, String> annotations = deployment.getSpec().getTemplate().getMetadata().getAnnotations();
    assertDataModified(annotations, "Deployment.spec.template.metadata.annotations");
    assertDataNotModified(resource.getSpec().getTemplate().getMetadata().getAnnotations(), "Original Deployment.spec.template.metadata.annotations");
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) Test(org.junit.Test)

Example 42 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class DebugEnricher method addMissingResources.

@Override
public void addMissingResources(KubernetesListBuilder builder) {
    if (debugEnabled()) {
        int count = 0;
        List<HasMetadata> items = builder.getItems();
        if (items != null) {
            for (HasMetadata item : items) {
                if (enableDebug(item)) {
                    count++;
                }
            }
        }
        if (count > 0) {
            builder.withItems(items);
        }
        log.verbose("Enabled debugging on " + count + " resource(s) thanks to the " + ENABLE_DEBUG_MAVEN_PROPERTY + " property");
    } else {
        log.verbose("Debugging not enabled. To enable try setting the " + ENABLE_DEBUG_MAVEN_PROPERTY + " maven or system property to 'true'");
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata)

Example 43 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class KubernetesClientUtil method deleteEntities.

public static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, Logger log) {
    List<HasMetadata> list = new ArrayList<>(entities);
    // For OpenShift cluster, also delete s2i buildconfig
    OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
    if (openshiftClient != null) {
        for (HasMetadata entity : list) {
            if ("ImageStream".equals(getKind(entity))) {
                ImageName imageName = new ImageName(entity.getMetadata().getName());
                String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix);
                log.info("Deleting resource BuildConfig " + namespace + "/" + buildName);
                openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete();
            }
        }
    }
    // lets delete in reverse order
    Collections.reverse(list);
    for (HasMetadata entity : list) {
        log.info("Deleting resource " + getKind(entity) + " " + namespace + "/" + getName(entity));
        kubernetes.resource(entity).inNamespace(namespace).cascading(true).delete();
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 44 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method readResourceFragmentsFrom.

/**
 * Read all Kubernetes resource fragments from a directory and create a {@link KubernetesListBuilder} which
 * can be adapted later.
 *
 * @param apiVersions the api versions to use
 * @param defaultName the default name to use when none is given
 * @param resourceFiles files to add.
 * @return the list builder
 * @throws IOException
 */
public static KubernetesListBuilder readResourceFragmentsFrom(ResourceVersioning apiVersions, String defaultName, File[] resourceFiles) throws IOException {
    KubernetesListBuilder builder = new KubernetesListBuilder();
    if (resourceFiles != null) {
        for (File file : resourceFiles) {
            HasMetadata resource = getResource(apiVersions, file, defaultName);
            builder.addToItems(resource);
        }
    }
    return builder;
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File)

Example 45 with Resource

use of io.fabric8.kubernetes.client.dsl.Resource in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method loadResources.

public static Set<HasMetadata> loadResources(File manifest) throws IOException {
    Object dto = KubernetesHelper.loadYaml(manifest, KubernetesResource.class);
    if (dto == null) {
        throw new IllegalStateException("Cannot load kubernetes YAML: " + manifest);
    }
    if (dto instanceof Template) {
        Template template = (Template) dto;
        boolean failOnMissingParameterValue = false;
        dto = Templates.processTemplatesLocally(template, failOnMissingParameterValue);
    }
    Set<KubernetesResource<?>> resources = new LinkedHashSet<>();
    Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
    for (KubernetesResource<?> resource : resources) {
        entities.addAll(KubernetesHelper.toItemList(resource));
    }
    entities.addAll(KubernetesHelper.toItemList(dto));
    return entities;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) HasMetadataComparator(io.fabric8.kubernetes.internal.HasMetadataComparator) TreeSet(java.util.TreeSet) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) Template(io.fabric8.openshift.api.model.Template)

Aggregations

HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)28 Test (org.junit.Test)28 Resource (io.fabric8.kubernetes.client.dsl.Resource)25 IOException (java.io.IOException)24 Map (java.util.Map)23 HashMap (java.util.HashMap)19 ArrayList (java.util.ArrayList)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)14 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)14 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)14 File (java.io.File)14 Resource (org.osgi.resource.Resource)14 Async (io.vertx.ext.unit.Async)12 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)11 List (java.util.List)11 Service (io.fabric8.kubernetes.api.model.Service)10 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)10 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)9 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)9