use of io.fabric8.agent.model.Dependency in project fabric8 by jboss-fuse.
the class DependencyFilters method parse.
/**
* Creates a filter from the given String
*/
public static Filter<Dependency> parse(String dependencyFilterText) {
List<Filter<Dependency>> filters = new ArrayList<Filter<Dependency>>();
StringTokenizer iter = new StringTokenizer(dependencyFilterText);
while (iter.hasMoreElements()) {
String text = iter.nextToken();
Filter<Dependency> filter = parseSingleFilter(text);
if (filter != null) {
filters.add(filter);
}
}
return Filters.compositeFilter(filters);
}
use of io.fabric8.agent.model.Dependency 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.agent.model.Dependency in project fabric8-maven-plugin by fabric8io.
the class OpenShiftDependencyResources method addMissingResources.
public void addMissingResources(List<HasMetadata> objects) {
Map<KindAndName, HasMetadata> itemMap = new HashMap<>();
for (HasMetadata item : objects) {
itemMap.put(new KindAndName(item), item);
}
// lets add any openshift specific dependencies (OAuthClient etc) which are not already added
for (Map.Entry<KindAndName, HasMetadata> entry : openshiftDependencyResources.entrySet()) {
KindAndName key = entry.getKey();
HasMetadata dependency = entry.getValue();
if (!itemMap.containsKey(key)) {
objects.add(dependency);
}
}
}
use of io.fabric8.agent.model.Dependency in project fabric8 by fabric8io.
the class SessionListener method start.
public void start(@Observes final Start event, KubernetesClient client, Controller controller, Configuration configuration) throws Exception {
Objects.requireNonNull(client, "KubernetesClient most not be null!");
Session session = event.getSession();
final Logger log = session.getLogger();
String namespace = session.getNamespace();
System.setProperty(Constants.KUBERNETES_NAMESPACE, namespace);
log.status("Using Kubernetes at: " + client.getMasterUrl());
log.status("Creating kubernetes resources inside namespace: " + namespace);
log.info("if you use OpenShift then type this switch namespaces: oc project " + namespace);
log.info("if you use kubernetes then type this to switch namespaces: kubectl namespace " + namespace);
clearTestResultDirectories(session);
controller.setNamespace(namespace);
controller.setThrowExceptionOnError(true);
controller.setRecreateMode(true);
controller.setIgnoreRunningOAuthClients(true);
if (configuration.isCreateNamespaceForTest()) {
createNamespace(client, controller, session);
} else {
String namespaceToUse = configuration.getNamespace();
checkNamespace(client, controller, session, configuration);
updateConfigMapStatus(client, session, Constants.RUNNING_STATUS);
namespace = namespaceToUse;
controller.setNamespace(namespace);
}
List<KubernetesList> kubeConfigs = new LinkedList<>();
shutdownHook = new ShutdownHook(client, controller, configuration, session, kubeConfigs);
Runtime.getRuntime().addShutdownHook(shutdownHook);
try {
URL configUrl = configuration.getEnvironmentConfigUrl();
List<String> dependencies = !configuration.getEnvironmentDependencies().isEmpty() ? configuration.getEnvironmentDependencies() : resolver.resolve(session);
if (configuration.isEnvironmentInitEnabled()) {
for (String dependency : dependencies) {
log.info("Found dependency: " + dependency);
loadDependency(log, kubeConfigs, dependency, controller, configuration, namespace);
}
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
if (configUrl == null) {
// lets try find the default configuration generated by the new fabric8-maven-plugin
String resourceName = "kubernetes.yml";
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE) && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
resourceName = "openshift.yml";
}
configUrl = findConfigResource("/META-INF/fabric8/" + resourceName);
}
if (configUrl != null) {
log.status("Applying kubernetes configuration from: " + configUrl);
String configText = readAsString(configUrl);
Object dto = null;
String configPath = configUrl.getPath();
if (configPath.endsWith(".yml") || configPath.endsWith(".yaml")) {
dto = loadYaml(configText, KubernetesResource.class);
} else {
dto = loadJson(configText);
}
dto = expandTemplate(controller, configuration, log, namespace, configUrl.toString(), dto);
KubernetesList kubeList = KubernetesHelper.asKubernetesList(dto);
List<HasMetadata> items = kubeList.getItems();
kubeConfigs.add(kubeList);
}
// Lets also try to load the image stream for the project.
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
File targetDir = new File(System.getProperty("basedir", ".") + "/target");
if (targetDir.exists() && targetDir.isDirectory()) {
File[] files = targetDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith("-is.yml")) {
loadDependency(log, kubeConfigs, file.toURI().toURL().toString(), controller, configuration, namespace);
}
}
}
}
//
}
}
if (!configuration.isEnvironmentInitEnabled() || applyConfiguration(client, controller, configuration, session, kubeConfigs)) {
displaySessionStatus(client, session);
} else {
throw new IllegalStateException("Failed to apply kubernetes configuration.");
}
} catch (Exception e) {
try {
cleanupSession(client, controller, configuration, session, kubeConfigs, Constants.ERROR_STATUS);
} catch (MultiException me) {
throw e;
} finally {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
throw new RuntimeException(e);
}
}
Aggregations