use of io.fabric8.kubernetes.client.informers.SharedInformerFactory in project kubernetes-client by fabric8io.
the class SharedInformerFactoryImplTest method testSharedIndexInformerForCustomResourceNoType.
@Test
void testSharedIndexInformerForCustomResourceNoType() {
// Given
SharedInformerFactory sharedInformerFactory = new SharedInformerFactoryImpl(mockBaseClient, executorService);
CustomResourceDefinitionContext context = new CustomResourceDefinitionContext.Builder().withKind("Dummy").withScope("Namespaced").withVersion("v1").withGroup("demos.fabric8.io").withPlural("dummies").build();
// When
SharedIndexInformer<GenericKubernetesResource> informer = sharedInformerFactory.inNamespace("ns1").sharedIndexInformerForCustomResource(context, 10 * 1000L);
// Then
assertThat(informer).isNotNull();
}
use of io.fabric8.kubernetes.client.informers.SharedInformerFactory in project kubernetes-client by fabric8io.
the class SharedInformerExample method main.
public static void main(String[] args) throws InterruptedException {
try (final KubernetesClient client = new KubernetesClientBuilder().build()) {
SharedInformerFactory sharedInformerFactory = client.informers();
SharedIndexInformer<Pod> podInformer = sharedInformerFactory.sharedIndexInformerFor(Pod.class, 30 * 1000L);
logger.info("Informer factory initialized.");
podInformer.addEventHandler(new ResourceEventHandler<Pod>() {
@Override
public void onAdd(Pod pod) {
logger.info("{} pod added", pod.getMetadata().getName());
}
@Override
public void onUpdate(Pod oldPod, Pod newPod) {
logger.info("{} pod updated", oldPod.getMetadata().getName());
}
@Override
public void onDelete(Pod pod, boolean deletedFinalStateUnknown) {
logger.info("{} pod deleted", pod.getMetadata().getName());
}
});
logger.info("Starting all registered informers");
sharedInformerFactory.startAllRegisteredInformers();
Pod testPod = new PodBuilder().withNewMetadata().withName(POD_NAME).withLabels(Collections.singletonMap("app", POD_NAME)).endMetadata().withNewSpec().addNewContainer().withName("myapp-container").withImage("busybox:1.28").withCommand("sh", "-c", "echo The app is running!; sleep 10").endContainer().addNewInitContainer().withName("init-myservice").withImage("busybox:1.28").withCommand("sh", "-c", "echo inititalizing...; sleep 5").endInitContainer().endSpec().build();
client.pods().inNamespace("default").create(testPod);
logger.info("Pod created");
Thread.sleep(3000L);
Lister<Pod> podLister = new Lister<>(podInformer.getIndexer(), "default");
Pod myPod = podLister.get(POD_NAME);
logger.info("PodLister has {}", podLister.list().size());
if (myPod != null) {
logger.info("***** myapp-pod created {}", myPod.getMetadata().getCreationTimestamp());
}
// Wait for some time now
TimeUnit.MINUTES.sleep(3);
sharedInformerFactory.stopAllRegisteredInformers();
}
}
use of io.fabric8.kubernetes.client.informers.SharedInformerFactory in project kubernetes-client by fabric8io.
the class CustomResourceInformerExample method main.
public static void main(String[] args) {
try (KubernetesClient client = new KubernetesClientBuilder().build()) {
SharedInformerFactory sharedInformerFactory = client.informers();
SharedIndexInformer<Dummy> podInformer = sharedInformerFactory.sharedIndexInformerForCustomResource(Dummy.class, 60 * 1000L);
logger.info("Informer factory initialized.");
podInformer.addEventHandler(new ResourceEventHandler<Dummy>() {
@Override
public void onAdd(Dummy pod) {
logger.info("{} dummy added", pod.getMetadata().getName());
}
@Override
public void onUpdate(Dummy oldPod, Dummy newPod) {
logger.info("{} dummy updated", oldPod.getMetadata().getName());
}
@Override
public void onDelete(Dummy pod, boolean deletedFinalStateUnknown) {
logger.info("{} dummy deleted", pod.getMetadata().getName());
}
});
sharedInformerFactory.addSharedInformerEventListener(ex -> logger.error("Exception occurred, but caught: {}", ex.getMessage()));
logger.info("Starting all registered informers");
sharedInformerFactory.startAllRegisteredInformers();
Executors.newSingleThreadExecutor().submit(() -> {
Thread.currentThread().setName("HAS_SYNCED_THREAD");
try {
for (; ; ) {
logger.info("podInformer.hasSynced() : {}", podInformer.hasSynced());
Thread.sleep(10 * 1000L);
}
} catch (InterruptedException inEx) {
Thread.currentThread().interrupt();
logger.warn("HAS_SYNCED_THREAD interrupted: {}", inEx.getMessage());
}
});
final Dummy toCreate = new Dummy();
toCreate.getMetadata().setName("dummy");
if (client.getConfiguration().getNamespace() != null) {
toCreate.getMetadata().setNamespace(client.getConfiguration().getNamespace());
} else if (client.getNamespace() != null) {
toCreate.getMetadata().setNamespace(client.getNamespace());
} else {
toCreate.getMetadata().setNamespace(client.namespaces().list().getItems().stream().findFirst().map(HasMetadata::getMetadata).map(ObjectMeta::getNamespace).orElse("default"));
}
client.customResources(Dummy.class).createOrReplace(toCreate);
// Wait for some time now
TimeUnit.MINUTES.sleep(5);
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
logger.warn("interrupted: {}", interruptedException.getMessage());
}
}
use of io.fabric8.kubernetes.client.informers.SharedInformerFactory in project kubernetes-client by fabric8io.
the class DefaultSharedIndexInformerIT method testPodSharedIndexInformerGetsEvents.
@Test
public void testPodSharedIndexInformerGetsEvents() throws InterruptedException {
// Given
CountDownLatch addEvents = new CountDownLatch(1);
CountDownLatch updateEvents = new CountDownLatch(0);
SharedInformerFactory informerFactory = client.informers();
SharedIndexInformer<Pod> podInformer = informerFactory.sharedIndexInformerFor(Pod.class, RESYNC_PERIOD);
podInformer.addEventHandler(new TestResourceEventHandler<>(addEvents, updateEvents));
// When
informerFactory.startAllRegisteredInformers();
addEvents.await(2 * RESYNC_PERIOD, TimeUnit.MILLISECONDS);
// Then
assertThat(addEvents.getCount()).isZero();
informerFactory.stopAllRegisteredInformers();
}
use of io.fabric8.kubernetes.client.informers.SharedInformerFactory in project integ-examples by fvaleri.
the class CakeOperator method main.
public static void main(String[] args) {
try (KubernetesClient client = new DefaultKubernetesClient()) {
String namespace = client.getNamespace();
if (namespace == null) {
LOG.warn("No namespace found via config, assuming default");
namespace = "default";
}
LOG.info("Using namespace {}", namespace);
OperationContext namespaced = new OperationContext().withNamespace(namespace);
// an infomer creates a watcher for a specific resource type and caches related events
// you can subscribe to these events by registering a resource event handler
SharedInformerFactory informerFactory = client.informers();
SharedIndexInformer<Pod> podSharedIndexInformer = informerFactory.sharedIndexInformerFor(Pod.class, namespaced, RESYNC_PERIOD_MS);
SharedIndexInformer<Cake> cakeSharedIndexInformer = informerFactory.sharedIndexInformerForCustomResource(Cake.class, namespaced, RESYNC_PERIOD_MS);
LOG.debug("Informer factories initialized");
MixedOperation<Cake, CakeList, Resource<Cake>> cakeClient = client.customResources(Cake.class, CakeList.class);
CakeController cakeController = new CakeController(client, cakeClient, podSharedIndexInformer, cakeSharedIndexInformer, namespace);
LOG.debug("Controller initialized");
cakeController.create();
informerFactory.startAllRegisteredInformers();
informerFactory.addSharedInformerEventListener(e -> LOG.error("Exception occurred", e));
cakeController.run();
} catch (KubernetesClientException e) {
LOG.error("Kubernetes client exception: {}", e.getMessage());
}
}
Aggregations