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 --port-forward to skaffold run #3263

Merged
merged 3 commits into from
Dec 20, 2019
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
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ var FlagRegistry = []Flag{
Value: &opts.PortForward.Enabled,
DefValue: false,
FlagAddMethod: "BoolVar",
DefinedOn: []string{"dev", "debug"},
DefinedOn: []string{"dev", "debug", "run"},
},
{
Name: "status-check",
Expand Down
2 changes: 2 additions & 0 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ Options:
-n, --namespace='': Run deployments in the specified namespace
--no-prune=false: Skip removing images and containers built by Skaffold
--no-prune-children=false: Skip removing layers reused by Skaffold
--port-forward=false: Port-forward exposed container ports within pods
-p, --profile=[]: Activate profiles by name
--render-only=false: Print rendered Kubernetes manifests instead of deploying them
--rpc-http-port=50052: tcp port to expose event REST API over HTTP
Expand Down Expand Up @@ -734,6 +735,7 @@ Env vars:
* `SKAFFOLD_NAMESPACE` (same as `--namespace`)
* `SKAFFOLD_NO_PRUNE` (same as `--no-prune`)
* `SKAFFOLD_NO_PRUNE_CHILDREN` (same as `--no-prune-children`)
* `SKAFFOLD_PORT_FORWARD` (same as `--port-forward`)
* `SKAFFOLD_PROFILE` (same as `--profile`)
* `SKAFFOLD_RENDER_ONLY` (same as `--render-only`)
* `SKAFFOLD_RPC_HTTP_PORT` (same as `--rpc-http-port`)
Expand Down
23 changes: 21 additions & 2 deletions integration/port_forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,29 @@ func TestPortForward(t *testing.T) {
portforward.WhiteBoxPortForwardCycle(t, kubectlCLI, ns.Name)
}

// TestPortForwardDeletePod tests that port forwarding works
func TestRunPortForward(t *testing.T) {
if testing.Short() || RunOnGCP() {
t.Skip("skipping kind integration test")
}

ns, _, deleteNs := SetupNamespace(t)
defer deleteNs()

rpcAddr := randomPort()
stop := skaffold.Run("--port-forward", "--rpc-port", rpcAddr, "--enable-rpc").InDir("examples/microservices").InNs(ns.Name).RunBackground(t)
defer stop()

_, entries, shutdown := apiEvents(t, rpcAddr)
defer shutdown()

address, localPort := getLocalPortFromPortForwardEvent(t, entries, "leeroy-app", "service", ns.Name)
assertResponseFromPort(t, address, localPort, constants.LeeroyAppResponse)
}

// TestDevPortForwardDeletePod tests that port forwarding works
// as expected. Then, the test force deletes a pod,
// and tests that the pod eventually comes up at the same port again.
func TestPortForwardDeletePod(t *testing.T) {
func TestDevPortForwardDeletePod(t *testing.T) {
if testing.Short() || RunOnGCP() {
t.Skip("skipping kind integration test")
}
Expand Down
33 changes: 23 additions & 10 deletions pkg/skaffold/runner/build_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func (r *SkaffoldRunner) BuildAndTest(ctx context.Context, out io.Writer, artifa

// DeployAndLog deploys a list of already built artifacts and optionally show the logs.
func (r *SkaffoldRunner) DeployAndLog(ctx context.Context, out io.Writer, artifacts []build.Artifact) error {
if !r.runCtx.Opts.Tail {
if !r.runCtx.Opts.Tail && !r.runCtx.Opts.PortForward.Enabled {
return r.Deploy(ctx, out, artifacts)
}

Expand All @@ -87,19 +88,31 @@ func (r *SkaffoldRunner) DeployAndLog(ctx context.Context, out io.Writer, artifa
r.podSelector.Add(artifact.Tag)
}

logger := r.newLoggerForImages(out, imageNames)
defer logger.Stop()

// Logs should be retrieve up to just before the deploy
logger.SetSince(time.Now())

if err := r.Deploy(ctx, out, artifacts); err != nil {
return err
}

// Start printing the logs after deploy is finished
if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
if r.runCtx.Opts.Tail {
logger := r.newLoggerForImages(out, imageNames)
defer logger.Stop()

// Logs should be retrieve up to just before the deploy
logger.SetSince(time.Now())

// Start printing the logs after deploy is finished
if err := logger.Start(ctx); err != nil {
return errors.Wrap(err, "starting logger")
}
}

if r.runCtx.Opts.PortForward.Enabled {
kubectlCLI := kubectl.NewFromRunContext(r.runCtx)
r.createForwarder(out, kubectlCLI)
defer r.forwarderManager.Stop()

if err := r.forwarderManager.Start(ctx); err != nil {
logrus.Warnln("Error starting port forwarding:", err)
}
}

<-ctx.Done()
Expand Down