Skip to content

Commit

Permalink
pr: heap the names as you go
Browse files Browse the repository at this point in the history
  • Loading branch information
rainest committed Oct 6, 2023
1 parent 978b618 commit b6e6b7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
14 changes: 7 additions & 7 deletions internal/dataplane/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2940,29 +2940,29 @@ func TestParserSNI(t *testing.T) {
state.Services[0].Routes[0].Route.Tags = nil
state.Services[0].Routes[1].Route.Tags = nil
assert.Equal(t, kong.Route{
Name: kong.String("default.foo.foo-svc._.example.com.80"),
Name: kong.String("default.foo.foo-svc.example.com.80"),
StripPath: kong.Bool(false),
RegexPriority: kong.Int(0),
ResponseBuffering: kong.Bool(true),
RequestBuffering: kong.Bool(true),
Hosts: kong.StringSlice("*.example.com"),
SNIs: nil,
Hosts: kong.StringSlice("example.com"),
PreserveHost: kong.Bool(true),
Paths: kong.StringSlice("/"),
Protocols: kong.StringSlice("http", "https"),
ID: kong.String("cbdfe994-15d4-5336-909a-e302ed66e19a"),
ID: kong.String("99296cc1-ab30-59f8-b204-7b1a45e64cac"),
}, state.Services[0].Routes[0].Route)
assert.Equal(t, kong.Route{
Name: kong.String("default.foo.foo-svc.example.com.80"),
Name: kong.String("default.foo.foo-svc._.example.com.80"),
StripPath: kong.Bool(false),
RegexPriority: kong.Int(0),
ResponseBuffering: kong.Bool(true),
RequestBuffering: kong.Bool(true),
Hosts: kong.StringSlice("example.com"),
Hosts: kong.StringSlice("*.example.com"),
SNIs: nil,
PreserveHost: kong.Bool(true),
Paths: kong.StringSlice("/"),
Protocols: kong.StringSlice("http", "https"),
ID: kong.String("99296cc1-ab30-59f8-b204-7b1a45e64cac"),
ID: kong.String("cbdfe994-15d4-5336-909a-e302ed66e19a"),
}, state.Services[0].Routes[1].Route)
})
t.Run("route does not include SNI when TLS info absent", func(t *testing.T) {
Expand Down
19 changes: 8 additions & 11 deletions internal/dataplane/parser/translators/ingress.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package translators

import (
"container/heap"
"fmt"
"regexp"
"slices"
"strings"
"time"
"unicode"
Expand Down Expand Up @@ -98,12 +98,14 @@ const (
type ingressTranslationIndex struct {
cache map[string]*ingressTranslationMeta
featureFlags TranslateIngressFeatureFlags
order *util.StringHeap
}

func newIngressTranslationIndex(flags TranslateIngressFeatureFlags) *ingressTranslationIndex {
return &ingressTranslationIndex{
cache: make(map[string]*ingressTranslationMeta),
featureFlags: flags,
order: &util.StringHeap{},
}
}

Expand Down Expand Up @@ -143,6 +145,9 @@ func (i *ingressTranslationIndex) Add(ingress *netv1.Ingress, addRegexPrefix add
servicePort: port,
addRegexPrefixFn: addRegexPrefix,
}
// the order of routes ultimately doesn't matter, but using a consistent order simplifies unit testing,
// as the output only has array indices and cannot be accessed by name
heap.Push(i.order, cacheKey)
}

meta.parentIngress = ingress
Expand All @@ -154,16 +159,8 @@ func (i *ingressTranslationIndex) Add(ingress *netv1.Ingress, addRegexPrefix add

func (i *ingressTranslationIndex) Translate() map[string]kongstate.Service {
kongStateServiceCache := make(map[string]kongstate.Service)
// although this has no effect on the end route set, some parser tests are sensitive to route indices,
// so we sort here to avoid flakes
keys := make([]string, len(i.cache))
n := 0
for key := range i.cache {
keys[n] = key
n++
}
slices.Sort(keys)
for _, key := range keys {
for i.order.Len() > 0 {
key := i.order.Pop().(string)
meta := i.cache[key]
kongServiceName := meta.generateKongServiceName()
kongStateService, ok := kongStateServiceCache[kongServiceName]
Expand Down
28 changes: 28 additions & 0 deletions internal/util/stringheap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package util

// StringHeap is an array of strings that implements container/heap.Interface
type StringHeap []string

func (h StringHeap) Len() int {
return len(h)
}

func (h StringHeap) Less(i, j int) bool {
return h[i] < h[j]
}

func (h StringHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}

func (h *StringHeap) Push(s any) {
*h = append(*h, s.(string))
}

func (h *StringHeap) Pop() any {
old := *h
n := len(old)
s := old[n-1]
*h = old[0 : n-1]
return s
}

0 comments on commit b6e6b7b

Please sign in to comment.