11package function
22
33import (
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
1933func 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
8394func 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
121128func 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