Skip to content

Commit

Permalink
add support for first-n-connections
Browse files Browse the repository at this point in the history
  • Loading branch information
based64god committed Jul 17, 2024
1 parent 7262d31 commit 7bc26e3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
18 changes: 10 additions & 8 deletions pkg/client/cli/intercept/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
)

type Command struct {
Name string // Command[0] || `${Command[0]}-${--namespace}` // which depends on a combinationof --workload and --namespace
AgentName string // --workload || Command[0] // only valid if !localOnly
Port string // --port // only valid if !localOnly
ServiceName string // --service // only valid if !localOnly
Address string // --address // only valid if !localOnly
LocalOnly bool // --local-only
LocalMountPort uint16 // --local-mount-port
Name string // Command[0] || `${Command[0]}-${--namespace}` // which depends on a combinationof --workload and --namespace
AgentName string // --workload || Command[0] // only valid if !localOnly
Port string // --port // only valid if !localOnly
ServiceName string // --service // only valid if !localOnly
Address string // --address // only valid if !localOnly
LocalOnly bool // --local-only
LocalMountPort uint16 // --local-mount-port
FirstNConnections uint32 // --first-n-connections

Replace bool // whether --replace was passed

Expand Down Expand Up @@ -117,7 +118,8 @@ func (a *Command) AddFlags(cmd *cobra.Command) {
flagSet.BoolVarP(&a.Replace, "replace", "", false,
`Indicates if the traffic-agent should replace application containers in workload pods. `+
`The default behavior is for the agent sidecar to be installed alongside existing containers.`)

// the default value of 0 is used to indicate that all connections should be intercepted
flagSet.Uint32Var(&a.FirstNConnections, "first-n-connections", 0, `Limit the number of connections to intercept`)
// Hide these flags. They are still functional but deprecated. Using them will yield a deprecation message.
flagSet.Lookup("local-only").Hidden = true
flagSet.Lookup("namespace").Hidden = true
Expand Down
16 changes: 15 additions & 1 deletion pkg/client/userd/trafficmgr/intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type intercept struct {

// Use bridged ftp/sftp mount through this local port
localMountPort int32

// nConnections is the number of connections that have been intercepted
nConnections uint32
}

// interceptResult is what gets written to the awaitIntercept's waitCh channel when the
Expand Down Expand Up @@ -140,7 +143,17 @@ func (ic *intercept) localPorts() []string {
}

func (ic *intercept) shouldForward() bool {
return len(ic.localPorts()) > 0
accumulator := true
b := []bool{}
b = append(b, len(ic.localPorts()) > 0)
if ic.Spec.FirstNConnections > 0 {
b = append(b, ic.nConnections < ic.Spec.FirstNConnections)

}

Check failure on line 152 in pkg/client/userd/trafficmgr/intercept.go

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

unnecessary trailing newline (whitespace)

Check failure on line 152 in pkg/client/userd/trafficmgr/intercept.go

View workflow job for this annotation

GitHub Actions / unit (ubuntu-arm64)

unnecessary trailing newline (whitespace)

Check failure on line 152 in pkg/client/userd/trafficmgr/intercept.go

View workflow job for this annotation

GitHub Actions / unit (macos-latest)

unnecessary trailing newline (whitespace)

Check failure on line 152 in pkg/client/userd/trafficmgr/intercept.go

View workflow job for this annotation

GitHub Actions / unit (windows-2019)

unnecessary trailing newline (whitespace)
for _, v := range b {
accumulator = accumulator && v
}
return accumulator
}

// startForwards starts port forwards and mounts for the given podInterceptKey.
Expand Down Expand Up @@ -241,6 +254,7 @@ func (lpf *podIntercepts) start(ctx context.Context, ic *intercept, rd daemon.Da
}
if ic.shouldForward() {
ic.startForwards(ctx, &lp.wg)
ic.nConnections++
}
dlog.Debugf(ctx, "Started mounts and port-forwards for %+v", fk)
lpf.alivePods[fk] = lp
Expand Down
16 changes: 14 additions & 2 deletions rpc/manager/manager.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/manager/manager.proto
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ message InterceptSpec {

// Whether to replace the running container.
bool replace = 22;

// How many connections should be used for the intercept
uint32 first_n_connections = 23;
}

enum InterceptDispositionType {
Expand Down

0 comments on commit 7bc26e3

Please sign in to comment.