From 4817261eb48030781fe8613ae78ec5bae3a86976 Mon Sep 17 00:00:00 2001 From: Philipp Hossner Date: Wed, 5 Mar 2025 23:56:07 +0100 Subject: [PATCH] BUG: Make ingress pathType "Exact" always override pathType "Prefix" --- deploy/tests/e2e/client.go | 4 ++-- deploy/tests/e2e/map-updates/update_test.go | 20 +++++++++++++++----- pkg/controller/controller.go | 6 ++++++ pkg/haproxy/main.go | 1 + pkg/route/route.go | 13 +++++++------ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/deploy/tests/e2e/client.go b/deploy/tests/e2e/client.go index 3eb62506..f5884016 100644 --- a/deploy/tests/e2e/client.go +++ b/deploy/tests/e2e/client.go @@ -193,7 +193,7 @@ func runtimeCommand(command string) (result []byte, err error) { if err != nil { return } - result = make([]byte, 1024) + result = make([]byte, 2048) _, err = conn.Read(result) conn.Close() return @@ -208,7 +208,7 @@ func GetHAProxyMapCount(mapName string) (count int, err error) { scanner := bufio.NewScanner(bytes.NewReader(result)) for scanner.Scan() { line := scanner.Text() - if strings.Contains(line, mapName) { + if strings.Contains(line, mapName+".map") { r := regexp.MustCompile("entry_cnt=[0-9]*") match := r.FindString(line) nbr := strings.Split(match, "=")[1] diff --git a/deploy/tests/e2e/map-updates/update_test.go b/deploy/tests/e2e/map-updates/update_test.go index 8777f6da..12db74a1 100644 --- a/deploy/tests/e2e/map-updates/update_test.go +++ b/deploy/tests/e2e/map-updates/update_test.go @@ -30,17 +30,27 @@ func (suite *MapUpdateSuite) Test_Update() { suite.tmplData.Paths = append(suite.tmplData.Paths, strconv.Itoa(i)) } oldInfo, err := e2e.GetGlobalHAProxyInfo() - oldCount, err := e2e.GetHAProxyMapCount("path-prefix") + oldCountExact, err := e2e.GetHAProxyMapCount("path-exact") + suite.Require().NoError(err) + oldCountPrefixExact, err := e2e.GetHAProxyMapCount("path-prefix-exact") + suite.Require().NoError(err) + oldCountPrefix, err := e2e.GetHAProxyMapCount("path-prefix") suite.Require().NoError(err) suite.Require().NoError(suite.test.Apply("config/ingress.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) suite.Require().Eventually(func() bool { newInfo, err := e2e.GetGlobalHAProxyInfo() suite.Require().NoError(err) - count, err := e2e.GetHAProxyMapCount("path-prefix") + countExact, err := e2e.GetHAProxyMapCount("path-exact") + suite.Require().NoError(err) + countPrefixExact, err := e2e.GetHAProxyMapCount("path-prefix-exact") + suite.Require().NoError(err) + countPrefix, err := e2e.GetHAProxyMapCount("path-prefix") suite.Require().NoError(err) - numOfAddedEntries := count - oldCount + 1 // We add one because there's already an entry at the begining which will be removed - suite.T().Logf("oldInfo.Pid(%s) == newInfo.Pid(%s) && additional path-prefix.count(%d) == %d", oldInfo.Pid, newInfo.Pid, numOfAddedEntries, n) - return oldInfo.Pid == newInfo.Pid && numOfAddedEntries == n + numOfAddedEntriesExact := countExact - oldCountExact + numOfAddedEntriesPrefixExact := countPrefixExact - oldCountPrefixExact + numOfAddedEntriesPrefix := countPrefix - oldCountPrefix + 1 // We add one because there's already an entry at the beginning which will be removed + suite.T().Logf("oldInfo.Pid(%s) == newInfo.Pid(%s) && additional path-exact.count(%d) == %d && additional path-prefix-exact.count(%d) == %d && additional path-prefix.count(%d) == %d", oldInfo.Pid, newInfo.Pid, numOfAddedEntriesExact, 0, numOfAddedEntriesPrefixExact, n, numOfAddedEntriesPrefix, n) + return oldInfo.Pid == newInfo.Pid && numOfAddedEntriesExact == 0 && numOfAddedEntriesPrefixExact == n && numOfAddedEntriesPrefix == n }, e2e.WaitDuration, e2e.TickDuration) }) } diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 18f3b115..46fb33d0 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -307,6 +307,12 @@ func (c *HAProxyController) setupHAProxyRules() error { Scope: "txn", Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map(%s)", maps.GetPath(route.PATH_EXACT)), }, false), + c.haproxy.AddRule(frontend, rules.ReqSetVar{ + Name: "path_match", + Scope: "txn", + Expression: fmt.Sprintf("var(txn.host_match),concat(,txn.path,),map(%s)", maps.GetPath(route.PATH_PREFIX_EXACT)), + CondTest: "!{ var(txn.path_match) -m found }", + }, false), c.haproxy.AddRule(frontend, rules.ReqSetVar{ Name: "path_match", Scope: "txn", diff --git a/pkg/haproxy/main.go b/pkg/haproxy/main.go index f9c945b0..b4f40f6b 100644 --- a/pkg/haproxy/main.go +++ b/pkg/haproxy/main.go @@ -53,6 +53,7 @@ func New(osArgs utils.OSArgs, env env.Env, cfgFile []byte, p process.Process, cl route.SNI, route.HOST, route.PATH_EXACT, + route.PATH_PREFIX_EXACT, route.PATH_PREFIX, } if h.Maps, err = maps.New(env.MapsDir, persistentMaps); err != nil { diff --git a/pkg/route/route.go b/pkg/route/route.go index 69b1e541..30b5ab81 100644 --- a/pkg/route/route.go +++ b/pkg/route/route.go @@ -33,10 +33,11 @@ const ( FrontendHTTP = "http" FrontendHTTPS = "https" // Routing Maps - SNI maps.Name = "sni" - HOST maps.Name = "host" - PATH_EXACT maps.Name = "path-exact" - PATH_PREFIX maps.Name = "path-prefix" + SNI maps.Name = "sni" + HOST maps.Name = "host" + PATH_EXACT maps.Name = "path-exact" + PATH_PREFIX_EXACT maps.Name = "path-prefix-exact" + PATH_PREFIX maps.Name = "path-prefix" ) var ( @@ -87,11 +88,11 @@ func AddHostPathRoute(route Route, mapFiles maps.Maps) error { mapFiles.MapAppend(PATH_PREFIX, route.Host+"/"+"\t\t\t"+value) case route.Path.PathTypeMatch == store.PATH_TYPE_PREFIX: path = strings.TrimSuffix(path, "/") - mapFiles.MapAppend(PATH_EXACT, route.Host+path+"\t\t\t"+value) + mapFiles.MapAppend(PATH_PREFIX_EXACT, route.Host+path+"\t\t\t"+value) mapFiles.MapAppend(PATH_PREFIX, route.Host+path+"/"+"\t\t\t"+value) case route.Path.PathTypeMatch == store.PATH_TYPE_IMPLEMENTATION_SPECIFIC: path = strings.TrimSuffix(path, "/") - mapFiles.MapAppend(PATH_EXACT, route.Host+path+"\t\t\t"+value) + mapFiles.MapAppend(PATH_PREFIX_EXACT, route.Host+path+"\t\t\t"+value) mapFiles.MapAppend(PATH_PREFIX, route.Host+path+"\t\t\t"+value) default: return fmt.Errorf("unknown path type '%s' with backend '%s'", route.Path.PathTypeMatch, route.BackendName)