-
Notifications
You must be signed in to change notification settings - Fork 413
/
rewrites.go
184 lines (134 loc) · 6.71 KB
/
rewrites.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
180
181
182
183
184
// -------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// --------------------------------------------------------------------------------------------
package appgw
import (
"fmt"
"strings"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/annotations"
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/apis/azureapplicationgatewayrewrite/v1beta1"
n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-03-01/network"
"github.com/Azure/go-autorest/autorest/to"
"k8s.io/klog/v2"
)
// RewriteRuleSets builds the RewriteRuleSets part of the configBuilder
func (c *appGwConfigBuilder) RewriteRuleSets(cbCtx *ConfigBuilderContext) error {
if c.appGw.RewriteRuleSets == nil {
c.appGw.RewriteRuleSets = &[]n.ApplicationGatewayRewriteRuleSet{}
}
rewriteRuleSets := removeAGICGeneratedRewriteRuleSets(c.appGw.RewriteRuleSets)
rewriteRuleSets = append(rewriteRuleSets, c.getAGICRewriteRuleSets(cbCtx)...)
c.appGw.RewriteRuleSets = &rewriteRuleSets
return nil
}
// removeAGICGeneratedRewriteRuleSets removes the rewrite rule sets that were generated by AGIC
func removeAGICGeneratedRewriteRuleSets(currentRewriteRuleSets *[]n.ApplicationGatewayRewriteRuleSet) []n.ApplicationGatewayRewriteRuleSet {
if len(*currentRewriteRuleSets) == 0 {
return *currentRewriteRuleSets
}
var appGwRewriteRuleSets []n.ApplicationGatewayRewriteRuleSet
for _, rrs := range *currentRewriteRuleSets {
if rewriteRuleSetName := *(rrs.Name); !(strings.HasPrefix(rewriteRuleSetName, "crd-")) {
appGwRewriteRuleSets = append(appGwRewriteRuleSets, rrs)
}
}
return appGwRewriteRuleSets
}
// getAGICRewriteRuleSets returns all the rewrite rule sets that are referenced in atleast one of the ingress manifests
func (c appGwConfigBuilder) getAGICRewriteRuleSets(cbCtx *ConfigBuilderContext) []n.ApplicationGatewayRewriteRuleSet {
type rewriteCRDInfo struct {
ingressNamespace string
crdName string
}
uniqueRewriteRuleSetCR := map[string]rewriteCRDInfo{}
// insert all referenced rewrite rule sets into a map to avoid duplicates
for _, ingress := range cbCtx.IngressList {
klog.V(9).Infof("Looking for %s annotation in %s/%s", annotations.RewriteRuleSetCustomResourceKey, ingress.Namespace, ingress.Name)
rewriteRuleSetCRName, err := annotations.RewriteRuleSetCustomResource(ingress)
// if there is error fetching CR or if the value is "", move onto the next ingress
if err != nil || rewriteRuleSetCRName == "" {
continue
}
uniqueRewriteRuleSetCR[ingress.Namespace+"/"+rewriteRuleSetCRName] = rewriteCRDInfo{
ingressNamespace: ingress.Namespace,
crdName: rewriteRuleSetCRName,
}
}
var appGwRewriteRuleSet []n.ApplicationGatewayRewriteRuleSet
// get details of all the unique rewrite rule sets referenced in various ingress manifest
for _, rewriteRuleSetCRInfo := range uniqueRewriteRuleSetCR {
rewrite, err := c.k8sContext.GetRewriteRuleSetCustomResource(rewriteRuleSetCRInfo.ingressNamespace, rewriteRuleSetCRInfo.crdName)
if err != nil {
klog.Errorf("Error occured while fetching rewrite rule set custom resource named %s.", rewriteRuleSetCRInfo.crdName)
continue
}
appGwRewriteRuleSet = append(appGwRewriteRuleSet, c.makeRewrite(rewriteRuleSetCRInfo.ingressNamespace, rewriteRuleSetCRInfo.crdName, rewrite))
}
return appGwRewriteRuleSet
}
// c.makeRewrite converts *v1beta1.AzureApplicationGatewayRewrite to n.ApplicationGatewayRewriteRuleSet
func (c appGwConfigBuilder) makeRewrite(namespace string, rewriteRuleSetCRName string, rewrite *v1beta1.AzureApplicationGatewayRewrite) n.ApplicationGatewayRewriteRuleSet {
// prefix AGIC built rewriteRuleSets by crd- to help differentiate from user created rewrite rule sets
rewriteRuleSetCRName = fmt.Sprintf("crd-%s-%s", namespace, rewriteRuleSetCRName)
appGwRewriteRules := []n.ApplicationGatewayRewriteRule{}
for _, rr := range rewrite.Spec.RewriteRules {
appGwNewRewriteRule := n.ApplicationGatewayRewriteRule{
Name: to.StringPtr(rr.Name),
RuleSequence: to.Int32Ptr(int32(rr.RuleSequence)),
Conditions: makeConditions(rr.Conditions),
ActionSet: makeActionSet(rr.Actions),
}
appGwRewriteRules = append(appGwRewriteRules, appGwNewRewriteRule)
}
return n.ApplicationGatewayRewriteRuleSet{
Name: to.StringPtr(rewriteRuleSetCRName),
ID: to.StringPtr(c.appGwIdentifier.rewriteRuleSetID(rewriteRuleSetCRName)),
ApplicationGatewayRewriteRuleSetPropertiesFormat: &n.ApplicationGatewayRewriteRuleSetPropertiesFormat{
RewriteRules: &appGwRewriteRules,
},
}
}
// makeConditions converts []v1beta.Condition to *[]n.ApplicationGatewayRewriteRuleCondition
func makeConditions(apiConditions []v1beta1.Condition) *[]n.ApplicationGatewayRewriteRuleCondition {
appGwConditions := []n.ApplicationGatewayRewriteRuleCondition{}
for _, c := range apiConditions {
appGwConditions = append(appGwConditions, n.ApplicationGatewayRewriteRuleCondition{
IgnoreCase: to.BoolPtr(c.IgnoreCase),
Negate: to.BoolPtr(c.Negate),
Variable: to.StringPtr(c.Variable),
Pattern: to.StringPtr(c.Pattern),
})
}
return &appGwConditions
}
// makeActionSet converts v1beta1.Actions to *n.ApplicationGatewayRewriteRuleActionSet
func makeActionSet(apiActions v1beta1.Actions) *n.ApplicationGatewayRewriteRuleActionSet {
return &n.ApplicationGatewayRewriteRuleActionSet{
RequestHeaderConfigurations: makeHeaderConfigs(apiActions.RequestHeaderConfigurations),
ResponseHeaderConfigurations: makeHeaderConfigs(apiActions.ResponseHeaderConfigurations),
URLConfiguration: makeURLConfig(apiActions.UrlConfiguration),
}
}
// makeHeaderConfigs converts []v1beta1.HeaderConfiguration to *[]n.ApplicationGatewayHeaderConfiguration
func makeHeaderConfigs(apiHeaderConfigs []v1beta1.HeaderConfiguration) *[]n.ApplicationGatewayHeaderConfiguration {
appGwHeaderConfig := []n.ApplicationGatewayHeaderConfiguration{}
for _, hc := range apiHeaderConfigs {
appGwHeaderConfig = append(appGwHeaderConfig, n.ApplicationGatewayHeaderConfiguration{
HeaderName: to.StringPtr(hc.HeaderName),
HeaderValue: to.StringPtr(hc.HeaderValue),
})
}
return &appGwHeaderConfig
}
// makeURLConfig converts v1beta1.UrlConfiguration to *n.ApplicationGatewayURLConfiguration
func makeURLConfig(apiURLConfig *v1beta1.UrlConfiguration) *n.ApplicationGatewayURLConfiguration {
if apiURLConfig == nil {
return nil
}
return &n.ApplicationGatewayURLConfiguration{
ModifiedPath: to.StringPtr(apiURLConfig.ModifiedPath),
ModifiedQueryString: to.StringPtr(apiURLConfig.ModifiedQueryString),
Reroute: to.BoolPtr(apiURLConfig.Reroute),
}
}