|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package controllers |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + istioclientnetworkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3" |
| 13 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + "k8s.io/apimachinery/pkg/api/meta" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 18 | + gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 19 | + gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" |
| 20 | + |
| 21 | + kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" |
| 22 | + "github.com/kuadrant/kuadrant-operator/pkg/common" |
| 23 | + limitadorv1alpha1 "github.com/kuadrant/limitador-operator/api/v1alpha1" |
| 24 | +) |
| 25 | + |
| 26 | +var _ = Describe("Limitador Cluster EnvoyFilter controller", func() { |
| 27 | + var ( |
| 28 | + testNamespace string |
| 29 | + gwName = "toystore-gw" |
| 30 | + rlpName = "toystore-rlp" |
| 31 | + efName = fmt.Sprintf("kuadrant-ratelimiting-cluster-%s", gwName) |
| 32 | + ) |
| 33 | + |
| 34 | + beforeEachCallback := func() { |
| 35 | + CreateNamespace(&testNamespace) |
| 36 | + gateway := testBuildBasicGateway(gwName, testNamespace) |
| 37 | + err := k8sClient.Create(context.Background(), gateway) |
| 38 | + Expect(err).ToNot(HaveOccurred()) |
| 39 | + |
| 40 | + Eventually(func() bool { |
| 41 | + existingGateway := &gatewayapiv1beta1.Gateway{} |
| 42 | + err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway) |
| 43 | + if err != nil { |
| 44 | + logf.Log.V(1).Info("[WARN] Creating gateway failed", "error", err) |
| 45 | + return false |
| 46 | + } |
| 47 | + |
| 48 | + if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType) { |
| 49 | + logf.Log.V(1).Info("[WARN] Gateway not ready") |
| 50 | + return false |
| 51 | + } |
| 52 | + |
| 53 | + return true |
| 54 | + }, 15*time.Second, 5*time.Second).Should(BeTrue()) |
| 55 | + |
| 56 | + ApplyKuadrantCR(testNamespace) |
| 57 | + |
| 58 | + // Check Limitador Status is Ready |
| 59 | + Eventually(func() bool { |
| 60 | + limitador := &limitadorv1alpha1.Limitador{} |
| 61 | + err := k8sClient.Get(context.Background(), client.ObjectKey{Name: common.LimitadorName, Namespace: testNamespace}, limitador) |
| 62 | + if err != nil { |
| 63 | + return false |
| 64 | + } |
| 65 | + if !meta.IsStatusConditionTrue(limitador.Status.Conditions, "Ready") { |
| 66 | + return false |
| 67 | + } |
| 68 | + return true |
| 69 | + }, time.Minute, 5*time.Second).Should(BeTrue()) |
| 70 | + } |
| 71 | + |
| 72 | + BeforeEach(beforeEachCallback) |
| 73 | + AfterEach(DeleteNamespaceCallback(&testNamespace)) |
| 74 | + |
| 75 | + Context("RLP targeting Gateway", func() { |
| 76 | + It("EnvoyFilter created when RLP exists and deleted with RLP is deleted", func() { |
| 77 | + // create ratelimitpolicy |
| 78 | + rlp := &kuadrantv1beta2.RateLimitPolicy{ |
| 79 | + TypeMeta: metav1.TypeMeta{ |
| 80 | + Kind: "RateLimitPolicy", |
| 81 | + APIVersion: kuadrantv1beta2.GroupVersion.String(), |
| 82 | + }, |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: rlpName, |
| 85 | + Namespace: testNamespace, |
| 86 | + }, |
| 87 | + Spec: kuadrantv1beta2.RateLimitPolicySpec{ |
| 88 | + TargetRef: gatewayapiv1alpha2.PolicyTargetReference{ |
| 89 | + Group: gatewayapiv1beta1.Group("gateway.networking.k8s.io"), |
| 90 | + Kind: "Gateway", |
| 91 | + Name: gatewayapiv1beta1.ObjectName(gwName), |
| 92 | + }, |
| 93 | + Limits: map[string]kuadrantv1beta2.Limit{ |
| 94 | + "l1": { |
| 95 | + Rates: []kuadrantv1beta2.Rate{ |
| 96 | + { |
| 97 | + Limit: 1, Duration: 3, Unit: kuadrantv1beta2.TimeUnit("minute"), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + err := k8sClient.Create(context.Background(), rlp) |
| 105 | + Expect(err).ToNot(HaveOccurred()) |
| 106 | + // Check RLP status is available |
| 107 | + rlpKey := client.ObjectKey{Name: rlpName, Namespace: testNamespace} |
| 108 | + Eventually(testRLPIsAvailable(rlpKey), time.Minute, 5*time.Second).Should(BeTrue()) |
| 109 | + |
| 110 | + // Check envoy filter |
| 111 | + Eventually(func() bool { |
| 112 | + existingEF := &istioclientnetworkingv1alpha3.EnvoyFilter{} |
| 113 | + efKey := client.ObjectKey{Name: efName, Namespace: testNamespace} |
| 114 | + err = k8sClient.Get(context.Background(), efKey, existingEF) |
| 115 | + if err != nil { |
| 116 | + return false |
| 117 | + } |
| 118 | + return true |
| 119 | + }, 15*time.Second, 5*time.Second).Should(BeTrue()) |
| 120 | + |
| 121 | + err = k8sClient.Delete(context.Background(), rlp) |
| 122 | + Expect(err).ToNot(HaveOccurred()) |
| 123 | + |
| 124 | + // Check envoy filter is gone |
| 125 | + Eventually(func() bool { |
| 126 | + existingEF := &istioclientnetworkingv1alpha3.EnvoyFilter{} |
| 127 | + efKey := client.ObjectKey{Name: efName, Namespace: testNamespace} |
| 128 | + err = k8sClient.Get(context.Background(), efKey, existingEF) |
| 129 | + return apierrors.IsNotFound(err) |
| 130 | + }, 15*time.Second, 5*time.Second).Should(BeTrue()) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments