Esempio n. 1
0
 @Override
 public Boolean delete() {
   if (name != null && !name.isEmpty()) {
     try {
       if (cascading && !isReaping()) {
         Reaper reaper = ReaperFactory.getReaper(this);
         if (reaper != null) {
           setReaping(true);
           reaper.reap();
         }
       }
       deleteThis();
       return true;
     } catch (KubernetesClientException e) {
       if (e.getCode() != 404) {
         throw e;
       }
       return false;
     }
   } else {
     try {
       deleteList();
       return true;
     } catch (KubernetesClientException e) {
       if (e.getCode() != 404) {
         throw e;
       }
       return false;
     }
   }
 }
Esempio n. 2
0
 public RootPaths getRootPaths() {
   try {
     URL requestUrl = client.getMasterUrl();
     Future<Response> f = getClient().getHttpClient().prepareGet(requestUrl.toString()).execute();
     Response r = f.get();
     assertResponseCode(r, 200);
     return OBJECT_MAPPER.readValue(r.getResponseBodyAsStream(), RootPaths.class);
   } catch (KubernetesClientException e) {
     if (e.getCode() != 404) {
       throw e;
     }
     return null;
   } catch (InterruptedException | ExecutionException | IOException e) {
     throw KubernetesClientException.launderThrowable(e);
   }
 }
  @Override
  public DoneableReplicationController edit() {
    if (!rolling) {
      return super.edit();
    }

    final Visitor<ReplicationController> visitor =
        new Visitor<ReplicationController>() {
          @Override
          public void visit(ReplicationController rc) {
            try {
              new RollingUpdater(client, config, namespace).rollUpdate(getMandatory(), rc);
            } catch (Exception e) {
              throw KubernetesClientException.launderThrowable(e);
            }
          }
        };

    try {
      return getDoneableType()
          .getDeclaredConstructor(getType(), Visitor.class)
          .newInstance(get(), visitor);
    } catch (InvocationTargetException
        | NoSuchMethodException
        | IllegalAccessException
        | InstantiationException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }
  private String authorize() {
    try {
      OkHttpClient.Builder builder = client.newBuilder();
      builder.interceptors().remove(this);
      OkHttpClient clone = builder.build();

      String credential = Credentials.basic(config.getUsername(), new String(config.getPassword()));
      URL url = new URL(URLUtils.join(config.getMasterUrl(), AUTHORIZE_PATH));
      Response response =
          clone
              .newCall(
                  new Request.Builder().get().url(url).header(AUTHORIZATION, credential).build())
              .execute();

      response.body().close();
      response = response.priorResponse() != null ? response.priorResponse() : response;
      response = response.networkResponse() != null ? response.networkResponse() : response;
      String token = response.header(LOCATION);
      if (token == null || token.isEmpty()) {
        throw new KubernetesClientException(
            "Unexpected response ("
                + response.code()
                + " "
                + response.message()
                + "), to the authorization request. Missing header:["
                + LOCATION
                + "]!");
      }
      token = token.substring(token.indexOf(BEFORE_TOKEN) + BEFORE_TOKEN.length());
      token = token.substring(0, token.indexOf(AFTER_TOKEN));
      return token;
    } catch (Exception e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }
Esempio n. 5
0
 @Override
 public Boolean delete(List<T> items) {
   try {
     for (T item : items) {
       handleDelete(item);
     }
   } catch (KubernetesClientException e) {
     if (e.getCode() != 404) {
       throw e;
     }
     return false;
   } catch (InterruptedException | ExecutionException | IOException e) {
     throw KubernetesClientException.launderThrowable(e);
   }
   return true;
 }
 public URL getRootUrl() {
   try {
     return new URL(
         URLUtils.join(config.getMasterUrl().toString(), "api", config.getApiVersion()));
   } catch (MalformedURLException e) {
     throw KubernetesClientException.launderThrowable(e);
   }
 }
Esempio n. 7
0
 @Override
 public EditReplaceDeletable<T, T, D, Boolean> cascading(boolean enabled) {
   try {
     return getClass()
         .getConstructor(Client.class, String.class, String.class, Boolean.class, type)
         .newInstance(client, namespace, name, enabled, item);
   } catch (Throwable t) {
     throw KubernetesClientException.launderThrowable(t);
   }
 }
Esempio n. 8
0
 @Override
 public ClientNonNamespaceOperation<C, T, L, D, R> inNamespace(String namespace) {
   try {
     return getClass()
         .getConstructor(Client.class, String.class, String.class, Boolean.class, type)
         .newInstance(client, namespace, name, cascading, item);
   } catch (Throwable t) {
     throw KubernetesClientException.launderThrowable(t);
   }
 }
Esempio n. 9
0
 void deleteThis() throws KubernetesClientException {
   try {
     if (item != null) {
       handleDelete(item);
     } else {
       handleDelete(getResourceUrl());
     }
   } catch (Exception e) {
     throw KubernetesClientException.launderThrowable(e);
   }
 }
Esempio n. 10
0
 @Override
 public R load(InputStream is) {
   try {
     return (R)
         getClass()
             .getConstructor(Client.class, String.class, String.class, Boolean.class, type)
             .newInstance(client, namespace, name, cascading, client.unmarshal(is, type));
   } catch (Throwable t) {
     throw KubernetesClientException.launderThrowable(t);
   }
 }
Esempio n. 11
0
 @Override
 public T get() {
   if (item != null) {
     return item;
   }
   try {
     URL requestUrl = getNamespacedUrl();
     if (name != null) {
       requestUrl = new URL(URLUtils.join(requestUrl.toString(), name));
     }
     return handleGet(requestUrl);
   } catch (KubernetesClientException e) {
     if (e.getCode() != 404) {
       throw e;
     }
     return null;
   } catch (InterruptedException | ExecutionException | IOException e) {
     throw KubernetesClientException.launderThrowable(e);
   }
 }
Esempio n. 12
0
 public Watch watch(String resourceVersion, final Watcher<T> watcher)
     throws KubernetesClientException {
   try {
     return new WatchConnectionManager<>(
         this,
         resourceVersion,
         watcher,
         client.getConfiguration().getWatchReconnectInterval(),
         client.getConfiguration().getWatchReconnectLimit());
   } catch (MalformedURLException | InterruptedException | ExecutionException e) {
     throw KubernetesClientException.launderThrowable(e);
   }
 }
Esempio n. 13
0
 @Override
 public T create(T... resources) throws KubernetesClientException {
   try {
     if (resources.length > 1) {
       throw new IllegalArgumentException("Too many items to create.");
     } else if (resources.length == 1) {
       return handleCreate(resources[0]);
     } else if (getItem() == null) {
       throw new IllegalArgumentException("Nothing to create.");
     } else {
       return handleCreate(getItem());
     }
   } catch (InterruptedException | ExecutionException | IOException e) {
     throw KubernetesClientException.launderThrowable(e);
   }
 }
  protected <T> T unmarshal(InputStream is, Class<T> type) throws KubernetesClientException {
    try (BufferedInputStream bis = new BufferedInputStream(is)) {
      bis.mark(-1);
      int intch;
      do {
        intch = bis.read();
      } while (intch > -1 && Character.isWhitespace(intch));
      bis.reset();

      ObjectMapper mapper = JSON_MAPPER;
      if (intch != '{') {
        mapper = YAML_MAPPER;
      }
      return mapper.readValue(bis, type);
    } catch (IOException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }
 @Override
 public ClientRollableScallableResource<ReplicationController, DoneableReplicationController> load(
     InputStream is) {
   try {
     ReplicationController item = unmarshal(is, ReplicationController.class);
     return new ReplicationControllerOperationsImpl(
         client,
         getConfig(),
         getAPIVersion(),
         getNamespace(),
         getName(),
         isCascading(),
         item,
         getResourceVersion(),
         getReloadingFromServer(),
         rolling,
         rollingTimeout,
         rollingTimeUnit);
   } catch (Throwable t) {
     throw KubernetesClientException.launderThrowable(t);
   }
 }
Esempio n. 16
0
  @Override
  public D createNew() throws KubernetesClientException {
    final Visitor<T> visitor =
        new Visitor<T>() {
          @Override
          public void visit(T resource) {
            try {
              create(resource);
            } catch (Exception e) {
              throw KubernetesClientException.launderThrowable(e);
            }
          }
        };

    try {
      return getDoneableType().getDeclaredConstructor(Visitor.class).newInstance(visitor);
    } catch (InvocationTargetException
        | NoSuchMethodException
        | IllegalAccessException
        | InstantiationException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }
Esempio n. 17
0
  public L list() throws KubernetesClientException {
    try {
      URL requestUrl = getNamespacedUrl();
      AsyncHttpClient.BoundRequestBuilder requestBuilder =
          getClient().getHttpClient().prepareGet(requestUrl.toString());

      String labelQueryParam = getLabelQueryParam();
      if (labelQueryParam.length() > 0) {
        requestBuilder.addQueryParam("labelSelector", labelQueryParam);
      }

      String fieldQueryString = getFieldQueryParam();
      if (fieldQueryString.length() > 0) {
        requestBuilder.addQueryParam("fieldSelector", fieldQueryString);
      }

      Future<Response> f = requestBuilder.execute();
      Response r = f.get();
      assertResponseCode(r, 200);
      return OBJECT_MAPPER.readValue(r.getResponseBodyAsStream(), listType);
    } catch (InterruptedException | ExecutionException | IOException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }