|
8 | 8 | "sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
|
9 | 9 | "sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
|
10 | 10 | elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
|
| 11 | + "strings" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | 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
|
65 | 66 | }
|
66 | 67 | var paths []string
|
67 | 68 | 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...) |
69 | 74 | }
|
70 | 75 | var conditions []elbv2model.RuleCondition
|
71 | 76 | for _, condition := range backend.Conditions {
|
@@ -118,6 +123,55 @@ func (t *defaultModelBuildTask) buildRuleConditions(ctx context.Context, rule ne
|
118 | 123 | return conditions, nil
|
119 | 124 | }
|
120 | 125 |
|
| 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 | + |
121 | 175 | func (t *defaultModelBuildTask) buildHTTPHeaderCondition(_ context.Context, condition RuleCondition) (elbv2model.RuleCondition, error) {
|
122 | 176 | if condition.HTTPHeaderConfig == nil {
|
123 | 177 | return elbv2model.RuleCondition{}, errors.New("missing HTTPHeaderConfig")
|
|
0 commit comments