-
Notifications
You must be signed in to change notification settings - Fork 33
/
wasm_utils.go
178 lines (152 loc) · 5.35 KB
/
wasm_utils.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
package rlptools
import (
"encoding/json"
"errors"
"fmt"
"reflect"
_struct "github.com/golang/protobuf/ptypes/struct"
istioclientgoextensionv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
"github.com/kuadrant/kuadrant-operator/pkg/common"
)
var (
WASMFilterImageURL = common.FetchEnv("RELATED_IMAGE_WASMSHIM", "oci://quay.io/kuadrant/wasm-shim:latest")
)
type GatewayAction struct {
Configurations []kuadrantv1beta1.Configuration `json:"configurations"`
// +optional
Rules []kuadrantv1beta1.Rule `json:"rules,omitempty"`
}
func DefaultGatewayConfiguration(key client.ObjectKey) []kuadrantv1beta1.Configuration {
return []kuadrantv1beta1.Configuration{
{
Actions: []kuadrantv1beta1.ActionSpecifier{
{
GenericKey: &kuadrantv1beta1.GenericKeySpec{
DescriptorValue: key.String(),
// using default value as specified in Envoy spec
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#config-route-v3-ratelimit-action-generickey
DescriptorKey: &[]string{"ratelimitpolicy"}[0],
},
},
},
},
}
}
// GatewayActionsFromRateLimitPolicy return flatten list from GatewayAction from the RLP
func GatewayActionsFromRateLimitPolicy(rlp *kuadrantv1beta1.RateLimitPolicy, route *gatewayapiv1alpha2.HTTPRoute) []GatewayAction {
flattenActions := make([]GatewayAction, 0)
if rlp == nil {
return flattenActions
}
for idx := range rlp.Spec.RateLimits {
// Skip those RateLimit objects with empty configurations, even if they have rules defined
if len(rlp.Spec.RateLimits[idx].Configurations) == 0 {
continue
}
// if HTTPRoute is available, fill empty rules with defaults from the route
rules := rlp.Spec.RateLimits[idx].Rules
if route != nil && len(rules) == 0 {
rules = HTTPRouteRulesToRLPRules(common.RulesFromHTTPRoute(route))
}
flattenActions = append(flattenActions, GatewayAction{
Configurations: rlp.Spec.RateLimits[idx].Configurations,
Rules: rules,
})
}
if len(rlp.Spec.RateLimits) > 0 && len(flattenActions) == 0 {
// no configurations specified in the rlp,
// then apply the default configuration (action list) and default rules from the route
flattenActions = []GatewayAction{
{
Configurations: DefaultGatewayConfiguration(client.ObjectKeyFromObject(rlp)),
Rules: HTTPRouteRulesToRLPRules(common.RulesFromHTTPRoute(route)),
},
}
}
return flattenActions
}
func HTTPRouteRulesToRLPRules(httpRouteRules []common.HTTPRouteRule) []kuadrantv1beta1.Rule {
rlpRules := make([]kuadrantv1beta1.Rule, 0, len(httpRouteRules))
for idx := range httpRouteRules {
var tmp []string
rlpRules = append(rlpRules, kuadrantv1beta1.Rule{
// copy slice
Paths: append(tmp, httpRouteRules[idx].Paths...),
Methods: append(tmp, httpRouteRules[idx].Methods...),
Hosts: append(tmp, httpRouteRules[idx].Hosts...),
})
}
return rlpRules
}
type RateLimitPolicy struct {
Name string `json:"name"`
RateLimitDomain string `json:"rate_limit_domain"`
UpstreamCluster string `json:"upstream_cluster"`
Hostnames []string `json:"hostnames"`
// +optional
GatewayActions []GatewayAction `json:"gateway_actions,omitempty"`
}
type WASMPlugin struct {
FailureModeDeny bool `json:"failure_mode_deny"`
RateLimitPolicies []RateLimitPolicy `json:"rate_limit_policies"`
}
func (w *WASMPlugin) ToStruct() (*_struct.Struct, error) {
wasmPluginJSON, err := json.Marshal(w)
if err != nil {
return nil, err
}
pluginConfigStruct := &_struct.Struct{}
if err := pluginConfigStruct.UnmarshalJSON(wasmPluginJSON); err != nil {
return nil, err
}
return pluginConfigStruct, nil
}
func WASMPluginFromStruct(structure *_struct.Struct) (*WASMPlugin, error) {
if structure == nil {
return nil, errors.New("cannot desestructure WASMPlugin from nil")
}
// Serialize struct into json
configJSON, err := structure.MarshalJSON()
if err != nil {
return nil, err
}
// Deserialize struct into PluginConfig struct
wasmPlugin := &WASMPlugin{}
if err := json.Unmarshal(configJSON, wasmPlugin); err != nil {
return nil, err
}
return wasmPlugin, nil
}
type GatewayActionsByDomain map[string][]GatewayAction
func (g GatewayActionsByDomain) String() string {
jsonData, _ := json.MarshalIndent(g, "", " ")
return string(jsonData)
}
func WASMPluginMutator(existingObj, desiredObj client.Object) (bool, error) {
update := false
existing, ok := existingObj.(*istioclientgoextensionv1alpha1.WasmPlugin)
if !ok {
return false, fmt.Errorf("%T is not a *istioclientgoextensionv1alpha1.WasmPlugin", existingObj)
}
desired, ok := desiredObj.(*istioclientgoextensionv1alpha1.WasmPlugin)
if !ok {
return false, fmt.Errorf("%T is not a *istioclientgoextensionv1alpha1.WasmPlugin", desiredObj)
}
existingWASMPlugin, err := WASMPluginFromStruct(existing.Spec.PluginConfig)
if err != nil {
return false, err
}
desiredWASMPlugin, err := WASMPluginFromStruct(desired.Spec.PluginConfig)
if err != nil {
return false, err
}
// TODO(eastizle): reflect.DeepEqual does not work well with lists without order
if !reflect.DeepEqual(desiredWASMPlugin, existingWASMPlugin) {
update = true
existing.Spec.PluginConfig = desired.Spec.PluginConfig
}
return update, nil
}