use of io.fabric8.kubernetes.api.model.HasMetadata in project fabric8-maven-plugin by fabric8io.
the class AppCatalogMojo method executeInternal.
public void executeInternal() throws MojoExecutionException, MojoFailureException {
List<HasMetadata> openshiftResources = new ArrayList<>();
List<HasMetadata> kubernetesResources = new ArrayList<>();
Map<URL, KubernetesResource> openshiftMap = loadYamlResourcesOnClassPath("META-INF/fabric8/openshift.yml");
log.info("Found " + openshiftMap.size() + " openshift resources");
for (Map.Entry<URL, KubernetesResource> entry : openshiftMap.entrySet()) {
URL url = entry.getKey();
KubernetesResource<?> resource = entry.getValue();
Template template = null;
if (resource instanceof Template) {
template = (Template) resource;
getOrCreateAnnotations(template).put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
log.debug("Found Template " + getName(template) + " with " + notNullList(template.getParameters()).size() + " parameters");
} else {
TemplateBuilder builder = new TemplateBuilder();
boolean foundMetadata = false;
if (resource instanceof HasMetadata) {
HasMetadata hasMetadata = (HasMetadata) resource;
ObjectMeta metadata = hasMetadata.getMetadata();
if (metadata != null) {
if (Strings.isNotBlank(metadata.getName())) {
foundMetadata = true;
getOrCreateAnnotations(hasMetadata).put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
builder.withMetadata(metadata);
}
}
}
if (!foundMetadata) {
Map<String, String> labels = new HashMap<>();
Map<String, String> annotations = new HashMap<>();
annotations.put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
String name = extractNameFromURL(url, labels);
if (name.equals("META-INF")) {
log.debug("Ignoring local build dependency %s", url);
continue;
}
if (Strings.isNullOrBlank(name)) {
log.warn("Cannot generate a template name from URL: %s", url);
continue;
}
populateLabelsFromResources(resource, labels);
populateAnnotationsFromResources(resource, annotations);
builder.withNewMetadata().withName(name).withLabels(labels).withAnnotations(annotations).endMetadata();
}
if (resource instanceof KubernetesList) {
KubernetesList list = (KubernetesList) resource;
List<HasMetadata> items = list.getItems();
if (items == null || items.isEmpty()) {
log.warn("Ignoring resource %s as it contains a List which contains no items!", url);
continue;
}
builder.withObjects(items);
}
template = builder.build();
}
if (template != null) {
openshiftResources.add(template);
}
}
Map<String, Template> kubernetesTemplates = new HashMap<>();
Map<URL, KubernetesResource> kubernetesTemplateMap = loadYamlResourcesOnClassPath("META-INF/fabric8/" + KUBERNETES_TEMPLATE.getValue() + ".yml");
for (Map.Entry<URL, KubernetesResource> entry : kubernetesTemplateMap.entrySet()) {
URL url = entry.getKey();
KubernetesResource<?> resource = entry.getValue();
if (resource instanceof Template) {
Template template = (Template) resource;
String name = getName(template);
if (Strings.isNullOrBlank(name)) {
log.warn("Ignoring Template from %s as it has no name!", url);
continue;
}
if (kubernetesTemplates.containsKey(name)) {
log.warn("Found duplicate template named: %s for url: %s", name, url);
}
kubernetesTemplates.put(name, template);
}
}
Set<String> kubernetesTemplateFileNames = new HashSet<>();
Set<String> openshiftTemplateFileNames = new HashSet<>();
Map<URL, KubernetesResource> kubernetesMap = loadYamlResourcesOnClassPath("META-INF/fabric8/kubernetes.yml");
for (Map.Entry<URL, KubernetesResource> entry : kubernetesMap.entrySet()) {
URL url = entry.getKey();
KubernetesResource<?> resource = entry.getValue();
Map<String, String> labels = new HashMap<>();
Map<String, String> annotations = new HashMap<>();
String name = extractNameFromURL(url, labels);
if (name.equals("META-INF")) {
log.debug("Ignoring local build dependency %s", url);
continue;
}
if (Strings.isNullOrBlank(name)) {
log.warn("Cannot generate a template name from URL: %s", url);
continue;
}
if (kubernetesTemplates.containsKey(name)) {
log.info("Ignoring duplicate template %s from url: %s", name, url);
continue;
}
populateLabelsFromResources(resource, labels);
populateAnnotationsFromResources(resource, annotations);
TemplateBuilder builder = new TemplateBuilder();
builder.withNewMetadata().withName(name).withLabels(labels).withAnnotations(annotations).endMetadata();
if (resource instanceof KubernetesList) {
KubernetesList list = (KubernetesList) resource;
List<HasMetadata> items = list.getItems();
if (items == null || items.isEmpty()) {
log.warn("Ignoring resource %s as it contains a List which contains no items!", url);
continue;
}
builder.withObjects(items);
} else if (resource instanceof HasMetadata) {
HasMetadata hasMetadata = (HasMetadata) resource;
builder.withObjects(hasMetadata);
}
Template template = builder.build();
if (template != null) {
kubernetesTemplates.put(name, template);
openshiftTemplateFileNames.add(name + "-template.yml");
}
}
for (Map.Entry<String, Template> entry : kubernetesTemplates.entrySet()) {
String name = entry.getKey();
Template template = entry.getValue();
String templateYaml = null;
try {
templateYaml = KubernetesHelper.toYaml(template);
} catch (IOException e) {
throw new MojoExecutionException("Failed to convert template " + name + " into YAML: " + e, e);
}
String catalogName = "catalog-" + name;
Map<String, String> labels = new LinkedHashMap<>(KubernetesHelper.getLabels(template));
Map<String, String> annotations = getOrCreateAnnotations(template);
annotations.put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
populateLabelsFromResources(template, labels);
populateAnnotationsFromResources(template, annotations);
labels.put("kind", "catalog");
Map<String, String> data = new HashMap<>();
data.put(catalogName + ".yml", templateYaml);
kubernetesTemplateFileNames.add(catalogName + "-configmap.yml");
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(catalogName).withLabels(labels).withAnnotations(annotations).endMetadata().withData(data).build();
kubernetesResources.add(configMap);
}
if (openshiftResources.isEmpty()) {
log.warn("No OpenShift resources generated");
} else {
writeResources(new KubernetesListBuilder().withItems(openshiftResources).build(), ResourceClassifier.OPENSHIFT, true);
}
if (kubernetesResources.isEmpty()) {
log.warn("No Kubernetes resources generated");
} else {
writeResources(new KubernetesListBuilder().withItems(kubernetesResources).build(), ResourceClassifier.KUBERNETES, true);
}
// lets remove the dependencies which are not app templates
removeGeneratedFilesNotMatchingSuffix("kubernetes", kubernetesTemplateFileNames);
removeGeneratedFilesNotMatchingSuffix("openshift", openshiftTemplateFileNames);
}
use of io.fabric8.kubernetes.api.model.HasMetadata in project fabric8-maven-plugin by fabric8io.
the class AppCatalogMojo method populateAnnotationsFromResources.
private void populateAnnotationsFromResources(KubernetesResource resource, Map<String, String> annotations) {
if (resource instanceof KubernetesList) {
KubernetesList list = (KubernetesList) resource;
List<HasMetadata> items = list.getItems();
if (items != null) {
for (HasMetadata item : items) {
populateAnnotationsForEntity(item, annotations);
}
}
} else if (resource instanceof HasMetadata) {
HasMetadata entity = (HasMetadata) resource;
populateAnnotationsForEntity(entity, annotations);
}
}
use of io.fabric8.kubernetes.api.model.HasMetadata in project fabric8-maven-plugin by fabric8io.
the class WatchMojo method executeInternal.
@Override
protected synchronized void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
this.hub = hub;
URL masterUrl = kubernetes.getMasterUrl();
KubernetesResourceUtil.validateKubernetesMasterUrl(masterUrl);
File manifest;
boolean isOpenshift = KubernetesHelper.isOpenShift(kubernetes);
if (isOpenshift) {
manifest = openshiftManifest;
} else {
manifest = kubernetesManifest;
}
try {
Set<HasMetadata> resources = KubernetesResourceUtil.loadResources(manifest);
WatcherContext context = getWatcherContext();
WatcherManager.watch(getResolvedImages(), resources, context);
} catch (KubernetesClientException ex) {
KubernetesResourceUtil.handleKubernetesClientException(ex, this.log);
} catch (Exception ex) {
throw new MojoExecutionException("An error has occurred while while trying to watch the resources", ex);
}
}
use of io.fabric8.kubernetes.api.model.HasMetadata in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method mergeResources.
/**
* Merges the given resources together into a single resource.
*
* If switchOnLocalCustomisation is false then the overrides from item2 are merged into item1
*
* @return the newly merged resources
*/
public static HasMetadata mergeResources(HasMetadata item1, HasMetadata item2, Logger log, boolean switchOnLocalCustomisation) {
if (item1 instanceof Deployment && item2 instanceof Deployment) {
return mergeDeployments((Deployment) item1, (Deployment) item2, log, switchOnLocalCustomisation);
}
if (item1 instanceof ConfigMap && item2 instanceof ConfigMap) {
ConfigMap cm1 = (ConfigMap) item1;
ConfigMap cm2 = (ConfigMap) item2;
return mergeConfigMaps(cm1, cm2, log, switchOnLocalCustomisation);
}
mergeMetadata(item1, item2);
return item1;
}
use of io.fabric8.kubernetes.api.model.HasMetadata in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method mergeMetadata.
protected static void mergeMetadata(HasMetadata item1, HasMetadata item2) {
if (item1 != null && item2 != null) {
ObjectMeta metadata1 = item1.getMetadata();
ObjectMeta metadata2 = item2.getMetadata();
if (metadata1 == null) {
item1.setMetadata(metadata2);
} else if (metadata2 != null) {
metadata1.setAnnotations(mergeMapsAndRemoveEmptyStrings(metadata2.getAnnotations(), metadata1.getAnnotations()));
metadata1.setLabels(mergeMapsAndRemoveEmptyStrings(metadata2.getLabels(), metadata1.getLabels()));
}
}
}
Aggregations