@@ -34,8 +34,8 @@ import (
3434)
3535
3636func GatewayClassMustHaveCondition (t testing.TestingT , cli client.Client , timeout time.Duration , gcNN types.NamespacedName , condition metav1.Condition ) {
37- err := PollUntilGatewayClassMustHaveStatus (cli , timeout , gcNN , func (gc gatewayv1.GatewayClass ) bool {
38- if err := kubernetes .ConditionsHaveLatestObservedGeneration (& gc , gc .Status .Conditions ); err != nil {
37+ err := PollUntilGatewayClassMustHaveStatus (cli , timeout , gcNN , func (gc * gatewayv1.GatewayClass ) bool {
38+ if err := kubernetes .ConditionsHaveLatestObservedGeneration (gc , gc .Status .Conditions ); err != nil {
3939 log .Printf ("GatewayClass %s %v" , gcNN , err )
4040 return false
4141 }
@@ -48,63 +48,51 @@ func GatewayClassMustHaveCondition(t testing.TestingT, cli client.Client, timeou
4848 require .NoError (t , err , "waiting for GatewayClass to have condition %+v" , condition )
4949}
5050
51- func PollUntilGatewayClassMustHaveStatus (cli client.Client , timeout time.Duration , gcNN types.NamespacedName , f func (gc gatewayv1.GatewayClass ) bool ) error {
51+ func PollUntilGatewayClassMustHaveStatus (cli client.Client , timeout time.Duration , gcNN types.NamespacedName , f func (gc * gatewayv1.GatewayClass ) bool ) error {
5252 if err := gatewayv1 .Install (cli .Scheme ()); err != nil {
5353 return err
5454 }
55- return wait .PollUntilContextTimeout (context .Background (), time .Second , timeout , true , func (ctx context.Context ) (done bool , err error ) {
56- var gc gatewayv1.GatewayClass
57- if err := cli .Get (ctx , gcNN , & gc ); err != nil {
58- return false , errors .Wrapf (err , "failed to get GatewayClass %s" , gcNN )
59- }
60- return f (gc ), nil
61- })
55+ return genericPollResource (new (gatewayv1.GatewayClass ), cli , timeout , gcNN , f )
6256}
6357
6458func GatewayMustHaveCondition (t testing.TestingT , cli client.Client , timeout time.Duration , gwNN types.NamespacedName , condition metav1.Condition ) {
65- err := PollUntilGatewayHaveStatus (cli , timeout , gwNN , func (gw gatewayv1.Gateway ) bool {
66- if err := kubernetes .ConditionsHaveLatestObservedGeneration (& gw , gw .Status .Conditions ); err != nil {
59+ err := PollUntilGatewayHaveStatus (cli , timeout , gwNN , func (gw * gatewayv1.Gateway ) bool {
60+ if err := kubernetes .ConditionsHaveLatestObservedGeneration (gw , gw .Status .Conditions ); err != nil {
6761 log .Printf ("Gateway %s %v" , gwNN , err )
6862 return false
6963 }
7064 if findConditionInList (gw .Status .Conditions , condition ) {
71- log .Printf ("found condition %v in list [%v] " , condition , gw .Status .Conditions )
65+ log .Printf ("found condition %v in list %v " , condition , gw .Status .Conditions )
7266 return true
7367 } else {
74- log .Printf ("not found condition %v in list [%v] " , condition , gw .Status .Conditions )
68+ log .Printf ("NOT FOUND condition %v in %v " , condition , gw .Status .Conditions )
7569 return false
7670 }
7771 })
7872 require .NoError (t , err , "waiting for Gateway to have condition %+v" , condition )
7973}
8074
81- func PollUntilGatewayHaveStatus (cli client.Client , timeout time.Duration , gwNN types.NamespacedName , f func (gateway gatewayv1.Gateway ) bool ) error {
75+ func PollUntilGatewayHaveStatus (cli client.Client , timeout time.Duration , gwNN types.NamespacedName , f func (gateway * gatewayv1.Gateway ) bool ) error {
8276 if err := gatewayv1 .Install (cli .Scheme ()); err != nil {
8377 return err
8478 }
85- return wait .PollUntilContextTimeout (context .Background (), time .Second , timeout , true , func (ctx context.Context ) (done bool , err error ) {
86- var gw gatewayv1.Gateway
87- if err := cli .Get (ctx , gwNN , & gw ); err != nil {
88- return false , errors .Wrapf (err , "failed to get Gateway %s" , gwNN )
89- }
90- return f (gw ), nil
91- })
79+ return genericPollResource (new (gatewayv1.Gateway ), cli , timeout , gwNN , f )
9280}
9381
9482func HTTPRouteMustHaveCondition (t testing.TestingT , cli client.Client , timeout time.Duration , refNN , hrNN types.NamespacedName , condition metav1.Condition ) {
95- err := PollUntilHTTPRouteHaveStatus (cli , timeout , hrNN , func (hr gatewayv1.HTTPRoute ) bool {
83+ err := PollUntilHTTPRouteHaveStatus (cli , timeout , hrNN , func (hr * gatewayv1.HTTPRoute ) bool {
9684 for _ , parent := range hr .Status .Parents {
97- if err := kubernetes .ConditionsHaveLatestObservedGeneration (& hr , parent .Conditions ); err != nil {
85+ if err := kubernetes .ConditionsHaveLatestObservedGeneration (hr , parent .Conditions ); err != nil {
9886 log .Printf ("HTTPRoute %s (parentRef=%v) %v" , hrNN , parentRefToString (parent .ParentRef ), err )
9987 return false
10088 }
10189 if (refNN .Name == "" || parent .ParentRef .Name == gatewayv1 .ObjectName (refNN .Name )) &&
10290 (refNN .Namespace == "" || (parent .ParentRef .Namespace != nil && string (* parent .ParentRef .Namespace ) == refNN .Namespace )) {
10391 if findConditionInList (parent .Conditions , condition ) {
104- log .Printf ("found condition %v in list [%v] for %s reference %s" , condition , parent .Conditions , hrNN , refNN )
92+ log .Printf ("found condition %v in %v for %s reference %s" , condition , parent .Conditions , hrNN , refNN )
10593 return true
10694 } else {
107- log .Printf ("found condition %v in list [%v] for %s reference %s" , condition , parent .Conditions , hrNN , refNN )
95+ log .Printf ("NOT FOUND condition %v in %v for %s reference %s" , condition , parent .Conditions , hrNN , refNN )
10896 }
10997 }
11098 }
@@ -113,35 +101,29 @@ func HTTPRouteMustHaveCondition(t testing.TestingT, cli client.Client, timeout t
113101 require .NoError (t , err , "error waiting for HTTPRoute status to have a Condition matching %+v" , condition )
114102}
115103
116- func PollUntilHTTPRouteHaveStatus (cli client.Client , timeout time.Duration , hrNN types.NamespacedName , f func (route gatewayv1.HTTPRoute ) bool ) error {
104+ func PollUntilHTTPRouteHaveStatus (cli client.Client , timeout time.Duration , hrNN types.NamespacedName , f func (route * gatewayv1.HTTPRoute ) bool ) error {
117105 if err := gatewayv1 .Install (cli .Scheme ()); err != nil {
118106 return err
119107 }
120- return wait .PollUntilContextTimeout (context .Background (), time .Second , timeout , true , func (ctx context.Context ) (done bool , err error ) {
121- var httpRoute gatewayv1.HTTPRoute
122- if err := cli .Get (ctx , hrNN , & httpRoute ); err != nil {
123- return false , errors .Wrapf (err , "failed to get HTTPRoute %s" , hrNN )
124- }
125- return f (httpRoute ), nil
126- })
108+ return genericPollResource (new (gatewayv1.HTTPRoute ), cli , timeout , hrNN , f )
127109}
128110
129111func HTTPRoutePolicyMustHaveCondition (t testing.TestingT , client client.Client , timeout time.Duration , refNN , hrpNN types.NamespacedName ,
130112 condition metav1.Condition ) {
131- err := PollUntilHTTPRoutePolicyHaveStatus (client , timeout , hrpNN , func (httpRoutePolicy v1alpha1.HTTPRoutePolicy , status v1alpha1. PolicyStatus ) bool {
132- for _ , ancestor := range status .Ancestors {
133- if err := kubernetes .ConditionsHaveLatestObservedGeneration (& httpRoutePolicy , ancestor .Conditions ); err != nil {
113+ err := PollUntilHTTPRoutePolicyHaveStatus (client , timeout , hrpNN , func (httpRoutePolicy * v1alpha1.HTTPRoutePolicy ) bool {
114+ for _ , ancestor := range httpRoutePolicy . Status .Ancestors {
115+ if err := kubernetes .ConditionsHaveLatestObservedGeneration (httpRoutePolicy , ancestor .Conditions ); err != nil {
134116 log .Printf ("HTTPRoutePolicy %s (parentRef=%v) %v" , hrpNN , parentRefToString (ancestor .AncestorRef ), err )
135117 return false
136118 }
137119
138120 if ancestor .AncestorRef .Name == gatewayv1 .ObjectName (refNN .Name ) &&
139121 (refNN .Namespace == "" || (ancestor .AncestorRef .Namespace != nil && string (* ancestor .AncestorRef .Namespace ) == refNN .Namespace )) {
140122 if findConditionInList (ancestor .Conditions , condition ) {
141- log .Printf ("found condition %v in list [%v] for %s reference %s" , condition , ancestor .Conditions , hrpNN , refNN )
123+ log .Printf ("found condition %v in list %v for %s reference %s" , condition , ancestor .Conditions , hrpNN , refNN )
142124 return true
143125 } else {
144- log .Printf ("not found condition %v in list [%v] for %s reference %s" , condition , ancestor .Conditions , hrpNN , refNN )
126+ log .Printf ("NOT FOUND condition %v in %v for %s reference %s" , condition , ancestor .Conditions , hrpNN , refNN )
145127 }
146128 }
147129 }
@@ -151,16 +133,12 @@ func HTTPRoutePolicyMustHaveCondition(t testing.TestingT, client client.Client,
151133 require .NoError (t , err , "error waiting for HTTPRoutePolicy %s status to have a Condition matching %+v" , hrpNN , condition )
152134}
153135
154- func PollUntilHTTPRoutePolicyHaveStatus (client client.Client , timeout time.Duration , hrpNN types.NamespacedName ,
155- f func (httpRoutePolicy v1alpha1.HTTPRoutePolicy , status v1alpha1.PolicyStatus ) bool ) error {
156- _ = v1alpha1 .AddToScheme (client .Scheme ())
157- return wait .PollUntilContextTimeout (context .Background (), time .Second , timeout , true , func (ctx context.Context ) (done bool , err error ) {
158- var httpRoutePolicy v1alpha1.HTTPRoutePolicy
159- if err = client .Get (ctx , hrpNN , & httpRoutePolicy ); err != nil {
160- return false , errors .Wrapf (err , "error fetching HTTPRoutePolicy %s" , hrpNN )
161- }
162- return f (httpRoutePolicy , httpRoutePolicy .Status ), nil
163- })
136+ func PollUntilHTTPRoutePolicyHaveStatus (cli client.Client , timeout time.Duration , hrpNN types.NamespacedName ,
137+ f func (httpRoutePolicy * v1alpha1.HTTPRoutePolicy ) bool ) error {
138+ if err := v1alpha1 .AddToScheme (cli .Scheme ()); err != nil {
139+ return err
140+ }
141+ return genericPollResource (new (v1alpha1.HTTPRoutePolicy ), cli , timeout , hrpNN , f )
164142}
165143
166144func parentRefToString (p gatewayv1.ParentReference ) string {
@@ -180,3 +158,12 @@ func findConditionInList(conditions []metav1.Condition, expected metav1.Conditio
180158 (expected .Message == "" || strings .Contains (item .Message , expected .Message ))
181159 })
182160}
161+
162+ func genericPollResource [Obj client.Object ](obj Obj , cli client.Client , timeout time.Duration , nn types.NamespacedName , predicate func (Obj ) bool ) error {
163+ return wait .PollUntilContextTimeout (context .Background (), time .Second , timeout , true , func (ctx context.Context ) (done bool , err error ) {
164+ if err := cli .Get (ctx , nn , obj ); err != nil {
165+ return false , errors .Wrapf (err , "error fetching Object %s" , nn )
166+ }
167+ return predicate (obj ), nil
168+ })
169+ }
0 commit comments