use of io.fabric8.support.api.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");
}
use of io.fabric8.support.api.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'");
}
}
use of io.fabric8.support.api.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();
}
}
use of io.fabric8.support.api.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;
}
use of io.fabric8.support.api.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;
}
Aggregations