Skip to content
This repository was archived by the owner on Mar 10, 2023. It is now read-only.

Commit d22cdd3

Browse files
martindekovalexellis
authored andcommitted
Vendor SDK for function-logs
Vendor faas-cli SDK for function logs instead of creating custom http requests. Also rewriting unit tests and removing excess error handling which is moved in the SDK and is excessive as we don't parse JSON into the function anymore Signed-off-by: Martin Dekov <mvdekov@gmail.com>
1 parent 8c3defb commit d22cdd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+14457
-133
lines changed

function-logs/Gopkg.lock

Lines changed: 72 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

function-logs/Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
name = "github.com/openfaas/openfaas-cloud"
33
version = "0.13.3"
44

5+
[[constraint]]
6+
name = "github.com/openfaas/faas-cli"
7+
version = "0.12.0"
8+
59
[prune]
610
go-tests = true
711
unused-packages = true

function-logs/handler.go

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
package function
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
7-
"io/ioutil"
88
"log"
99
"net/http"
1010
"net/url"
1111
"os"
1212
"strings"
1313
"time"
1414

15+
faasSDK "github.com/openfaas/faas-cli/proxy"
16+
"github.com/openfaas/faas-provider/logs"
17+
"github.com/openfaas/faas-provider/types"
1518
"github.com/openfaas/openfaas-cloud/sdk"
1619
)
1720

21+
type FaaSAuth struct{}
22+
23+
func (auth *FaaSAuth) Set(req *http.Request) error {
24+
return sdk.AddBasicAuth(req)
25+
}
26+
27+
var (
28+
timeout = 5 * time.Second
29+
namespace = ""
30+
)
31+
1832
// Handle grabs the logs for the fn that is named in the input
1933
func Handle(req []byte) string {
2034

@@ -40,136 +54,88 @@ func Handle(req []byte) string {
4054
allowed, err := isUserFunction(function, gatewayURL, user)
4155

4256
if err != nil {
43-
log.Fatalf("there was an error requesting the function %q", function)
57+
log.Fatalf("there was an error requesting the function %q: %s", function, err.Error())
4458
}
4559

4660
if !allowed {
4761
log.Fatalf("requested function %q could not be found or you are not allowed to access it", function)
4862
}
4963

50-
formattedLogs, fmtErr := getFormattedLogs(gatewayURL, function)
64+
client := faasSDK.NewClient(&FaaSAuth{}, gatewayURL, nil, &timeout)
65+
66+
ctx := context.Background()
67+
68+
formattedLogs, fmtErr := getFormattedLogs(*client, ctx, function)
5169

5270
if fmtErr != nil {
5371
log.Fatalf("there was an error formatting logs for the function %q, %s", function, fmtErr)
5472
}
5573
return formattedLogs
5674
}
5775

58-
func getFormattedLogs(gatewayURL string, function string) (string, error) {
76+
func getFormattedLogs(client faasSDK.Client, ctx context.Context, function string) (string, error) {
5977

6078
if len(function) == 0 {
6179
return "", errors.New("function name was empty, please provide a valid function name")
6280
}
63-
queryParams := make(map[string]string)
64-
65-
queryParams["name"] = function
66-
queryParams["follow"] = "false"
67-
queryParams["since"] = time.Now().Add(-1 * time.Minute * 30).Format(time.RFC3339)
68-
69-
response, bodyBytes := makeGatewayHttpReq(gatewayURL+"/system/logs", queryParams)
81+
timeSince := time.Now().Add(-1 * time.Minute * 30)
82+
logRequest := logs.Request{Name: function, Since: &timeSince, Follow: false}
7083

71-
if response.StatusCode != http.StatusOK {
72-
return "", errors.New(fmt.Sprintf("unable to query logs, status: %d, message: %s", response.StatusCode, string(bodyBytes)))
84+
logChan, err := client.GetLogs(ctx, logRequest)
85+
if err != nil {
86+
return "", errors.New(fmt.Sprintf("unable to query logs, message: %s", err.Error()))
7387
}
7488

75-
formattedLogs, formatErr := formatLogs(bodyBytes)
89+
formattedLogs := formatLogs(logChan)
7690

77-
if formatErr != nil {
78-
return "", formatErr
79-
}
8091
return formattedLogs, nil
8192
}
8293

8394
func isUserFunction(function string, gatewayURL string, user string) (bool, error) {
84-
queryParams := make(map[string]string)
85-
queryParams["user"] = user
8695

8796
if len(user) == 0 {
8897
return false, errors.New("user is not set, user must be set for us to find logs")
8998
}
9099

91-
response, bodyBytes := makeGatewayHttpReq(gatewayURL+"/function/list-functions", queryParams)
100+
bytesIn := []byte("")
101+
102+
nameQuery := []string{fmt.Sprintf("user=%s", user)}
103+
104+
resBytes, err := faasSDK.InvokeFunction(gatewayURL, "list-functions", &bytesIn, "", nameQuery, nil, false, "POST", true, "")
92105

93-
if response.StatusCode != http.StatusOK {
94-
return false, errors.New(fmt.Sprintf("unable to query functions list, status: %d, message: %s", response.StatusCode, string(bodyBytes)))
106+
if err != nil {
107+
return false, errors.New(fmt.Sprintf("unable to query functions list message: %s", err.Error()))
95108
}
96109

97-
res, err := functionInResponse(bodyBytes, function, user)
110+
res, err := functionInResponse(*resBytes, function, user)
98111
if err != nil {
99112
return false, err
100113
}
101114

102115
return res, nil
103116
}
104117

105-
func formatLogs(msgBody []byte) (string, error) {
106-
if len(msgBody) == 0 {
107-
return "", nil
108-
}
118+
func formatLogs(logChan <-chan logs.Message) string {
109119
var b strings.Builder
110-
for _, line := range strings.Split(strings.TrimSuffix(string(msgBody), "\n"), "\n") {
111-
data := Message{}
112-
if err := json.Unmarshal([]byte(line), &data); err != nil {
113-
return "", err
120+
for v := range logChan {
121+
for _, line := range strings.Split(strings.TrimSuffix(v.Text, "\n"), "\n") {
122+
b.WriteString(line + "\n")
114123
}
115-
b.WriteString(data.Text)
116124
}
117-
118-
return strings.TrimRight(b.String(), "\n"), nil
125+
return strings.TrimRight(b.String(), "\n")
119126
}
120127

121128
func functionInResponse(bodyBytes []byte, function string, owner string) (bool, error) {
122-
functions := []sdk.Function{}
129+
functions := []types.FunctionStatus{}
123130
mErr := json.Unmarshal(bodyBytes, &functions)
124131
if mErr != nil {
125132
return false, mErr
126133
}
127134

128135
for _, fn := range functions {
129136
if fn.Name == function {
130-
return fn.Labels["com.openfaas.cloud.git-owner"] == owner, nil
137+
return (*fn.Labels)["com.openfaas.cloud.git-owner"] == owner, nil
131138
}
132139
}
133140
return false, nil
134141
}
135-
136-
func makeGatewayHttpReq(URL string, queryParams map[string]string) (*http.Response, []byte) {
137-
c := http.Client{
138-
Timeout: time.Second * 3,
139-
}
140-
141-
httpReq, _ := http.NewRequest(http.MethodGet, URL, nil)
142-
143-
query := url.Values{}
144-
145-
for key, value := range queryParams {
146-
query.Add(key, value)
147-
}
148-
149-
addAuthErr := sdk.AddBasicAuth(httpReq)
150-
if addAuthErr != nil {
151-
log.Fatalf("Basic auth error %s", addAuthErr)
152-
}
153-
154-
httpReq.URL.RawQuery = query.Encode()
155-
156-
response, err := c.Do(httpReq)
157-
158-
if err != nil {
159-
log.Fatal(err)
160-
}
161-
defer response.Body.Close()
162-
bodyBytes, bErr := ioutil.ReadAll(response.Body)
163-
if bErr != nil {
164-
log.Fatal(bErr)
165-
}
166-
167-
return response, bodyBytes
168-
}
169-
170-
type Message struct {
171-
Name string `json:"name"`
172-
Instance string `json:"instance"`
173-
Timestamp time.Time `json:"timestamp"`
174-
Text string `json:"text"`
175-
}

0 commit comments

Comments
 (0)