|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +package framework |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "log" |
| 19 | + "slices" |
| 20 | + "strings" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/gruntwork-io/terratest/modules/testing" |
| 24 | + "github.com/pkg/errors" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/apimachinery/pkg/util/wait" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 31 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 32 | + |
| 33 | + "github.com/apache/apisix-ingress-controller/api/v1alpha1" |
| 34 | +) |
| 35 | + |
| 36 | +func HTTPRouteMustHaveCondition(t testing.TestingT, cli client.Client, timeout time.Duration, refNN, hrNN types.NamespacedName, condition metav1.Condition) { |
| 37 | + err := PollUntilHTTPRouteHaveStatus(cli, timeout, hrNN, func(hr *gatewayv1.HTTPRoute) bool { |
| 38 | + for _, parent := range hr.Status.Parents { |
| 39 | + if err := kubernetes.ConditionsHaveLatestObservedGeneration(hr, parent.Conditions); err != nil { |
| 40 | + log.Printf("HTTPRoute %s (parentRef=%v) %v", hrNN, parentRefToString(parent.ParentRef), err) |
| 41 | + return false |
| 42 | + } |
| 43 | + if (refNN.Name == "" || parent.ParentRef.Name == gatewayv1.ObjectName(refNN.Name)) && |
| 44 | + (refNN.Namespace == "" || (parent.ParentRef.Namespace != nil && string(*parent.ParentRef.Namespace) == refNN.Namespace)) { |
| 45 | + if findConditionInList(parent.Conditions, condition) { |
| 46 | + log.Printf("found condition %v in %v for %s reference %s", condition, parent.Conditions, hrNN, refNN) |
| 47 | + return true |
| 48 | + } else { |
| 49 | + log.Printf("NOT FOUND condition %v in %v for %s reference %s", condition, parent.Conditions, hrNN, refNN) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return false |
| 54 | + }) |
| 55 | + require.NoError(t, err, "error waiting for HTTPRoute status to have a Condition matching %+v", condition) |
| 56 | +} |
| 57 | + |
| 58 | +func PollUntilHTTPRouteHaveStatus(cli client.Client, timeout time.Duration, hrNN types.NamespacedName, f func(route *gatewayv1.HTTPRoute) bool) error { |
| 59 | + if err := gatewayv1.Install(cli.Scheme()); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + return genericPollResource(new(gatewayv1.HTTPRoute), cli, timeout, hrNN, f) |
| 63 | +} |
| 64 | + |
| 65 | +func HTTPRoutePolicyMustHaveCondition(t testing.TestingT, client client.Client, timeout time.Duration, refNN, hrpNN types.NamespacedName, |
| 66 | + condition metav1.Condition) { |
| 67 | + err := PollUntilHTTPRoutePolicyHaveStatus(client, timeout, hrpNN, func(httpRoutePolicy *v1alpha1.HTTPRoutePolicy) bool { |
| 68 | + for _, ancestor := range httpRoutePolicy.Status.Ancestors { |
| 69 | + if err := kubernetes.ConditionsHaveLatestObservedGeneration(httpRoutePolicy, ancestor.Conditions); err != nil { |
| 70 | + log.Printf("HTTPRoutePolicy %s (parentRef=%v) %v", hrpNN, parentRefToString(ancestor.AncestorRef), err) |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + if ancestor.AncestorRef.Name == gatewayv1.ObjectName(refNN.Name) && |
| 75 | + (refNN.Namespace == "" || (ancestor.AncestorRef.Namespace != nil && string(*ancestor.AncestorRef.Namespace) == refNN.Namespace)) { |
| 76 | + if findConditionInList(ancestor.Conditions, condition) { |
| 77 | + log.Printf("found condition %v in list %v for %s reference %s", condition, ancestor.Conditions, hrpNN, refNN) |
| 78 | + return true |
| 79 | + } else { |
| 80 | + log.Printf("NOT FOUND condition %v in %v for %s reference %s", condition, ancestor.Conditions, hrpNN, refNN) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return false |
| 85 | + }) |
| 86 | + |
| 87 | + require.NoError(t, err, "error waiting for HTTPRoutePolicy %s status to have a Condition matching %+v", hrpNN, condition) |
| 88 | +} |
| 89 | + |
| 90 | +func PollUntilHTTPRoutePolicyHaveStatus(cli client.Client, timeout time.Duration, hrpNN types.NamespacedName, |
| 91 | + f func(httpRoutePolicy *v1alpha1.HTTPRoutePolicy) bool) error { |
| 92 | + if err := v1alpha1.AddToScheme(cli.Scheme()); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + return genericPollResource(new(v1alpha1.HTTPRoutePolicy), cli, timeout, hrpNN, f) |
| 96 | +} |
| 97 | + |
| 98 | +func parentRefToString(p gatewayv1.ParentReference) string { |
| 99 | + if p.Namespace != nil && *p.Namespace != "" { |
| 100 | + return fmt.Sprintf("%v/%v", p.Namespace, p.Name) |
| 101 | + } |
| 102 | + return string(p.Name) |
| 103 | +} |
| 104 | + |
| 105 | +func findConditionInList(conditions []metav1.Condition, expected metav1.Condition) bool { |
| 106 | + return slices.ContainsFunc(conditions, func(item metav1.Condition) bool { |
| 107 | + // an empty Status string means "Match any status". |
| 108 | + // an empty Reason string means "Match any reason". |
| 109 | + return expected.Type == item.Type && |
| 110 | + (expected.Status == "" || expected.Status == item.Status) && |
| 111 | + (expected.Reason == "" || expected.Reason == item.Reason) && |
| 112 | + (expected.Message == "" || strings.Contains(item.Message, expected.Message)) |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +func genericPollResource[Obj client.Object](obj Obj, cli client.Client, timeout time.Duration, nn types.NamespacedName, predicate func(Obj) bool) error { |
| 117 | + return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) { |
| 118 | + if err := cli.Get(ctx, nn, obj); err != nil { |
| 119 | + return false, errors.Wrapf(err, "error fetching Object %s", nn) |
| 120 | + } |
| 121 | + return predicate(obj), nil |
| 122 | + }) |
| 123 | +} |
0 commit comments