Skip to content

Commit 59b6a88

Browse files
authored
Merge pull request #1772 from M00nF1sh/pathtype_support
add support for Ingress pathType
2 parents dd8f8c6 + d8a4fc7 commit 59b6a88

File tree

3 files changed

+392
-6
lines changed

3 files changed

+392
-6
lines changed

pkg/ingress/model_build_listener_rules.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
99
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
1010
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
11+
"strings"
1112
)
1213

1314
func (t *defaultModelBuildTask) buildListenerRules(ctx context.Context, lsARN core.StringToken, port int64, protocol elbv2model.Protocol, ingList []*networking.Ingress) error {
@@ -65,7 +66,11 @@ func (t *defaultModelBuildTask) buildRuleConditions(ctx context.Context, rule ne
6566
}
6667
var paths []string
6768
if path.Path != "" {
68-
paths = append(paths, path.Path)
69+
pathPatterns, err := t.buildPathPatterns(path.Path, path.PathType)
70+
if err != nil {
71+
return nil, err
72+
}
73+
paths = append(paths, pathPatterns...)
6974
}
7075
var conditions []elbv2model.RuleCondition
7176
for _, condition := range backend.Conditions {
@@ -118,6 +123,55 @@ func (t *defaultModelBuildTask) buildRuleConditions(ctx context.Context, rule ne
118123
return conditions, nil
119124
}
120125

126+
// buildPathPatterns will build ELBv2's path patterns for given path and pathType.
127+
func (t *defaultModelBuildTask) buildPathPatterns(path string, pathType *networking.PathType) ([]string, error) {
128+
normalizedPathType := networking.PathTypeImplementationSpecific
129+
if pathType != nil {
130+
normalizedPathType = *pathType
131+
}
132+
switch normalizedPathType {
133+
case networking.PathTypeImplementationSpecific:
134+
return t.buildPathPatternsForImplementationSpecificPathType(path)
135+
case networking.PathTypeExact:
136+
return t.buildPathPatternsForExactPathType(path)
137+
case networking.PathTypePrefix:
138+
return t.buildPathPatternsForPrefixPathType(path)
139+
default:
140+
return nil, errors.Errorf("unsupported pathType: %v", normalizedPathType)
141+
}
142+
}
143+
144+
// buildPathPatternsForImplementationSpecificPathType will build path patterns for implementationSpecific pathType.
145+
func (t *defaultModelBuildTask) buildPathPatternsForImplementationSpecificPathType(path string) ([]string, error) {
146+
return []string{path}, nil
147+
}
148+
149+
// buildPathPatternsForExactPathType will build path patterns for exact pathType.
150+
// exact path shouldn't contains any wildcards.
151+
func (t *defaultModelBuildTask) buildPathPatternsForExactPathType(path string) ([]string, error) {
152+
if strings.ContainsAny(path, "*?") {
153+
return nil, errors.Errorf("exact path shouldn't contain wildcards: %v", path)
154+
}
155+
return []string{path}, nil
156+
}
157+
158+
// buildPathPatternsForPrefixPathType will build path patterns for prefix pathType.
159+
// prefix path shouldn't contains any wildcards.
160+
// with prefixType type, both "/foo" or "/foo/" should matches path like "/foo" or "/foo/" or "/foo/bar".
161+
// for above case, we'll generate two path pattern: "/foo/" and "/foo/*".
162+
// an special case is "/", which matches all paths, thus we generate the path pattern as "/*"
163+
func (t *defaultModelBuildTask) buildPathPatternsForPrefixPathType(path string) ([]string, error) {
164+
if path == "/" {
165+
return []string{"/*"}, nil
166+
}
167+
if strings.ContainsAny(path, "*?") {
168+
return nil, errors.Errorf("prefix path shouldn't contain wildcards: %v", path)
169+
}
170+
171+
normalizedPath := strings.TrimSuffix(path, "/")
172+
return []string{normalizedPath, normalizedPath + "/*"}, nil
173+
}
174+
121175
func (t *defaultModelBuildTask) buildHTTPHeaderCondition(_ context.Context, condition RuleCondition) (elbv2model.RuleCondition, error) {
122176
if condition.HTTPHeaderConfig == nil {
123177
return elbv2model.RuleCondition{}, errors.New("missing HTTPHeaderConfig")

0 commit comments

Comments
 (0)