use of io.fabric8.openshift.api.model.Template in project fabric8 by fabric8io.
the class KubernetesModelProcessorProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
Set<TypeElement> processors = new HashSet<>();
// 1st pass collect classes to compile.
for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
processors.add(getClassElement(element));
}
if (processors.isEmpty()) {
return true;
}
StringWriter writer = new StringWriter();
try {
Callable<Boolean> compileTask = compilationTaskFactory.create(processors, writer);
if (!compileTask.call()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
printCompileErrors(compilationTaskFactory);
return false;
}
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error to compile provider classes, due to: " + e.getMessage() + ". See output below.");
return false;
} finally {
String output = writer.toString();
if (Strings.isNullOrBlank(output)) {
output = "success";
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Fabric8 model generator compiler output:" + output);
}
// 2nd pass generate json.
for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
KubernetesModelProcessor annotation = element.getAnnotation(KubernetesModelProcessor.class);
String kubernetesJsonFileName = annotation.value();
KubernetesResource json = readJson(kubernetesJsonFileName);
Builder<? extends KubernetesResource> builder;
if (json instanceof KubernetesList) {
builder = new KubernetesListBuilder((KubernetesList) json);
} else if (json instanceof Template) {
builder = new TemplateBuilder((Template) json);
} else if (json != null) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Unknown Kubernetes json type:" + json.getClass());
return false;
} else {
return false;
}
try {
if (element instanceof TypeElement) {
for (ExecutableElement methodElement : ElementFilter.methodsIn(element.getEnclosedElements())) {
TypeElement classElement = getClassElement(element);
Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
final Object instance = cls.newInstance();
final String methodName = methodElement.getSimpleName().toString();
if (builder instanceof Visitable) {
((Visitable) builder).accept(new Visitor() {
@Override
public void visit(Object o) {
for (Method m : findMethods(instance, methodName, o.getClass())) {
Named named = m.getAnnotation(Named.class);
if (named != null && !Strings.isNullOrBlank(named.value())) {
String objectName = getName(o);
// If a name has been explicitly specified check if there is a match
if (!named.value().equals(objectName)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
return;
}
}
try {
m.invoke(instance, o);
} catch (IllegalAccessException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
} catch (InvocationTargetException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
}
}
}
});
} else {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Json type is not visitable.");
}
}
}
json = builder.build();
generateJson(kubernetesJsonFileName, json);
} catch (Exception ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration:" + ex.getMessage());
}
}
return true;
}
use of io.fabric8.openshift.api.model.Template in project fabric8 by fabric8io.
the class ExampleTest method testNullNavigationOnRC.
@Test
public void testNullNavigationOnRC() throws Exception {
final ReplicationController rc = new ReplicationController();
assertAssertionError(new Block() {
@Override
public void invoke() throws Exception {
assertThat(rc).spec().template().spec().containers().first().image().isEqualTo("someDockerImageName");
}
});
}
use of io.fabric8.openshift.api.model.Template 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));
}
use of io.fabric8.openshift.api.model.Template in project fabric8 by fabric8io.
the class Templates method overrideTemplateParameters.
/**
* Lets allow template parameters to be overridden with a Properties object
*/
public static void overrideTemplateParameters(Template template, Map<String, String> properties, String propertyNamePrefix) {
List<Parameter> parameters = template.getParameters();
if (parameters != null && properties != null) {
boolean missingProperty = false;
for (Parameter parameter : parameters) {
String parameterName = parameter.getName();
String name = propertyNamePrefix + parameterName;
String propertyValue = properties.get(name);
if (Strings.isNotBlank(propertyValue)) {
LOG.info("Overriding template parameter " + name + " with value: " + propertyValue);
parameter.setValue(propertyValue);
} else {
missingProperty = true;
LOG.info("No property defined for template parameter: " + name);
}
}
if (missingProperty) {
LOG.debug("current properties " + new TreeSet<>(properties.keySet()));
}
}
}
use of io.fabric8.openshift.api.model.Template 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;
}
Aggregations