@@ -2,6 +2,7 @@ package sdk
22
33import (
44 "bytes"
5+ "context"
56 "encoding/json"
67 "fmt"
78 "io"
@@ -35,12 +36,12 @@ func NewClient(gatewayURL *url.URL, auth ClientAuth, client *http.Client) *Clien
3536}
3637
3738// GetNamespaces get openfaas namespaces
38- func (s * Client ) GetNamespaces () ([]string , error ) {
39+ func (s * Client ) GetNamespaces (ctx context. Context ) ([]string , error ) {
3940 u := s .GatewayURL
4041 namespaces := []string {}
4142 u .Path = "/system/namespaces"
4243
43- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
44+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
4445 if err != nil {
4546 return namespaces , fmt .Errorf ("unable to create request: %s, error: %w" , u .String (), err )
4647 }
@@ -81,7 +82,7 @@ func (s *Client) GetNamespaces() ([]string, error) {
8182}
8283
8384// GetFunctions lists all functions
84- func (s * Client ) GetFunctions (namespace string ) ([]types.FunctionStatus , error ) {
85+ func (s * Client ) GetFunctions (ctx context. Context , namespace string ) ([]types.FunctionStatus , error ) {
8586 u := s .GatewayURL
8687
8788 u .Path = "/system/functions"
@@ -92,7 +93,7 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
9293 u .RawQuery = query .Encode ()
9394 }
9495
95- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
96+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
9697 if err != nil {
9798 return []types.FunctionStatus {}, fmt .Errorf ("unable to create request for %s, error: %w" , u .String (), err )
9899 }
@@ -123,12 +124,12 @@ func (s *Client) GetFunctions(namespace string) ([]types.FunctionStatus, error)
123124 return functions , nil
124125}
125126
126- func (s * Client ) GetInfo () (SystemInfo , error ) {
127+ func (s * Client ) GetInfo (ctx context. Context ) (SystemInfo , error ) {
127128 u := s .GatewayURL
128129
129130 u .Path = "/system/info"
130131
131- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
132+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
132133 if err != nil {
133134 return SystemInfo {}, fmt .Errorf ("unable to create request for %s, error: %w" , u .String (), err )
134135 }
@@ -160,7 +161,7 @@ func (s *Client) GetInfo() (SystemInfo, error) {
160161}
161162
162163// GetFunction gives a richer payload than GetFunctions, but for a specific function
163- func (s * Client ) GetFunction (name , namespace string ) (types.FunctionDeployment , error ) {
164+ func (s * Client ) GetFunction (ctx context. Context , name , namespace string ) (types.FunctionDeployment , error ) {
164165 u := s .GatewayURL
165166
166167 u .Path = "/system/function/" + name
@@ -171,7 +172,7 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
171172 u .RawQuery = query .Encode ()
172173 }
173174
174- req , err := http .NewRequest ( http .MethodGet , u .String (), nil )
175+ req , err := http .NewRequestWithContext ( ctx , http .MethodGet , u .String (), nil )
175176 if err != nil {
176177 return types.FunctionDeployment {}, fmt .Errorf ("unable to create request for %s, error: %w" , u .String (), err )
177178 }
@@ -202,16 +203,16 @@ func (s *Client) GetFunction(name, namespace string) (types.FunctionDeployment,
202203 return functions , nil
203204}
204205
205- func (s * Client ) Deploy (spec types.FunctionDeployment ) (int , error ) {
206- return s .deploy (http .MethodPost , spec )
206+ func (s * Client ) Deploy (ctx context. Context , spec types.FunctionDeployment ) (int , error ) {
207+ return s .deploy (ctx , http .MethodPost , spec )
207208
208209}
209210
210- func (s * Client ) Update (spec types.FunctionDeployment ) (int , error ) {
211- return s .deploy (http .MethodPut , spec )
211+ func (s * Client ) Update (ctx context. Context , spec types.FunctionDeployment ) (int , error ) {
212+ return s .deploy (ctx , http .MethodPut , spec )
212213}
213214
214- func (s * Client ) deploy (method string , spec types.FunctionDeployment ) (int , error ) {
215+ func (s * Client ) deploy (ctx context. Context , method string , spec types.FunctionDeployment ) (int , error ) {
215216
216217 bodyBytes , err := json .Marshal (spec )
217218 if err != nil {
@@ -223,7 +224,7 @@ func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, erro
223224 u := s .GatewayURL
224225 u .Path = "/system/functions"
225226
226- req , err := http .NewRequest ( method , u .String (), bodyReader )
227+ req , err := http .NewRequestWithContext ( ctx , method , u .String (), bodyReader )
227228 if err != nil {
228229 return http .StatusBadGateway , err
229230 }
@@ -258,7 +259,7 @@ func (s *Client) deploy(method string, spec types.FunctionDeployment) (int, erro
258259}
259260
260261// ScaleFunction scales a function to a number of replicas
261- func (s * Client ) ScaleFunction (functionName , namespace string , replicas uint64 ) error {
262+ func (s * Client ) ScaleFunction (ctx context. Context , functionName , namespace string , replicas uint64 ) error {
262263
263264 scaleReq := types.ScaleServiceRequest {
264265 ServiceName : functionName ,
@@ -277,7 +278,7 @@ func (s *Client) ScaleFunction(functionName, namespace string, replicas uint64)
277278
278279 u .Path = functionPath
279280
280- req , err := http .NewRequest ( http .MethodPost , u .String (), bodyReader )
281+ req , err := http .NewRequestWithContext ( ctx , http .MethodPost , u .String (), bodyReader )
281282 if err != nil {
282283 return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , u .String (), err )
283284 }
@@ -320,7 +321,7 @@ func (s *Client) ScaleFunction(functionName, namespace string, replicas uint64)
320321}
321322
322323// DeleteFunction deletes a function
323- func (s * Client ) DeleteFunction (functionName , namespace string ) error {
324+ func (s * Client ) DeleteFunction (ctx context. Context , functionName , namespace string ) error {
324325
325326 delReq := types.DeleteFunctionRequest {
326327 FunctionName : functionName ,
@@ -335,7 +336,7 @@ func (s *Client) DeleteFunction(functionName, namespace string) error {
335336 u := s .GatewayURL
336337 u .Path = "/system/functions"
337338
338- req , err := http .NewRequest ( http .MethodDelete , u .String (), bodyReader )
339+ req , err := http .NewRequestWithContext ( ctx , http .MethodDelete , u .String (), bodyReader )
339340 if err != nil {
340341 return fmt .Errorf ("cannot connect to OpenFaaS on URL: %s, error: %s" , u .String (), err )
341342 }
0 commit comments