kuma

Форк
0
/
wait.go 
76 строк · 2.1 Кб
1
package cmd
2

3
import (
4
	"fmt"
5
	"io"
6
	"net/http"
7
	"time"
8

9
	"github.com/spf13/cobra"
10
)
11

12
var waitLog = dataplaneLog.WithName("wait")
13

14
func newWaitCmd() *cobra.Command {
15
	args := struct {
16
		url            string
17
		requestTimeout time.Duration
18
		timeout        time.Duration
19
		checkFrequency time.Duration
20
	}{}
21
	cmd := &cobra.Command{
22
		Use:   "wait",
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,
28
			}
29
			ticker := time.NewTicker(args.checkFrequency)
30
			defer ticker.Stop()
31
			timeout := time.After(args.timeout)
32

33
			waitLog.Info("waiting for data plane proxy to be ready", "timeout", args.timeout)
34
			for {
35
				select {
36
				case <-ticker.C:
37
					if err := checkIfEnvoyReady(client, args.url); err != nil {
38
						waitLog.Info("data plane proxy is not ready", "err", err)
39
					} else {
40
						waitLog.Info("data plane is ready")
41
						return nil
42
					}
43
				case <-timeout:
44
					return fmt.Errorf("timeout occurred while waiting for data plane proxy to be ready")
45
				}
46
			}
47
		},
48
	}
49

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`)
54

55
	return cmd
56
}
57

58
func checkIfEnvoyReady(client *http.Client, url string) error {
59
	req, err := http.NewRequest(http.MethodGet, url, nil)
60
	if err != nil {
61
		return err
62
	}
63
	resp, err := client.Do(req)
64
	if err != nil {
65
		return err
66
	}
67
	defer resp.Body.Close()
68
	_, err = io.ReadAll(resp.Body)
69
	if err != nil {
70
		return err
71
	}
72
	if resp.StatusCode != 200 {
73
		return fmt.Errorf("HTTP status code %v", resp.StatusCode)
74
	}
75
	return nil
76
}
77

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.