Search code examples
gokubernetesclient-go

Watching pods in two different go routines. Will both routines get similar events?


I currently am doing something like this

watch, err := s.clientset.CoreV1().Pods("").Watch(context.TODO(), metav1.ListOptions{
    FieldSelector: fmt.Sprintf("spec.myfoo=%s", s.foo),
})

for event := range watch.ResultChan() {
    .......
}

I am curious if I have something similar in two different go routines will both of the watches get the same events or if both routines might get different events. Based on who got it first?


Solution

  • Watch internally establishes a long poll connection with the API server. After establishing a connection, the API server will send a batch of initial events and any subsequent changes. Once a timeout has occurred, the connection will be dropped.

    Since your scenario involves two go routines, we cannot guarantee that both will start executing simultaneously and that both long poll connections will be established simultaneously. Furthermore, the connection may drop at some point.

    In a large cluster, pods are constantly being killed and created. Thus, it is certainly possible for two go routines to receive different events.