Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing: lookup host without port #609

Merged
merged 3 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Changelog.md → CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This changelog keeps track of work items that have been completed and are ready

### Fixes

- **General**: TODO ([#TODO](https://github.com/kedacore/http-add-on/issues/TODO))
- **Routing**: Lookup host without port ([#608](https://github.com/kedacore/http-add-on/issues/608))

### Deprecations

Expand Down
2 changes: 1 addition & 1 deletion interceptor/proxy_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func newForwardingHandler(
}
return
}
targetSvcURL, err := targetSvcURL(routingTarget)
targetSvcURL, err := targetSvcURL(*routingTarget)
if err != nil {
lggr.Error(err, "forwarding failed")
w.WriteHeader(500)
Expand Down
2 changes: 1 addition & 1 deletion operator/controllers/routing_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestRoutingTable(t *testing.T) {

retTarget, err := table.Lookup(host)
r.NoError(err)
r.Equal(target, retTarget)
r.Equal(&target, retTarget)

r.NoError(removeAndUpdateRoutingTable(
ctx,
Expand Down
13 changes: 13 additions & 0 deletions pkg/routing/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package routing

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestRouting(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Routing Suite")
}
21 changes: 15 additions & 6 deletions pkg/routing/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
"sync"
)

type TableReader interface {
Lookup(string) (Target, error)
Lookup(string) (*Target, error)
Hosts() []string
HasHost(string) bool
}
Expand Down Expand Up @@ -70,14 +71,22 @@ func (t *Table) UnmarshalJSON(data []byte) error {
return json.NewDecoder(b).Decode(&t.m)
}

func (t *Table) Lookup(host string) (Target, error) {
func (t *Table) Lookup(host string) (*Target, error) {
t.l.RLock()
defer t.l.RUnlock()
ret, ok := t.m[host]
if !ok {
return Target{}, ErrTargetNotFound

keys := []string{host}
if i := strings.LastIndex(host, ":"); i != -1 {
keys = append(keys, host[:i])
}
return ret, nil

for _, key := range keys {
if target, ok := t.m[key]; ok {
return &target, nil
}
}

return nil, ErrTargetNotFound
}

// AddTarget registers target for host in the routing table t
Expand Down
122 changes: 120 additions & 2 deletions pkg/routing/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package routing

import (
"encoding/json"
"math"
"math/rand"
"strconv"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -55,13 +60,13 @@ func TestTableRemove(t *testing.T) {
// add the target to the table and ensure that you can look it up
r.NoError(tbl.AddTarget(host, tgt))
retTgt, err := tbl.Lookup(host)
r.Equal(tgt, retTgt)
r.Equal(&tgt, retTgt)
r.NoError(err)

// remove the target and ensure that you can't look it up
r.NoError(tbl.RemoveTarget(host))
retTgt, err = tbl.Lookup(host)
r.Equal(Target{}, retTgt)
r.Equal((*Target)(nil), retTgt)
r.Equal(ErrTargetNotFound, err)
}

Expand Down Expand Up @@ -96,3 +101,116 @@ func TestTableReplace(t *testing.T) {

r.Equal(tbl1, tbl2)
}

var _ = Describe("Table", func() {
Describe("Lookup", func() {
var (
tltcs = newTableLookupTestCases(5)
table = NewTable()
)

Context("with new port-agnostic configuration", func() {
BeforeEach(func() {
for _, tltc := range tltcs {
err := table.AddTarget(tltc.HostWithoutPort(), tltc.Target())
Expect(err).NotTo(HaveOccurred())
}
})

AfterEach(func() {
for _, tltc := range tltcs {
err := table.RemoveTarget(tltc.HostWithoutPort())
Expect(err).NotTo(HaveOccurred())
}
})

It("should return correct target for host without port", func() {
for _, tltc := range tltcs {
target, err := table.Lookup(tltc.HostWithoutPort())
Expect(err).NotTo(HaveOccurred())
Expect(target).To(HaveValue(Equal(tltc.Target())))
}
})

It("should return correct target for host with port", func() {
for _, tltc := range tltcs {
target, err := table.Lookup(tltc.HostWithPort())
Expect(err).NotTo(HaveOccurred())
Expect(target).To(HaveValue(Equal(tltc.Target())))
}
})
})

Context("with legacy port-specific configuration", func() {
BeforeEach(func() {
for _, tltc := range tltcs {
err := table.AddTarget(tltc.HostWithPort(), tltc.Target())
Expect(err).NotTo(HaveOccurred())
}
})

AfterEach(func() {
for _, tltc := range tltcs {
err := table.RemoveTarget(tltc.HostWithPort())
Expect(err).NotTo(HaveOccurred())
}
})

It("should error for host without port", func() {
for _, tltc := range tltcs {
target, err := table.Lookup(tltc.HostWithoutPort())
Expect(err).To(MatchError(ErrTargetNotFound))
Expect(target).To(BeNil())
}
})

It("should return correct target for host with port", func() {
for _, tltc := range tltcs {
target, err := table.Lookup(tltc.HostWithPort())
Expect(err).NotTo(HaveOccurred())
Expect(target).To(HaveValue(Equal(tltc.Target())))
}
})
})
})
})

type tableLookupTestCase struct {
target Target
}

func newTableLookupTestCase() tableLookupTestCase {
target := NewTarget(
strconv.Itoa(rand.Int()),
strconv.Itoa(rand.Int()),
rand.Intn(math.MaxUint16),
strconv.Itoa(rand.Int()),
int32(rand.Intn(math.MaxUint8)),
)

return tableLookupTestCase{
target: target,
}
}

func (tltc tableLookupTestCase) Target() Target {
return tltc.target
}

func (tltc tableLookupTestCase) HostWithoutPort() string {
return tltc.target.Service + "." + tltc.target.Namespace + ".svc.cluster.local"
}

func (tltc tableLookupTestCase) HostWithPort() string {
return tltc.HostWithoutPort() + ":" + strconv.Itoa(tltc.target.Port)
}

type tableLookupTestCases []tableLookupTestCase

func newTableLookupTestCases(count uint) tableLookupTestCases {
tltcs := make(tableLookupTestCases, count)
for i := uint(0); i < count; i++ {
tltcs[i] = newTableLookupTestCase()
}
return tltcs
}