Hi :slightly_smiling_face: kind of new in Kotlin i...
# getting-started
l
Hi 🙂 kind of new in Kotlin i am using the https://github.com/fabric8io/kubernetes-client and i trying to create a fun to wait until all pods are ready so i first took all the names of the pods
Copy code
private fun printAllPodsName() {
    val podList = client.pods().inNamespace("default").list()
    podList.items.forEach(Consumer { obj: Pod ->
        println(obj.metadata.name)
    })
}
now i want to go over this pod name list so it will check if the status is ready or not i tried to do something like this
Copy code
fun waitUntilAllPodsAreReady() {
        for (podname in printAllPodsName().toString()) {
            client.pods().inNamespace("default").waitUntilCondition({ pods ->
                pods.status != null && pods.status.conditions != null
            }, 10, TimeUnit.MINUTES)
            println(podname.toString())
        }
    }
but it seems like not working any idea will be blessed
r
The problem is your
printAllPodsName
function - as its name says, it is printing names of all pods, not returning them So when you try to use the names of the pods, it does not compile, as you may have noticed by having to add
.toString()
in the for cycle:
for (podname in printAllPodsName().toString()) {
Since your function does not have a return value, it implicitly returns
Unit
, of which
.toString()
is `kotlin.Unit`: so you are searching for pods named
k
,
o
,
t
, ... and so on
also no idea about the API of the client, but you never actually mention the
podname
variable while trying to wait on it
l
Great thank you @Roukanken But how can i use a return? inside the forEach regarding the
podname
i changed the func to this:
Copy code
fun waitUntilAllPodsAreReady() {
    for (podname in printAllPodsName().toString()) {
        client.pods().inNamespace("default").withName("$podname").waitUntilReady(20, TimeUnit.SECONDS)
        Gauge.writeMessage("$podname is ready")
    }
}
r
What is type of
podList.items
? It probably implements
List<T>
so you should be able to call
.map { pod -> pod.metadata.name }
on it - which will return you a list of pod names, and you can pass that onwards
n
Without any deeper knowledge of the API, you might want to have something like this?
Copy code
fun getAllPodNames(): List<String> {
        val podList = client.pods().inNamespace("default").list()
        return podList.items.map { it.metadata.name }
    }

    fun waitUntilAllPodsAreReady() {
        getAllPodNames().forEach { podName ->
            client.pods().inNamespace("default").withName(podName).waitUntilCondition(
                { pod ->
                    pod.status != null && pod.status.conditions != null
                }, 10, TimeUnit.MINUTES
            )
        }
    }
l
Yes @Roukanken the
.map
saved me 10xxx and @Nico Filzmoser yeap i did like you wrote thanks you both 👍 i also succeeded to write other fun to print just the running pods
Copy code
fun printAllTheRunningPods() {
client.pods().withField("status.phase", "Running").list().items.stream()
    .map { obj: Pod -> obj.metadata }.map { obj: ObjectMeta -> obj.name }
    .forEach { msg: String? -> println("$msg is running OK") }
}