func()

in internal/kubelib/logs.go [110:180]


func (f *logsFollower) discoverPods(ctx context.Context) {
	defer f.wg.Done()

	shouldHandlePod := func(pod *k8scorev1.Pod) bool {
		matchContainer := false
		for _, container := range pod.Spec.Containers {
			if container.Name == f.container {
				matchContainer = true
				break
			}
		}
		if !matchContainer {
			return false
		}

		if _, ok := logsAblePodPhase[pod.Status.Phase]; !ok {
			return false
		}

		return true
	}

	// TODO: log error
	_ = func() error {
		podsClient := f.client.CoreV1().Pods(f.namespace)

		watch, err := podsClient.Watch(ctx, k8smetav1.ListOptions{
			LabelSelector: f.selector.String(),
		})
		if err != nil {
			return err
		}
		defer watch.Stop()

		for {
			select {
			case <-ctx.Done():
				return nil
			case event, ok := <-watch.ResultChan():
				if !ok {
					return nil
				}

				pod, ok := event.Object.(*k8scorev1.Pod)
				if !ok {
					continue
				}
				if !shouldHandlePod(pod) {
					continue
				}

				objectRef := k8scorev1.ObjectReference{
					Kind:      "Pod",
					Namespace: pod.Namespace,
					Name:      pod.Name,
				}
				if !f.isNewTarget(objectRef) {
					continue
				}
				f.podLogsChan <- podLog{
					target: objectRef,
					request: podsClient.GetLogs(pod.Name, &k8scorev1.PodLogOptions{
						Container: f.container,
						Follow:    true,
					}),
				}
			}

		}
	}()
}