-
Notifications
You must be signed in to change notification settings - Fork 215
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
fix: skip transient grpc errors in pktmon #1265
Open
matmerr
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
matmerr:matmerr/pktmontesting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−129
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package pktmon | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
|
||
observerv1 "github.com/cilium/cilium/api/v1/observer" | ||
"github.com/microsoft/retina/pkg/log" | ||
"github.com/microsoft/retina/pkg/utils" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapio" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
var ErrNilStream = errors.New("stream is nil") | ||
|
||
type GRPCManager interface { | ||
SetupStream() error | ||
StartStream(ctx context.Context) error | ||
ReceiveFromStream() (*observerv1.GetFlowsResponse, error) | ||
|
||
RunPktMonServer(ctx context.Context) error | ||
Stop() error | ||
} | ||
|
||
type WindowsGRPCManager struct { | ||
grpcClient *GRPCClient | ||
stream observerv1.Observer_GetFlowsClient | ||
l *log.ZapLogger | ||
|
||
pktmonCmd *exec.Cmd | ||
stdWriter *zapio.Writer | ||
errWriter *zapio.Writer | ||
} | ||
|
||
func (p *WindowsGRPCManager) SetupStream() error { | ||
var err error | ||
fn := func() error { | ||
p.l.Info("creating pktmon client") | ||
p.grpcClient, err = newGRPCClient() | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to create pktmon client before getting flows") | ||
} | ||
|
||
return nil | ||
} | ||
err = utils.Retry(fn, connectionRetryAttempts) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to create pktmon client") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *WindowsGRPCManager) StartStream(ctx context.Context) error { | ||
if p.grpcClient == nil { | ||
return errors.Wrapf(ErrNilGrpcClient, "unable to start stream") | ||
} | ||
|
||
var err error | ||
fn := func() error { | ||
p.stream, err = p.grpcClient.GetFlows(ctx, &observerv1.GetFlowsRequest{}) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to open pktmon stream") | ||
} | ||
return nil | ||
} | ||
err = utils.Retry(fn, connectionRetryAttempts) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to create pktmon client") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *WindowsGRPCManager) ReceiveFromStream() (*observerv1.GetFlowsResponse, error) { | ||
if p.stream == nil { | ||
return nil, errors.Wrapf(ErrNilStream, "unable to receive from stream") | ||
} | ||
|
||
return p.stream.Recv() //nolint:wrapcheck // wrapcheck is disabled because we want to return the error as is | ||
} | ||
|
||
func newGRPCClient() (*GRPCClient, error) { | ||
retryPolicy := map[string]any{ | ||
"methodConfig": []map[string]any{ | ||
{ | ||
"waitForReady": true, | ||
"retryPolicy": map[string]any{ | ||
"MaxAttempts": connectionRetryAttempts, | ||
"InitialBackoff": ".01s", | ||
"MaxBackoff": ".01s", | ||
"BackoffMultiplier": 1.0, | ||
"RetryableStatusCodes": []string{"UNAVAILABLE"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
bytes, err := json.Marshal(retryPolicy) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to marshal retry policy") | ||
} | ||
|
||
retryPolicyStr := string(bytes) | ||
|
||
conn, err := grpc.NewClient(fmt.Sprintf("%s:%s", "unix", socket), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(retryPolicyStr)) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to dial pktmon server") | ||
} | ||
|
||
return &GRPCClient{observerv1.NewObserverClient(conn)}, nil | ||
} | ||
|
||
func (p *WindowsGRPCManager) RunPktMonServer(ctx context.Context) error { | ||
p.stdWriter = &zapio.Writer{Log: p.l.Logger, Level: zap.InfoLevel} | ||
defer p.stdWriter.Close() | ||
p.errWriter = &zapio.Writer{Log: p.l.Logger, Level: zap.ErrorLevel} | ||
defer p.errWriter.Close() | ||
|
||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to get current working directory for pktmon") | ||
} | ||
|
||
cmd := pwd + "\\" + "controller-pktmon.exe" | ||
|
||
p.pktmonCmd = exec.CommandContext(ctx, cmd) | ||
p.pktmonCmd.Dir = pwd | ||
p.pktmonCmd.Args = append(p.pktmonCmd.Args, "--socketpath", socket) | ||
p.pktmonCmd.Env = os.Environ() | ||
p.pktmonCmd.Stdout = p.stdWriter | ||
p.pktmonCmd.Stderr = p.errWriter | ||
|
||
p.l.Info("calling start on pktmon stream server", zap.String("cmd", p.pktmonCmd.String())) | ||
|
||
// block this thread, and should it ever return, it's a problem | ||
err = p.pktmonCmd.Run() | ||
if err != nil { | ||
return errors.Wrapf(err, "pktmon server exited when it should not have") | ||
} | ||
|
||
// we never want to return happy from this | ||
return errors.Wrapf(ErrUnexpectedExit, "pktmon server exited unexpectedly") | ||
} | ||
|
||
func (p *WindowsGRPCManager) Stop() error { | ||
if p.pktmonCmd != nil { | ||
err := p.pktmonCmd.Process.Kill() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fire-and-forget. Do we care? If so, we need |
||
if err != nil { | ||
return errors.Wrapf(err, "failed to kill pktmon server during stop") | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to locate this with
exec.LookPath
?