Skip to content

Commit 85db2aa

Browse files
authored
Merge pull request #3263 from oke-py/run-port-forward
Add --port-forward to skaffold run
2 parents 31589dc + 647b3c3 commit 85db2aa

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

cmd/skaffold/app/cmd/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ var FlagRegistry = []Flag{
209209
Value: &opts.PortForward.Enabled,
210210
DefValue: false,
211211
FlagAddMethod: "BoolVar",
212-
DefinedOn: []string{"dev", "debug"},
212+
DefinedOn: []string{"dev", "debug", "run"},
213213
},
214214
{
215215
Name: "status-check",

docs/content/en/docs/references/cli/_index.md

+2
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Options:
701701
-n, --namespace='': Run deployments in the specified namespace
702702
--no-prune=false: Skip removing images and containers built by Skaffold
703703
--no-prune-children=false: Skip removing layers reused by Skaffold
704+
--port-forward=false: Port-forward exposed container ports within pods
704705
-p, --profile=[]: Activate profiles by name
705706
--render-only=false: Print rendered Kubernetes manifests instead of deploying them
706707
--rpc-http-port=50052: tcp port to expose event REST API over HTTP
@@ -734,6 +735,7 @@ Env vars:
734735
* `SKAFFOLD_NAMESPACE` (same as `--namespace`)
735736
* `SKAFFOLD_NO_PRUNE` (same as `--no-prune`)
736737
* `SKAFFOLD_NO_PRUNE_CHILDREN` (same as `--no-prune-children`)
738+
* `SKAFFOLD_PORT_FORWARD` (same as `--port-forward`)
737739
* `SKAFFOLD_PROFILE` (same as `--profile`)
738740
* `SKAFFOLD_RENDER_ONLY` (same as `--render-only`)
739741
* `SKAFFOLD_RPC_HTTP_PORT` (same as `--rpc-http-port`)

integration/port_forward_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,29 @@ func TestPortForward(t *testing.T) {
5555
portforward.WhiteBoxPortForwardCycle(t, kubectlCLI, ns.Name)
5656
}
5757

58-
// TestPortForwardDeletePod tests that port forwarding works
58+
func TestRunPortForward(t *testing.T) {
59+
if testing.Short() || RunOnGCP() {
60+
t.Skip("skipping kind integration test")
61+
}
62+
63+
ns, _, deleteNs := SetupNamespace(t)
64+
defer deleteNs()
65+
66+
rpcAddr := randomPort()
67+
stop := skaffold.Run("--port-forward", "--rpc-port", rpcAddr, "--enable-rpc").InDir("examples/microservices").InNs(ns.Name).RunBackground(t)
68+
defer stop()
69+
70+
_, entries, shutdown := apiEvents(t, rpcAddr)
71+
defer shutdown()
72+
73+
address, localPort := getLocalPortFromPortForwardEvent(t, entries, "leeroy-app", "service", ns.Name)
74+
assertResponseFromPort(t, address, localPort, constants.LeeroyAppResponse)
75+
}
76+
77+
// TestDevPortForwardDeletePod tests that port forwarding works
5978
// as expected. Then, the test force deletes a pod,
6079
// and tests that the pod eventually comes up at the same port again.
61-
func TestPortForwardDeletePod(t *testing.T) {
80+
func TestDevPortForwardDeletePod(t *testing.T) {
6281
if testing.Short() || RunOnGCP() {
6382
t.Skip("skipping kind integration test")
6483
}

pkg/skaffold/runner/build_deploy.go

+23-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
3131
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
3232
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
33+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
3334
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
3435
)
3536

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

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

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

90-
logger := r.newLoggerForImages(out, imageNames)
91-
defer logger.Stop()
92-
93-
// Logs should be retrieve up to just before the deploy
94-
logger.SetSince(time.Now())
95-
9691
if err := r.Deploy(ctx, out, artifacts); err != nil {
9792
return err
9893
}
9994

100-
// Start printing the logs after deploy is finished
101-
if err := logger.Start(ctx); err != nil {
102-
return errors.Wrap(err, "starting logger")
95+
if r.runCtx.Opts.Tail {
96+
logger := r.newLoggerForImages(out, imageNames)
97+
defer logger.Stop()
98+
99+
// Logs should be retrieve up to just before the deploy
100+
logger.SetSince(time.Now())
101+
102+
// Start printing the logs after deploy is finished
103+
if err := logger.Start(ctx); err != nil {
104+
return errors.Wrap(err, "starting logger")
105+
}
106+
}
107+
108+
if r.runCtx.Opts.PortForward.Enabled {
109+
kubectlCLI := kubectl.NewFromRunContext(r.runCtx)
110+
r.createForwarder(out, kubectlCLI)
111+
defer r.forwarderManager.Stop()
112+
113+
if err := r.forwarderManager.Start(ctx); err != nil {
114+
logrus.Warnln("Error starting port forwarding:", err)
115+
}
103116
}
104117

105118
<-ctx.Done()

0 commit comments

Comments
 (0)