Skip to content

Commit

Permalink
fix: sticky sessions initialization (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski authored Oct 23, 2023
1 parent ffde5d4 commit 33d4fd2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,23 @@ jobs:
- name: Test with pytest
run: |
poetry run pytest
test-revproxy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Test
run: |
go test -timeout 300s -p 1 -v github.com/SwissDataScienceCenter/renku-gateway/cmd/revproxy github.com/SwissDataScienceCenter/renku-gateway/internal/stickysessions
publish-images:
runs-on: ubuntu-latest
needs: test
needs:
- test
- test-revproxy
if: "startsWith(github.ref, 'refs/tags/')"
steps:
- uses: actions/checkout@v2
Expand Down
22 changes: 20 additions & 2 deletions cmd/revproxy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -64,10 +65,15 @@ func setupTestAuthServer(ID string, responseHeaders map[string]string, responseS
}

func setupTestRevproxy(ctx context.Context, upstreamServerURL *url.URL, upstreamServerURL2 *url.URL, authURL *url.URL, externalGitlabURL *url.URL) (*echo.Echo, *url.URL) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
port := listener.Addr().(*net.TCPAddr).Port
config := revProxyConfig{
RenkuBaseURL: upstreamServerURL,
ExternalGitlabURL: externalGitlabURL,
Port: 8090,
Port: port,
RenkuServices: renkuServicesConfig{
Notebooks: upstreamServerURL,
CoreServiceNames: []string{upstreamServerURL.String(), upstreamServerURL.String(), upstreamServerURL2.String()},
Expand All @@ -81,10 +87,12 @@ func setupTestRevproxy(ctx context.Context, upstreamServerURL *url.URL, upstream
}
proxy := setupServer(ctx, config)
go func() {
proxy.Listener = listener
err := proxy.Start(fmt.Sprintf(":%d", config.Port))
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
defer listener.Close()
}()
url, err := url.Parse(fmt.Sprintf("http://localhost:%d", config.Port))
if err != nil {
Expand Down Expand Up @@ -145,7 +153,17 @@ func ParametrizedRouteTest(scenario TestCase) func(*testing.T) {
reqURLQuery.Add(k, v)
}
reqURL.RawQuery = reqURLQuery.Encode()
res, err := http.Get(reqURL.String())
// Force ipv4 becaues Github actions seem to constantly switch to ipv6 and fail
transport := http.DefaultTransport.(*http.Transport).Clone()
var zeroDialer net.Dialer
var httpClient = &http.Client{
Timeout: 10 * time.Second,
}
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return zeroDialer.DialContext(ctx, "tcp4", addr)
}
httpClient.Transport = transport
res, err := httpClient.Get(reqURL.String())
assert.NoError(t, err)
reqs := requestTracker.getAllRequests()

Expand Down
2 changes: 1 addition & 1 deletion internal/stickysessions/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewEndpointStoreFromEndpointItems(input []EndpointStoreItem, includeNonRead
// NOTE: Make a copy of loop variable, golang uses the same pointer for the whole loop
newEndpoint := endpoint
endpoints.list = append(endpoints.list, &newEndpoint)
endpoints.index[endpoint.UID] = &endpoint
endpoints.index[endpoint.UID] = &newEndpoint
}
sort.Sort(endpoints)
return endpoints
Expand Down
15 changes: 8 additions & 7 deletions internal/stickysessions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -121,7 +122,6 @@ func setupTestUpstream(ID string) (*httptest.Server, *url.URL, discoveryV1.Endpo
func TestUpstreamSelection(t *testing.T) {
type TestCase struct {
RequestCookies []http.Cookie
ProxyPort int
UpstreamPortName string
UpstreamIDs []string
SessionCookieName string
Expand Down Expand Up @@ -155,11 +155,16 @@ func TestUpstreamSelection(t *testing.T) {
proxy := middleware.ProxyWithConfig(middleware.ProxyConfig{
Balancer: &balancer,
})
proxyListener, err := net.Listen("tcp", "127.0.0.1:0")
assert.NoError(t, err)
proxyPort := proxyListener.Addr().(*net.TCPAddr).Port
e.Listener = proxyListener
defer proxyListener.Close()
e.Use(middleware.Logger())
e.Group("/").Use(proxy)
go e.Start(fmt.Sprintf(":%d", testCase.ProxyPort))
go e.Start(fmt.Sprintf(":%d", proxyPort))
defer e.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/", testCase.ProxyPort), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/", proxyPort), nil)
assert.NoError(t, err)
for _, c := range testCase.RequestCookies {
aCookie := c
Expand Down Expand Up @@ -187,7 +192,6 @@ func TestUpstreamSelection(t *testing.T) {

testCases := []TestCase{
{
ProxyPort: 8080,
UpstreamPortName: "http",
UpstreamIDs: []string{"host1"},
RequestCookies: []http.Cookie{},
Expand All @@ -197,7 +201,6 @@ func TestUpstreamSelection(t *testing.T) {
ExpectedResponseCookies: map[string]string{"session-cookie": "host1"},
},
{
ProxyPort: 8080,
UpstreamPortName: "http",
UpstreamIDs: []string{"host1", "host2"},
RequestCookies: []http.Cookie{{Name: "session-cookie", Value: "host2"}},
Expand All @@ -207,7 +210,6 @@ func TestUpstreamSelection(t *testing.T) {
ExpectedResponseCookies: map[string]string{},
},
{
ProxyPort: 8080,
UpstreamPortName: "http",
UpstreamIDs: []string{"host1"},
RequestCookies: []http.Cookie{{Name: "session-cookie", Value: "host2"}},
Expand All @@ -217,7 +219,6 @@ func TestUpstreamSelection(t *testing.T) {
ExpectedResponseCookies: map[string]string{"session-cookie": "host1"},
},
{
ProxyPort: 8080,
UpstreamPortName: "http",
UpstreamIDs: []string{},
RequestCookies: []http.Cookie{},
Expand Down

0 comments on commit 33d4fd2

Please sign in to comment.