open-consul/troubleshoot/proxy/utils.go
Nitya Dhanushkodi 77f6b20db0
refactor: remove troubleshoot module dependency on consul top level module (#16162)
Ensure nothing in the troubleshoot go module depends on consul's top level module. This is so we can import troubleshoot into consul-k8s and not import all of consul.

* turns troubleshoot into a go module [authored by @curtbushko]
* gets the envoy protos into the troubleshoot module [authored by @curtbushko]
* adds a new go module `envoyextensions` which has xdscommon and extensioncommon folders that both the xds package and the troubleshoot package can import
* adds testing and linting for the new go modules
* moves the unit tests in `troubleshoot/validateupstream` that depend on proxycfg/xds into the xds package, with a comment describing why those tests cannot be in the troubleshoot package
* fixes all the imports everywhere as a result of these changes 

Co-authored-by: Curt Bushko <cbushko@gmail.com>
2023-02-06 09:14:35 -08:00

103 lines
2.1 KiB
Go

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
}
// TODO: validate here
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
}
func (t *Troubleshoot) getEnvoyClusters() (*envoy_admin_v3.Clusters, error) {
clustersRaw, err := t.request("clusters?format=json")
if err != nil {
return nil, err
}
clusters := &envoy_admin_v3.Clusters{}
unmarshal := &protojson.UnmarshalOptions{
DiscardUnknown: true,
}
err = unmarshal.Unmarshal(clustersRaw, clusters)
if err != nil {
return nil, err
}
// TODO: validate here
t.envoyClusters = clusters
return clusters, nil
}