Skip to content

Commit

Permalink
Add termination boolean to port forward event
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Jul 31, 2019
1 parent 1df9667 commit 6229426
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 61 deletions.
3 changes: 2 additions & 1 deletion pkg/skaffold/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func BuildComplete(imageName string) {
}

// PortForwarded notifies that a remote port has been forwarded locally.
func PortForwarded(localPort, remotePort int32, podName, containerName, namespace string, portName string, resourceType, resourceName string) {
func PortForwarded(localPort, remotePort int32, podName, containerName, namespace string, portName string, resourceType, resourceName string, terminated bool) {
go handler.handle(&proto.Event{
EventType: &proto.Event_PortEvent{
PortEvent: &proto.PortEvent{
Expand All @@ -187,6 +187,7 @@ func PortForwarded(localPort, remotePort int32, podName, containerName, namespac
PortName: portName,
ResourceType: resourceType,
ResourceName: resourceName,
Terminated: terminated,
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestPortForwarded(t *testing.T) {
}

wait(t, func() bool { return handler.getState().ForwardedPorts["container"] == nil })
PortForwarded(8080, 8888, "pod", "container", "ns", "portname", "resourceType", "resourceName")
PortForwarded(8080, 8888, "pod", "container", "ns", "portname", "resourceType", "resourceName", false)
wait(t, func() bool { return handler.getState().ForwardedPorts["container"] != nil })
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/skaffold/kubernetes/portforward/entry_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
var (
// For testing
forwardingTimeoutTime = time.Minute
portForwardEvent = func(entry *portForwardEntry) {
portForwardEvent = func(entry *portForwardEntry, terminated bool) {
// TODO priyawadhwa@, change event API to accept ports of type int
event.PortForwarded(
int32(entry.localPort),
Expand All @@ -41,7 +41,8 @@ var (
entry.resource.Namespace,
entry.portName,
string(entry.resource.Type),
entry.resource.Name)
entry.resource.Name,
terminated)
}
)

Expand Down Expand Up @@ -182,7 +183,7 @@ func (b *EntryManager) forwardPortForwardEntry(ctx context.Context, entry *portF

color.Default.Fprintln(b.output, fmt.Sprintf("Port forwarded %s/%s from remote port %d to local port %d", entry.resource.Type, entry.resource.Name, entry.resource.Port, entry.localPort))

portForwardEvent(entry)
portForwardEvent(entry, false)
return nil
}

Expand All @@ -198,6 +199,7 @@ func (b *EntryManager) Terminate(p *portForwardEntry) {
b.forwardedResources.Delete(p.key())
b.forwardedPorts.Delete(p.localPort)
b.EntryForwarder.Terminate(p)
portForwardEvent(p, true)
}

func (b *EntryManager) Retry(ctx context.Context, p *portForwardEntry) error {
Expand Down
4 changes: 4 additions & 0 deletions pkg/skaffold/kubernetes/portforward/entry_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func TestStop(t *testing.T) {
localPort: 9001,
}

portForwardEventHandler := portForwardEvent
defer func() { portForwardEvent = portForwardEventHandler }()
portForwardEvent = func(entry *portForwardEntry, terminated bool) {}

em := NewEntryManager(ioutil.Discard)

em.forwardedResources = newForwardedResources()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func WhiteBoxPortForwardCycle(namespace string, t *testing.T) {
em := NewEntryManager(os.Stdout)
portForwardEventHandler := portForwardEvent
defer func() { portForwardEvent = portForwardEventHandler }()
portForwardEvent = func(entry *portForwardEntry) {}
portForwardEvent = func(entry *portForwardEntry, terminated bool) {}
ctx := context.Background()
localPort := retrieveAvailablePort(9000, em.forwardedPorts)
pfe := &portForwardEntry{
Expand Down
119 changes: 64 additions & 55 deletions proto/skaffold.pb.go

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

1 change: 1 addition & 0 deletions proto/skaffold.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ message PortEvent {
string portName = 6;
string resourceType=7;
string resourceName=8;
bool terminated=9;
}

message LogEntry {
Expand Down

0 comments on commit 6229426

Please sign in to comment.