2023-02-07 22:57:31 +00:00
|
|
|
package troubleshoot
|
|
|
|
|
|
|
|
import (
|
2023-02-17 18:53:20 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-02-13 20:50:32 +00:00
|
|
|
envoy_admin_v3 "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
|
2023-02-07 22:57:31 +00:00
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2023-02-13 20:50:32 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-02-07 22:57:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetUpstreamIPsFromFilterChain(t *testing.T) {
|
2023-02-13 20:50:32 +00:00
|
|
|
file, err := os.Open("testdata/upstreams/config.json")
|
2023-02-07 22:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
jsonBytes, err := io.ReadAll(file)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []UpstreamIP{
|
|
|
|
{
|
|
|
|
IPs: []string{
|
|
|
|
"10.244.0.63",
|
|
|
|
"10.244.0.64",
|
|
|
|
},
|
|
|
|
IsVirtual: false,
|
|
|
|
ClusterNames: map[string]struct{}{
|
|
|
|
"passthrough~foo.default.dc1.internal.dc1.consul": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPs: []string{
|
|
|
|
"10.96.5.96",
|
|
|
|
"240.0.0.1",
|
|
|
|
},
|
|
|
|
IsVirtual: true,
|
|
|
|
ClusterNames: map[string]struct{}{
|
|
|
|
"foo.default.dc1.internal.dc1.consul": {},
|
|
|
|
},
|
|
|
|
},
|
2023-02-13 20:50:32 +00:00
|
|
|
{
|
|
|
|
IPs: []string{
|
|
|
|
"10.4.6.160",
|
|
|
|
"240.0.0.3",
|
|
|
|
},
|
|
|
|
IsVirtual: true,
|
|
|
|
ClusterNames: map[string]struct{}{
|
|
|
|
"backend.default.dc1.internal.domain.consul": {},
|
|
|
|
"backend2.default.dc1.internal.domain.consul": {},
|
|
|
|
},
|
|
|
|
},
|
2023-02-07 22:57:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 20:50:32 +00:00
|
|
|
var upstreamIPs []UpstreamIP
|
|
|
|
cfgDump := &envoy_admin_v3.ConfigDump{}
|
2023-02-07 22:57:31 +00:00
|
|
|
unmarshal := &protojson.UnmarshalOptions{
|
|
|
|
DiscardUnknown: true,
|
|
|
|
}
|
2023-02-13 20:50:32 +00:00
|
|
|
err = unmarshal.Unmarshal(jsonBytes, cfgDump)
|
2023-02-07 22:57:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-13 20:50:32 +00:00
|
|
|
for _, cfg := range cfgDump.Configs {
|
|
|
|
switch cfg.TypeUrl {
|
2023-02-17 18:53:20 +00:00
|
|
|
case listenersType:
|
2023-02-13 20:50:32 +00:00
|
|
|
lcd := &envoy_admin_v3.ListenersConfigDump{}
|
|
|
|
|
|
|
|
err := proto.Unmarshal(cfg.GetValue(), lcd)
|
|
|
|
require.NoError(t, err)
|
2023-02-07 22:57:31 +00:00
|
|
|
|
2023-02-13 20:50:32 +00:00
|
|
|
for _, listener := range lcd.GetDynamicListeners() {
|
|
|
|
l := &envoy_listener_v3.Listener{}
|
|
|
|
err = proto.Unmarshal(listener.GetActiveState().GetListener().GetValue(), l)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
upstreamIPs, err = getUpstreamIPsFromFilterChain(l.GetFilterChains(), cfgDump)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, upstreamIPs)
|
2023-02-07 22:57:31 +00:00
|
|
|
}
|