Skip to content

Commit

Permalink
PR feedback, DRY-ing up some error handling logic, cleaning up
Browse files Browse the repository at this point in the history
validation, check bounds of returned slice for envoy parsing, check
status code of response from envoy
  • Loading branch information
jm96441n authored and david-yu committed Feb 14, 2023
1 parent cd7b8be commit a3ef037
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
57 changes: 38 additions & 19 deletions cli/cmd/proxy/loglevel/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ const (

type LoggerConfig map[string]string

var ErrMissingPodName = errors.New("Exactly one positional argument is required: <pod-name>")
var (
ErrIncorrectArgFormat = errors.New("Exactly one positional argument is required: <pod-name>")
ErrNoLoggersReturned = errors.New("No loggers were returned from Envoy")
)

var levelToColor = map[string]string{
"trace": terminal.Green,
Expand Down Expand Up @@ -93,16 +96,11 @@ func (l *LogLevelCommand) Run(args []string) int {

err := l.parseFlags(args)
if err != nil {
l.UI.Output(err.Error(), terminal.WithErrorStyle())
l.UI.Output(fmt.Sprintf("\n%s", l.Help()))
return 1
return l.logOutputAndDie(err)
}

err = l.validateFlags()
if err != nil {
l.UI.Output(err.Error(), terminal.WithErrorStyle())
l.UI.Output(fmt.Sprintf("\n%s", l.Help()))
return 1
return l.logOutputAndDie(err)
}

if l.logLevelFetcher == nil {
Expand All @@ -111,29 +109,27 @@ func (l *LogLevelCommand) Run(args []string) int {

err = l.initKubernetes()
if err != nil {
l.UI.Output(err.Error(), terminal.WithErrorStyle())
l.UI.Output(fmt.Sprintf("\n%s", l.Help()))
return 1
return l.logOutputAndDie(err)
}

adminPorts, err := l.fetchAdminPorts()
if err != nil {
l.UI.Output(err.Error(), terminal.WithErrorStyle())
l.UI.Output(fmt.Sprintf("\n%s", l.Help()))
return 1
return l.logOutputAndDie(err)
}

logLevels, err := l.fetchLogLevels(adminPorts)
if err != nil {
l.UI.Output(err.Error(), terminal.WithErrorStyle())
l.UI.Output(fmt.Sprintf("\n%s", l.Help()))
return 1
return l.logOutputAndDie(err)
}
l.outputLevels(logLevels)
return 0
}

func (l *LogLevelCommand) parseFlags(args []string) error {
if len(args) == 0 {
return ErrIncorrectArgFormat
}

positional := []string{}
// Separate positional args from keyed args
for _, arg := range args {
Expand All @@ -145,7 +141,7 @@ func (l *LogLevelCommand) parseFlags(args []string) error {
keyed := args[len(positional):]

if len(positional) != 1 {
return ErrMissingPodName
return ErrIncorrectArgFormat
}

l.podName = positional[0]
Expand All @@ -158,9 +154,15 @@ func (l *LogLevelCommand) parseFlags(args []string) error {
}

func (l *LogLevelCommand) validateFlags() error {
if errs := validation.ValidateNamespaceName(l.namespace, false); l.namespace != "" && len(errs) > 0 {
if l.namespace == "" {
return nil
}

errs := validation.ValidateNamespaceName(l.namespace, false)
if len(errs) > 0 {
return fmt.Errorf("invalid namespace name passed for -namespace/-n: %v", strings.Join(errs, "; "))
}

return nil
}

Expand Down Expand Up @@ -256,11 +258,21 @@ func FetchLogLevel(ctx context.Context, portForward common.PortForwarder) (Logge
if err != nil {
return nil, err
}

body, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("failed to reach envoy: %v", err)
}

if response.StatusCode >= 400 {
return nil, fmt.Errorf("call to envoy failed with status code: %d, and message: %s", response.StatusCode, body)
}

loggers := strings.Split(string(body), "\n")
if len(loggers) == 0 {
return nil, ErrNoLoggersReturned
}

logLevels := make(map[string]string)
var name string
var level string
Expand All @@ -274,6 +286,7 @@ func FetchLogLevel(ctx context.Context, portForward common.PortForwarder) (Logge
name = strings.TrimRight(name, ":")
logLevels[name] = level
}

return logLevels, nil
}

Expand Down Expand Up @@ -316,3 +329,9 @@ func (l *LogLevelCommand) AutocompleteFlags() complete.Flags {
func (l *LogLevelCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}

func (l *LogLevelCommand) logOutputAndDie(err error) int {
l.UI.Output(err.Error(), terminal.WithErrorStyle())
l.UI.Output(fmt.Sprintf("\n%s", l.Help()))
return 1
}
2 changes: 2 additions & 0 deletions cli/cmd/proxy/loglevel/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestOutputForGettingLogLevel(t *testing.T) {
}

func TestHelp(t *testing.T) {
t.Parallel()
buf := bytes.NewBuffer([]byte{})
c := setupCommand(buf)
expectedSynposis := "Inspect and Modify the Envoy Log configuration for a given Pod."
Expand All @@ -149,6 +150,7 @@ func TestHelp(t *testing.T) {
}

func TestFetchLogLevel(t *testing.T) {
t.Parallel()
rawLogLevels, err := os.ReadFile("testdata/fetch_debug_levels.txt")
require.NoError(t, err)
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit a3ef037

Please sign in to comment.