Skip to content

Commit b1c45e2

Browse files
nitishkumar71alexellis
authored andcommitted
add faas debug support
Signed-off-by: Nitishkumar Singh <[email protected]>
1 parent f4ad8ae commit b1c45e2

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

client.go

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,60 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11+
"os"
1112
"path/filepath"
13+
"strings"
1214

1315
"github.com/openfaas/faas-provider/types"
1416
)
1517

1618
// Client is used to manage OpenFaaS functions
1719
type Client struct {
1820
GatewayURL *url.URL
19-
Client *http.Client
21+
client *http.Client
2022
ClientAuth ClientAuth
2123
}
2224

25+
// Wrap http request Do function to support debug capabilities
26+
func (s *Client) do(req *http.Request) (*http.Response, error) {
27+
if os.Getenv("FAAS_DEBUG") == "1" {
28+
29+
fmt.Printf("%s %s\n", req.Method, req.URL.String())
30+
for k, v := range req.Header {
31+
if k == "Authorization" {
32+
auth := "[REDACTED]"
33+
if len(v) == 0 {
34+
auth = "[NOT_SET]"
35+
} else {
36+
l, _, ok := strings.Cut(v[0], " ")
37+
if ok && (l == "Basic" || l == "Bearer") {
38+
auth = l + " REDACTED"
39+
}
40+
}
41+
fmt.Printf("%s: %s\n", k, auth)
42+
43+
} else {
44+
fmt.Printf("%s: %s\n", k, v)
45+
}
46+
}
47+
48+
if req.Body != nil {
49+
r := io.NopCloser(req.Body)
50+
buf := new(strings.Builder)
51+
_, err := io.Copy(buf, r)
52+
if err != nil {
53+
return nil, err
54+
}
55+
bodyDebug := buf.String()
56+
if len(bodyDebug) > 0 {
57+
fmt.Printf("%s\n", bodyDebug)
58+
}
59+
req.Body = io.NopCloser(strings.NewReader(buf.String()))
60+
}
61+
}
62+
return s.client.Do(req)
63+
}
64+
2365
// ClientAuth an interface for client authentication.
2466
// to add authentication to the client implement this interface
2567
type ClientAuth interface {
@@ -30,7 +72,7 @@ type ClientAuth interface {
3072
func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Client {
3173
return &Client{
3274
GatewayURL: gatewayURL,
33-
Client: client,
75+
client: client,
3476
ClientAuth: auth,
3577
}
3678
}
@@ -52,7 +94,7 @@ func (s *Client) GetNamespaces(ctx context.Context) ([]string, error) {
5294
}
5395
}
5496

55-
res, err := s.Client.Do(req)
97+
res, err := s.do(req)
5698
if err != nil {
5799
return namespaces, fmt.Errorf("unable to make request: %w", err)
58100
}
@@ -97,7 +139,7 @@ func (s *Client) GetNamespace(ctx context.Context, namespace string) (types.Func
97139
}
98140
}
99141

100-
res, err := s.Client.Do(req)
142+
res, err := s.do(req)
101143
if err != nil {
102144
return types.FunctionNamespace{}, fmt.Errorf("unable to make HTTP request: %w", err)
103145
}
@@ -163,7 +205,7 @@ func (s *Client) CreateNamespace(ctx context.Context, spec types.FunctionNamespa
163205
}
164206
}
165207

166-
res, err := s.Client.Do(req)
208+
res, err := s.do(req)
167209
if err != nil {
168210
return http.StatusBadGateway, err
169211
}
@@ -222,7 +264,7 @@ func (s *Client) UpdateNamespace(ctx context.Context, spec types.FunctionNamespa
222264
}
223265
}
224266

225-
res, err := s.Client.Do(req)
267+
res, err := s.do(req)
226268
if err != nil {
227269
return http.StatusBadGateway, err
228270
}
@@ -276,7 +318,7 @@ func (s *Client) DeleteNamespace(ctx context.Context, namespace string) error {
276318
return fmt.Errorf("unable to set Authorization header: %w", err)
277319
}
278320
}
279-
res, err := http.DefaultClient.Do(req)
321+
res, err := s.do(req)
280322
if err != nil {
281323
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s, error: %s", s.GatewayURL, err)
282324

@@ -331,7 +373,7 @@ func (s *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
331373
}
332374
}
333375

334-
res, err := s.Client.Do(req)
376+
res, err := s.do(req)
335377
if err != nil {
336378
return []types.FunctionStatus{}, fmt.Errorf("unable to make HTTP request: %w", err)
337379
}
@@ -367,7 +409,7 @@ func (s *Client) GetInfo(ctx context.Context) (SystemInfo, error) {
367409
}
368410
}
369411

370-
res, err := s.Client.Do(req)
412+
res, err := s.do(req)
371413
if err != nil {
372414
return SystemInfo{}, fmt.Errorf("unable to make HTTP request: %w", err)
373415
}
@@ -410,7 +452,7 @@ func (s *Client) GetFunction(ctx context.Context, name, namespace string) (types
410452
}
411453
}
412454

413-
res, err := s.Client.Do(req)
455+
res, err := s.do(req)
414456
if err != nil {
415457
return types.FunctionStatus{}, fmt.Errorf("unable to make HTTP request: %w", err)
416458
}
@@ -462,7 +504,7 @@ func (s *Client) deploy(ctx context.Context, method string, spec types.FunctionD
462504
}
463505
}
464506

465-
res, err := s.Client.Do(req)
507+
res, err := s.do(req)
466508
if err != nil {
467509
return http.StatusBadGateway, err
468510
}
@@ -515,7 +557,7 @@ func (s *Client) ScaleFunction(ctx context.Context, functionName, namespace stri
515557
return fmt.Errorf("unable to set Authorization header: %w", err)
516558
}
517559
}
518-
res, err := http.DefaultClient.Do(req)
560+
res, err := s.do(req)
519561
if err != nil {
520562
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s, error: %s", s.GatewayURL, err)
521563

@@ -573,7 +615,7 @@ func (s *Client) DeleteFunction(ctx context.Context, functionName, namespace str
573615
return fmt.Errorf("unable to set Authorization header: %w", err)
574616
}
575617
}
576-
res, err := http.DefaultClient.Do(req)
618+
res, err := s.do(req)
577619
if err != nil {
578620
return fmt.Errorf("cannot connect to OpenFaaS on URL: %s, error: %s", s.GatewayURL, err)
579621

0 commit comments

Comments
 (0)