This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ratelimitpolicy_finalizers.go
179 lines (155 loc) · 6.39 KB
/
ratelimitpolicy_finalizers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package apim
import (
"context"
"strings"
"github.com/go-logr/logr"
apimv1alpha1 "github.com/kuadrant/kuadrant-controller/apis/apim/v1alpha1"
"github.com/kuadrant/kuadrant-controller/pkg/common"
istio "istio.io/client-go/pkg/apis/networking/v1alpha3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
patchesFinalizer = "kuadrant.io/ratelimitpatches"
ParentRefsSeparator = ","
EnvoyFilterParentRefsAnnotation = "kuadrant.io/parentRefs"
)
// finalizeEnvoyFilters makes sure orphan EnvoyFilter resources are not left when deleting the owner RateLimitPolicy.
func (r *RateLimitPolicyReconciler) finalizeEnvoyFilters(ctx context.Context, rlp *apimv1alpha1.RateLimitPolicy) error {
logger := logr.FromContext(ctx)
logger.Info("Removing/Updating EnvoyFilter resources")
ownerRlp := client.ObjectKeyFromObject(rlp).String()
// TODO(rahulanand16nov): do the same for HTTPRoute
for idx := range rlp.Status.VirtualServices {
vs := &rlp.Status.VirtualServices[idx]
vsKey := client.ObjectKey{Namespace: rlp.Namespace, Name: vs.Name}
for _, gw := range vs.Gateways {
gwKey := client.ObjectKey{Namespace: gw.Namespace, Name: gw.Name}
filtersPatchkey := client.ObjectKey{
Namespace: gwKey.Namespace,
Name: rlFiltersPatchName(gwKey.Name),
}
filtersPatch := &istio.EnvoyFilter{}
if err := r.Client().Get(ctx, filtersPatchkey, filtersPatch); err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "failed to get ratelimits filters patch")
return err
}
logger.V(1).Info("filters patch not found", "object key", filtersPatchkey.String())
}
// TODO(eastizle): if the patch was not found, the patch name is empty, returning error
// TODO(eastizle): The ownerRef added was VS name/NS. Now removing with RLP name/NS
if err := r.removeParentRefEntry(ctx, filtersPatch, ownerRlp); err != nil {
logger.Error(err, "failed to remove parentRef on filters patch")
return err
}
logger.Info("successfully removed parentRef entry on the filters patch")
ratelimitsPatchKey := client.ObjectKey{
Namespace: gwKey.Namespace,
Name: ratelimitsPatchName(gwKey.Name, vsKey),
}
ratelimitsPatch := &istio.EnvoyFilter{}
if err := r.Client().Get(ctx, ratelimitsPatchKey, ratelimitsPatch); err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "failed to get ratelimits patch")
return err
}
logger.V(1).Info("ratelimits patch not found", "object key", ratelimitsPatchKey.String())
}
// TODO(eastizle): if the patch was not found, the patch name is empty, returning error
// TODO(eastizle): ratelimitspatch require parentRef? They are unique per VS
if err := r.removeParentRefEntry(ctx, ratelimitsPatch, ownerRlp); err != nil {
logger.Error(err, "failed to remove remove parentRef entry on ratelimits patch")
return err
}
logger.Info("successfully removed parentRef tag on ratelimits patch")
}
}
return nil
}
func (r *RateLimitPolicyReconciler) removeParentRefEntry(ctx context.Context, patch *istio.EnvoyFilter, owner string) error {
logger := logr.FromContext(ctx)
logger.Info("Removing parentRef entry from EnvoyFilter", "EnvoyFilter", patch.Name)
// find the annotation
ownerRlpsVal, present := patch.Annotations[EnvoyFilterParentRefsAnnotation]
if !present {
logger.V(1).Info("Deleting the patch since parentRef annotation was not present to avoid orphans")
// if it's not deleted then the patch will remain as an orphan once all the rlps are removed.
if err := r.Client().Delete(ctx, patch); err != nil {
logger.Error(err, "failed to delete the patch")
return err
}
return nil
}
// split into array of RateLimitPolicy names
ownerRlps := strings.Split(ownerRlpsVal, ParentRefsSeparator)
// remove the target owner
finalOwnerRlps := []string{}
for idx := range ownerRlps {
if ownerRlps[idx] == owner {
continue
}
finalOwnerRlps = append(finalOwnerRlps, ownerRlps[idx])
}
if len(finalOwnerRlps) == 0 {
logger.V(1).Info("Deleting filters patch because 0 parentRef entries found on it")
if err := r.Client().Delete(ctx, patch); err != nil {
logger.Error(err, "failed to delete the patch")
return err
}
} else {
finalOwnerRlpsVal := strings.Join(finalOwnerRlps, ParentRefsSeparator)
patch.Annotations[EnvoyFilterParentRefsAnnotation] = finalOwnerRlpsVal
if err := r.Client().Update(ctx, patch); err != nil {
logger.Error(err, "failed to update the patch")
return err
}
}
return nil
}
func (r *RateLimitPolicyReconciler) addParentRefEntry(ctx context.Context, patch *istio.EnvoyFilter, owner string) error {
logger := logr.FromContext(ctx)
logger.Info("Adding parentRef entry to EnvoyFilter", "EnvoyFilter", patch.Name)
// make sure annotation map is initialized
patchOwnerList := []string{}
if patch.Annotations == nil {
patch.Annotations = make(map[string]string)
}
if ogOwnerRlps, present := patch.Annotations[EnvoyFilterParentRefsAnnotation]; present {
patchOwnerList = strings.Split(ogOwnerRlps, ParentRefsSeparator)
}
// add the owner name if not present already
if !common.Contains(patchOwnerList, owner) {
patchOwnerList = append(patchOwnerList, owner)
}
finalOwnerVal := strings.Join(patchOwnerList, ParentRefsSeparator)
patch.Annotations[EnvoyFilterParentRefsAnnotation] = finalOwnerVal
if err := r.ReconcileResource(ctx, &istio.EnvoyFilter{}, patch, alwaysUpdateEnvoyPatches); err != nil {
logger.Error(err, "failed to create/update EnvoyFilter that patches-in ratelimit filters")
return err
}
logger.Info("Successfully added parentRef entry to the EnvoyFilter")
return nil
}
func (r *RateLimitPolicyReconciler) deleteRateLimits(ctx context.Context, rlp *apimv1alpha1.RateLimitPolicy) error {
logger := logr.FromContext(ctx)
rlpKey := client.ObjectKeyFromObject(rlp)
for i := range rlp.Spec.Limits {
ratelimitfactory := common.RateLimitFactory{
Key: client.ObjectKey{
Name: limitadorRatelimitsName(rlpKey, i+1),
// Currently, Limitador Operator (v0.2.0) will configure limitador services with
// RateLimit CRs created in the same namespace.
Namespace: common.KuadrantNamespace,
},
// rest of the parameters empty
}
rateLimit := ratelimitfactory.RateLimit()
err := r.DeleteResource(ctx, rateLimit)
logger.V(1).Info("Removing rate limit", "ratelimit", client.ObjectKeyFromObject(rateLimit), "error", err)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
}
return nil
}