Java Code Examples for org.codehaus.jackson.node.ArrayNode#size()
The following examples show how to use
org.codehaus.jackson.node.ArrayNode#size() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lineage.java From Cubert with Apache License 2.0 | 6 votes |
private static boolean visitMappers(ObjectNode jobNode, OperatorVisitor visitorObj, boolean reverse) { ArrayNode mappers = (ArrayNode) jobNode.get("map"); int si = (reverse ? mappers.size() - 1 : 0); int ei = (reverse ? -1 : mappers.size()); // TODO Auto-generated method stub for (int i = si; i != ei; i += increment(reverse)) { if (!visitMapNode(jobNode, mappers.get(i), visitorObj, reverse)) return false; } return true; }
Example 2
Source File: Lineage.java From Cubert with Apache License 2.0 | 6 votes |
public static void visitOperators(ObjectNode programNode, ObjectNode jobNode, OperatorVisitor tracerObj, boolean reverse) { ArrayNode jobs = (ArrayNode) programNode.get("jobs"); int si = (reverse ? jobs.size() - 1 : 0); int ei = (reverse ? -1 : jobs.size()); for (int i = si; i != ei; i = i + increment(reverse)) { if (jobNode != null && jobNode != jobs.get(i)) continue; if (!visitOperatorsInJob(programNode, (ObjectNode) jobs.get(i), tracerObj, reverse)) return; } }
Example 3
Source File: JsonUtils.java From Cubert with Apache License 2.0 | 6 votes |
public static String[] asArray(JsonNode node) { if (node == null) throw new IllegalArgumentException("Specified JsonNode is null"); if (node.isArray()) { ArrayNode anode = (ArrayNode) node; int nelements = anode.size(); String[] array = new String[nelements]; for (int i = 0; i < nelements; i++) { array[i] = anode.get(i).getTextValue(); } return array; } else { return new String[] { node.getTextValue() }; } }
Example 4
Source File: LineageHelper.java From Cubert with Apache License 2.0 | 5 votes |
public List<ObjectNode> findOperatorInputSources(ObjectNode opNode, String inputRelation) { int opSequence = this.getOpSequence(opNode); ObjectNode jobNode = (ObjectNode) (this.getJobPhase(opNode).getFirst()); JsonNode phaseNode = this.getJobPhase(opNode).getSecond(); List<ObjectNode> result = new ArrayList<ObjectNode>(); if (isReducePhase(phaseNode) && (opSequence == 0 || getOperatorSourceInPhase(jobNode, (ArrayNode) phaseNode, opNode, inputRelation) == null)) { // if either first operator in reduce phase or a matching source within // the same phase cannot be found, // look inside all the map jobs. ArrayNode mapsArray = (ArrayNode) getJobPhase(opNode).getFirst().get("map"); for (JsonNode mapNode : mapsArray) { ArrayNode mapOps = (ArrayNode) ((ObjectNode) mapNode).get("operators"); if (mapOps == null || mapOps.size() == 0) continue; ObjectNode lastOp = (ObjectNode) mapOps.get(mapOps.size() - 1); result.add(findOperatorSourcePrior(getOpSequence(lastOp), inputRelation)); } } else result.add(findOperatorSource(opNode, inputRelation)); return result; }
Example 5
Source File: PerfProfiler.java From Cubert with Apache License 2.0 | 5 votes |
private void SetupPass(int passIndex, ArrayNode operatorsJson) { profileOperatorBlock[passIndex] = new BufferedTupleOperatorBlock[operatorsJson.size()]; cumulativeOperatorTime[passIndex] = new LongWritable[operatorsJson.size()]; operatorDependency[passIndex] = getOperatorDependency(operatorsJson); }
Example 6
Source File: QueueInformation.java From sequenceiq-samples with Apache License 2.0 | 5 votes |
public void printQueueInfo(HttpClient client, ObjectMapper mapper, String schedulerResourceURL) { //http://sandbox.hortonworks.com:8088/ws/v1/cluster/scheduler in case of HDP GetMethod get = new GetMethod(schedulerResourceURL); get.setRequestHeader("Accept", "application/json"); try { int statusCode = client.executeMethod(get); if (statusCode != HttpStatus.SC_OK) { LOGGER.error("Method failed: " + get.getStatusLine()); } InputStream in = get.getResponseBodyAsStream(); JsonNode jsonNode = mapper.readValue(in, JsonNode.class); ArrayNode queues = (ArrayNode) jsonNode.path("scheduler").path("schedulerInfo").path("queues").get("queue"); for (int i = 0; i < queues.size(); i++) { JsonNode queueNode = queues.get(i); LOGGER.info("queueName / usedCapacity / absoluteUsedCap / absoluteCapacity / absMaxCapacity: " + queueNode.findValue("queueName") + " / " + queueNode.findValue("usedCapacity") + " / " + queueNode.findValue("absoluteUsedCapacity") + " / " + queueNode.findValue("absoluteCapacity") + " / " + queueNode.findValue("absoluteMaxCapacity")); } } catch (IOException e) { LOGGER.error("Exception occured", e); } finally { get.releaseConnection(); } }
Example 7
Source File: Lineage.java From Cubert with Apache License 2.0 | 5 votes |
private static boolean visitOperatorsInArray(ObjectNode jobNode, JsonNode phaseNode, ArrayNode operatorArray, OperatorVisitor visitorObj, boolean reverse) { int si = reverse ? operatorArray.size() - 1 : 0; int ei = reverse ? -1 : operatorArray.size(); for (int i = si; i != ei; i += increment(reverse)) { if (!visitorObj.inspect(jobNode, phaseNode, (ObjectNode) operatorArray.get(i))) return false; } return true; }
Example 8
Source File: BoundaryEventJsonConverter.java From fixflow with Apache License 2.0 | 5 votes |
private String lookForAttachedRef(String boundaryEventId, JsonNode childShapesNode) { String attachedRefId = null; if (childShapesNode != null) { for (JsonNode childNode : childShapesNode) { ArrayNode outgoingNode = (ArrayNode) childNode.get("outgoing"); if (outgoingNode != null && outgoingNode.size() > 0) { for (JsonNode outgoingChildNode : outgoingNode) { JsonNode resourceNode = outgoingChildNode.get(EDITOR_SHAPE_ID); if (resourceNode != null && boundaryEventId.equals(resourceNode.asText())) { attachedRefId = BpmnJsonConverterUtil.getElementId(childNode); break; } } if (attachedRefId != null) { break; } } attachedRefId = lookForAttachedRef(boundaryEventId, childNode.get(EDITOR_CHILD_SHAPES)); if (attachedRefId != null) { break; } } } return attachedRefId; }
Example 9
Source File: JsonConverterUtil.java From fixflow with Apache License 2.0 | 5 votes |
/** * 查找ObjectNode下propertyName属性为name的json节点 * @param name * @param propertyName * @param objectNode * @return */ public static JsonNode getChildElementByProperty(String name,String propertyName,ArrayNode objectNode){ JsonNode jsonNode = null; for( int i = 0; i< objectNode.size();i++){ JsonNode childNode = objectNode.get(i); if(childNode.get(propertyName)!=null && name.equals(childNode.get(propertyName).asText())){ return childNode; } } return jsonNode; }
Example 10
Source File: JsonUtils.java From Cubert with Apache License 2.0 | 5 votes |
public static boolean isPrefixArray(ArrayNode pnode, ArrayNode snode) { if (pnode == null || snode == null || pnode.size() > snode.size()) return false; for (int i = 0; i < pnode.size(); i++) { if (!pnode.get(i).getTextValue().equals(snode.get(i).getTextValue())) return false; } return true; }
Example 11
Source File: JsonUtils.java From Cubert with Apache License 2.0 | 5 votes |
public static int getIndexFromArray(ArrayNode anode, JsonNode element) { for (int i = 0; i < anode.size(); i++) { if (anode.get(i) == element) return i; } return -1; }
Example 12
Source File: KubernetesBackUpTask.java From kardio with Apache License 2.0 | 5 votes |
/** * This function returns a Map with key as service name and value as Map with all selector values * @param authToken * @param eVo * @return * @throws Exception */ private static Map<String, Map<String, String>> getServiceLabelValues(String authToken, EnvironmentVO eVo) throws Exception { // TODO Auto-generated method stub String serviceApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_SERVICE_PATH); String serviceJson = RestCommunicationHandler.getResponse(serviceApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken); ObjectMapper mapper = new ObjectMapper(); JsonNode rootNodeServ = mapper.readTree(serviceJson); ArrayNode serviceNode = (ArrayNode) rootNodeServ.get("items"); if(serviceNode == null || serviceNode.size() == 0){ logger.info("No Service is available for the environment : "+eVo.getEnvironmentName()); return null; } Map<String, Map<String, String>> serviceLabelMap = new HashMap<String, Map<String, String>>(); Iterator<JsonNode> serviceIterator = serviceNode.getElements(); while (serviceIterator.hasNext()) { JsonNode appsInNode = serviceIterator.next(); JsonNode servMetadataNode = appsInNode.get("metadata"); JsonNode servNameNode = servMetadataNode.get("name"); String serviceName = servNameNode.getValueAsText(); JsonNode namespaceNode = servMetadataNode.get("namespace"); String namespace = namespaceNode.getValueAsText(); JsonNode specNode = appsInNode.get("spec"); JsonNode selectorNode = specNode.get("selector"); if (namespace.equals("default") || !namespace.contains("-")) { logger.info("Excluding service - " + serviceName + " in the namespace - " + namespace); continue; } Map<String, String> selectorMap = mapper.convertValue(selectorNode, Map.class); serviceLabelMap.put(namespace+"/"+serviceName, selectorMap); } return serviceLabelMap; }
Example 13
Source File: AggregateRewriter.java From Cubert with Apache License 2.0 | 4 votes |
private void incrementalizeInputLoad(ObjectNode programNode, ObjectNode inputNode, ObjectNode cubeOperatorNode, String mvName, String mvPath) throws IOException, AggregateRewriteException { // extract input paths from inputNode and adjust start-date to MV refresh date+1. ArrayNode paths = (ArrayNode) inputNode.get("path"); System.out.println("Incrementalize InputNode = " + inputNode.toString()); int newMvRefreshTime = 0; for (int i = 0; i < paths.size(); i++) { JsonNode pathNode = paths.get(i); if (pathNode instanceof ObjectNode) { String startDate = ((ObjectNode) pathNode).get("startDate").getTextValue(); // System.out.println("startDate = " + startDate); DateTime loadStart = DateTimeUtilities.getDateTime((startDate)); String endDate = ((ObjectNode) pathNode).get("endDate").getTextValue(); DateTime loadEnd = DateTimeUtilities.getDateTime(endDate); if (mvRefreshDate != 0 && incLoadDate != null) { if (loadStart.isBefore(incLoadDate) && loadEnd.isAfter(incLoadDate)) { ((ObjectNode) pathNode).put("origStartDate", startDate); ((ObjectNode) pathNode).put("startDate", Integer.toString(DateTimeUtilities.asInt(incLoadDate))); } else throw new AggregateRewriteException(String.format("MV date range mis-matches load range[%s, %s] ", startDate, endDate)); } newMvRefreshTime = Math.max(Integer.parseInt(((ObjectNode) pathNode).get("endDate") .getTextValue()), newMvRefreshTime); } } System.out.println("Setting MV refresh time for " + mvName + " to " + newMvRefreshTime); mvRefreshMap.put(mvName, newMvRefreshTime); }
Example 14
Source File: TPSLookupTask.java From kardio with Apache License 2.0 | 4 votes |
/**Method to get All the deployment and the match label associated to each deployment. * @param eVo * @param authToken * @return * @throws Exception */ private static Map<String, List<ServiceLabelVO>> getDepServiceLabel(EnvironmentVO eVo, String authToken) throws Exception { String deployementApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_DEPLOYMENT_PATH); String deploymentJson = RestCommunicationHandler.getResponse(deployementApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken); ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(deploymentJson); ArrayNode deploymentNode = (ArrayNode) rootNode.get("items"); if (deploymentNode.size() == 0) { String errorString = "Surveiller task DOK8STPSLOOKUPLOADTASK - FAILED : Enviroment -" + eVo.getEnvironmentName() + " Deployment JSON Cannot be empty"; logger.error(errorString); throw new GeneralException(errorString); } Map<String, List<ServiceLabelVO>> depLabelMap = new HashMap<String, List<ServiceLabelVO>>(); Iterator<JsonNode> deploymentIterator = deploymentNode.getElements(); while (deploymentIterator.hasNext()) { JsonNode appsInNode = deploymentIterator.next(); JsonNode depMetadataNode = appsInNode.get("metadata"); JsonNode depNameNode = depMetadataNode.get("name"); String depName = depNameNode.getValueAsText(); JsonNode namespaceNode = depMetadataNode.get("namespace"); String namespace = namespaceNode.getValueAsText(); JsonNode specNode = appsInNode.get("spec"); JsonNode selectorNode = specNode.get("selector"); if (namespace.equals("default") || !namespace.contains("-")) { logger.info("Excluding deployment - " + depName + " in the namespace - " + namespace); continue; } JsonNode matchLabelsNode = selectorNode.get("matchLabels"); if(matchLabelsNode == null) { logger.info("The matchLabelsNode is null for the deployment - " + depName + " in the namespace - " + namespace); continue; } Map<String, String> labelMap = mapper.convertValue(matchLabelsNode, Map.class); ServiceLabelVO slVo = new ServiceLabelVO(); slVo.setLabel(labelMap); String serviceName = namespace + "/" + depName; slVo.setServiceName(serviceName); String appName = namespace.substring(0, namespace.indexOf("-")); slVo.setAppName(appName); List<ServiceLabelVO> servLblList = null; if (depLabelMap.containsKey(namespace)) { servLblList = depLabelMap.get(namespace); } else { servLblList = new ArrayList<ServiceLabelVO>(); } servLblList.add(slVo); depLabelMap.put(namespace, servLblList); } return depLabelMap; }
Example 15
Source File: TPSLookupTask.java From kardio with Apache License 2.0 | 4 votes |
private static Map<String, String> getAppService(EnvironmentVO eVo, String authToken) throws Exception { // TODO Auto-generated method stub /*Method to get All the deployment and the match label associated to each deployment. * The method will return a Map with namespace as key and value List<ServiceLabelVO> * */ Map<String, List<ServiceLabelVO>> depLabelMap = getDepServiceLabel(eVo, authToken); /*Service Lookup Code*/ String serviceApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_SERVICE_PATH); String serviceJson = RestCommunicationHandler.getResponse(serviceApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken); ObjectMapper mapper = new ObjectMapper(); JsonNode rootNodeServ = mapper.readTree(serviceJson); ArrayNode serviceNode = (ArrayNode) rootNodeServ.get("items"); if (serviceNode.size() == 0) { String errorString = "Surveiller task DOK8STPSLOOKUPLOADTASK - FAILED : Enviroment -" + eVo.getEnvironmentName() + " Service JSON Cannot be empty"; logger.error(errorString); throw new GeneralException(errorString); } Map<String, String> appServiceMap = new HashMap<String, String>(); Iterator<JsonNode> serviceIterator = serviceNode.getElements(); while (serviceIterator.hasNext()) { JsonNode appsInNode = serviceIterator.next(); JsonNode servMetadataNode = appsInNode.get("metadata"); JsonNode servNameNode = servMetadataNode.get("name"); String serviceName = servNameNode.getValueAsText(); JsonNode namespaceNode = servMetadataNode.get("namespace"); String namespace = namespaceNode.getValueAsText(); JsonNode specNode = appsInNode.get("spec"); JsonNode selectorNode = specNode.get("selector"); if (namespace.equals("default") || !namespace.contains("-")) { logger.info("Excluding service - " + serviceName + " in the namespace - " + namespace); continue; } Map<String, String> selectorMap = mapper.convertValue(selectorNode, Map.class); List<ServiceLabelVO> servLblList = depLabelMap.get(namespace); if(servLblList == null){ continue; } for(ServiceLabelVO servLabelVo : servLblList){ if(servLabelVo.getLabel() == null){ continue; } if(servLabelVo.getLabel().equals(selectorMap)){ appServiceMap.put(serviceName, servLabelVo.getServiceName()); } } } return appServiceMap; }
Example 16
Source File: AbstractSiteToSiteReportingTask.java From nifi with Apache License 2.0 | 4 votes |
protected Object getRawNodeValue(final JsonNode fieldNode, final DataType dataType) throws IOException { if (fieldNode == null || fieldNode.isNull()) { return null; } if (fieldNode.isNumber()) { return fieldNode.getNumberValue(); } if (fieldNode.isBinary()) { return fieldNode.getBinaryValue(); } if (fieldNode.isBoolean()) { return fieldNode.getBooleanValue(); } if (fieldNode.isTextual()) { return fieldNode.getTextValue(); } if (fieldNode.isArray()) { final ArrayNode arrayNode = (ArrayNode) fieldNode; final int numElements = arrayNode.size(); final Object[] arrayElements = new Object[numElements]; int count = 0; final DataType elementDataType; if (dataType != null && dataType.getFieldType() == RecordFieldType.ARRAY) { final ArrayDataType arrayDataType = (ArrayDataType) dataType; elementDataType = arrayDataType.getElementType(); } else { elementDataType = null; } for (final JsonNode node : arrayNode) { final Object value = getRawNodeValue(node, elementDataType); arrayElements[count++] = value; } return arrayElements; } if (fieldNode.isObject()) { RecordSchema childSchema; if (dataType != null && RecordFieldType.RECORD == dataType.getFieldType()) { final RecordDataType recordDataType = (RecordDataType) dataType; childSchema = recordDataType.getChildSchema(); } else { childSchema = null; } if (childSchema == null) { childSchema = new SimpleRecordSchema(Collections.emptyList()); } final Iterator<String> fieldNames = fieldNode.getFieldNames(); final Map<String, Object> childValues = new HashMap<>(); while (fieldNames.hasNext()) { final String childFieldName = fieldNames.next(); final Object childValue = getRawNodeValue(fieldNode.get(childFieldName), dataType); childValues.put(childFieldName, childValue); } final MapRecord record = new MapRecord(childSchema, childValues); return record; } return null; }
Example 17
Source File: CountDistinctRewriter.java From Cubert with Apache License 2.0 | 4 votes |
public void rewriteFactBlockgenPath(ObjectNode cubeNode, LineagePath opSequencePath, ObjectNode factNode, Pair<ObjectNode, ObjectNode> bgInfo) throws AggregateRewriteException { // if CREATE_BLOCK is not one of the ultimate or penultimate opNodes, report // rewrite exception ObjectNode bgNode = bgInfo.getSecond(); ArrayNode phaseOps = lineage.getPhaseOperators(lineage.getPhase(bgNode)); int len = phaseOps.size(); if (phaseOps.get(len - 1) != bgNode) throw new AggregateRewriteException("Xformed Fact CREATE_BLOCK should be last operator before store"); /* * boolean formattedDateColumn = false; boolean regenerateAfterJoin = false; * String dateColumnAfterJoin = null; */ getFactTimeSpecNode(factNode, cubeNode); formattedDateColumn = false; dateColumnAlias = tNode.get("dateColumn").getTextValue(); for (LineageGraphVertex opNodeVertex : opSequencePath.nodes) { ObjectNode opNode = ((OperatorLineage) opNodeVertex).node; // if incompatible operator, trigger exception if (incompatibleWithBlockgenPath(opNode)) throw new AggregateRewriteException("Found incompatible operator along fact blockgen path " + opNode); // If generate inject date column if (opNode.get("operator") != null && opNode.get("operator").getTextValue().equalsIgnoreCase("GENERATE")) { ArrayNode generateArgs = (ArrayNode) (opNode.get("outputTuple")); if (!formattedDateColumn) { JsonNode dateColumnNode = generateDateColumn(cubeNode, factNode, bgInfo); generateArgs.add(dateColumnNode); formattedDateColumn = true; } else { generateArgs.add(regenerateDateColumn(dateColumnAlias)); } dateColumnAlias = DATE_COLUMN_NAME; } // If join operator, stash the renamed date column if (lineage.isJoinOperator(opNode)) { BlockSchema joinSchema = new BlockSchema(opNode.get("schema")); String[] colNames = joinSchema.getColumnNames(); String newAlias = dateColumnAlias; String[] joinInputs = JsonUtils.asArray(opNode.get("input")); int factIndex = lineage.getDescendantInputIndex(joinInputs, opNode, factNode); newAlias = joinInputs[factIndex] + "___" + dateColumnAlias; // TODO add an explict generate if the very next statement after JOIN is // not a GENERATE. if (addGenerateAfterJoin(opNode)) continue; dateColumnAlias = newAlias; } // if group by on all columns OR distinct that is either immediately before // or after the CREATE_BLOCK, remove if (opNode.get("operator") != null && (opNode.get("operator") .getTextValue() .equalsIgnoreCase("DISTINCT") || lineage.isDistinctGroupBy(opNode)) && lineage.isParentOperator(opNode, bgInfo.getSecond())) { JsonNode phaseNode = lineage.getPhase(opNode); JsonUtils.deleteFromArrayNode(phaseOps, opNode); // remember new input to bgNode this.factBgInput = opNode.get("input").getTextValue(); bgInfo.getSecond().put("input", factBgInput); } } }
Example 18
Source File: CountDistinctRewriter.java From Cubert with Apache License 2.0 | 4 votes |
private void insertIncrementalMultipleDayGroupBy(ObjectNode programNode, Pair<ObjectNode, ObjectNode> bgInfo) throws AggregateRewriteException { String[] factColumns = lineage.getSchemaOutputColumns(bgInfo.getSecond()); String[] sortKeys; String[] groupByColumns; if (lineage.isBlockgenByIndex(bgInfo.getSecond())) { ObjectNode jobNode = lineage.getOperatorJobNode(bgInfo.getSecond()); sortKeys = JsonUtils.asArray(((ObjectNode) (jobNode.get("shuffle"))).get("pivotKeys")); groupByColumns = (String[]) ArrayUtils.addAll(new String[] { "BLOCK_ID" }, factColumns); } else { sortKeys = JsonUtils.asArray(bgInfo.getSecond().get("pivotKeys")); groupByColumns = factColumns; } // check sort key condition if (!CommonUtils.isPrefix(sortKeys, groupByColumns)) throw new AggregateRewriteException("Blockgen of union fact not sorted by fact collumns"); ArrayNode aggsNode = JsonUtils.createArrayNode(); ArrayNode udafArgsNode = JsonUtils.createArrayNode(); String startDateHyphenated = DateTimeUtilities.getHyphenated(this.factStartDate); udafArgsNode.add(startDateHyphenated); aggsNode.add(RewriteUtils.createObjectNode("type", "USER_DEFINED_AGGREGATION", "udaf", "com.linkedin.cubert.operator.aggregate.PresenceBitmapUDAF", "constructorArgs", udafArgsNode, "input", DATE_COLUMN_NAME, "output", BITMAP_COLUMN_NAME)); String blockgenInputRelation = lineage.getOperatorSources(bgInfo.getSecond()) .get(0) .get("output") .getTextValue(); ObjectNode groupByNode = RewriteUtils.createObjectNode("operator", "GROUP_BY", "input", blockgenInputRelation, "output", blockgenInputRelation, "groupBy", JsonUtils.createArrayNode(groupByColumns), "aggregates", aggsNode); ArrayNode phaseOperators = lineage.getPhaseOperators(lineage.getPhase(bgInfo.getSecond())); int blockGenIndex = 0; for (; blockGenIndex < phaseOperators.size(); blockGenIndex++) { if (phaseOperators.get(blockGenIndex) == bgInfo.getSecond()) break; } if (blockGenIndex == phaseOperators.size()) throw new RuntimeException("Cannot find CREATE_BLOCK operator in phase operator list"); phaseOperators.insert(blockGenIndex, groupByNode); // phaseOperators.insert(blockGenIndex + 1, generateNode); }
Example 19
Source File: KubernetesBackUpTask.java From kardio with Apache License 2.0 | 4 votes |
/** * The function returns all Ingress as a Map, with key as service name & Ingress root node * @param ingressJson * @param eVo * @return * @throws Exception */ private static Map<String, JsonNode> getIngressLookupMap(String authToken, EnvironmentVO eVo) throws Exception { // TODO Auto-generated method stub String ingressApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_INGRESS_PATH); String ingressJson = RestCommunicationHandler.getResponse(ingressApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken); ObjectMapper mapper = new ObjectMapper(); JsonNode ingRootNode = mapper.readTree(ingressJson); ArrayNode ingressArrayNode = (ArrayNode) ingRootNode.get("items"); if(ingressArrayNode == null || ingressArrayNode.size() == 0){ logger.info("No Ingress is available for the environment : "+eVo.getEnvironmentName()); return null; } Iterator<JsonNode> ingressIterator = ingressArrayNode.getElements(); Map<String, JsonNode> ingressMap = new HashMap<String, JsonNode>(); while (ingressIterator.hasNext()) { JsonNode ingressInNode = ingressIterator.next(); JsonNode metadataNode = ingressInNode.get("metadata"); JsonNode nameNode = metadataNode.get("name"); String ingressName = nameNode.getValueAsText(); JsonNode namespaceNode = metadataNode.get("namespace"); String namespace = namespaceNode.getValueAsText(); if (!namespace.contains("-")) { logger.info("Excluding Ingress - " + ingressName + " in the namespace - " + namespace); continue; } /*This particular block of code is to avoid code executions for the applications with multiple ingress * FIXME: Remove if this logic is not necessary * CASE: Services with multiple Ingress * In our case the annotation "kubernetes.io/ingress.class" contains values "nginx-internal" or "nginx-external" * */ JsonNode annotNode = metadataNode.get("annotations"); if(annotNode == null){ logger.info("The annotations node is null for the Ingress - "+ingressName+" in the namespace - "+ namespace); }else{ JsonNode ingClasstNode = annotNode.get("kubernetes.io/ingress.class"); if(ingClasstNode == null || !ingClasstNode.getTextValue().equals("nginx-internal")){ logger.info("The hostname node is "+ingClasstNode+ "for the Ingress - "+ingressName+" in the namespace - "+ namespace); continue; } } /******/ JsonNode specNode = ingressInNode.get("spec"); if (specNode == null) { logger.info("The specNode is null for the ingress - " + ingressName + " in the namespace - "+ namespace); continue; } String serviceName = null; ArrayNode ruleArrayNode = (ArrayNode)specNode.get("rules"); Iterator<JsonNode> ruleNodeItr = ruleArrayNode.getElements(); while (ruleNodeItr.hasNext()) { JsonNode ruleNode = ruleNodeItr.next(); JsonNode httpNode = ruleNode.get("http"); if(httpNode == null){ logger.info("The httpNode is null for the ingress - " + ingressName + " in the namespace - "+ namespace); continue; } ArrayNode pathArrayNode = (ArrayNode)httpNode.get("paths"); Iterator<JsonNode> pathNodeItr = pathArrayNode.getElements(); while (pathNodeItr.hasNext()) { JsonNode pathNode = pathNodeItr.next(); JsonNode backendNode = pathNode.get("backend"); JsonNode serviceNode = backendNode.get("serviceName"); if (serviceNode == null) { logger.info("The serviceNode is null for the ingress - " + ingressName + " in the namespace - "+ namespace); continue; } serviceName = serviceNode.getTextValue(); ingressMap.put(namespace+"/"+serviceName, ingressInNode); } } } return ingressMap; }
Example 20
Source File: K8sAPIDashboardTask.java From kardio with Apache License 2.0 | 4 votes |
/** * Method to get all Deployment and MatchLabels of the given cluster * * @param authToken * @param eVo * @param depLabelMap * */ private static void getDeploymentAndMatchLabels(String authToken, EnvironmentVO eVo, Map<String, List<ServiceLabelVO>> depLabelMap) throws IOException { String deployementApiUrl = eVo.getK8sUrl() + PropertyUtil.getInstance().getValue(SurveillerConstants.K8S_API_DEPLOYMENT_PATH); String deploymentJson = RestCommunicationHandler.getResponse(deployementApiUrl, true, SurveillerConstants.BEARER_TOKEN, authToken); ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(deploymentJson); ArrayNode deploymentNode = (ArrayNode) rootNode.get("items"); if (deploymentNode.size() == 0) { String errorString = "Surveiller task DOK8SAPIDASHBOARDTASK - FAILED : Enviroment -" + eVo.getEnvironmentName() + " Deployment JSON Cannot be empty"; logger.error(errorString); throw new GeneralException(errorString); } Iterator<JsonNode> deploymentIterator = deploymentNode.getElements(); while (deploymentIterator.hasNext()) { JsonNode appsInNode = deploymentIterator.next(); JsonNode depMetadataNode = appsInNode.get("metadata"); JsonNode depNameNode = depMetadataNode.get("name"); String depName = depNameNode.getValueAsText(); JsonNode namespaceNode = depMetadataNode.get("namespace"); String namespace = namespaceNode.getValueAsText(); JsonNode specNode = appsInNode.get("spec"); JsonNode selectorNode = specNode.get("selector"); if (namespace.equals("default") || !namespace.contains("-")) { logger.info("Excluding deployment - " + depName + " in the namespace - " + namespace); continue; } JsonNode matchLabelsNode = selectorNode.get("matchLabels"); if (matchLabelsNode == null) { logger.info("The matchLabelsNode is null for the deployment - " + depName + " in the namespace - " + namespace); continue; } Map<String, String> labelMap = mapper.convertValue(matchLabelsNode, Map.class); ServiceLabelVO slVo = new ServiceLabelVO(); slVo.setLabel(labelMap); String serviceName = namespace + "/" + depName; slVo.setServiceName(serviceName); String appName = namespace.substring(0, namespace.indexOf("-")); slVo.setAppName(appName); List<ServiceLabelVO> servLblList = null; if (depLabelMap.containsKey(namespace)) { servLblList = depLabelMap.get(namespace); } else { servLblList = new ArrayList<ServiceLabelVO>(); } servLblList.add(slVo); depLabelMap.put(namespace, servLblList); } }