2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-02-06 17:14:35 +00:00
|
|
|
package troubleshoot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
envoy_admin_v3 "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *Troubleshoot) request(path string) ([]byte, error) {
|
|
|
|
client := &http.Client{}
|
|
|
|
url := fmt.Sprintf("http://%v:%s/%s", t.envoyAddr.IP, t.envoyAdminPort, path)
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req.WithContext(context.Background()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
|
|
|
return nil, fmt.Errorf("ErrBackendNotMounted")
|
|
|
|
}
|
|
|
|
|
|
|
|
rawConfig, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Troubleshoot) GetEnvoyConfigDump() error {
|
|
|
|
configDumpRaw, err := t.request("config_dump")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &envoy_admin_v3.ConfigDump{}
|
|
|
|
|
|
|
|
unmarshal := &protojson.UnmarshalOptions{
|
|
|
|
DiscardUnknown: true,
|
|
|
|
}
|
|
|
|
err = unmarshal.Unmarshal(configDumpRaw, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.envoyConfigDump = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func envoyID(listenerName string) string {
|
|
|
|
|
|
|
|
parts := strings.Split(listenerName, ":")
|
|
|
|
if parts != nil || len(parts) > 0 {
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Troubleshoot) parseClusters(clusters *envoy_admin_v3.Clusters) ([]string, error) {
|
|
|
|
upstreams := []string{}
|
|
|
|
|
|
|
|
for _, cs := range clusters.GetClusterStatuses() {
|
|
|
|
if cs.Name == "local_app" || cs.Name == "local_agent" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
upstreams = append(upstreams, cs.GetName())
|
|
|
|
}
|
|
|
|
|
|
|
|
return upstreams, nil
|
|
|
|
}
|
|
|
|
|
2023-02-08 21:03:15 +00:00
|
|
|
func (t *Troubleshoot) getEnvoyClusters() error {
|
2023-02-06 17:14:35 +00:00
|
|
|
clustersRaw, err := t.request("clusters?format=json")
|
|
|
|
if err != nil {
|
2023-02-08 21:03:15 +00:00
|
|
|
return err
|
2023-02-06 17:14:35 +00:00
|
|
|
}
|
|
|
|
clusters := &envoy_admin_v3.Clusters{}
|
|
|
|
|
|
|
|
unmarshal := &protojson.UnmarshalOptions{
|
|
|
|
DiscardUnknown: true,
|
|
|
|
}
|
|
|
|
err = unmarshal.Unmarshal(clustersRaw, clusters)
|
|
|
|
if err != nil {
|
2023-02-08 21:03:15 +00:00
|
|
|
return err
|
2023-02-06 17:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.envoyClusters = clusters
|
2023-02-08 21:03:15 +00:00
|
|
|
return nil
|
2023-02-06 17:14:35 +00:00
|
|
|
}
|