1
// Copyright Istio Authors
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
25
multierror "github.com/hashicorp/go-multierror"
27
"istio.io/istio/pkg/http"
31
statCdsRejected = "cluster_manager.cds.update_rejected"
32
statsCdsSuccess = "cluster_manager.cds.update_success"
33
statLdsRejected = "listener_manager.lds.update_rejected"
34
statLdsSuccess = "listener_manager.lds.update_success"
35
statServerState = "server.state"
36
statWorkersStarted = "listener_manager.workers_started"
37
readyStatsRegex = "^(server\\.state|listener_manager\\.workers_started)"
38
updateStatsRegex = "^(cluster_manager\\.cds|listener_manager\\.lds)\\.(update_success|update_rejected)$"
41
var readinessTimeout = time.Second * 3 // Default Readiness timeout. It is set the same in helm charts.
49
// Stats contains values of interest from a poll of Envoy stats.
52
CDSUpdatesSuccess uint64
53
CDSUpdatesRejection uint64
54
LDSUpdatesSuccess uint64
55
LDSUpdatesRejection uint64
56
// Server State of Envoy.
61
// String representation of the Stats.
62
func (s *Stats) String() string {
63
return fmt.Sprintf("cds updates: %d successful, %d rejected; lds updates: %d successful, %d rejected",
65
s.CDSUpdatesRejection,
67
s.LDSUpdatesRejection)
70
// GetReadinessStats returns the current Envoy state by checking the "server.state" stat.
71
func GetReadinessStats(localHostAddr string, adminPort uint16) (*uint64, bool, error) {
72
// If the localHostAddr was not set, we use 'localhost' to void empty host in URL.
73
if localHostAddr == "" {
74
localHostAddr = "localhost"
77
hostPort := net.JoinHostPort(localHostAddr, strconv.Itoa(int(adminPort)))
78
readinessURL := fmt.Sprintf("http://%s/stats?usedonly&filter=%s", hostPort, readyStatsRegex)
79
stats, err := http.DoHTTPGetWithTimeout(readinessURL, readinessTimeout)
81
return nil, false, err
83
if !strings.Contains(stats.String(), "server.state") {
84
return nil, false, fmt.Errorf("server.state is not yet updated: %s", stats.String())
87
if !strings.Contains(stats.String(), "listener_manager.workers_started") {
88
return nil, false, fmt.Errorf("listener_manager.workers_started is not yet updated: %s", stats.String())
93
{name: statServerState, value: &s.ServerState},
94
{name: statWorkersStarted, value: &s.WorkersStarted},
96
if err := parseStats(stats, allStats); err != nil {
97
return nil, false, err
100
return &s.ServerState, s.WorkersStarted == 1, nil
103
// GetUpdateStatusStats returns the version stats for CDS and LDS.
104
func GetUpdateStatusStats(localHostAddr string, adminPort uint16) (*Stats, error) {
105
// If the localHostAddr was not set, we use 'localhost' to void empty host in URL.
106
if localHostAddr == "" {
107
localHostAddr = "localhost"
110
hostPort := net.JoinHostPort(localHostAddr, strconv.Itoa(int(adminPort)))
111
stats, err := http.DoHTTPGet(fmt.Sprintf("http://%s/stats?usedonly&filter=%s", hostPort, updateStatsRegex))
118
{name: statsCdsSuccess, value: &s.CDSUpdatesSuccess},
119
{name: statCdsRejected, value: &s.CDSUpdatesRejection},
120
{name: statLdsSuccess, value: &s.LDSUpdatesSuccess},
121
{name: statLdsRejected, value: &s.LDSUpdatesRejection},
123
if err := parseStats(stats, allStats); err != nil {
130
func parseStats(input *bytes.Buffer, stats []*stat) (err error) {
131
for input.Len() > 0 {
132
line, _ := input.ReadString('\n')
133
for _, stat := range stats {
134
if e := stat.processLine(line); e != nil {
135
err = multierror.Append(err, e)
139
for _, stat := range stats {
147
func (s *stat) processLine(line string) error {
148
if !s.found && strings.HasPrefix(line, s.name) {
151
parts := strings.Split(line, ":")
153
return fmt.Errorf("envoy stat %s missing separator. line:%s", s.name, line)
156
val, err := strconv.ParseUint(strings.TrimSpace(parts[1]), 10, 64)
158
return fmt.Errorf("failed parsing Envoy stat %s (error: %s) line: %s", s.name, err.Error(), line)