private static Config parseConfigs() { File file = getKubernetesConfigFile(); if (file.exists() && file.isFile()) { try { return ResourceUtil.load(file, Config.class, ResourceFileType.yaml); } catch (IOException e) { e.printStackTrace(); } } return null; }
private static File writeResource(File resourceFileBase, Object entity, ResourceFileType resourceFileType) throws MojoExecutionException { try { return ResourceUtil.save(resourceFileBase, entity, resourceFileType); } catch (IOException e) { throw new MojoExecutionException("Failed to write resource to " + resourceFileBase + ". " + e, e); } }
public static File save(File file, Object data, ResourceFileType type) throws IOException { File output = type.addExtensionIfMissing(file); ensureDir(file); getObjectMapper(type).writeValue(output, data); return output; }
if (parameters != null && !parameters.isEmpty()) { String json = "{\"kind\": \"List\", \"apiVersion\": \"" + DEFAULT_API_VERSION + "\",\n" + " \"items\": " + ResourceUtil.toJson(objects) + " }";
public static <T> T load(InputStream in, Class<T> clazz, ResourceFileType resourceFileType) throws IOException { return getObjectMapper(resourceFileType).readValue(in, clazz); }
public static String toYaml(Object resource) throws JsonProcessingException { return serializeAsString(resource, ResourceFileType.yaml); }
protected String createAnnotation(Visitable<?> builder) { final List<ImageChangeTrigger> triggerList = new ArrayList<>(); builder.accept(new TypedVisitor<ContainerBuilder>() { @Override public void visit(ContainerBuilder cb) { Container container = cb.build(); String containerName = container.getName(); String containerImage = container.getImage(); ImageName image = new ImageName(containerImage); if (isContainerAllowed(containerName) && image.getRegistry() == null && image.getUser() == null) { // Imagestreams used as trigger are in the same namespace String tag = image.getTag() != null ? image.getTag() : "latest"; ImageChangeTrigger trigger = new ImageChangeTriggerBuilder() .withNewFrom() .withKind("ImageStreamTag") .withName(image.getSimpleName() + ":" + tag) .endFrom() .build(); trigger.setAdditionalProperty("fieldPath", "spec.template.spec.containers[?(@.name==\"" + containerName + "\")].image"); triggerList.add(trigger); } } }); try { return ResourceUtil.toJson(triggerList); } catch (JsonProcessingException e) { getLog().error("Error while creating ImageStreamTag triggers for Kubernetes resources: %s", e); return "[]"; } }
public static <T> T load(File file, Class<T> clazz, ResourceFileType resourceFileType) throws IOException { return getObjectMapper(resourceFileType).readValue(file, clazz); }
public static String toJson(Object resource) throws JsonProcessingException { return serializeAsString(resource, ResourceFileType.json); }
} else { try { text = ResourceUtil.toJson(result); } catch (JsonProcessingException e) { log.warn("Cannot convert " + result + " to JSON: " + e, e);
public static File save(File file, Object data) throws IOException { return save(file, data, ResourceFileType.fromFile(file)); }
public static <T> T load(File file, Class<T> clazz) throws IOException { ResourceFileType type = ResourceFileType.fromFile(file); return load(file, clazz, type); }
private static String serializeAsString(Object resource, ResourceFileType resourceFileType) throws JsonProcessingException { return getObjectMapper(resourceFileType).writeValueAsString(resource); }
private Route createRouteForService(String routeDomainPostfix, String namespace, Service service) { Route route = null; String id = KubernetesHelper.getName(service); if (StringUtils.isNotBlank(id) && hasExactlyOneService(service, id)) { route = new Route(); ObjectMeta routeMeta = KubernetesHelper.getOrCreateMetadata(route); routeMeta.setName(id); routeMeta.setNamespace(namespace); RouteSpec routeSpec = new RouteSpec(); RouteTargetReference objectRef = new RouteTargetReferenceBuilder().withName(id).build(); //objectRef.setNamespace(namespace); routeSpec.setTo(objectRef); if (StringUtils.isNotBlank(routeDomainPostfix)) { routeSpec.setHost(prepareHostForRoute(routeDomainPostfix, id)); } else { routeSpec.setHost(""); } route.setSpec(routeSpec); String json; try { json = ResourceUtil.toJson(route); } catch (JsonProcessingException e) { json = e.getMessage() + ". object: " + route; } log.debug("Created route: " + json); } return route; }
private void copyTemplateResourcesToTemplatesDir(File templatesDir, Template template) throws MojoExecutionException { List<HasMetadata> objects = template.getObjects(); if (objects != null) { for (HasMetadata object : objects) { String name = KubernetesResourceUtil.getNameWithSuffix(KubernetesHelper.getName(object), KubernetesHelper.getKind(object)) + ".yaml"; File outFile = new File(templatesDir, name); try { ResourceUtil.save(outFile, object); } catch (IOException e) { throw new MojoExecutionException("Failed to save template " + outFile + ": " + e, e); } } } }
private Template findTemplate() throws MojoExecutionException { if (kubernetesTemplate != null && kubernetesTemplate.isFile()) { Object dto = null; try { dto = ResourceUtil.load(kubernetesTemplate, KubernetesResource.class,ResourceFileType.yaml); } catch (IOException e) { throw new MojoExecutionException("Failed to load kubernetes YAML " + kubernetesTemplate + ". " + e, e); } if (dto instanceof Template) { return (Template) dto; } if (dto instanceof KubernetesList) { KubernetesList list = (KubernetesList) dto; List<HasMetadata> items = list.getItems(); if (items != null) { for (HasMetadata item : items) { if (item instanceof Template) { return (Template) item; } } } } } return null; }
json = ResourceUtil.toJson(ingress); } catch (JsonProcessingException e) { json = e.getMessage() + ". object: " + ingress;
ResourceUtil.save(outputChartFile, values, ResourceFileType.yaml); } catch (IOException e) { throw new MojoExecutionException("Failed to save chart values " + outputChartFile + ": " + e, e);
public static Set<HasMetadata> loadResources(File manifest) throws IOException { Object dto = ResourceUtil.load(manifest, KubernetesResource.class); if (dto == null) { throw new IllegalStateException("Cannot load kubernetes manifest " + manifest); } if (dto instanceof Template) { Template template = (Template) dto; dto = OpenshiftHelper.processTemplatesLocally(template, false); } Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator()); entities.addAll(KubernetesHelper.toItemList(dto)); return entities; }
private void appendImageStreamToFile(ImageStream is, File target) throws MojoExecutionException, IOException { Map<String, ImageStream> imageStreams = readAlreadyExtractedImageStreams(target); // Override with given image stream imageStreams.put(is.getMetadata().getName(),is); KubernetesList isList = new KubernetesListBuilder().withItems(new ArrayList<HasMetadata>(imageStreams.values())).build(); ResourceUtil.save(target, isList); }