/
describe.go
72 lines (59 loc) · 1.92 KB
/
describe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package proxy
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
types "github.com/openfaas/faas-provider/types"
)
// GetFunctionInfo get an OpenFaaS function information
func (c *Client) GetFunctionInfo(ctx context.Context, functionName string, namespace string) (types.FunctionStatus, error) {
var (
result types.FunctionStatus
err error
)
values := url.Values{}
if len(namespace) > 0 {
values.Set("namespace", namespace)
}
// Request CPU/RAM usage if available
values.Set("usage", "1")
queryPath := path.Join(functionPath, functionName)
req, err := c.newRequest(http.MethodGet, queryPath, values, nil)
if err != nil {
return result, fmt.Errorf("cannot create URL: %s, error: %w", queryPath, err)
}
res, err := c.doRequest(ctx, req)
if err != nil {
return result, fmt.Errorf("cannot connect to URL: %s, error: %w", c.GatewayURL.String(), err)
}
if res.Body != nil {
defer res.Body.Close()
}
switch res.StatusCode {
case http.StatusOK:
bytesOut, err := io.ReadAll(res.Body)
if err != nil {
return result, fmt.Errorf("cannot read result from OpenFaaS on URL: %s", c.GatewayURL.String())
}
if err := json.Unmarshal(bytesOut, &result); err != nil {
return result, fmt.Errorf("cannot parse result from OpenFaaS on URL: %s\n%w",
c.GatewayURL.String(), err)
}
case http.StatusUnauthorized:
return result, fmt.Errorf("unauthorized access, run \"faas-cli login\" to setup authentication for this server")
case http.StatusNotFound:
return result, fmt.Errorf("no such function: %s", functionName)
default:
bytesOut, err := io.ReadAll(res.Body)
if err == nil {
return result, fmt.Errorf("server returned unexpected status code: %d - %s", res.StatusCode, string(bytesOut))
}
}
return result, nil
}