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

The following examples show how to use io.fabric8.kubernetes.client.KubernetesClientException#getMessage() . 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: OpenShiftProjectFactory.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected List<KubernetesNamespaceMeta> fetchNamespaces() throws InfrastructureException {
  try {
    return clientFactory
        .createOC()
        .projects()
        .list()
        .getItems()
        .stream()
        .map(this::asNamespaceMeta)
        .collect(Collectors.toList());
  } catch (KubernetesClientException e) {
    throw new InfrastructureException(
        "Error occurred when tried to list all available projects. Cause: " + e.getMessage(), e);
  }
}
 
Example 2
Source File: KubernetesNamespaceFactory.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Fetched namespace from a k8s cluster.
 *
 * @return list with available k8s namespace metas.
 * @throws InfrastructureException when any error occurs during namespaces fetching
 */
protected List<KubernetesNamespaceMeta> fetchNamespaces() throws InfrastructureException {
  try {
    return clientFactory
        .create()
        .namespaces()
        .list()
        .getItems()
        .stream()
        .map(this::asNamespaceMeta)
        .collect(Collectors.toList());
  } catch (KubernetesClientException e) {
    throw new InfrastructureException(
        "Error occurred when tried to list all available namespaces. Cause: " + e.getMessage(),
        e);
  }
}
 
Example 3
Source File: BaseOperation.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public T require() throws ResourceNotFoundException {
  try {
    T answer = getMandatory();
    if (answer == null) {
      throw new ResourceNotFoundException("The resource you request doesn't exist or couldn't be fetched.");
    }
    if (answer instanceof HasMetadata) {
      HasMetadata hasMetadata = (HasMetadata) answer;
      updateApiVersion(hasMetadata);
    } else if (answer instanceof KubernetesResourceList) {
      KubernetesResourceList list = (KubernetesResourceList) answer;
      updateApiVersion(list);
    }
    return answer;
  } catch (KubernetesClientException e) {
    if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
      throw e;
    }
    throw new ResourceNotFoundException("Resource not found : " + e.getMessage());
  }
}
 
Example 4
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static void handleKubernetesClientException(KubernetesClientException e, KitLogger logger) {
    Throwable cause = e.getCause();
    if (cause instanceof UnknownHostException) {
        logger.error( "Could not connect to kubernetes cluster!");
        logger.error( "Connection error: %s", cause);

        String message = "Could not connect to kubernetes cluster. Have you started a cluster via `mvn jkube:cluster-start` or connected to a remote cluster via `kubectl`? Error: " + cause;
        throw new IllegalStateException(message, e);
    } else {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 5
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static void handleKubernetesClientException(KubernetesClientException e, KitLogger logger) throws IllegalStateException {
    Throwable cause = e.getCause();
    if (cause instanceof UnknownHostException) {
        logger.error( "Could not connect to kubernetes cluster!");
        logger.error("Have you started a local cluster via `mvn jkube:cluster-start` or connected to a remote cluster via `kubectl`?");
        logger.info("For more help see: http://jkube.io/guide/getStarted/");
        logger.error( "Connection error: %s", cause);

        String message = "Could not connect to kubernetes cluster. Have you started a cluster via `minikube start` or connected to a remote cluster via `kubectl`? Error: " + cause;
        throw new IllegalStateException(message, e);
    } else {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 6
Source File: KubernetesRecipeParser.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Parses Kubernetes objects from recipe content.
 *
 * @param recipeContent recipe content that should be parsed
 * @return parsed objects
 * @throws IllegalArgumentException if recipe content is null
 * @throws ValidationException if recipe content has broken format
 * @throws ValidationException if recipe content has unrecognized objects
 * @throws InfrastructureException when exception occurred during kubernetes client creation
 */
public List<HasMetadata> parse(String recipeContent)
    throws ValidationException, InfrastructureException {
  checkNotNull(recipeContent, "Recipe content type must not be null");

  try {
    // Behavior:
    // - If `content` is a single object like Deployment, load().get() will get the object in that
    // list
    // - If `content` is a Kubernetes List, load().get() will get the objects in that list
    // - If `content` is an OpenShift template, load().get() will get the objects in the template
    //   with parameters substituted (e.g. with default values).
    List<HasMetadata> parsed =
        clientFactory.create().load(new ByteArrayInputStream(recipeContent.getBytes())).get();

    // needed because Che master namespace is set by K8s API during list loading
    parsed
        .stream()
        .filter(o -> o.getMetadata() != null)
        .forEach(o -> o.getMetadata().setNamespace(null));

    return parsed;
  } catch (KubernetesClientException e) {
    // KubernetesClient wraps the error when a JsonMappingException occurs so we need the cause
    String message = e.getCause() == null ? e.getMessage() : e.getCause().getMessage();
    if (message.contains("\n")) {
      // Clean up message if it comes from JsonMappingException. Format is e.g.
      // `No resource type found for:v1#Route1\n at [...]`
      message = message.split("\\n", 2)[0];
    }
    throw new ValidationException(format("Could not parse Kubernetes recipe: %s", message));
  }
}
 
Example 7
Source File: KubernetesInfrastructureException.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static String extractMessage(KubernetesClientException e) {
  int code = e.getCode();
  if (code == 401 || code == 403) {
    return e.getMessage()
        + " The error may be caused by an expired token or changed password. "
        + "Update Che server deployment with a new token or password.";
  } else {
    return e.getMessage();
  }
}
 
Example 8
Source File: KubernetesNamespaceFactory.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Fetches the specified namespace from a cluster.
 *
 * @param name name of namespace that should be fetched.
 * @return optional with kubernetes namespace meta
 * @throws InfrastructureException when any error occurs during namespace fetching
 */
protected Optional<KubernetesNamespaceMeta> fetchNamespace(String name)
    throws InfrastructureException {
  try {
    Namespace namespace = clientFactory.create().namespaces().withName(name).get();
    if (namespace == null) {
      return Optional.empty();
    } else {
      return Optional.of(asNamespaceMeta(namespace));
    }
  } catch (KubernetesClientException e) {
    throw new InfrastructureException(
        "Error occurred when tried to fetch default namespace. Cause: " + e.getMessage(), e);
  }
}