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

Get envoy log level per pod #1844

Merged
merged 11 commits into from
Jan 27, 2023
Merged

Get envoy log level per pod #1844

merged 11 commits into from
Jan 27, 2023

Conversation

jm96441n
Copy link
Member

@jm96441n jm96441n commented Jan 20, 2023

Changes proposed in this PR:

How I've tested this PR:

  • Unit tests
  • ran consul on k8's locally using kind https://gist.github.com/jm96441n/a56cd2ebd53f4484c3b40e725e305372
    • create a cluster using kind: kind create cluster
    • install consul on that cluster using the consul-values.yml file in the above gist: consul-k8s install -config-file=consul-values.yaml -set global.image=hashicorp/consul:1.14.3
    • once that is up apply the server.yaml to your cluster kubectl apply server.yaml
    • once that is setup get one of the server pod names using kubectl get pods
    • in another terminal window set up port forwarding for that pod kubectl port-forward <POD_NAME> 19000:19000 (where 19000 is the default admin port for the envoy proxy)
    • in the original terminal run consul-k8s proxy log <POD_NAME> and compare the output to the envoy admin interface which would be at localhost:19000/logging while port forwarding is enabled

How I expect reviewers to test this PR:

  • code review
  • potentially running above steps (but probably not necessary)

NOTE Didn't add a CHANGELOG yet, planning on adding the CHANGELOG entry once the work on the consul-k8s proxy log command is complete, though can add one for the current work if that's preferred

Checklist:

  • Tests added
  • CHANGELOG entry added

    HashiCorp engineers only, community PRs should not add a changelog entry.
    Entries should use present tense (e.g. Add support for...)

@hashicorp-cla
Copy link

hashicorp-cla commented Jan 20, 2023

CLA assistant check
All committers have signed the CLA.


var ErrMissingPodName = errors.New("Exactly one positional argument is required: <pod-name>")

var levelToColor = map[string]string{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@andrewstucki andrewstucki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass and some suggestions to potentially simplify some of this code! Good work! 🎉

cli/cmd/proxy/loglevel/command.go Outdated Show resolved Hide resolved
cli/cmd/proxy/loglevel/command.go Outdated Show resolved Hide resolved
return 0
}

func (l *LogLevelCommand) parseFlags(args []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, wondering why not just do something like l.set.Parse(args) and then l.set.Args(). Something like:

func (l *LogLevelCommand) parseFlags(args []string) error {
    if err := l.set.Parse(args); err != nil {
        return err
    }

    positional := l.set.Args()
    if len(positional) != 1 {
        return ErrMissingPodName
    }

    l.podName = positional[0]
    return nil
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason in particular, this definitely reads a lot clearer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the flag lib doesn't actually handle parsing keyword arguments in this situation because it stops parsing args once it sees the first positional argument:

Flag parsing stops just before the first non-flag argument ("-" is a non-flag argument) or after the terminator "--".
from:
https://pkg.go.dev/flag#hdr-Command_line_flag_syntax

so we have to pull all the positional arguments out of the args slice before calling parse on them

cli/cmd/proxy/loglevel/command.go Outdated Show resolved Hide resolved
cli/cmd/proxy/loglevel/command.go Show resolved Hide resolved
}

func (l *LogLevelCommand) Help() string {
l.once.Do(l.init)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering what the call to init here is for? Is Help ever called without first calling Run? If so, maybe consider adding a New method that initializes the struct and calls init rather than the bare initialization in commands.go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK it can be called before Run, I kept this pattern with the init to match the rest of the codebase tbh, I'd typically prefer to use a New command to ensure everything is initialized first prior to running but went this route to keep it in line with the rest of the repo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a quirk of this CLI library. Help can be called without first calling Run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if we moved to a constructor and called that in the commands.go rather than just initializing the struct we could handle that, the issue would be that we'd be instantiating all of our deps/connections even if this command wasn't called which could slow down usage for other commands (unless the CLI lazily inits the commands, which it kinda looks like it does)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind what I said about slowing down initialization with deps, I was mistaken in thinking we could have access to the args and pass them to the constructor but it doesn't look like that's the case so we still init the deps in Run but are able to push creating the flags into a constructor, I put together what it would look like here, the only drawback is that we'd be deviating from the pattern in the rest of the cli

validation, check bounds of returned slice for envoy parsing, check
status code of response from envoy
@jm96441n jm96441n force-pushed the get-envoy-log-level-per-pod branch from ea3d258 to 7425f12 Compare January 20, 2023 22:53
Copy link
Contributor

@t-eckert t-eckert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I still need to test it out, but it all looks correct!

@jm96441n jm96441n merged commit 97ab255 into main Jan 27, 2023
@jm96441n jm96441n deleted the get-envoy-log-level-per-pod branch January 27, 2023 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants