Skip to content

Commit faf4f56

Browse files
weltekialexellis
authored andcommitted
Don't modify the client gateway url struct in client methods
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 6e40ab0 commit faf4f56

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

client.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Clien
8484

8585
// GetNamespaces get openfaas namespaces
8686
func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
87-
u := s.GatewayURL
8887
namespaces := []string{}
88+
89+
u, _ := url.Parse(s.GatewayURL.String())
8990
u.Path = "/system/namespaces"
9091

9192
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
@@ -130,7 +131,7 @@ func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
130131

131132
// GetNamespaces get openfaas namespaces
132133
func (s *Client) GetNamespace(ctx context.Context, namespace string) (types.FunctionNamespace, error) {
133-
u := s.GatewayURL
134+
u, _ := url.Parse(s.GatewayURL.String())
134135
u.Path = fmt.Sprintf("/system/namespace/%s", namespace)
135136

136137
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
@@ -196,7 +197,7 @@ func (s *Client) CreateNamespace(ctx context.Context, spec types.FunctionNamespa
196197

197198
bodyReader := bytes.NewReader(bodyBytes)
198199

199-
u := s.GatewayURL
200+
u, _ := url.Parse(s.GatewayURL.String())
200201
u.Path = "/system/namespace/"
201202

202203
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bodyReader)
@@ -255,7 +256,7 @@ func (s *Client) UpdateNamespace(ctx context.Context, spec types.FunctionNamespa
255256

256257
bodyReader := bytes.NewReader(bodyBytes)
257258

258-
u := s.GatewayURL
259+
u, _ := url.Parse(s.GatewayURL.String())
259260
u.Path = fmt.Sprintf("/system/namespace/%s", spec.Name)
260261

261262
req, err := http.NewRequestWithContext(ctx, http.MethodPut, u.String(), bodyReader)
@@ -310,7 +311,7 @@ func (s *Client) DeleteNamespace(ctx context.Context, namespace string) error {
310311
bodyBytes, _ := json.Marshal(delReq)
311312
bodyReader := bytes.NewReader(bodyBytes)
312313

313-
u := s.GatewayURL
314+
u, _ := url.Parse(s.GatewayURL.String())
314315
u.Path = fmt.Sprintf("/system/namespace/%s", namespace)
315316

316317
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), bodyReader)
@@ -357,8 +358,7 @@ func (s *Client) DeleteNamespace(ctx context.Context, namespace string) error {
357358

358359
// GetFunctions lists all functions
359360
func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.FunctionStatus, error) {
360-
u := s.GatewayURL
361-
361+
u, _ := url.Parse(s.GatewayURL.String())
362362
u.Path = "/system/functions"
363363

364364
if len(namespace) > 0 {
@@ -399,8 +399,7 @@ func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
399399
}
400400

401401
func (s *Client) GetInfo(ctx context.Context) (SystemInfo, error) {
402-
u := s.GatewayURL
403-
402+
u, _ := url.Parse(s.GatewayURL.String())
404403
u.Path = "/system/info"
405404

406405
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
@@ -436,8 +435,7 @@ func (s *Client) GetInfo(ctx context.Context) (SystemInfo, error) {
436435

437436
// GetFunction gives a richer payload than GetFunctions, but for a specific function
438437
func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types.FunctionStatus, error) {
439-
u := s.GatewayURL
440-
438+
u, _ := url.Parse(s.GatewayURL.String())
441439
u.Path = "/system/function/" + name
442440

443441
if len(namespace) > 0 {
@@ -495,7 +493,7 @@ func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionD
495493

496494
bodyReader := bytes.NewReader(bodyBytes)
497495

498-
u := s.GatewayURL
496+
u, _ := url.Parse(s.GatewayURL.String())
499497
u.Path = "/system/functions"
500498

501499
req, err := http.NewRequestWithContext(ctx, method, u.String(), bodyReader)
@@ -546,11 +544,8 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
546544
bodyBytes, _ := json.Marshal(scaleReq)
547545
bodyReader := bytes.NewReader(bodyBytes)
548546

549-
u := s.GatewayURL
550-
551-
functionPath := filepath.Join("/system/scale-function", functionName)
552-
553-
u.Path = functionPath
547+
u, _ := url.Parse(s.GatewayURL.String())
548+
u.Path = filepath.Join("/system/scale-function", functionName)
554549

555550
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bodyReader)
556551
if err != nil {
@@ -607,7 +602,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
607602
bodyBytes, _ := json.Marshal(delReq)
608603
bodyReader := bytes.NewReader(bodyBytes)
609604

610-
u := s.GatewayURL
605+
u, _ := url.Parse(s.GatewayURL.String())
611606
u.Path = "/system/functions"
612607

613608
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), bodyReader)
@@ -654,7 +649,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
654649

655650
// GetSecrets list all secrets
656651
func (s *Client) GetSecrets(ctx context.Context, namespace string) ([]types.Secret, error) {
657-
u := s.GatewayURL
652+
u, _ := url.Parse(s.GatewayURL.String())
658653
u.Path = "/system/secrets"
659654

660655
if len(namespace) > 0 {
@@ -704,7 +699,7 @@ func (s *Client) CreateSecret(ctx context.Context, spec types.Secret) (int, erro
704699

705700
bodyReader := bytes.NewReader(bodyBytes)
706701

707-
u := s.GatewayURL
702+
u, _ := url.Parse(s.GatewayURL.String())
708703
u.Path = "/system/secrets"
709704

710705
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bodyReader)
@@ -751,7 +746,7 @@ func (s *Client) UpdateSecret(ctx context.Context, spec types.Secret) (int, erro
751746

752747
bodyReader := bytes.NewReader(bodyBytes)
753748

754-
u := s.GatewayURL
749+
u, _ := url.Parse(s.GatewayURL.String())
755750
u.Path = "/system/secrets"
756751

757752
req, err := http.NewRequestWithContext(ctx, http.MethodPut, u.String(), bodyReader)
@@ -804,7 +799,7 @@ func (s *Client) DeleteSecret(ctx context.Context, secretName, namespace string)
804799
bodyBytes, _ := json.Marshal(delReq)
805800
bodyReader := bytes.NewReader(bodyBytes)
806801

807-
u := s.GatewayURL
802+
u, _ := url.Parse(s.GatewayURL.String())
808803
u.Path = "/system/secrets"
809804

810805
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), bodyReader)
@@ -877,7 +872,7 @@ func (s *Client) GetLogs(ctx context.Context, functionName, namespace string, fo
877872

878873
var err error
879874

880-
u := s.GatewayURL
875+
u, _ := url.Parse(s.GatewayURL.String())
881876
u.Path = "/system/logs"
882877

883878
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)

0 commit comments

Comments
 (0)