Hold until pods are created java-client Kubernetes

12/17/2018

I create a custom object using java-client for Kubernetes API in the beforeAll method of the integration tests. After the custom object is created the pods get created too. However, it only works when I set Thread.sleep for a few seconds. Without it, the object is created and then all the tests executed. I also defined watch on custom object statuses, but it does not help either. Is there any other way (except for Thread.sleep) to hold for a few seconds until the pods get created?

My code for the custom object creation:

def createWatchCustomObjectsCalls() = {
    client.getHttpClient.setReadTimeout(0, TimeUnit.SECONDS)
    val watchCalls: Watch[V1Namespace]  = Watch.createWatch(client,
      apiInstance.listNamespacedCustomObjectCall(crdGroup, crdVersion,  crdNamespace, crdPlural, "true",  null, null, true,null,  null),
      new TypeToken[Watch.Response[V1Namespace]]{}.getType)
    watchCalls
  }

 override def beforeAll(): Unit = {
    val creationResourcePath = Source.getClass.getResource("/" + httpServerScriptName).getPath
    val serverStartupProcessBuilder = Seq("sh", creationResourcePath, "&") #> Console.out
    serverStartupProcessBuilder.run()

    val body = convertYamlToJson()
    val sparkAppCreation = apiInstance.createNamespacedCustomObject(crdGroup, crdVersion, crdNamespace, crdPlural, body,"true")
    println(sparkAppCreation)
}
-- Cassie
kubernetes
pod
scala
watch

1 Answer

5/23/2019

You can synchronously check in a while loop if pods had been created:

// while
val currentPodList = getCoreV1Api()
                    .listPodForAllNamespaces(null /* _continue */,
                                            null /* fieldSelector */,
                                            null /* includeUninitialized */,
                                            null /* labelSelector */,
                                            null /* limit */,
                                            "false" /* pretty */,
                                            null /* resourceVersion */,
                                            null /* timeoutSeconds */,
                                            false /* watch */)
                    .getItems();
// check items from currentPodList
// end while
-- Danut Radoaica
Source: StackOverflow