private void processPodState(Operation get, PodState podState) { Map<String, LogServiceState> resultLogs = new ConcurrentHashMap<>(); logInfo("Getting logs for following containers: %s", podState.pod.spec.containers.stream() .map(c -> c.name).collect(Collectors.toList())); String podLogLink = KubernetesUtil.buildLogUriPath(podState, container.name); sendRequest(Operation.createGet(this, podLogLink) .setCompletion((o, ex) -> { if (ex != null) { logWarning("Couldn't get logs for container: %s, reason: %s", podLogLink, Utils.toString(ex)); LogServiceState log = new LogServiceState(); } else { LogServiceState log = o.getBody(LogServiceState.class); logInfo("Fetched log for container: %s, log: %s", container.name, new String(log.logs)); resultLogs.put(container.name, log); get.setBody(resultLogs); get.complete(); createAdapterRequest(podState);
private void createAdapterRequest(PodState pod) { AdapterRequest request = new AdapterRequest(); request.resourceReference = UriUtils.buildPublicUri(getHost(), pod.documentSelfLink); request.operationTypeId = KubernetesOperationType.FETCH_LOGS.id; request.serviceTaskCallback = ServiceTaskCallback.createEmpty(); sendRequest(Operation.createPatch(getHost(), ManagementUriParts.ADAPTER_KUBERNETES) .setBody(request) .setContextId(Service.getId(getSelfLink())) .setCompletion((op, ex) -> { if (ex != null) { logWarning("Adapter request for container logs %s failed. Error: %s", pod.documentSelfLink, Utils.toString(ex)); } })); }
@Override public void handleGet(Operation get) { Map<String, String> params = UriUtils.parseUriQueryParams(get.getUri()); String podId = params.remove(POD_ID_QUERY_PARAM); if (podId == null || podId.isEmpty()) { get.fail(new IllegalArgumentException( "URL parameter 'id' expected with container id as value.")); return; } String podStateLink = UriUtils.buildUriPath(PodFactoryService.SELF_LINK, podId); sendRequest(Operation .createGet(this, podStateLink) .setCompletion((o, ex) -> { if (ex != null) { get.fail(ex); } else { PodState podState = o.getBody(PodState.class); processPodState(get, podState); } })); }