public static Optional getOrCreateSecondaryResource()

in spark-operator/src/main/java/org/apache/spark/k8s/operator/utils/ReconcilerUtils.java [74:112]


  public static <T extends HasMetadata> Optional<T> getOrCreateSecondaryResource(
      final KubernetesClient client, final T resource) {
    Optional<T> current = getResource(client, resource);
    if (current.isEmpty()) {
      // Adding retry logic to overcome known k8s issue:
      // https://github.com/kubernetes/kubernetes/issues/67761
      long maxAttempts = API_SECONDARY_RESOURCE_CREATE_MAX_ATTEMPTS.getValue();
      long attemptCount = 1;
      while (true) {
        try {
          current = Optional.ofNullable(client.resource(resource).create());
          break;
        } catch (KubernetesClientException e) {
          if (log.isErrorEnabled()) {
            log.error(
                "Failed to request resource with responseCode={} attemptCount={}/{}",
                e.getCode(),
                attemptCount,
                maxAttempts);
          }
          if (e.getCode() == HTTP_CONFLICT) {
            if (isConflictForExistingResource(e)) {
              current = getResource(client, resource);
              if (current.isPresent()) {
                return current;
              }
            }
            if (++attemptCount > maxAttempts) {
              log.error("Max Retries exceeded while trying to create resource");
              throw e;
            }
          } else {
            throw e;
          }
        }
      }
    }
    return current;
  }