-
Notifications
You must be signed in to change notification settings - Fork 27
/
envoy_filters.go
86 lines (78 loc) · 2.71 KB
/
envoy_filters.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
package istio
import (
"encoding/json"
"fmt"
istioapiv1alpha3 "istio.io/api/networking/v1alpha3"
istionetworkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kuadrant/kuadrant-operator/pkg/common"
)
// LimitadorClusterPatch returns an EnvoyFilter patch that adds a custom cluster entry to compensate for kuadrant/limitador#53.
// Note: This should be removed once the mentioned issue is fixed but that will take some time.
func LimitadorClusterPatch(limitadorSvcHost string, limitadorGRPCPort int) ([]*istioapiv1alpha3.EnvoyFilter_EnvoyConfigObjectPatch, error) {
// The patch defines the rate_limit_cluster, which provides the endpoint location of the external rate limit service.
patchUnstructured := map[string]any{
"operation": "ADD",
"value": map[string]any{
"name": common.KuadrantRateLimitClusterName,
"type": "STRICT_DNS",
"connect_timeout": "1s",
"lb_policy": "ROUND_ROBIN",
"http2_protocol_options": map[string]any{},
"load_assignment": map[string]any{
"cluster_name": common.KuadrantRateLimitClusterName,
"endpoints": []map[string]any{
{
"lb_endpoints": []map[string]any{
{
"endpoint": map[string]any{
"address": map[string]any{
"socket_address": map[string]any{
"address": limitadorSvcHost,
"port_value": limitadorGRPCPort,
},
},
},
},
},
},
},
},
},
}
patchRaw, _ := json.Marshal(patchUnstructured)
patch := &istioapiv1alpha3.EnvoyFilter_Patch{}
err := patch.UnmarshalJSON(patchRaw)
if err != nil {
return nil, err
}
return []*istioapiv1alpha3.EnvoyFilter_EnvoyConfigObjectPatch{
{
ApplyTo: istioapiv1alpha3.EnvoyFilter_CLUSTER,
Match: &istioapiv1alpha3.EnvoyFilter_EnvoyConfigObjectMatch{
ObjectTypes: &istioapiv1alpha3.EnvoyFilter_EnvoyConfigObjectMatch_Cluster{
Cluster: &istioapiv1alpha3.EnvoyFilter_ClusterMatch{
Service: limitadorSvcHost,
},
},
},
Patch: patch,
},
}, nil
}
func AlwaysUpdateEnvoyFilter(existingObj, desiredObj client.Object) (bool, error) {
existing, ok := existingObj.(*istionetworkingv1alpha3.EnvoyFilter)
if !ok {
return false, fmt.Errorf("%T is not a *istionetworkingv1alpha3.EnvoyFilter", existingObj)
}
desired, ok := desiredObj.(*istionetworkingv1alpha3.EnvoyFilter)
if !ok {
return false, fmt.Errorf("%T is not a *istionetworkingv1alpha3.EnvoyFilter", desiredObj)
}
existing.Spec = istioapiv1alpha3.EnvoyFilter{
WorkloadSelector: desired.Spec.WorkloadSelector,
ConfigPatches: desired.Spec.ConfigPatches,
Priority: desired.Spec.Priority,
}
return true, nil
}