io.fabric8.kubernetes.client.ResourceHandler Java Examples

The following examples show how to use io.fabric8.kubernetes.client.ResourceHandler. 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: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public HasMetadata createOrReplace() {
  HasMetadata meta = acceptVisitors(asHasMetadata(item), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  HasMetadata r = h.reload(client, config, meta.getMetadata().getNamespace(), meta);
  String namespaceToUse = meta.getMetadata().getNamespace();

  if (r == null) {
    return h.create(client, config, namespaceToUse, meta);
  } else if (deletingExisting) {
    Boolean deleted = h.delete(client, config, namespaceToUse, propagationPolicy, meta);
    if (!deleted) {
      throw new KubernetesClientException("Failed to delete existing item:" + meta);
    }
    return h.create(client, config, namespaceToUse, meta);
  } else if (ResourceCompare.equals(r, meta)) {
    LOGGER.debug("Item has not changed. Skipping");
    return meta;
  } else {
    return h.replace(client, config, namespaceToUse, meta);
  }
}
 
Example #2
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public HasMetadata get() {
  if (fromServer) {
    HasMetadata meta = acceptVisitors(asHasMetadata(item), visitors);
    ResourceHandler<HasMetadata, ? extends VisitableBuilder> h = handlerOf(meta);
    HasMetadata reloaded = h.reload(client, config, meta.getMetadata().getNamespace(), meta);
    if (reloaded != null) {
      HasMetadata edited = reloaded;
      //Let's apply any visitor that might have been specified.
      for (Visitor v : visitors) {
        edited = h.edit(edited).accept(v).build();
      }
      return edited;
    }
    return null;
  } else {
    return acceptVisitors(asHasMetadata(item), visitors);
  }
}
 
Example #3
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public List<HasMetadata> get() {
    if (fromServer) {
        List<HasMetadata> result = new ArrayList<>();
        for (HasMetadata meta : acceptVisitors(asHasMetadata(item, true), visitors)) {
            ResourceHandler<HasMetadata, ? extends VisitableBuilder> h = handlerOf(meta);
            HasMetadata reloaded = h.reload(client, config, meta.getMetadata().getNamespace(), meta);
            if (reloaded != null) {
                HasMetadata edited = reloaded;
                //Let's apply any visitor that might have been specified.
                for (Visitor v : visitors) {
                    h.edit(edited).accept(v).build();
                }
                result.add(reloaded);
            }
        }
        return result;
    } else {
        return acceptVisitors(asHasMetadata(item, true), visitors);
    }
}
 
Example #4
Source File: KubernetesListOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean delete(List<KubernetesList> lists) {
  for (KubernetesList list : lists) {
    for (HasMetadata item : list.getItems()) {
      ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind(), item.getApiVersion());
      if (!handler.delete(client, config, namespace, DeletionPropagation.BACKGROUND, item)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #5
Source File: KubernetesListHandler.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public KubernetesList reload(OkHttpClient client, Config config, String namespace, KubernetesList item) {
  List<HasMetadata> replacedItems = new ArrayList<>();

  for (HasMetadata metadata : item.getItems()) {
    ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind(), item.getApiVersion());
    if (handler == null) {
      LOGGER.warn("No handler found for:" + item.getKind() + ". Ignoring");
    } else {
      replacedItems.add(handler.reload(client, config, namespace, metadata));
    }
  }
  return new KubernetesListBuilder(item).withItems(replacedItems).build();
}
 
Example #6
Source File: KubernetesListHandler.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public KubernetesList replace(OkHttpClient client, Config config, String namespace, KubernetesList item) {
  List<HasMetadata> replacedItems = new ArrayList<>();

  for (HasMetadata metadata : item.getItems()) {
    ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind(), item.getApiVersion());
    if (handler == null) {
      LOGGER.warn("No handler found for:" + item.getKind() + ". Ignoring");
    } else {
      replacedItems.add(handler.replace(client, config, namespace, metadata));
    }
  }
  return new KubernetesListBuilder(item).withItems(replacedItems).build();
}
 
Example #7
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static <T> ResourceHandler handlerOf(T item) {
  if (item instanceof HasMetadata) {
    return Handlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind(), ((HasMetadata) item).getApiVersion());
  } else if (item instanceof KubernetesList) {
    return new KubernetesListHandler();
  }  else {
    throw new IllegalArgumentException("Could not find a registered handler for item: [" + item + "].");
  }
}
 
Example #8
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static List<HasMetadata> acceptVisitors(List<HasMetadata> list, List<Visitor> visitors) {
    List<HasMetadata> result = new ArrayList<>();
    for (HasMetadata item : list) {
        ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(item);
        VisitableBuilder<HasMetadata, ?> builder = h.edit(item);

        //Let's apply any visitor that might have been specified.
        for (Visitor v : visitors) {
            builder.accept(v);
        }
        result.add(builder.build());
    }
    return result;
}
 
Example #9
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static <T> ResourceHandler handlerOf(T item) {
  if (item instanceof HasMetadata) {
    return Handlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind(), ((HasMetadata) item).getApiVersion());
  } else if (item instanceof KubernetesList) {
    return new KubernetesListHandler();
  } else {
    throw new IllegalArgumentException("Could not find a registered handler for item: [" + item + "].");
  }
}
 
Example #10
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static List<HasMetadata> acceptVisitors(List<HasMetadata> list, List<Visitor> visitors) {
  List<HasMetadata> result = new ArrayList<>();
  for (HasMetadata item : list) {
    ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(item);
    VisitableBuilder<HasMetadata, ?> builder = h.edit(item);

    //Let's apply any visitor that might have been specified.
    for (Visitor v : visitors) {
      builder.accept(v);
    }
    result.add(builder.build());
  }
  return result;
}
 
Example #11
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private static HasMetadata acceptVisitors(HasMetadata item, List<Visitor> visitors) {
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(item);
  VisitableBuilder<HasMetadata, ?> builder = h.edit(item);

  //Let's apply any visitor that might have been specified.
  for (Visitor v : visitors) {
    builder.accept(v);
  }
  return builder.build();
}
 
Example #12
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public HasMetadata waitUntilCondition(Predicate<HasMetadata> condition, long amount,
  TimeUnit timeUnit) throws InterruptedException {
  HasMetadata meta = acceptVisitors(asHasMetadata(get()), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  return h.waitUntilCondition(client, config, meta.getMetadata().getNamespace(), meta, condition, amount, timeUnit);
}
 
Example #13
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean delete() {
  //First pass check before deleting
  HasMetadata meta = acceptVisitors(asHasMetadata(item), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  return h.delete(client, config, meta.getMetadata().getNamespace(), propagationPolicy, meta);
}
 
Example #14
Source File: KubernetesListOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private <T extends HasMetadata, V extends VisitableBuilder<T, V>> T create(T resource) {
  ResourceHandler<T, V> handler = Handlers.get(resource.getKind(), resource.getApiVersion());
  if (handler != null) {
    return handler.create(client, config, namespace, resource);
  }
  throw new IllegalStateException("Could not find handler");
}
 
Example #15
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public HasMetadata waitUntilReady(long amount, TimeUnit timeUnit) throws InterruptedException {
  HasMetadata meta = acceptVisitors(asHasMetadata(get()), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  return h.waitUntilReady(client, config, meta.getMetadata().getNamespace(), meta, amount, timeUnit);
}
 
Example #16
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Watch watch(ListOptions options, Watcher<HasMetadata> watcher) {
  HasMetadata meta = acceptVisitors(asHasMetadata(item), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  return h.watch(client, config, meta.getMetadata().getNamespace(), meta, options, watcher);
}
 
Example #17
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Watch watch(String resourceVersion, Watcher<HasMetadata> watcher) {
  HasMetadata meta = acceptVisitors(asHasMetadata(item), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  return h.watch(client, config, meta.getMetadata().getNamespace(), meta, resourceVersion, watcher);
}
 
Example #18
Source File: NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public Watch watch(Watcher<HasMetadata> watcher) {
  HasMetadata meta = acceptVisitors(asHasMetadata(item), visitors);
  ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> h = handlerOf(meta);
  return h.watch(client, config, meta.getMetadata().getNamespace(), meta, watcher);
}
 
Example #19
Source File: ManagedKubernetesClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
public void bindResourceHandler(ResourceHandler resourceHandler) {
  Handlers.register(resourceHandler);
}
 
Example #20
Source File: ManagedKubernetesClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
public void unbindResourceHandler(ResourceHandler resourceHandler) {
  Handlers.unregister(resourceHandler);
}
 
Example #21
Source File: HandlersTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
private void checkHandler(HasMetadata hasMetadata, ResourceHandler handler) {
  assertEquals(hasMetadata.getKind().toLowerCase(Locale.ROOT), handler.getKind().toLowerCase(Locale.ROOT));
  assertEquals(hasMetadata.getApiVersion().toLowerCase(Locale.ROOT), handler.getApiVersion().toLowerCase(Locale.ROOT));
}