-
Notifications
You must be signed in to change notification settings - Fork 229
/
ready.go
160 lines (128 loc) · 4.09 KB
/
ready.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"time"
"github.com/openfaas/faas-cli/proxy"
"github.com/openfaas/faas-cli/stack"
"github.com/spf13/cobra"
)
func init() {
// Setup flags that are used by multiple commands (variables defined in faas.go)
readyCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
readyCmd.Flags().StringVarP(&functionNamespace, "namespace", "n", "", "Namespace of the function")
readyCmd.Flags().BoolVar(&tlsInsecure, "tls-no-verify", false, "Disable TLS validation")
readyCmd.Flags().Int("attempts", 60, "Number of attempts to check the gateway")
readyCmd.Flags().Duration("interval", time.Second*1, "Interval between attempts in seconds")
faasCmd.AddCommand(readyCmd)
}
var readyCmd = &cobra.Command{
Use: `ready [--gateway GATEWAY_URL] [--tls-no-verify] [FUNCTION_NAME]`,
Short: "Block until the gateway or a function is ready for use",
Example: ` # Block until the gateway is ready
faas-cli ready --gateway https://127.0.0.1:8080
# Block until the env function is ready
faas-cli store deploy env && \
faas-cli ready env
# Block until the env function is ready in staging-fn namespace
faas-cli store deploy env --namespace staging-fn && \
faas-cli ready env --namespace staging-fn
`,
RunE: runReadyCmd,
}
func runReadyCmd(cmd *cobra.Command, args []string) error {
interval, err := cmd.Flags().GetDuration("interval")
if err != nil {
return err
}
attempts, err := cmd.Flags().GetInt("attempts")
if err != nil {
return err
}
if attempts < 1 {
return fmt.Errorf("attempts must be greater than 0")
}
var services stack.Services
var gatewayAddress string
var yamlGateway string
if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter, envsubst)
if err != nil {
return err
}
if parsedServices != nil {
services = *parsedServices
yamlGateway = services.Provider.GatewayURL
}
}
gatewayAddress = getGatewayURL(gateway, defaultGateway, yamlGateway, os.Getenv(openFaaSURLEnvironment))
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
if len(args) == 0 {
ready := false
c := &http.Client{
Transport: transport,
}
u, err := url.Parse(gatewayAddress)
if err != nil {
return err
}
u.Path = "/healthz"
for i := 0; i < attempts; i++ {
fmt.Printf("[%d/%d] Waiting for gateway\n", i+1, attempts)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return err
}
res, err := c.Do(req)
if err != nil {
fmt.Printf("[%d/%d] Error reaching OpenFaaS gateway: %s\n", i+1, attempts, err.Error())
} else if res.StatusCode == http.StatusOK {
fmt.Printf("OpenFaaS gateway is ready\n")
ready = true
break
}
time.Sleep(interval)
}
if !ready {
return fmt.Errorf("gateway: %s not ready after: %s", gatewayAddress, interval*time.Duration(attempts).Round(time.Second))
}
} else {
functionName := args[0]
ready := false
cliAuth, err := proxy.NewCLIAuth(token, gatewayAddress)
if err != nil {
return err
}
cliClient, err := proxy.NewClient(cliAuth, gatewayAddress, transport, &commandTimeout)
if err != nil {
return err
}
ctx := context.Background()
for i := 0; i < attempts; i++ {
suffix := ""
if len(functionNamespace) > 0 {
suffix = "." + functionNamespace
}
fmt.Printf("[%d/%d] Waiting for function %s%s\n", i+1, attempts, functionName, suffix)
function, err := cliClient.GetFunctionInfo(ctx, functionName, functionNamespace)
if err != nil {
fmt.Printf("[%d/%d] Error getting function info: %s\n", i+1, attempts, err.Error())
}
if function.AvailableReplicas > 0 {
fmt.Printf("Function %s is ready\n", functionName)
ready = true
break
}
time.Sleep(interval)
}
if !ready {
return fmt.Errorf("function %s not ready after: %s", functionName, interval*time.Duration(attempts).Round(time.Second))
}
}
return nil
}