Skip to content

Commit d07d725

Browse files
committed
Refactor HTTPRoutePolicy status handling and test logic
Removed unstable test case loop and streamlined HTTPRoutePolicy status condition checks. Improved condition matching logic and updated related utility functions for better readability and maintainability. The changes also include minor package import reorganizations and logging improvements across multiple files.
1 parent 0f2977b commit d07d725

File tree

5 files changed

+57
-88
lines changed

5 files changed

+57
-88
lines changed

internal/controller/httproute_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"strings"
1919

20+
"github.com/api7/gopkg/pkg/log"
2021
"github.com/go-logr/logr"
2122
"github.com/pkg/errors"
2223
"go.uber.org/zap"
@@ -38,8 +39,6 @@ import (
3839
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3940
"sigs.k8s.io/gateway-api/apis/v1alpha2"
4041

41-
"github.com/api7/gopkg/pkg/log"
42-
4342
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
4443
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4544
"github.com/apache/apisix-ingress-controller/internal/provider"

internal/controller/httproutepolicy.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,12 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.Co
190190
func modifyHTTPRoutePolicyStatus(parentRefs []gatewayv1.ParentReference, policy *v1alpha1.HTTPRoutePolicy, status bool, reason, message string) {
191191
condition := metav1.Condition{
192192
Type: string(v1alpha2.PolicyConditionAccepted),
193-
Status: metav1.ConditionTrue,
193+
Status: ConditionStatus(status),
194194
ObservedGeneration: policy.GetGeneration(),
195195
LastTransitionTime: metav1.Now(),
196196
Reason: reason,
197197
Message: message,
198198
}
199-
if !status {
200-
condition.Status = metav1.ConditionFalse
201-
}
202199
_ = SetAncestors(&policy.Status, parentRefs, condition)
203200
}
204201

internal/controller/ingress_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
148148

149149
tctx.RouteParentRefs = append(tctx.RouteParentRefs, gatewayv1.ParentReference{
150150
Group: ptr.To(gatewayv1.Group(ingressClass.GroupVersionKind().Group)),
151-
Kind: ptr.To(gatewayv1.Kind("IngressClass")),
151+
Kind: ptr.To(gatewayv1.Kind(KindIngressClass)),
152152
Name: gatewayv1.ObjectName(ingressClass.Name),
153153
})
154154

test/e2e/framework/utils.go

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import (
2323
"log"
2424
"net/http"
2525
"net/url"
26+
"slices"
2627
"strings"
2728
"sync"
2829
"time"
2930

30-
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3131
"github.com/gavv/httpexpect/v2"
3232
"github.com/gruntwork-io/terratest/modules/testing"
3333
"github.com/onsi/gomega"
@@ -36,32 +36,18 @@ import (
3636
"golang.org/x/net/html"
3737
corev1 "k8s.io/api/core/v1"
3838
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39-
"k8s.io/apimachinery/pkg/runtime"
4039
"k8s.io/apimachinery/pkg/types"
41-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4240
"k8s.io/apimachinery/pkg/util/wait"
4341
"k8s.io/client-go/kubernetes/scheme"
4442
"k8s.io/client-go/tools/remotecommand"
4543
"k8s.io/utils/ptr"
4644
"sigs.k8s.io/controller-runtime/pkg/client"
4745
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
4846
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
49-
)
5047

51-
var (
52-
scheme_ = runtime.NewScheme()
48+
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
5349
)
5450

55-
func init() {
56-
utilruntime.Must(scheme.AddToScheme(scheme_))
57-
if err := gatewayv1.Install(scheme_); err != nil {
58-
panic(err)
59-
}
60-
if err := v1alpha1.AddToScheme(scheme_); err != nil {
61-
panic(err)
62-
}
63-
}
64-
6551
func (f *Framework) NewExpectResponse(httpBody any) *httpexpect.Response {
6652
body, err := json.Marshal(httpBody)
6753
f.GomegaT.Expect(err).ShouldNot(gomega.HaveOccurred())
@@ -395,31 +381,32 @@ func CreateTestZipFile(sourceCode, metadata string) ([]byte, error) {
395381
return zipBuffer.Bytes(), nil
396382
}
397383

398-
func HTTPRoutePolicyMustHaveCondition(t testing.TestingT, client client.Client, timeout time.Duration, refNN, hrpNN types.NamespacedName, condition metav1.Condition) {
384+
func HTTPRoutePolicyMustHaveCondition(t testing.TestingT, client client.Client, timeout time.Duration, refNN, hrpNN types.NamespacedName,
385+
condition metav1.Condition) {
399386
err := EventuallyHTTPRoutePolicyHaveStatus(client, timeout, hrpNN, func(httpRoutePolicy v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool {
400-
var (
401-
ancestors = status.Ancestors
402-
conditionFound bool
403-
)
404-
for _, ancestor := range ancestors {
387+
for _, ancestor := range status.Ancestors {
405388
if err := kubernetes.ConditionsHaveLatestObservedGeneration(&httpRoutePolicy, ancestor.Conditions); err != nil {
406389
log.Printf("HTTPRoutePolicy %s (parentRef=%v) %v", hrpNN, parentRefToString(ancestor.AncestorRef), err)
407390
return false
408391
}
409392

410-
if ancestor.AncestorRef.Name == gatewayv1.ObjectName(refNN.Name) && (ancestor.AncestorRef.Namespace == nil || string(*ancestor.AncestorRef.Namespace) == refNN.Namespace) {
411-
if findConditionInList(t, ancestor.Conditions, condition) {
412-
conditionFound = true
393+
if ancestor.AncestorRef.Name == gatewayv1.ObjectName(refNN.Name) && (ancestor.AncestorRef.Namespace == nil ||
394+
string(*ancestor.AncestorRef.Namespace) == refNN.Namespace) {
395+
if findConditionInList(ancestor.Conditions, condition) {
396+
return true
397+
} else {
398+
log.Printf("not found condition %v in list [%v]", condition, ancestor.Conditions)
413399
}
414400
}
415401
}
416-
return conditionFound
402+
return false
417403
})
418404

419405
require.NoError(t, err, "error waiting for HTTPRoutePolicy status to have a Condition matching expectations")
420406
}
421407

422-
func EventuallyHTTPRoutePolicyHaveStatus(client client.Client, timeout time.Duration, hrpNN types.NamespacedName, f func(httpRoutePolicy v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool) error {
408+
func EventuallyHTTPRoutePolicyHaveStatus(client client.Client, timeout time.Duration, hrpNN types.NamespacedName,
409+
f func(httpRoutePolicy v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool) error {
423410
_ = v1alpha1.AddToScheme(client.Scheme())
424411
return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) {
425412
var httpRoutePolicy v1alpha1.HTTPRoutePolicy
@@ -447,22 +434,10 @@ func parentRefToString(p gatewayv1.ParentReference) string {
447434
return string(p.Name)
448435
}
449436

450-
func findConditionInList(t testing.TestingT, conditions []metav1.Condition, expected metav1.Condition) bool {
451-
for _, cond := range conditions {
452-
if cond.Type == expected.Type {
453-
// an empty Status string means "Match any status".
454-
if expected.Status == "" || cond.Status == expected.Status {
455-
// an empty Reason string means "Match any reason".
456-
if expected.Reason == "" || cond.Reason == expected.Reason {
457-
return true
458-
}
459-
log.Printf("%s condition Reason set to %s, expected %s", expected.Type, cond.Reason, expected.Reason)
460-
}
461-
462-
log.Printf("%s condition set to Status %s with Reason %v, expected Status %s", expected.Type, cond.Status, cond.Reason, expected.Status)
463-
}
464-
}
465-
466-
log.Printf("%s was not in conditions list [%v]", expected.Type, conditions)
467-
return false
437+
func findConditionInList(conditions []metav1.Condition, expected metav1.Condition) bool {
438+
return slices.ContainsFunc(conditions, func(item metav1.Condition) bool {
439+
// an empty Status string means "Match any status".
440+
// an empty Reason string means "Match any reason".
441+
return expected.Type == item.Type && (expected.Status == "" || expected.Status == item.Status) && (expected.Reason == "" || expected.Reason == item.Reason)
442+
})
468443
}

test/e2e/ingress/ingress.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ import (
2020
"strings"
2121
"time"
2222

23-
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
24-
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
25-
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
2623
. "github.com/onsi/ginkgo/v2"
2724
. "github.com/onsi/gomega"
2825
"github.com/stretchr/testify/assert"
2926
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3027
"k8s.io/apimachinery/pkg/types"
3128
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
29+
30+
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
31+
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
32+
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
3233
)
3334

3435
const _secretName = "test-ingress-tls"
@@ -725,39 +726,36 @@ spec:
725726
WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
726727
})
727728

728-
// todo: unstable test case, pending for now
729-
for i := 0; i < 100; i++ {
730-
It("HTTPRoutePolicy status changes on Ingress deleting", func() {
731-
By("create Ingress")
732-
err := s.CreateResourceFromString(ingressSpec)
733-
Expect(err).NotTo(HaveOccurred(), "creating Ingress")
734-
735-
By("create HTTPRoutePolicy")
736-
err = s.CreateResourceFromString(httpRoutePolicySpec0)
737-
Expect(err).NotTo(HaveOccurred(), "creating HTTPRoutePolicy")
738-
framework.HTTPRoutePolicyMustHaveCondition(s.GinkgoT, s.K8sClient, 8*time.Second,
739-
types.NamespacedName{Namespace: s.Namespace(), Name: "apisix"},
740-
types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
741-
metav1.Condition{
742-
Type: string(gatewayv1alpha2.PolicyConditionAccepted),
743-
Status: metav1.ConditionTrue,
744-
Reason: string(gatewayv1alpha2.PolicyReasonAccepted),
745-
},
746-
)
747-
748-
By("delete ingress")
749-
err = s.DeleteResource("Ingress", "default")
750-
Expect(err).NotTo(HaveOccurred(), "delete Ingress")
751-
752-
err = framework.EventuallyHTTPRoutePolicyHaveStatus(s.K8sClient, 8*time.Second,
753-
types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
754-
func(_ v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool {
755-
return len(status.Ancestors) == 0
756-
},
757-
)
758-
Expect(err).NotTo(HaveOccurred(), "expected HTTPRoutePolicy.Status has no Ancestor")
759-
})
760-
}
729+
It("HTTPRoutePolicy status changes on Ingress deleting", func() {
730+
By("create Ingress")
731+
err := s.CreateResourceFromString(ingressSpec)
732+
Expect(err).NotTo(HaveOccurred(), "creating Ingress")
733+
734+
By("create HTTPRoutePolicy")
735+
err = s.CreateResourceFromString(httpRoutePolicySpec0)
736+
Expect(err).NotTo(HaveOccurred(), "creating HTTPRoutePolicy")
737+
framework.HTTPRoutePolicyMustHaveCondition(s.GinkgoT, s.K8sClient, 8*time.Second,
738+
types.NamespacedName{Namespace: s.Namespace(), Name: "apisix"},
739+
types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
740+
metav1.Condition{
741+
Type: string(gatewayv1alpha2.PolicyConditionAccepted),
742+
Status: metav1.ConditionTrue,
743+
Reason: string(gatewayv1alpha2.PolicyReasonAccepted),
744+
},
745+
)
746+
747+
By("delete ingress")
748+
err = s.DeleteResource("Ingress", "default")
749+
Expect(err).NotTo(HaveOccurred(), "delete Ingress")
750+
751+
err = framework.EventuallyHTTPRoutePolicyHaveStatus(s.K8sClient, 8*time.Second,
752+
types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
753+
func(_ v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool {
754+
return len(status.Ancestors) == 0
755+
},
756+
)
757+
Expect(err).NotTo(HaveOccurred(), "expected HTTPRoutePolicy.Status has no Ancestor")
758+
})
761759
})
762760

763761
Context("Ingress with GatewayProxy Update", func() {

0 commit comments

Comments
 (0)