|
5 | 5 | "strings"
|
6 | 6 |
|
7 | 7 | "github.com/pkg/errors"
|
| 8 | + elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" |
8 | 9 | elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
|
9 | 10 | gwv1 "sigs.k8s.io/gateway-api/apis/v1"
|
10 | 11 | )
|
@@ -234,6 +235,55 @@ func buildGrpcMethodCondition(method *gwv1.GRPCMethodMatch) ([]elbv2model.RuleCo
|
234 | 235 | }, nil
|
235 | 236 | }
|
236 | 237 |
|
| 238 | +func buildSourceIpCondition(condition elbv2gw.ListenerRuleCondition) []elbv2model.RuleCondition { |
| 239 | + return []elbv2model.RuleCondition{ |
| 240 | + { |
| 241 | + Field: elbv2model.RuleConditionField(condition.Field), |
| 242 | + SourceIPConfig: &elbv2model.SourceIPConditionConfig{ |
| 243 | + Values: condition.SourceIPConfig.Values, |
| 244 | + }, |
| 245 | + }, |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +// getRoutingConditions: returns routing conditions from listener rule configuration |
| 250 | +func getRoutingConditions(config *elbv2gw.ListenerRuleConfiguration) []elbv2gw.ListenerRuleCondition { |
| 251 | + conditions := make([]elbv2gw.ListenerRuleCondition, 0) |
| 252 | + if config != nil && config.Spec.Conditions != nil { |
| 253 | + for _, condition := range config.Spec.Conditions { |
| 254 | + conditions = append(conditions, condition) |
| 255 | + } |
| 256 | + return conditions |
| 257 | + } |
| 258 | + return nil |
| 259 | +} |
| 260 | + |
| 261 | +// BuildSourceIpInCondition : takes source ip configuration from listener rule configuration CRD, then AND it to condition list |
| 262 | +func BuildSourceIpInCondition(ruleWithPrecedence RulePrecedence, conditionsList []elbv2model.RuleCondition) []elbv2model.RuleCondition { |
| 263 | + rule := ruleWithPrecedence.CommonRulePrecedence.Rule |
| 264 | + ruleIndex := ruleWithPrecedence.CommonRulePrecedence.RuleIndexInRoute |
| 265 | + matchIndex := ruleWithPrecedence.CommonRulePrecedence.MatchIndexInRule |
| 266 | + if rule.GetListenerRuleConfig() != nil { |
| 267 | + conditionsFromRuleConfig := getRoutingConditions(rule.GetListenerRuleConfig()) |
| 268 | + for _, condition := range conditionsFromRuleConfig { |
| 269 | + switch condition.Field { |
| 270 | + case elbv2gw.ListenerRuleConditionFieldSourceIP: |
| 271 | + sourceIpCondition := buildSourceIpCondition(condition) |
| 272 | + if condition.Indexes == nil { |
| 273 | + conditionsList = append(conditionsList, sourceIpCondition...) |
| 274 | + } else { |
| 275 | + for _, index := range *condition.Indexes { |
| 276 | + if index.RuleIndex == ruleIndex && index.MatchIndex == matchIndex { |
| 277 | + conditionsList = append(conditionsList, sourceIpCondition...) |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + return conditionsList |
| 285 | +} |
| 286 | + |
237 | 287 | // generateValuesFromMatchHeaderValue takes in header value from route match
|
238 | 288 | // returns list of values
|
239 | 289 | // for a given HTTPHeaderName/GRPCHeaderName, ALB rule can accept a list of values. However, gateway route headers only accept one value per name, and name cannot duplicate.
|
|
0 commit comments