9
"github.com/spf13/cobra"
12
var waitLog = dataplaneLog.WithName("wait")
14
func newWaitCmd() *cobra.Command {
17
requestTimeout time.Duration
19
checkFrequency time.Duration
21
cmd := &cobra.Command{
23
Short: "Waits for data plane proxy to be ready",
24
Long: `Waits for data plane proxy (Envoy) to be ready.`,
25
RunE: func(cmd *cobra.Command, _ []string) error {
26
client := &http.Client{
27
Timeout: args.requestTimeout,
29
ticker := time.NewTicker(args.checkFrequency)
31
timeout := time.After(args.timeout)
33
waitLog.Info("waiting for data plane proxy to be ready", "timeout", args.timeout)
37
if err := checkIfEnvoyReady(client, args.url); err != nil {
38
waitLog.Info("data plane proxy is not ready", "err", err)
40
waitLog.Info("data plane is ready")
44
return fmt.Errorf("timeout occurred while waiting for data plane proxy to be ready")
50
cmd.PersistentFlags().DurationVar(&args.checkFrequency, "check-frequency", time.Second, `frequency of checking if the data plane proxy is ready`)
51
cmd.PersistentFlags().DurationVar(&args.timeout, "timeout", 180*time.Second, `timeout defines how long waits for the data plane proxy`)
52
cmd.PersistentFlags().DurationVar(&args.requestTimeout, "request-timeout", 500*time.Millisecond, `requestTimeout defines timeout for the request to the data plane proxy`)
53
cmd.PersistentFlags().StringVar(&args.url, "url", "http://localhost:9901/ready", `url at which admin is exposed`)
58
func checkIfEnvoyReady(client *http.Client, url string) error {
59
req, err := http.NewRequest(http.MethodGet, url, nil)
63
resp, err := client.Do(req)
67
defer resp.Body.Close()
68
_, err = io.ReadAll(resp.Body)
72
if resp.StatusCode != 200 {
73
return fmt.Errorf("HTTP status code %v", resp.StatusCode)