Skip to content

Commit

Permalink
clean up from rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jm96441n committed Feb 6, 2023
1 parent 6e8739e commit 587f91c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 567 deletions.
29 changes: 15 additions & 14 deletions cli/cmd/proxy/read/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/utils/strings/slices"

"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/consul-k8s/cli/common/envoy"
"github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
)
Expand Down Expand Up @@ -68,7 +69,7 @@ type ReadCommand struct {
flagKubeConfig string
flagKubeContext string

fetchConfig func(context.Context, common.PortForwarder) (*EnvoyConfig, error)
fetchConfig func(context.Context, common.PortForwarder) (*envoy.EnvoyConfig, error)

restConfig *rest.Config

Expand All @@ -78,7 +79,7 @@ type ReadCommand struct {

func (c *ReadCommand) init() {
if c.fetchConfig == nil {
c.fetchConfig = FetchConfig
c.fetchConfig = envoy.FetchConfig
}

c.set = flag.NewSets()
Expand Down Expand Up @@ -321,8 +322,8 @@ func (c *ReadCommand) fetchAdminPorts() (map[string]int, error) {
return adminPorts, nil
}

func (c *ReadCommand) fetchConfigs(adminPorts map[string]int) (map[string]*EnvoyConfig, error) {
configs := make(map[string]*EnvoyConfig, 0)
func (c *ReadCommand) fetchConfigs(adminPorts map[string]int) (map[string]*envoy.EnvoyConfig, error) {
configs := make(map[string]*envoy.EnvoyConfig, 0)

for name, adminPort := range adminPorts {
pf := common.PortForward{
Expand All @@ -344,7 +345,7 @@ func (c *ReadCommand) fetchConfigs(adminPorts map[string]int) (map[string]*Envoy
return configs, nil
}

func (c *ReadCommand) outputConfigs(configs map[string]*EnvoyConfig) error {
func (c *ReadCommand) outputConfigs(configs map[string]*envoy.EnvoyConfig) error {
switch c.flagOutput {
case Table:
return c.outputTables(configs)
Expand Down Expand Up @@ -397,7 +398,7 @@ func (c *ReadCommand) filterWarnings() []string {
return warnings
}

func (c *ReadCommand) outputTables(configs map[string]*EnvoyConfig) error {
func (c *ReadCommand) outputTables(configs map[string]*envoy.EnvoyConfig) error {
if c.flagFQDN != "" || c.flagAddress != "" || c.flagPort != -1 {
c.UI.Output("Filters applied", terminal.WithHeaderStyle())

Expand Down Expand Up @@ -432,7 +433,7 @@ func (c *ReadCommand) outputTables(configs map[string]*EnvoyConfig) error {
return nil
}

func (c *ReadCommand) outputJSON(configs map[string]*EnvoyConfig) error {
func (c *ReadCommand) outputJSON(configs map[string]*envoy.EnvoyConfig) error {
cfgs := make(map[string]interface{})
for name, config := range configs {
cfg := make(map[string]interface{})
Expand Down Expand Up @@ -468,11 +469,11 @@ func (c *ReadCommand) outputJSON(configs map[string]*EnvoyConfig) error {
return nil
}

func (c *ReadCommand) outputRaw(configs map[string]*EnvoyConfig) error {
func (c *ReadCommand) outputRaw(configs map[string]*envoy.EnvoyConfig) error {
cfgs := make(map[string]interface{}, 0)
for name, config := range configs {
var cfg interface{}
if err := json.Unmarshal(config.rawCfg, &cfg); err != nil {
if err := json.Unmarshal(config.RawCfg, &cfg); err != nil {
return err
}

Expand All @@ -489,7 +490,7 @@ func (c *ReadCommand) outputRaw(configs map[string]*EnvoyConfig) error {
return nil
}

func (c *ReadCommand) outputClustersTable(clusters []Cluster) {
func (c *ReadCommand) outputClustersTable(clusters []envoy.Cluster) {
if !c.shouldPrintTable(c.flagClusters) {
return
}
Expand All @@ -506,7 +507,7 @@ func (c *ReadCommand) outputClustersTable(clusters []Cluster) {
c.UI.Output("")
}

func (c *ReadCommand) outputEndpointsTable(endpoints []Endpoint) {
func (c *ReadCommand) outputEndpointsTable(endpoints []envoy.Endpoint) {
if !c.shouldPrintTable(c.flagEndpoints) {
return
}
Expand All @@ -515,7 +516,7 @@ func (c *ReadCommand) outputEndpointsTable(endpoints []Endpoint) {
c.UI.Table(formatEndpoints(endpoints))
}

func (c *ReadCommand) outputListenersTable(listeners []Listener) {
func (c *ReadCommand) outputListenersTable(listeners []envoy.Listener) {
if !c.shouldPrintTable(c.flagListeners) {
return
}
Expand All @@ -524,7 +525,7 @@ func (c *ReadCommand) outputListenersTable(listeners []Listener) {
c.UI.Table(formatListeners(listeners))
}

func (c *ReadCommand) outputRoutesTable(routes []Route) {
func (c *ReadCommand) outputRoutesTable(routes []envoy.Route) {
if !c.shouldPrintTable(c.flagRoutes) {
return
}
Expand All @@ -533,7 +534,7 @@ func (c *ReadCommand) outputRoutesTable(routes []Route) {
c.UI.Table(formatRoutes(routes))
}

func (c *ReadCommand) outputSecretsTable(secrets []Secret) {
func (c *ReadCommand) outputSecretsTable(secrets []envoy.Secret) {
if !c.shouldPrintTable(c.flagSecrets) {
return
}
Expand Down
75 changes: 73 additions & 2 deletions cli/cmd/proxy/read/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/client-go/kubernetes/fake"

"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/consul-k8s/cli/common/envoy"
cmnFlag "github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestReadCommandOutput(t *testing.T) {
c.kubernetes = fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{fakePod}})

// A fetchConfig function that just returns the test Envoy config.
c.fetchConfig = func(context.Context, common.PortForwarder) (*EnvoyConfig, error) {
c.fetchConfig = func(context.Context, common.PortForwarder) (*envoy.EnvoyConfig, error) {
return testEnvoyConfig, nil
}

Expand Down Expand Up @@ -241,7 +242,7 @@ func TestFilterWarnings(t *testing.T) {
buf := new(bytes.Buffer)
c := setupCommand(buf)
c.kubernetes = fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{fakePod}})
c.fetchConfig = func(context.Context, common.PortForwarder) (*EnvoyConfig, error) {
c.fetchConfig = func(context.Context, common.PortForwarder) (*envoy.EnvoyConfig, error) {
return testEnvoyConfig, nil
}

Expand Down Expand Up @@ -306,3 +307,73 @@ func TestTaskCreateCommand_AutocompleteArgs(t *testing.T) {
c := cmd.AutocompleteArgs()
assert.Equal(t, complete.PredictNothing, c)
}

// testEnvoyConfig is what we expect the config at `test_config_dump.json` to be.

var testEnvoyConfig = &envoy.EnvoyConfig{
Clusters: []envoy.Cluster{
{Name: "local_agent", FullyQualifiedDomainName: "local_agent", Endpoints: []string{"192.168.79.187:8502"}, Type: "STATIC", LastUpdated: "2022-05-13T04:22:39.553Z"},

{Name: "client", FullyQualifiedDomainName: "client.default.dc1.internal.bc3815c2-1a0f-f3ff-a2e9-20d791f08d00.consul", Endpoints: []string{"192.168.18.110:20000", "192.168.52.101:20000", "192.168.65.131:20000"}, Type: "EDS", LastUpdated: "2022-08-10T12:30:32.326Z"},

{Name: "frontend", FullyQualifiedDomainName: "frontend.default.dc1.internal.bc3815c2-1a0f-f3ff-a2e9-20d791f08d00.consul", Endpoints: []string{"192.168.63.120:20000"}, Type: "EDS", LastUpdated: "2022-08-10T12:30:32.233Z"},

{Name: "local_app", FullyQualifiedDomainName: "local_app", Endpoints: []string{"127.0.0.1:8080"}, Type: "STATIC", LastUpdated: "2022-05-13T04:22:39.655Z"},

{Name: "original-destination", FullyQualifiedDomainName: "original-destination", Endpoints: []string{}, Type: "ORIGINAL_DST", LastUpdated: "2022-05-13T04:22:39.743Z"},
},

Endpoints: []envoy.Endpoint{
{Address: "192.168.79.187:8502", Cluster: "local_agent", Weight: 1, Status: "HEALTHY"},

{Address: "192.168.18.110:20000", Cluster: "client", Weight: 1, Status: "HEALTHY"},

{Address: "192.168.52.101:20000", Cluster: "client", Weight: 1, Status: "HEALTHY"},

{Address: "192.168.65.131:20000", Cluster: "client", Weight: 1, Status: "HEALTHY"},

{Address: "192.168.63.120:20000", Cluster: "frontend", Weight: 1, Status: "HEALTHY"},

{Address: "127.0.0.1:8080", Cluster: "local_app", Weight: 1, Status: "HEALTHY"},
},

Listeners: []envoy.Listener{
{Name: "public_listener", Address: "192.168.69.179:20000", FilterChain: []envoy.FilterChain{{Filters: []string{"HTTP: * -> local_app/"}, FilterChainMatch: "Any"}}, Direction: "INBOUND", LastUpdated: "2022-08-10T12:30:47.142Z"},

{Name: "outbound_listener", Address: "127.0.0.1:15001", FilterChain: []envoy.FilterChain{
{Filters: []string{"TCP: -> client"}, FilterChainMatch: "10.100.134.173/32, 240.0.0.3/32"},

{Filters: []string{"TCP: -> frontend"}, FilterChainMatch: "10.100.31.2/32, 240.0.0.5/32"},

{Filters: []string{"TCP: -> original-destination"}, FilterChainMatch: "Any"},
}, Direction: "OUTBOUND", LastUpdated: "2022-07-18T15:31:03.246Z"},
},

Routes: []envoy.Route{
{
Name: "public_listener",

DestinationCluster: "local_app/",

LastUpdated: "2022-08-10T12:30:47.141Z",
},
},

Secrets: []envoy.Secret{
{
Name: "default",

Type: "Dynamic Active",

LastUpdated: "2022-05-24T17:41:59.078Z",
},

{
Name: "ROOTCA",

Type: "Dynamic Warming",

LastUpdated: "2022-03-15T05:14:22.868Z",
},
},
}
Loading

0 comments on commit 587f91c

Please sign in to comment.