Skip to content

Instantly share code, notes, and snippets.

@xguerin
Created March 7, 2019 23:04
Show Gist options
  • Save xguerin/240a2c14e25c1a3772ea87c7a51b4233 to your computer and use it in GitHub Desktop.
Save xguerin/240a2c14e25c1a3772ea87c7a51b4233 to your computer and use it in GitHub Desktop.
public class WatchExample {
private static final Logger logger = LoggerFactory.getLogger(WatchExample.class);
public static void main(String[] args) throws InterruptedException {
final CountDownLatch closeLatch = new CountDownLatch(1);
Config config = new ConfigBuilder().build();
final KubernetesClient client = new DefaultKubernetesClient(config);
/*
* Pre-register our CRD signature with the embedded JSON deserializer. This is a required step.
*
* See: fabric8io/kubernetes-client#1099
*/
KubernetesDeserializer.registerCustomKind(MYCRD_API_VERSION, "MyCRD", MyCRD.class);
/*
* Grab the CRD.
*/
CustomResourceDefinition crd = client.customResourceDefinitions()
.list()
.getItems()
.stream()
.filter(e -> e.getMetadata().getName().equals(MY_CRD_NAME))
.findFirst()
.orElseThrow(RuntimeException::new);
/*
* Create a watcher.
*/
client.customResources(crd, MyCRD.class, MyCRDList.class, DoneableMyCRD.class)
.inNamespace("default")
.withLabel("test", "yes")
.watch(new Watcher<MyCRD>() {
@Override
public void eventReceived(Action action, MyCRD resource) {
logger.info("{}: {}", action, resource.getMetadata().getResourceVersion());
}
@Override
public void onClose(KubernetesClientException e) {
logger.debug("Watcher onClose");
if (e != null) {
logger.error(e.getMessage(), e);
closeLatch.countDown();
}
}
});
closeLatch.await(10, TimeUnit.SECONDS);
Thread.sleep(60000l);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment