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

Add --replace flag to intercept command #3469

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions integration_test/intercept_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package integration_test

import (
"regexp"
"strconv"
"time"

"github.com/datawire/dlib/dlog"
"github.com/telepresenceio/telepresence/v2/integration_test/itest"
)

type interceptFlagSuite struct {
itest.Suite
itest.SingleService
}

func (s *interceptFlagSuite) SuiteName() string {
return "InterceptFlag"
}

func init() {
itest.AddSingleServiceSuite("-intercept-flag", "echo", func(h itest.SingleService) itest.TestingSuite {
return &interceptFlagSuite{Suite: itest.Suite{Harness: h}, SingleService: h}
})
}

func (s *interceptFlagSuite) SetupSuite() {
if s.CompatVersion() != "" {
s.T().Skip("Not part of compatibility suite")
}
s.Suite.SetupSuite()
}

func (s *interceptFlagSuite) Test_ContainerReplace() {
ctx := s.Context()
require := s.Require()
iceptName := "container-replaced"
port, cancel := itest.StartLocalHttpEchoServer(ctx, iceptName)
defer cancel()
expectedOutput := regexp.MustCompile(iceptName + ` from intercept at`)

stdout := itest.TelepresenceOk(ctx, "intercept", "--replace", "--port", strconv.Itoa(port), s.ServiceName())
defer func() {
itest.TelepresenceOk(ctx, "leave", s.ServiceName())
s.Eventually(func() bool {
out, err := itest.Output(ctx, "curl", "--silent", "--max-time", "1", s.ServiceName())
if err != nil {
dlog.Error(ctx, err)
return false
}
dlog.Info(ctx, out)
return !expectedOutput.MatchString(out)
}, 1*time.Minute, 6*time.Second)
require.Contains(stdout, "1/1")
}()
require.Contains(stdout, "Using Deployment "+s.ServiceName())

require.Eventually(func() bool {
out, err := itest.Output(ctx, "curl", "--silent", "--max-time", "1", s.ServiceName())
if err != nil {
dlog.Error(ctx, err)
return false
}
dlog.Info(ctx, out)
return expectedOutput.MatchString(out)
}, 1*time.Minute, 6*time.Second)

stdout, err := itest.KubectlOut(ctx, s.AppNamespace(), "get", "pod", "-lapp="+s.ServiceName())
require.NoError(err)
require.NotContains(stdout, "2/2")
require.Contains(stdout, "1/1")
}
6 changes: 6 additions & 0 deletions pkg/client/cli/intercept/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Command struct {
LocalOnly bool // --local-only
LocalMountPort uint16 // --local-mount-port

Replace bool // whether --replace was passed

EnvFile string // --env-file
EnvJSON string // --env-json
Mount string // --mount // "true", "false", or desired mount point // only valid if !localOnly
Expand Down Expand Up @@ -105,6 +107,10 @@ func (a *Command) AddFlags(cmd *cobra.Command) {
flagSet.Uint16Var(&a.LocalMountPort, "local-mount-port", 0,
`Do not mount remote directories. Instead, expose this port on localhost to an external mounter`)

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.`)

// 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
3 changes: 2 additions & 1 deletion pkg/client/cli/intercept/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func (s *state) Cmd() *cobra.Command {

func (s *state) CreateRequest(ctx context.Context) (*connector.CreateInterceptRequest, error) {
spec := &manager.InterceptSpec{
Name: s.Name(),
Name: s.Name(),
Replace: s.Replace,
}
ir := &connector.CreateInterceptRequest{
Spec: spec,
Expand Down
Loading