Skip to content

Commit 8ea1d02

Browse files
added fix and test for handling istio header (#26)
* added test for handling istio header * added current branch to push image * updated host manager test * removed push from branch
1 parent 7d9bf56 commit 8ea1d02

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

resolver/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ require (
1212
k8s.io/client-go v0.31.0
1313
)
1414

15+
require github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
16+
1517
replace github.com/truefoundry/elasti/pkg v0.0.0 => ../pkg
1618

1719
require (
@@ -39,6 +41,7 @@ require (
3941
github.com/prometheus/client_model v0.6.1 // indirect
4042
github.com/prometheus/common v0.55.0 // indirect
4143
github.com/prometheus/procfs v0.15.1 // indirect
44+
github.com/stretchr/testify v1.9.0
4245
github.com/x448/float16 v0.8.4 // indirect
4346
go.uber.org/multierr v1.11.0 // indirect
4447
golang.org/x/oauth2 v0.21.0 // indirect

resolver/internal/hostManager/hostManager.go

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func (hm *HostManager) GetHost(req *http.Request) (*messages.Host, error) {
5151
}
5252
targetService := utils.GetPrivateSerivceName(sourceService)
5353
sourceHost := hm.removeTrailingWildcardIfNeeded(incomingHost)
54+
sourceHost = hm.removeTrailingPathIfNeeded(sourceHost)
5455
sourceHost = hm.addHTTPIfNeeded(sourceHost)
5556
targetHost := hm.replaceServiceName(sourceHost, targetService)
5657
targetHost = hm.addHTTPIfNeeded(targetHost)
@@ -149,6 +150,13 @@ func (hm *HostManager) removeTrailingWildcardIfNeeded(serviceURL string) string
149150
return serviceURL
150151
}
151152

153+
func (hm *HostManager) removeTrailingPathIfNeeded(serviceURL string) string {
154+
if idx := strings.Index(serviceURL, "/"); idx != -1 {
155+
return serviceURL[:idx]
156+
}
157+
return serviceURL
158+
}
159+
152160
// replaceServiceName replaces the service name in the service URL
153161
func (hm *HostManager) replaceServiceName(serviceURL, newServiceName string) string {
154162
parts := strings.Split(serviceURL, ".")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package hostManager
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/truefoundry/elasti/pkg/messages"
10+
"go.uber.org/zap"
11+
)
12+
13+
func TestGetHost(t *testing.T) {
14+
logger, _ := zap.NewDevelopment()
15+
hm := NewHostManager(logger, 10*time.Second, "X-Envoy-Decorator-Operation")
16+
17+
tests := []struct {
18+
name string
19+
req *http.Request
20+
expectedHost *messages.Host
21+
expectedError bool
22+
}{
23+
{
24+
name: "Host in header",
25+
req: &http.Request{
26+
Host: "target.com",
27+
Header: http.Header{
28+
"X-Envoy-Decorator-Operation": []string{"service.namespace.svc.cluster.local:8080/test/*"},
29+
},
30+
},
31+
expectedHost: &messages.Host{
32+
IncomingHost: "service.namespace.svc.cluster.local:8080/test/*",
33+
Namespace: "namespace",
34+
SourceService: "service",
35+
TargetService: "elasti-service-pvt-9df6b026a8",
36+
SourceHost: "http://service.namespace.svc.cluster.local:8080",
37+
TargetHost: "http://elasti-service-pvt-9df6b026a8.namespace.svc.cluster.local:8080",
38+
TrafficAllowed: true,
39+
},
40+
expectedError: false,
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
host, err := hm.GetHost(tt.req)
47+
if tt.expectedError {
48+
assert.Error(t, err)
49+
} else {
50+
assert.NoError(t, err)
51+
}
52+
assert.Equal(t, tt.expectedHost, host)
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)