istio

Форк
0
88 строк · 2.5 Кб
1
// Copyright Istio Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package app
16

17
import (
18
	"fmt"
19
	"io"
20
	"net/http"
21
	"time"
22

23
	"github.com/spf13/cobra"
24

25
	"istio.io/istio/pkg/log"
26
)
27

28
var (
29
	timeoutSeconds       int
30
	requestTimeoutMillis int
31
	periodMillis         int
32
	url                  string
33

34
	waitCmd = &cobra.Command{
35
		Use:   "wait",
36
		Short: "Waits until the Envoy proxy is ready",
37
		RunE: func(c *cobra.Command, args []string) error {
38
			client := &http.Client{
39
				Timeout: time.Duration(requestTimeoutMillis) * time.Millisecond,
40
			}
41
			log.Infof("Waiting for Envoy proxy to be ready (timeout: %d seconds)...", timeoutSeconds)
42

43
			var err error
44
			timeout := time.After(time.Duration(timeoutSeconds) * time.Second)
45

46
			for {
47
				select {
48
				case <-timeout:
49
					return fmt.Errorf("timeout waiting for Envoy proxy to become ready. Last error: %v", err)
50
				case <-time.After(time.Duration(periodMillis) * time.Millisecond):
51
					err = checkIfReady(client, url)
52
					if err == nil {
53
						log.Infof("Envoy is ready!")
54
						return nil
55
					}
56
					log.Debugf("Not ready yet: %v", err)
57
				}
58
			}
59
		},
60
	}
61
)
62

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

83
func init() {
84
	waitCmd.PersistentFlags().IntVar(&timeoutSeconds, "timeoutSeconds", 60, "maximum number of seconds to wait for Envoy to be ready")
85
	waitCmd.PersistentFlags().IntVar(&requestTimeoutMillis, "requestTimeoutMillis", 500, "number of milliseconds to wait for response")
86
	waitCmd.PersistentFlags().IntVar(&periodMillis, "periodMillis", 500, "number of milliseconds to wait between attempts")
87
	waitCmd.PersistentFlags().StringVar(&url, "url", "http://localhost:15021/healthz/ready", "URL to use in requests")
88
}
89

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

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

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

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