txn2 / kubefwd

Bulk port forwarding Kubernetes services for local development.
https://imti.co/kubernetes-port-forwarding/
Apache License 2.0
3.8k stars 205 forks source link

No pods running error #254

Open giulianob opened 1 year ago

giulianob commented 1 year ago

When a new service comes up but the pods are still starting up, I get a no pods error as the watcher tries too quickly to start forwarding ports. I added a retry and fixes it for me. Is this something I could PR or is there a better solution?

func (svcFwd *ServiceFWD) SyncPodForwards(force bool) {
    sync := func() {

        defer func() { svcFwd.LastSyncedAt = time.Now() }()

        var k8sPods []v1.Pod;

        for i := 1; i <= 3 || len(k8sPods) > 0; i++ {
            k8sPods = svcFwd.GetPodsForService()
            if len(k8sPods) > 0 {
                break;
            }

            log.Debugf("No Running Pods returned for service %s, attempt %d", svcFwd, i)
            time.Sleep(5 * time.Second)
        }

        // If no pods are found currently. Will try again next re-sync period.
        if len(k8sPods) == 0 {
            log.Warnf("WARNING: No Running Pods returned for service %s", svcFwd)
            return
        }
....