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

Separate agent from cli #209

Merged
merged 5 commits into from
Jun 16, 2023
Merged

Separate agent from cli #209

merged 5 commits into from
Jun 16, 2023

Conversation

pablochacin
Copy link
Collaborator

@pablochacin pablochacin commented Jun 16, 2023

Description

Presently there is an intermix of the logic that process the CLI (mostly, parameter processing) and logic to inject faults in a target.

When combined with the complexity of the execution of cobra commands (pre and post-run functions across a hierarchy of commands, including a well-known bug), it makes it hard to reason about this logic, creating complex bugs such as #206.

This change set creates an Agent object that encapsulates the logic for controlling the execution of the fault injection, separated from the CLI.

These changes also point in the direction to run the agent as a grpc service with the CLI as a frontend for its API #52

Fixes #206 #205

Checklist:

  • My code follows the coding style of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works.
  • Any dependent changes have been merged and published in downstream modules

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
@pablochacin pablochacin marked this pull request as ready for review June 16, 2023 07:32
@pablochacin pablochacin requested a review from roobre June 16, 2023 07:32
Copy link
Collaborator

@roobre roobre left a comment

Choose a reason for hiding this comment

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

I think this gets us closer to a separation between CLI and agent, but I'm still not 100% in with the current approach. I Think the separation between our objects is still a bit blurry.

I've left comments with an approach that I think could be nicer, but it might as well be too much work for now if we plan to further refactor this into a daemon mode. LMK if you think we should postpone this discussion until then.

cmd/agent/main.go Outdated Show resolved Hide resolved
pkg/agent/agent.go Show resolved Hide resolved
}

// do executes a command in the Agent
func (r *Agent) do(ctx context.Context, action func(context.Context) error) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this action parameter really wants to become an interface, as said above.

pkg/agent/agent.go Outdated Show resolved Hide resolved
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
@pablochacin pablochacin requested a review from roobre June 16, 2023 11:25
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
Copy link
Collaborator

@roobre roobre left a comment

Choose a reason for hiding this comment

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

I really like how this is looking! I think we've converged into a nice solution that keeps Cobra isolated.

I've left three very minor comments, but it's looking great already!

cmd/agent/commands/root.go Outdated Show resolved Hide resolved
cmd/agent/commands/grpc.go Show resolved Hide resolved
Comment on lines +102 to +126
if d.config.Transparent {
trCfg := iptables.TrafficRedirectorConfig{
Executor: d.executor,
}
// Redirect traffic to the proxy
tr := &iptables.TrafficRedirectionSpec{
Iface: d.config.Iface,
DestinationPort: d.config.TargetPort,
RedirectPort: d.config.RedirectPort,
}

redirector, err := iptables.NewTrafficRedirectorWithConfig(tr, trCfg)
if err != nil {
return err
}

if err := redirector.Start(); err != nil {
return fmt.Errorf(" failed traffic redirection: %w", err)
}

defer func() {
_ = redirector.Stop()
}()
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another alternative to this, would be to make the redirector an interface, and have two implementations: iptables.Redirector and a NoopRedirector that does nothing. Then, instead of the big if block here, we could pass the disruptor a NoopRedirector if we're just proxying and an iptables.Redirector if we're running in transparent mode.

Dropping this here as an alternative approach in case you want to consider it. I don't have strong concerns against the simple if.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This makes sense. I will give it a try.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

On second thought I will make it in a follow-up PR so as not to mix concerns here.

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
@pablochacin pablochacin merged commit f405082 into main Jun 16, 2023
7 checks passed
@pablochacin pablochacin deleted the separate-agent-from-cli branch June 16, 2023 13:58
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.

Agent instrumentation data is not generated
2 participants