Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Branch: consul-k8s Envoy Debugging #1271

Merged
merged 175 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
175 commits
Select commit Hold shift + click to select a range
d97067b
Add PortForward struct
May 19, 2022
4eaf81f
Handle listener.Close error
May 19, 2022
9ab33d2
Channels now have a default size of 1.
May 24, 2022
ca15154
Remove http:// from endpoint
May 24, 2022
11b7c17
Open returns endpoint (bye Endpoint())
May 25, 2022
177a0fe
Add stub of TestOpen
May 25, 2022
6e9036c
Add unit tests to portforward
Jun 8, 2022
8d192ed
Assert correct error messages
Jun 10, 2022
5d554c9
Add FetchPods
May 25, 2022
7fce121
Add tests for fetch_pods
May 26, 2022
bb13ad3
Rename table.Rich to table.AddRow
May 31, 2022
62a8da3
Add the proxy list command
May 31, 2022
22b1c20
Register the proxy list command
May 31, 2022
74a5231
Change TableEntry to Cell
Jun 1, 2022
c5a6d15
Move constants to top of table
Jun 1, 2022
454e0ae
Format output to match Consul CLI
Jun 1, 2022
90c196b
Simplify status table output
Jun 1, 2022
7c8c295
Move pod fetching into the command
Jun 1, 2022
1a79e3c
Add comments to functions
Jun 1, 2022
a060386
Use c.fetchPods
Jun 1, 2022
99cb8e9
Use a switch to format gateway type
Jun 1, 2022
57429e1
Rename Command to ListCommand
Jun 1, 2022
2177e0f
Add tests
Jun 2, 2022
905a330
Update cli/cmd/proxy/list/command_test.go
Jun 8, 2022
ec793c7
Change synopsis to say "managed by Consul"
Jun 8, 2022
36d5619
Use Kubernetes validation module
Jun 8, 2022
e7b0277
Use the default namespace specified in the kubeconfig
Jun 8, 2022
2423a9b
Add comments to pod fetching
Jun 8, 2022
90ccbf1
Improve errors and output formatting
Jun 8, 2022
3a3143c
Add the namespace field
Jun 8, 2022
fa285ef
Fix namespace handling
Jun 8, 2022
632a097
Add a pod fetching test and improve how pods are created in the fake
Jun 9, 2022
a3c3f49
sidecar -> pod1
Jun 9, 2022
dd43e58
Improve initKubernetes
Jun 9, 2022
9b8fdb2
Add fake kubernetes back into arg parsing
Jun 9, 2022
cf07f4e
Add chart:consul-helm to label selector
Jun 14, 2022
3ee3cfb
Add stub for proxy/read
May 25, 2022
dac65dd
Stub out command and tests
Jun 10, 2022
2e41c7c
Add proxy read to command map
Jun 10, 2022
e05631e
Add PortForwarder interface
Jun 13, 2022
30fa5c0
Add test_config_dump.json
Jun 13, 2022
64fa0cb
Stub out config
Jun 13, 2022
8f338f7
Add FetchConfig
Jun 13, 2022
e5c0006
Have Print take a Config type
Jun 13, 2022
b878e7a
Add initKubernetes
Jun 13, 2022
5668c99
Fix test config embed
Jun 13, 2022
38dc1f6
Change fs to fetch_fs
Jun 13, 2022
8426fa1
Add config and config_test
Jun 13, 2022
5db07c2
Add Envoy config
Jun 14, 2022
63d3b59
Run go mod tidy
Jun 14, 2022
901efec
Consolidate into config struct
Jun 14, 2022
ce0ad6b
Reconfigure config around parsing EnvoyConfig proto
Jun 15, 2022
1ad59a3
Remove the JSON ref for now
Jun 15, 2022
94c5c52
Fill out the config sections and stub out parsers
Jun 15, 2022
0479f60
Catch the error when closing the response body
Jun 15, 2022
bc027f9
Add parsers for endpoints, listeners, and routes
Jun 15, 2022
c9f618d
Clean up endpoint parsing
Jun 15, 2022
99e4880
Properly return error
Jun 15, 2022
6e1f254
Add Envoy types for serialization
Jun 20, 2022
d83cfda
Fix LastUpdate to LastUpdated
Jun 20, 2022
28527e3
Add manual unmarshaling for Config
Jun 20, 2022
d54372c
Add outputConfig for clusters and endpoints
Jun 20, 2022
3909076
Change podname to positional arg
Jun 20, 2022
800b8b6
Modify PortForward to take a RestClient instead of creating one
Jun 21, 2022
ffbe6bd
Add filtering flags and fix issue with RestConfig
Jun 21, 2022
35b130e
Add Listeners, Routes, and Secrets to the output
Jun 21, 2022
cc2ffe3
Print a header above the tables
Jun 21, 2022
b47d763
Validate namespace input
Jun 21, 2022
813ee64
Add unit tests for read command
Jun 21, 2022
f872e6a
Remove the podname passed test
Jun 21, 2022
09cf538
Remove filter and filter chain match types
Jun 21, 2022
dcf9275
Unskip config tests
Jun 21, 2022
071f0fe
Fixup config tests
Jun 21, 2022
742261b
Fix ineffectual err assignment
Jun 21, 2022
702e394
Add secrets to output
Jun 23, 2022
e61c45b
Add a call for filter formatting
Jun 24, 2022
3b1d0c5
Print the Listener row once with full context, then each filter
Jun 24, 2022
0c5a5a7
Add filters to listeners in test
Jun 24, 2022
e1095c0
Add types for filters in listeners
Jun 24, 2022
a50ab01
Add first pass at filter formatting
Jun 24, 2022
6a94d43
Run go mod tidy
Jun 24, 2022
5683147
Re-add last updated to listeners
Jun 24, 2022
a119b43
Add static listeners and fix formatting of all listeners
Jun 24, 2022
0f8caf3
Format weight with 2 decimal points
Jun 24, 2022
82da7b5
Fix config test
Jun 29, 2022
609cc84
Add comment on implementing unmarshaling interface
Jun 30, 2022
1f9a035
Ran gofmt :facepalm:
Jun 30, 2022
636356c
Update CHANGELOG
Jul 11, 2022
76a14c8
Add proxy command to show subcommands
Jul 11, 2022
04b0259
Debug -> Inspect
Jul 11, 2022
84ea647
Add GetSetFlags
Jul 12, 2022
9e6db0a
Change -json to -raw-config and block on filters passed with it
Jul 12, 2022
f3e12d2
Print -json of values in the tables
Jul 12, 2022
bde69af
Set config values to empty slices by default
Jul 13, 2022
369986b
Safely return marshaling error
Jul 13, 2022
a333af8
:facepalm: like, actually handle the output error
Jul 13, 2022
5e71b48
Update config test to match the empty slices from the config
Jul 13, 2022
348af87
Add -o flag which takes table, json, or raw
Jul 13, 2022
9f61063
Add test for -output
Jul 13, 2022
c7cb2f7
Use switch for output instead
Jul 13, 2022
a00647f
Remove extra sizing arg in `make`
Jul 13, 2022
520d689
Remove unused help field
Jul 13, 2022
814f4df
Remove unused dependency
Jun 14, 2022
85a6579
Allow for passing in any output buffer for the UI
Jul 8, 2022
56da407
Allow passing in a buffer for terminal output
Jul 11, 2022
7eb8859
Test end-to-end for list command
Jul 11, 2022
4cbcdc8
Add end-to-end test for read command
Jul 11, 2022
155a0b5
Remove self-assignment
Jul 11, 2022
ce1e089
Fixed bug I created with the uninstall tests
Jul 11, 2022
8920b4a
TestArgumentParsing -> TestFlagParsing
Jul 13, 2022
6e5eb81
context.TODO() -> context.Background()
Jul 14, 2022
8a94ace
Move getInitializedCommand to the bottom of the test file
Jul 14, 2022
072334f
mockFetchConfig -> fakeFetchConfig
Jul 14, 2022
f1086c6
TestArgumentParsing -> TestFlagParsing (Part 2)
Jul 15, 2022
1c3b822
getInitializedCommand -> setupCommand
Jul 15, 2022
2269927
EndToEnd -> *CommandOutput
Jul 15, 2022
b19adbd
Remove Init() function from commands (was no-op)
Jul 15, 2022
3c9ea61
interpret -> parse
Jul 15, 2022
ca46a3a
Use NewUI in NewBasicUI
Jul 15, 2022
ddf2106
Check that dont-fetch is not in the output
Jul 15, 2022
209ac14
Remove once to fix linter error
Jul 15, 2022
fa28ee1
Resolve merge conflicts
Jul 18, 2022
363a883
Get admin port for all services
Jul 19, 2022
c7e15b8
Add UI to all commands
Jul 19, 2022
bb81073
Separate out formatting of tables
Jul 19, 2022
b23296a
Add fake pod to fix command_test
Jul 19, 2022
dba095b
Add tests for formatting
Jul 19, 2022
8d4b62a
Actually check the error from outputConfigs
Jul 19, 2022
c794e25
Add back in the e???
Jul 19, 2022
3e19e7a
Resolve merge conflicts
Jul 18, 2022
99af8a6
Get admin port for all services
Jul 19, 2022
4b534d9
Add back in the e???
Jul 19, 2022
a7d9678
Read output for table formatting
Jul 25, 2022
03b340f
Fix issues that came up with rebasing
Aug 1, 2022
8534fb6
Add FQDN filtering
Jul 13, 2022
0df7152
Add port filtering
Jul 14, 2022
9435738
Rename filters to reflect their association with clusters
Jul 14, 2022
1bd6eab
Stub out other filter functions
Jul 14, 2022
524a499
Add comments to filter functions
Jul 15, 2022
292fb69
Redo filters to make them nicer functions
Jul 15, 2022
f6398af
Namespace -> namespace
Jul 20, 2022
31c00d6
tableFitlersPassed -> areTablesFiltered
Jul 20, 2022
6c64ff8
Make the filtering verbiage nicer
Jul 20, 2022
7e6515d
Add more combos to cluster filtering test
Jul 20, 2022
3b9578d
Add cases to make the filters tests more complete
Jul 20, 2022
2c1e75e
Fix test output
Jul 25, 2022
67058c8
Replace -> with "to" in output
Jul 25, 2022
4252bb7
Update tests to account for -> to "to" change
Jul 25, 2022
c3ff050
Fix rebase issues
Aug 2, 2022
21b49c6
Clarify wording on -port help text
Aug 2, 2022
a7466e6
Remove unused output method
Aug 2, 2022
5148199
Make the filter output a bit more pronounced
Aug 2, 2022
2c18202
Remove excess values in filter tests
Aug 2, 2022
4eab2a3
Change filter logic from negative to positive
Aug 2, 2022
ad7a73c
Use "shouldPrintTable" method
Aug 2, 2022
cbdc1b5
Change the separator on endpoints in the filter code
Aug 2, 2022
5fc6f3c
endpoint addresses -> addresses
Aug 2, 2022
637b3d2
Add test with multiple endpoints
Aug 2, 2022
7f20394
Add note on combining filters
Aug 2, 2022
67c097f
Test multiple combinations of table filtering flags
Aug 3, 2022
48a0c92
Add cluster mapping to the config
Jul 26, 2022
6c1dfb3
Create mappings of clusters/endpoint and pass them in to parsers
Jul 26, 2022
c527691
Add cluster endpoint parsing to tests
Jul 26, 2022
d7ae0d3
Add ability to run CLI commands without re-compiling each time
Jul 25, 2022
527a713
Add method for extracting CLI binary from cli_cluster obj
Aug 2, 2022
758863b
Add test for Envoy debugging
Aug 2, 2022
2f14a2d
Add check for correct output
Aug 8, 2022
a5e1e05
Remove commented out output
Aug 8, 2022
eb29fcb
Add retry to read check
Aug 8, 2022
1935fcf
Shift list and read testing into existing Connect Inject test
Aug 8, 2022
878fec6
Fix helper test out of order bug
Aug 8, 2022
6c0e61a
Fix silly off-by-one error :facepalm:
Aug 8, 2022
987afc1
Move helper into connect inject test
Aug 8, 2022
eabafe3
Fix list translation
Aug 8, 2022
9a9a55d
Fix merge conflict on CHANGELOG
Aug 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
FEATURES:
* Transparent Proxy Egress
* Add support for Destinations on the Service Defaults CRD. [[GH-1352](https://github.com/hashicorp/consul-k8s/pull/1352)]
* CLI:
* Add `consul-k8s proxy list` command for displaying Pods running Envoy managed by Consul. [[GH-1271](https://github.com/hashicorp/consul-k8s/pull/1271)
* Add `consul-k8s proxy read podname` command for displaying Envoy configuration for a given Pod. [[GH-1271](https://github.com/hashicorp/consul-k8s/pull/1271)
* [Experimental] Cluster Peering:
* Add support for ACLs and TLS. [[GH-1343](https://github.com/hashicorp/consul-k8s/pull/1343)] [[GH-1366](https://github.com/hashicorp/consul-k8s/pull/1366)]
* Add support for Load Balancers or external addresses in front of Consul servers for peering stream.
Expand Down
31 changes: 31 additions & 0 deletions acceptance/framework/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cli
t-eckert marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os/exec"

"github.com/hashicorp/consul-k8s/acceptance/framework/config"
)

// CLI provides access to compile and execute commands with the `consul-k8s` CLI.
type CLI struct {
initialized bool
}

// NewCLI compiles the `consul-k8s` CLI and returns a handle to execute commands
// with the binary.
func NewCLI() (*CLI, error) {
cmd := exec.Command("go", "install", ".")
cmd.Dir = config.CLIPath
_, err := cmd.Output()
return &CLI{true}, err
}

// Run runs the CLI with the given args.
func (c *CLI) Run(args ...string) ([]byte, error) {
if !c.initialized {
return nil, fmt.Errorf("CLI must be initialized before calling Run, use `cli.NewCLI()` to initialize.")
}
cmd := exec.Command("cli", args...)
return cmd.Output()
}
26 changes: 13 additions & 13 deletions acceptance/framework/consul/cli_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package consul
import (
t-eckert marked this conversation as resolved.
Show resolved Hide resolved
"context"
"fmt"
"os/exec"
"strings"
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/helm"
terratestk8s "github.com/gruntwork-io/terratest/modules/k8s"
terratestLogger "github.com/gruntwork-io/terratest/modules/logger"
"github.com/hashicorp/consul-k8s/acceptance/framework/cli"
"github.com/hashicorp/consul-k8s/acceptance/framework/config"
"github.com/hashicorp/consul-k8s/acceptance/framework/environment"
"github.com/hashicorp/consul-k8s/acceptance/framework/helpers"
Expand Down Expand Up @@ -44,6 +44,7 @@ type CLICluster struct {
noCleanupOnFailure bool
debugDirectory string
logger terratestLogger.TestLogger
cli cli.CLI
}

// NewCLICluster creates a new Consul cluster struct which can be used to create
Expand Down Expand Up @@ -90,6 +91,9 @@ func NewCLICluster(
Logger: logger,
}

cli, err := cli.NewCLI()
require.NoError(t, err)

return &CLICluster{
ctx: ctx,
helmOptions: hopts,
Expand All @@ -103,6 +107,7 @@ func NewCLICluster(
noCleanupOnFailure: cfg.NoCleanupOnFailure,
debugDirectory: cfg.DebugDirectory,
logger: logger,
cli: *cli,
}
}

Expand All @@ -129,7 +134,7 @@ func (c *CLICluster) Create(t *testing.T) {
args = append(args, "-timeout", "15m")
args = append(args, "-auto-approve")

out, err := c.runCLI(args)
out, err := c.cli.Run(args...)
if err != nil {
c.logger.Logf(t, "error running command `consul-k8s %s`: %s", strings.Join(args, " "), err.Error())
c.logger.Logf(t, "command stdout: %s", string(out))
Expand Down Expand Up @@ -162,7 +167,7 @@ func (c *CLICluster) Upgrade(t *testing.T, helmValues map[string]string) {
args = append(args, "-timeout", "15m")
args = append(args, "-auto-approve")

out, err := c.runCLI(args)
out, err := c.cli.Run(args...)
if err != nil {
c.logger.Logf(t, "error running command `consul-k8s %s`: %s", strings.Join(args, " "), err.Error())
c.logger.Logf(t, "command stdout: %s", string(out))
Expand All @@ -186,7 +191,7 @@ func (c *CLICluster) Destroy(t *testing.T) {
// Use `go run` so that the CLI is recompiled and therefore uses the local
// charts directory rather than the directory from whenever it was last
// compiled.
out, err := c.runCLI(args)
out, err := c.cli.Run(args...)
if err != nil {
c.logger.Logf(t, "error running command `consul-k8s %s`: %s", strings.Join(args, " "), err.Error())
c.logger.Logf(t, "command stdout: %s", string(out))
Expand Down Expand Up @@ -267,6 +272,10 @@ func (c *CLICluster) SetupConsulClient(t *testing.T, secure bool) (*api.Client,
return consulClient, config.Address
}

func (c *CLICluster) CLI() cli.CLI {
return c.cli
}

func createOrUpdateNamespace(t *testing.T, client kubernetes.Interface, namespace string) {
_, err := client.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -296,12 +305,3 @@ func (c *CLICluster) setKube(args []string) []string {

return args
}

// runCLI runs the CLI with the given args.
// Use `go run` so that the CLI is recompiled and therefore uses the local
// charts directory rather than the directory from whenever it was last compiled.
func (c *CLICluster) runCLI(args []string) ([]byte, error) {
cmd := exec.Command("go", append([]string{"run", "."}, args...)...)
cmd.Dir = config.CLIPath
return cmd.Output()
}
59 changes: 59 additions & 0 deletions acceptance/tests/connect/connect_inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/hashicorp/consul-k8s/acceptance/framework/cli"
"github.com/hashicorp/consul-k8s/acceptance/framework/consul"
"github.com/hashicorp/consul-k8s/acceptance/framework/helpers"
"github.com/hashicorp/consul-k8s/acceptance/framework/k8s"
Expand All @@ -18,6 +19,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const ipv4RegEx = "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"

// TestConnectInject tests that Connect works in a default and a secure installation.
func TestConnectInject(t *testing.T) {
cases := map[string]struct {
Expand Down Expand Up @@ -60,6 +63,9 @@ func TestConnectInject(t *testing.T) {

for name, c := range cases {
t.Run(name, func(t *testing.T) {
cli, err := cli.NewCLI()
require.NoError(t, err)

cfg := suite.Config()
ctx := suite.Environment().DefaultContext(t)

Expand All @@ -80,6 +86,40 @@ func TestConnectInject(t *testing.T) {
connHelper.TestConnectionFailureWithoutIntention(t)
connHelper.CreateIntention(t)
}

// Run proxy list and get the two results.
listOut, err := cli.Run("proxy", "list")
require.NoError(t, err)
logger.Log(t, string(listOut))
list := translateListOutput(listOut)
require.Equal(t, 2, len(list))
for _, proxyType := range list {
require.Equal(t, "Sidecar", proxyType)
}

// Run proxy read and check that the connection is present in the output.
retrier := &retry.Timer{Timeout: 160 * time.Second, Wait: 2 * time.Second}
retry.RunWith(retrier, t, func(r *retry.R) {
for podName := range list {
out, err := cli.Run("proxy", "read", podName)
require.NoError(t, err)

output := string(out)
logger.Log(t, output)

// Both proxies must see their own local agent and app as clusters.
require.Regexp(r, "local_agent.*STATIC", output)
require.Regexp(r, "local_app.*STATIC", output)

// Static Client must have Static Server as a cluster and endpoint.
if strings.Contains(podName, "static-client") {
require.Regexp(r, "static-server.*static-server\\.default\\.dc1\\.internal.*EDS", output)
require.Regexp(r, ipv4RegEx+".*static-server.default.dc1.internal", output)
}

}
})

connHelper.TestConnectionSuccess(t)
connHelper.TestConnectionFailureWhenUnhealthy(t)
})
Expand Down Expand Up @@ -428,3 +468,22 @@ func TestConnectInject_MultiportServices(t *testing.T) {
})
}
}

// translateListOutput takes the raw output from the proxy list command and
// translates the table into a map.
func translateListOutput(raw []byte) map[string]string {
formatted := make(map[string]string)
for _, pod := range strings.Split(strings.TrimSpace(string(raw)), "\n")[3:] {
row := strings.Split(strings.TrimSpace(pod), "\t")

var name string
if len(row) == 3 { // Handle the case where namespace is present
name = fmt.Sprintf("%s/%s", strings.TrimSpace(row[0]), strings.TrimSpace(row[1]))
} else if len(row) == 2 {
name = strings.TrimSpace(row[0])
}
formatted[name] = row[len(row)-1]
}

return formatted
}
3 changes: 0 additions & 3 deletions cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ func (c *Command) init() {
})

c.help = c.set.Help()

// c.Init() calls the embedded BaseCommand's initialization function.
c.Init()
}

// Run installs Consul into a Kubernetes cluster.
Expand Down
26 changes: 26 additions & 0 deletions cli/cmd/proxy/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package proxy

import (
"fmt"

"github.com/hashicorp/consul-k8s/cli/common"
"github.com/mitchellh/cli"
)

// ProxyCommand provides a synopsis for the proxy subcommands (e.g. read).
type ProxyCommand struct {
*common.BaseCommand
}

// Run prints out information about the subcommands.
func (c *ProxyCommand) Run(args []string) int {
return cli.RunResultHelp
}

func (c *ProxyCommand) Help() string {
return fmt.Sprintf("%s\n\nUsage: consul-k8s proxy <subcommand>", c.Synopsis())
}

func (c *ProxyCommand) Synopsis() string {
return "Inspect Envoy proxies managed by Consul."
}
Loading