Java Code Examples for io.fabric8.kubernetes.client.KubernetesClientException#launderThrowable()

The following examples show how to use io.fabric8.kubernetes.client.KubernetesClientException#launderThrowable() . 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: ClusterOperationsImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public VersionInfo fetchVersion() {
  try {
    Response response = handleVersionGet(versionEndpoint);
    // Handle Openshift 4 version case
    if (HttpURLConnection.HTTP_NOT_FOUND == response.code() && versionEndpoint.equals(OPENSHIFT_VERSION_ENDPOINT)) {
      response.close();
      return fetchOpenshift4Version();
    }

    Map<String, String> myMap = objectMapper.readValue(response.body().string(), HashMap.class);
    return fetchVersionInfoFromResponse(myMap);
  } catch(Exception e) {
    KubernetesClientException.launderThrowable(e);
  }
  return null;
}
 
Example 2
Source File: WatchConnectionManager.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public void close() {
  logger.debug("Force closing the watch {}", this);
  closeEvent(null);
  closeWebSocket(webSocketRef.getAndSet(null));
  if (!executor.isShutdown()) {
    try {
      executor.shutdown();
      if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
        logger.warn("Executor didn't terminate in time after shutdown in close(), killing it in: {}", this);
        executor.shutdownNow();
      }
    } catch (Throwable t) {
      throw KubernetesClientException.launderThrowable(t);
    }
  }
}
 
Example 3
Source File: LogWatchCallback.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public LogWatchCallback(Config config, OutputStream out) {
  this.config = config;
  if (out == null) {
    this.out = new PipedOutputStream();
    this.output = new PipedInputStream();
    toClose.add(this.out);
    toClose.add(this.output);
  } else {
    this.out = out;
    this.output = null;
  }

  //We need to connect the pipe here, because onResponse might not be called in time (if log is empty)
  //This will cause a `Pipe not connected` exception for everyone that tries to read. By always opening
  //the pipe the user will get a ready to use inputstream, which will block until there is actually something to read.
  if (this.out instanceof PipedOutputStream && this.output != null) {
    try {
      this.output.connect((PipedOutputStream) this.out);
    } catch (IOException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }
}
 
Example 4
Source File: RawWatchConnectionManager.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public void close() {
  logger.debug("Force closing the watch {}", this);
  closeEvent(null);
  closeWebSocket(webSocketRef.getAndSet(null));
  if (!executor.isShutdown()) {
    try {
      executor.shutdown();
      if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
        logger.warn("Executor didn't terminate in time after shutdown in close(), killing it in: {}", this);
        executor.shutdownNow();
      }
    } catch (Throwable t) {
      throw KubernetesClientException.launderThrowable(t);
    }
  }
}
 
Example 5
Source File: BaseOperation.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
protected T sendPatchedObject(T oldObject, T updatedObject) {
  try {
    return handlePatch(oldObject, updatedObject);
  } catch (InterruptedException interruptedException) {
    Thread.currentThread().interrupt();
    throw KubernetesClientException.launderThrowable(interruptedException);
  } catch (ExecutionException | IOException e) {
    throw KubernetesClientException.launderThrowable(e);
  }
}
 
Example 6
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static <T> List<HasMetadata> asHasMetadata(T item, Boolean enableProccessing) {
  List<HasMetadata> result = new ArrayList<>();
  if (item instanceof KubernetesList) {
    result.addAll(((KubernetesList) item).getItems());
  } else if (item instanceof Template) {

    if (!enableProccessing) {
      result.addAll(((Template) item).getObjects());
    } else {
      result.addAll(processTemplate((Template)item, false));
    }
  } else if (item instanceof KubernetesResourceList) {
    result.addAll(((KubernetesResourceList) item).getItems());
  } else if (item instanceof HasMetadata) {
    result.add((HasMetadata) item);
  }  else if (item instanceof String) {
    try (InputStream is = new ByteArrayInputStream(((String)item).getBytes(StandardCharsets.UTF_8))) {
      return asHasMetadata(unmarshal(is), enableProccessing);
    } catch (IOException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  } else if (item instanceof Collection) {
    for (Object o : (Collection)item) {
      if (o instanceof HasMetadata) {
        result.add((HasMetadata) o);
      }
    }
  }
  return result;
}
 
Example 7
Source File: ProjectRequestsOperationImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public URL getRootUrl() {
  // This is an OpenShift resource. If no API Group Name is specified, use /oapi endpoint
  if (Utils.isNullOrEmpty(context.getApiGroupName())) {
    try {
      return new URL(OpenShiftConfig.wrap(getConfig()).getOpenShiftUrl());
    } catch (MalformedURLException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  } else {
    return super.getRootUrl();
  }
}
 
Example 8
Source File: SubjectAccessReviewOperationImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public SubjectAccessReviewResponse create(SubjectAccessReview... resources) {
  try {
    if (resources.length > 1) {
      throw new IllegalArgumentException("Too many items to create.");
    } else if (resources.length == 1) {
      return handleCreate(resources[0], SubjectAccessReviewResponse.class);
    } else {
      throw new IllegalArgumentException("Nothing to create.");
    }
  } catch (InterruptedException | ExecutionException | IOException e) {
    throw KubernetesClientException.launderThrowable(e);
  }
}
 
Example 9
Source File: KubernetesListOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public DoneableKubernetesList createNew() {
  return new DoneableKubernetesList(item -> {
    try {
      return create(item);
    } catch (Exception e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  });
}
 
Example 10
Source File: BuildOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  try {
    URL url = new URL(URLUtils.join(getResourceUrl().toString(), getLogParameters() + "&follow=true"));
    Request request = new Request.Builder().url(url).get().build();
    final LogWatchCallback callback = new LogWatchCallback(out);
    OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    clone.newCall(request).enqueue(callback);
    callback.waitUntilReady();
    return callback;
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(forOperationType("watchLog"), t);
  }
}
 
Example 11
Source File: CronJobOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<CronJob, DoneableCronJob> load(InputStream is) {
  try {
    CronJob item = unmarshal(is, CronJob.class);
    return new CronJobOperationsImpl(context.withItem(item));
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(t);
  }
}
 
Example 12
Source File: Serialization.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static <T> String asYaml(T object) throws KubernetesClientException {
  try {
    return YAML_MAPPER.writeValueAsString(object);
  } catch (JsonProcessingException e) {
    throw KubernetesClientException.launderThrowable(e);
  }
}
 
Example 13
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public PortForward portForward(int port, ReadableByteChannel in, WritableByteChannel out) {
  try {
    return new PortForwarderWebsocket(client).forward(getResourceUrl(), port, in, out);
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(t);
  }
}
 
Example 14
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LogWatch watchLog(OutputStream out) {
  try {
    URL url = new URL(URLUtils.join(getResourceUrl().toString(), getLogParameters() + "&follow=true"));
    Request request = new Request.Builder().url(url).get().build();
    final LogWatchCallback callback = new LogWatchCallback(out);
    OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    clone.newCall(request).enqueue(callback);
    callback.waitUntilReady();
    return callback;
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(forOperationType("watchLog"), t);
  }
}
 
Example 15
Source File: TemplateOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public KubernetesList processLocally(File f) {
  try (FileInputStream is = new FileInputStream(f)) {
    return processLocally(is);
  } catch (IOException e) {
    throw KubernetesClientException.launderThrowable(forOperationType("processLocally"), e);
  }
}
 
Example 16
Source File: NodeMetricOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public NodeMetrics metrics(String nodeName) {
	try {
		String resourceUrl = URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL, nodeName);
		return handleMetric(resourceUrl, NodeMetrics.class);
	} catch(Exception e) {
		throw KubernetesClientException.launderThrowable(e);
	}
}
 
Example 17
Source File: SubjectAccessReviewOperationImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public URL getRootUrl() {
  // This is an OpenShift resource. If no API Group Name is specified, use /oapi endpoint
  if (Utils.isNullOrEmpty(context.getApiGroupName())) {
    try {
      return new URL(OpenShiftConfig.wrap(getConfig()).getOpenShiftUrl());
    } catch (MalformedURLException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  } else {
    return super.getRootUrl();
  }
}
 
Example 18
Source File: JobOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScalableResource<Job, DoneableJob> load(InputStream is) {
  try {
    Job item = unmarshal(is, Job.class);
    return new JobOperationsImpl(context.withItem(item));
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(t);
  }
}
 
Example 19
Source File: Serialization.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
/**
 * Unmarshals a {@link String} optionally performing placeholder substitution to the String.
 * @param str         The {@link String}.
 * @param type        The target type.
 * @param <T>         Template argument denoting type
 * @param parameters  A hashmap containing parameters
 *
 * @return returns de-serialized object
 * @throws KubernetesClientException KubernetesClientException
 */
public static <T> T unmarshal(String str, final Class<T> type, Map<String, String> parameters) throws KubernetesClientException {
  try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
    return unmarshal(is, new TypeReference<T>() {
      @Override
      public Type getType() {
        return type;
      }
    }, parameters);
  } catch (IOException e) {
    throw KubernetesClientException.launderThrowable(e);
  }
}
 
Example 20
Source File: NodeMetricOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public NodeMetricsList metrics(Map<String, Object> labelsMap) {
	try {
     HttpUrl.Builder httpUrlBuilder = HttpUrl.get(URLUtils.join(config.getMasterUrl(), METRIC_ENDPOINT_URL)).newBuilder();

     StringBuilder sb = new StringBuilder();
		for(Map.Entry<String, Object> entry : labelsMap.entrySet()) {
			sb.append(entry.getKey()).append("=").append(entry.getValue().toString()).append(",");
		}
     httpUrlBuilder.addQueryParameter("labelSelector", sb.toString().substring(0, sb.toString().length() - 1));
		return handleMetric(httpUrlBuilder.build().toString(), NodeMetricsList.class);
	} catch(Exception e) {
		throw KubernetesClientException.launderThrowable(e);
	}
}