func()

in internal/testhelpers/resources.go [425:466]


func (cc *TestCaseClient) ExpectPodContainerCount(ctx context.Context, podSelector *metav1.LabelSelector, count int, allOrAny string) error {

	var (
		countBadPods int
		countPods    int
	)

	err := RetryUntilSuccess(24, DefaultRetryInterval, func() error {
		countBadPods = 0
		pods, err := ListPods(ctx, cc.Client, cc.Namespace, podSelector)
		if err != nil {
			return err
		}
		countPods = len(pods.Items)
		if len(pods.Items) == 0 {
			return fmt.Errorf("got 0 pods, want at least 1 pod")
		}
		for _, pod := range pods.Items {
			// The use len(Containers) + len(InitContainers) as the total number
			// of containers on the pod. This way the assertion works the same
			// when it runs with k8s <= 1.28 using regular containers for the proxy
			// as well as with k8s >= 1.29 using init containers for the proxy.
			got := len(pod.Spec.Containers) + len(pod.Spec.InitContainers)
			if got != count {
				countBadPods++
			}
		}
		switch {
		case allOrAny == "all" && countBadPods > 0:
			return fmt.Errorf("got the wrong number of containers on %d of %d pods", countBadPods, len(pods.Items))
		case allOrAny == "any" && countBadPods == countPods:
			return fmt.Errorf("got the wrong number of containers on %d of %d pods", countBadPods, len(pods.Items))
		}
		return nil
	})

	if err != nil {
		return fmt.Errorf("want %v containers, got the wrong number of containers on %d of %d pods", count, countBadPods, countPods)
	}

	return nil
}