Skip to content

Commit b0a70a3

Browse files
committed
Add client SDK for making proxy API calls
Decouples the "config" package from the "proxy" package so that it can stand alone. Signed-off-by: Vivek Singh <[email protected]> Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent ea68765 commit b0a70a3

33 files changed

+671
-404
lines changed

Gopkg.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353
[[constraint]]
5454
name = "github.com/alexellis/go-execute"
5555
version = "0.3.0"
56+

commands/deploy.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package commands
55

66
import (
7+
"context"
78
"encoding/base64"
89
"encoding/json"
910
"fmt"
@@ -166,13 +167,19 @@ func runDeployCommand(args []string, image string, fprocess string, functionName
166167
}
167168
}
168169

170+
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
171+
ctx := context.Background()
172+
169173
var failedStatusCodes = make(map[string]int)
170174
if len(services.Functions) > 0 {
171175

172176
if len(services.Provider.Network) == 0 {
173177
services.Provider.Network = defaultNetwork
174178
}
175179

180+
cliAuth := NewCLIAuth(token, services.Provider.GatewayURL)
181+
proxyClient := proxy.NewClient(cliAuth, services.Provider.GatewayURL, transport, &commandTimeout)
182+
176183
for k, function := range services.Functions {
177184

178185
functionSecrets := deployFlags.secrets
@@ -268,7 +275,6 @@ Error: %s`, fprocessErr.Error())
268275

269276
deploySpec := &proxy.DeployFunctionSpec{
270277
FProcess: function.FProcess,
271-
Gateway: services.Provider.GatewayURL,
272278
FunctionName: function.Name,
273279
Image: function.Image,
274280
RegistryAuth: function.RegistryAuth,
@@ -288,12 +294,10 @@ Error: %s`, fprocessErr.Error())
288294
Namespace: function.Namespace,
289295
}
290296

291-
if msg := checkTLSInsecure(deploySpec.Gateway, deploySpec.TLSInsecure); len(msg) > 0 {
297+
if msg := checkTLSInsecure(services.Provider.GatewayURL, deploySpec.TLSInsecure); len(msg) > 0 {
292298
fmt.Println(msg)
293299
}
294-
295-
statusCode := proxy.DeployFunction(deploySpec)
296-
300+
statusCode := proxyClient.DeployFunction(ctx, deploySpec)
297301
if badStatusCode(statusCode) {
298302
failedStatusCodes[k] = statusCode
299303
}
@@ -303,6 +307,8 @@ Error: %s`, fprocessErr.Error())
303307
return fmt.Errorf("To deploy a function give --yaml/-f or a --image and --name flag")
304308
}
305309
gateway = getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment))
310+
cliAuth := NewCLIAuth(token, gateway)
311+
proxyClient := proxy.NewClient(cliAuth, gateway, transport, &commandTimeout)
306312

307313
var registryAuth string
308314
if deployFlags.sendRegistryAuth {
@@ -317,7 +323,7 @@ Error: %s`, fprocessErr.Error())
317323
// default to a readable filesystem until we get more input about the expected behavior
318324
// and if we want to add another flag for this case
319325
defaultReadOnlyRFS := false
320-
statusCode, err := deployImage(image, fprocess, functionName, registryAuth, deployFlags,
326+
statusCode, err := deployImage(ctx, proxyClient, image, fprocess, functionName, registryAuth, deployFlags,
321327
tlsInsecure, defaultReadOnlyRFS, token, functionNamespace)
322328
if err != nil {
323329
return err
@@ -337,6 +343,8 @@ Error: %s`, fprocessErr.Error())
337343

338344
// deployImage deploys a function with the given image
339345
func deployImage(
346+
ctx context.Context,
347+
client *proxy.Client,
340348
image string,
341349
fprocess string,
342350
functionName string,
@@ -370,7 +378,6 @@ func deployImage(
370378

371379
deploySpec := &proxy.DeployFunctionSpec{
372380
FProcess: fprocess,
373-
Gateway: gateway,
374381
FunctionName: functionName,
375382
Image: image,
376383
RegistryAuth: registryAuth,
@@ -390,11 +397,11 @@ func deployImage(
390397
Namespace: namespace,
391398
}
392399

393-
if msg := checkTLSInsecure(deploySpec.Gateway, deploySpec.TLSInsecure); len(msg) > 0 {
400+
if msg := checkTLSInsecure(gateway, deploySpec.TLSInsecure); len(msg) > 0 {
394401
fmt.Println(msg)
395402
}
396403

397-
statusCode = proxy.DeployFunction(deploySpec)
404+
statusCode = client.DeployFunction(ctx, deploySpec)
398405

399406
return statusCode, nil
400407
}

commands/describe.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package commands
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910
"strconv"
@@ -63,14 +64,18 @@ func runDescribe(cmd *cobra.Command, args []string) error {
6364
}
6465
}
6566
gatewayAddress := getGatewayURL(gateway, defaultGateway, yamlGateway, os.Getenv(openFaaSURLEnvironment))
67+
cliAuth := NewCLIAuth(token, gatewayAddress)
68+
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
69+
cliClient := proxy.NewClient(cliAuth, gatewayAddress, transport, &commandTimeout)
70+
ctx := context.Background()
6671

67-
function, err := proxy.GetFunctionInfoToken(gatewayAddress, functionName, tlsInsecure, token, functionNamespace)
72+
function, err := cliClient.GetFunctionInfo(ctx, functionName, functionNamespace)
6873
if err != nil {
6974
return err
7075
}
7176

7277
//To get correct value for invocation count from /system/functions endpoint
73-
functionList, err := proxy.ListFunctionsToken(gatewayAddress, tlsInsecure, token, functionNamespace)
78+
functionList, err := cliClient.ListFunctions(ctx, functionNamespace)
7479
if err != nil {
7580
return err
7681
}

commands/general.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package commands
2+
3+
import (
4+
"crypto/tls"
5+
"net"
6+
"net/http"
7+
"time"
8+
9+
"github.com/openfaas/faas-cli/config"
10+
"github.com/openfaas/faas-cli/proxy"
11+
)
12+
13+
var (
14+
commandTimeout = 60 * time.Second
15+
)
16+
17+
//CLIAuth auth struct for the CLI
18+
type CLIAuth struct {
19+
Username string
20+
Password string
21+
Token string
22+
}
23+
24+
//BasicAuth basic authentication type
25+
type BasicAuth struct {
26+
username string
27+
password string
28+
}
29+
30+
func (auth *BasicAuth) Set(req *http.Request) error {
31+
req.SetBasicAuth(auth.username, auth.password)
32+
return nil
33+
}
34+
35+
//BearerToken bearer token
36+
type BearerToken struct {
37+
token string
38+
}
39+
40+
func (c *BearerToken) Set(req *http.Request) error {
41+
req.Header.Set("Authorization", "Bearer "+c.token)
42+
return nil
43+
}
44+
45+
//NewCLIAuth returns a new CLI Auth
46+
func NewCLIAuth(token string, gateway string) proxy.ClientAuth {
47+
authConfig, _ := config.LookupAuthConfig(gateway)
48+
49+
var (
50+
username string
51+
password string
52+
bearerToken string
53+
)
54+
55+
if authConfig.Auth == config.BasicAuthType {
56+
username, password, _ = config.DecodeAuth(authConfig.Token)
57+
58+
return &BasicAuth{
59+
username: username,
60+
password: password,
61+
}
62+
63+
}
64+
65+
// User specified token gets priority
66+
if len(token) > 0 {
67+
bearerToken = token
68+
} else {
69+
bearerToken = authConfig.Token
70+
}
71+
72+
return &BearerToken{
73+
token: bearerToken,
74+
}
75+
}
76+
77+
func GetDefaultCLITransport(tlsInsecure bool, timeout *time.Duration) *http.Transport {
78+
if timeout != nil || tlsInsecure {
79+
tr := &http.Transport{
80+
Proxy: http.ProxyFromEnvironment,
81+
DisableKeepAlives: false,
82+
}
83+
84+
if timeout != nil {
85+
tr.DialContext = (&net.Dialer{
86+
Timeout: *timeout,
87+
}).DialContext
88+
89+
tr.IdleConnTimeout = 120 * time.Millisecond
90+
tr.ExpectContinueTimeout = 1500 * time.Millisecond
91+
}
92+
93+
if tlsInsecure {
94+
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: tlsInsecure}
95+
}
96+
tr.DisableKeepAlives = false
97+
98+
return tr
99+
}
100+
return nil
101+
}

commands/list.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package commands
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910

@@ -54,9 +55,12 @@ func runList(cmd *cobra.Command, args []string) error {
5455
yamlGateway = services.Provider.GatewayURL
5556
}
5657
}
57-
5858
gatewayAddress = getGatewayURL(gateway, defaultGateway, yamlGateway, os.Getenv(openFaaSURLEnvironment))
59-
functions, err := proxy.ListFunctionsToken(gatewayAddress, tlsInsecure, token, functionNamespace)
59+
60+
cliAuth := NewCLIAuth(token, gatewayAddress)
61+
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
62+
proxyClient := proxy.NewClient(cliAuth, gatewayAddress, transport, &commandTimeout)
63+
functions, err := proxyClient.ListFunctions(context.Background(), functionNamespace)
6064
if err != nil {
6165
return err
6266
}

commands/logs.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
package commands
55

66
import (
7+
"context"
8+
"crypto/tls"
79
"fmt"
10+
"net/http"
811
"os"
912
"time"
1013

@@ -90,7 +93,10 @@ func runLogs(cmd *cobra.Command, args []string) error {
9093
}
9194

9295
logRequest := logRequestFromFlags(cmd, args)
93-
logEvents, err := proxy.GetLogs(gatewayAddress, tlsInsecure, logRequest, logFlagValues.token)
96+
cliAuth := NewCLIAuth(logFlagValues.token, gatewayAddress)
97+
transport := getLogStreamingTransport(tlsInsecure)
98+
cliClient := proxy.NewClient(cliAuth, gatewayAddress, transport, nil)
99+
logEvents, err := cliClient.GetLogs(context.Background(), logRequest)
94100
if err != nil {
95101
return err
96102
}
@@ -123,3 +129,15 @@ func sinceValue(t time.Time, d time.Duration) *time.Time {
123129
}
124130
return nil
125131
}
132+
133+
func getLogStreamingTransport(tlsInsecure bool) http.RoundTripper {
134+
if tlsInsecure {
135+
tr := &http.Transport{
136+
Proxy: http.ProxyFromEnvironment,
137+
}
138+
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: tlsInsecure}
139+
140+
return tr
141+
}
142+
return nil
143+
}

commands/namespaces.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67

@@ -29,14 +30,14 @@ var namespacesCmd = &cobra.Command{
2930

3031
func runNamespaces(cmd *cobra.Command, args []string) error {
3132
gatewayAddress := getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment))
32-
33-
namespaces, err := proxy.ListNamespacesToken(gatewayAddress, tlsInsecure, token)
33+
cliAuth := NewCLIAuth(token, gatewayAddress)
34+
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
35+
client := proxy.NewClient(cliAuth, gatewayAddress, transport, &commandTimeout)
36+
namespaces, err := client.ListNamespaces(context.Background())
3437
if err != nil {
3538
return err
3639
}
37-
3840
printNamespaces(namespaces)
39-
4041
return nil
4142
}
4243

commands/remove.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package commands
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910

@@ -59,6 +60,11 @@ func runDelete(cmd *cobra.Command, args []string) error {
5960

6061
gatewayAddress = getGatewayURL(gateway, defaultGateway, yamlGateway, os.Getenv(openFaaSURLEnvironment))
6162

63+
cliAuth := NewCLIAuth(token, gatewayAddress)
64+
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
65+
proxyclient := proxy.NewClient(cliAuth, gatewayAddress, transport, &commandTimeout)
66+
ctx := context.Background()
67+
6268
if len(services.Functions) > 0 {
6369
if len(services.Provider.Network) == 0 {
6470
services.Provider.Network = defaultNetwork
@@ -68,7 +74,7 @@ func runDelete(cmd *cobra.Command, args []string) error {
6874
function.Name = k
6975
fmt.Printf("Deleting: %s.\n", function.Name)
7076

71-
proxy.DeleteFunctionToken(gatewayAddress, function.Name, tlsInsecure, token, functionNamespace)
77+
proxyclient.DeleteFunction(ctx, function.Name, functionNamespace)
7278
}
7379
} else {
7480
if len(args) < 1 {
@@ -77,7 +83,7 @@ func runDelete(cmd *cobra.Command, args []string) error {
7783

7884
functionName = args[0]
7985
fmt.Printf("Deleting: %s.\n", functionName)
80-
proxy.DeleteFunctionToken(gatewayAddress, functionName, tlsInsecure, token, functionNamespace)
86+
proxyclient.DeleteFunction(ctx, functionName, functionNamespace)
8187
}
8288

8389
return nil

commands/secret_create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package commands
55

66
import (
7+
"context"
78
"fmt"
89
"io/ioutil"
910
"os"
@@ -109,9 +110,11 @@ func runSecretCreate(cmd *cobra.Command, args []string) error {
109110
if msg := checkTLSInsecure(gatewayAddress, tlsInsecure); len(msg) > 0 {
110111
fmt.Println(msg)
111112
}
112-
113+
cliAuth := NewCLIAuth(token, gatewayAddress)
114+
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
115+
client := proxy.NewClient(cliAuth, gatewayAddress, transport, &commandTimeout)
113116
fmt.Println("Creating secret: " + secret.Name)
114-
_, output := proxy.CreateSecretToken(gatewayAddress, secret, tlsInsecure, token)
117+
_, output := client.CreateSecret(context.Background(), secret)
115118
fmt.Printf(output)
116119

117120
return nil

0 commit comments

Comments
 (0)