Fix an issue with gateway URL mutation

The gateway URL was not being deep cloned, but mutated and
that affected downstream tests in the certifier.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd)
2022-02-24 11:48:35 +00:00
parent 830ec7286d
commit 56b1a7db77
4 changed files with 16 additions and 38 deletions

View File

@@ -54,26 +54,6 @@ func NewClient(auth ClientAuth, gatewayURL string, transport http.RoundTripper,
}, nil
}
func (c *Client) newRequestByURL(method string, uri *url.URL, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, uri.String(), body)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
c.ClientAuth.Set(req)
return req, err
}
//newRequest create a new HTTP request with authentication
func (c *Client) newRequest(method, path string, query url.Values, body io.Reader) (*http.Request, error) {

View File

@@ -9,6 +9,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
types "github.com/openfaas/faas-provider/types"
@@ -21,21 +22,19 @@ func (c *Client) GetFunctionInfo(ctx context.Context, functionName string, names
err error
)
u := c.GatewayURL
v := u.Query()
values := url.Values{}
if len(namespace) > 0 {
v.Set("namespace", namespace)
values.Set("namespace", namespace)
}
// Request CPU/RAM usage if available
v.Set("usage", "1")
values.Set("usage", "1")
u.Path = path.Join(functionPath, functionName)
u.RawQuery = v.Encode()
queryPath := path.Join(functionPath, functionName)
req, err := c.newRequestByURL(http.MethodGet, u, nil)
req, err := c.newRequest(http.MethodGet, queryPath, values, nil)
if err != nil {
return result, fmt.Errorf("cannot create URL: %s, error: %w", u.String(), err)
return result, fmt.Errorf("cannot create URL: %s, error: %w", queryPath, err)
}
res, err := c.doRequest(ctx, req)

View File

@@ -6,11 +6,11 @@ package proxy
import (
"context"
"encoding/json"
"path"
"fmt"
"io/ioutil"
"net/http"
"net/url"
types "github.com/openfaas/faas-provider/types"
)
@@ -26,15 +26,14 @@ func (c *Client) ListFunctions(ctx context.Context, namespace string) ([]types.F
return http.ErrUseLastResponse
})
u := c.GatewayURL
u.Path = path.Join(systemPath)
v := u.Query()
if len(namespace) > 0 {
v.Set("namespace", namespace)
}
u.RawQuery = v.Encode()
queryPath := systemPath
getRequest, err := c.newRequestByURL(http.MethodGet, u, nil)
values := url.Values{}
if len(namespace) > 0 {
values.Set("namespace", namespace)
}
getRequest, err := c.newRequest(http.MethodGet, queryPath, values, nil)
if err != nil {
return nil, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", c.GatewayURL.String())
}

View File

@@ -16,8 +16,8 @@ func (c *Client) ListNamespaces(ctx context.Context) ([]string, error) {
c.AddCheckRedirect(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
})
query := url.Values{}
query := url.Values{}
getRequest, err := c.newRequest(http.MethodGet, namespacesPath, query, nil)
if err != nil {