Skip to content

fix: WaitControllerManagerLog dose not work in test framework #2458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/controller/apisixroute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,14 +607,14 @@ func (r *ApisixRouteReconciler) filterEndpointSlicesBySubsetLabels(ctx context.C
in[i] = r.filterEndpointSliceByTargetPod(ctx, in[i], labels)
}

return utils.Filter(in, func(v discoveryv1.EndpointSlice) bool {
return pkgutils.Filter(in, func(v discoveryv1.EndpointSlice) bool {
return len(v.Endpoints) > 0
})
}

// filterEndpointSliceByTargetPod filters item.Endpoints which is not a subset of labels
func (r *ApisixRouteReconciler) filterEndpointSliceByTargetPod(ctx context.Context, item discoveryv1.EndpointSlice, labels map[string]string) discoveryv1.EndpointSlice {
item.Endpoints = utils.Filter(item.Endpoints, func(v discoveryv1.Endpoint) bool {
item.Endpoints = pkgutils.Filter(item.Endpoints, func(v discoveryv1.Endpoint) bool {
if v.TargetRef == nil || v.TargetRef.Kind != KindPod {
return true
}
Expand Down
5 changes: 3 additions & 2 deletions internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ func Run(ctx context.Context, logger logr.Logger) error {

go func() {
setupLog.Info("starting provider sync")
initalSyncDelay := config.ControllerConfig.ProviderConfig.InitSyncDelay.Duration
time.AfterFunc(initalSyncDelay, func() {
initialSyncDelay := config.ControllerConfig.ProviderConfig.InitSyncDelay.Duration
time.AfterFunc(initialSyncDelay, func() {
setupLog.Info("trying to initialize provider")
if err := provider.Sync(ctx); err != nil {
setupLog.Error(err, "unable to sync resources to provider")
return
}
setupLog.Info("All cache synced successfully")
})

syncPeriod := config.ControllerConfig.ProviderConfig.SyncPeriod.Duration
Expand Down
13 changes: 0 additions & 13 deletions internal/utils/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,6 @@ func MatchHostDef(host string) bool {
return hostDefRegex.MatchString(host)
}

func AppendFunc[T any](s []T, keep func(v T) bool, values ...T) []T {
for _, v := range values {
if keep(v) {
s = append(s, v)
}
}
return s
}

func Filter[T any](s []T, keep func(v T) bool) []T {
return AppendFunc(make([]T, 0), keep, s...)
}

func IsSubsetOf(a, b map[string]string) bool {
if len(a) == 0 {
// Empty labels matches everything.
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/datastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ func DedupComparable[T comparable](s []T) []T {
}
return results
}

func AppendFunc[T any](s []T, keep func(v T) bool, values ...T) []T {
for _, v := range values {
if keep(v) {
s = append(s, v)
}
}
return s
}

func Filter[T any](s []T, keep func(v T) bool) []T {
return AppendFunc(make([]T, 0), keep, s...)
}
2 changes: 1 addition & 1 deletion test/e2e/crds/gatewayproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ spec:
}

By(fmt.Sprintf("wait for keyword: %s", keyword))
s.WaitControllerManagerLog(keyword, 60, time.Minute)
s.WaitControllerManagerLog(s.Namespace(), keyword, 60, time.Minute)
})
})
})
2 changes: 1 addition & 1 deletion test/e2e/framework/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ func (f *Framework) DeployIngress(opts IngressDeployOpts) {
LabelSelector: "control-plane=controller-manager",
})
f.GomegaT.Expect(err).ToNot(HaveOccurred(), "waiting for controller-manager pod ready")
f.WaitControllerManagerLog("All cache synced successfully", 0, time.Minute)
f.WaitControllerManagerLog(opts.Namespace, "All cache synced successfully", 60, time.Minute)
}
39 changes: 14 additions & 25 deletions test/e2e/framework/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/utils/ptr"

"github.com/apache/apisix-ingress-controller/pkg/utils"
)

// buildRestConfig builds the rest.Config object from kubeconfig filepath and
Expand Down Expand Up @@ -207,11 +209,14 @@ func (f *Framework) GetPodIP(namespace, selector string) string {
return pods[0].Status.PodIP
}

func (f *Framework) GetPods(namespace, selector string) []corev1.Pod {
func (f *Framework) GetPods(namespace, selector string, filters ...func(pod corev1.Pod) bool) []corev1.Pod {
podList, err := f.clientset.CoreV1().Pods(cmp.Or(namespace, _namespace)).List(f.Context, metav1.ListOptions{
LabelSelector: selector,
})
f.GomegaT.Expect(err).ShouldNot(HaveOccurred())
for _, filter := range filters {
podList.Items = utils.Filter(podList.Items, filter)
}
return podList.Items
}

Expand Down Expand Up @@ -295,27 +300,10 @@ func (f *Framework) NewExpectResponse(httpBody any) *httpexpect.Response {
})
}

// ListPods query pods by label selector.
func (f *Framework) ListPods(selector string) []corev1.Pod {
pods, err := f.clientset.CoreV1().Pods(_namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: selector,
func (f *Framework) ListRunningPods(namespace, selector string) []corev1.Pod {
return f.GetPods(namespace, selector, func(pod corev1.Pod) bool {
return pod.Status.Phase == corev1.PodRunning && pod.DeletionTimestamp == nil
})
f.GomegaT.Expect(err).ShouldNot(HaveOccurred(), "list pod: ", selector)
return pods.Items
}

func (f *Framework) ListRunningPods(selector string) []corev1.Pod {
pods, err := f.clientset.CoreV1().Pods(_namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: selector,
})
f.GomegaT.Expect(err).ShouldNot(HaveOccurred(), "list pod: ", selector)
runningPods := make([]corev1.Pod, 0)
for _, p := range pods.Items {
if p.Status.Phase == corev1.PodRunning && p.DeletionTimestamp == nil {
runningPods = append(runningPods, p)
}
}
return runningPods
}

// ExecCommandInPod exec cmd in specify pod and return the output from stdout and stderr
Expand Down Expand Up @@ -362,12 +350,13 @@ func (f *Framework) GetPodLogs(name string, previous bool) string {
return string(logs)
}

func (f *Framework) WaitControllerManagerLog(keyword string, sinceSeconds int64, timeout time.Duration) {
f.WaitPodsLog("control-plane=controller-manager", keyword, sinceSeconds, timeout)
func (f *Framework) WaitControllerManagerLog(namespace, keyword string, sinceSeconds int64, timeout time.Duration) {
f.WaitPodsLog(namespace, "control-plane=controller-manager", keyword, sinceSeconds, timeout)
}

func (f *Framework) WaitPodsLog(selector, keyword string, sinceSeconds int64, timeout time.Duration) {
pods := f.ListRunningPods(selector)
func (f *Framework) WaitPodsLog(namespace, selector, keyword string, sinceSeconds int64, timeout time.Duration) {
pods := f.ListRunningPods(namespace, selector)
f.GomegaT.Expect(pods).ToNot(BeEmpty())
wg := sync.WaitGroup{}
for _, p := range pods {
wg.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ spec:
err = s.DeleteResource("Ingress", "default")
Expect(err).NotTo(HaveOccurred(), "delete Ingress")

err = framework.PollUntilHTTPRoutePolicyHaveStatus(s.K8sClient, 8*time.Second,
err = framework.PollUntilHTTPRoutePolicyHaveStatus(s.K8sClient, 20*time.Second,
types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
func(hrp *v1alpha1.HTTPRoutePolicy) bool {
return len(hrp.Status.Ancestors) == 0
Expand Down
Loading