Skip to content

Commit

Permalink
Add file sync to Event and State APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Multiply committed Oct 7, 2019
1 parent 8e42805 commit b4df5d9
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 75 deletions.
43 changes: 43 additions & 0 deletions pkg/skaffold/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func emptyStateWithArtifacts(builds map[string]string) proto.State {
Resources: map[string]string{},
},
ForwardedPorts: make(map[int32]*proto.PortEvent),
FileSyncState: &proto.FileSyncState{
Status: NotStarted,
},
}
}

Expand Down Expand Up @@ -239,6 +242,21 @@ func BuildComplete(imageName string) {
handler.handleBuildEvent(&proto.BuildEvent{Artifact: imageName, Status: Complete})
}

// FileSyncInProgress notifies that a file sync has been started.
func FileSyncInProgress(fileCount int, image string) {
handler.handleFileSyncEvent(&proto.FileSyncEvent{FileCount: int32(fileCount), Image: image, Status: InProgress})
}

// FileSyncFailed notifies that a file sync has failed.
func FileSyncFailed(fileCount int, image string, err error) {
handler.handleFileSyncEvent(&proto.FileSyncEvent{FileCount: int32(fileCount), Image: image, Status: Failed, Err: err.Error()})
}

// FileSyncSucceeded notifies that a file sync has succeeded.
func FileSyncSucceeded(fileCount int, image string) {
handler.handleFileSyncEvent(&proto.FileSyncEvent{FileCount: int32(fileCount), Image: image, Status: Succeeded})
}

// PortForwarded notifies that a remote port has been forwarded locally.
func PortForwarded(localPort, remotePort int32, podName, containerName, namespace string, portName string, resourceType, resourceName string) {
go handler.handle(&proto.Event{
Expand Down Expand Up @@ -295,6 +313,14 @@ func (ev *eventHandler) handleBuildEvent(e *proto.BuildEvent) {
})
}

func (ev *eventHandler) handleFileSyncEvent(e *proto.FileSyncEvent) {
go ev.handle(&proto.Event{
EventType: &proto.Event_FileSyncEvent{
FileSyncEvent: e,
},
})
}

func LogSkaffoldMetadata(info *version.Info) {
handler.logEvent(proto.LogEntry{
Timestamp: ptypes.TimestampNow(),
Expand Down Expand Up @@ -382,6 +408,23 @@ func (ev *eventHandler) handle(event *proto.Event) {
logEntry.Entry = fmt.Sprintf("Resource %s status failed with %s", rseName, rse.Err)
default:
}
case *proto.Event_FileSyncEvent:
fse := e.FileSyncEvent
fseFileCount := fse.FileCount
fseImage := fse.Image
ev.stateLock.Lock()
ev.state.FileSyncState.Status = fse.Status
ev.stateLock.Unlock()
switch fse.Status {
case InProgress:
logEntry.Entry = fmt.Sprintf("File sync started for %d files for %s", fseFileCount, fseImage)
case Succeeded:
logEntry.Entry = fmt.Sprintf("File sync succeeded for %d files for %s", fseFileCount, fseImage)
case Failed:
logEntry.Entry = fmt.Sprintf("File sync failed for %d files for %s", fseFileCount, fseImage)
// logEntry.Err = fse.Err
default:
}

default:
return
Expand Down
38 changes: 38 additions & 0 deletions pkg/skaffold/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,42 @@ func TestResourceStatusCheckEventFailed(t *testing.T) {
wait(t, func() bool { return handler.getState().StatusCheckState.Resources["ns:pod/foo"] == Failed })
}

func TestFileSyncInProgress(t *testing.T) {
defer func() { handler = &eventHandler{} }()

handler = &eventHandler{
state: emptyState(latest.BuildConfig{}),
}

wait(t, func() bool { return handler.getState().FileSyncState.Status == NotStarted })
FileSyncInProgress(5, "image")
wait(t, func() bool { return handler.getState().FileSyncState.Status == InProgress })
}

func TestFileSyncFailed(t *testing.T) {
defer func() { handler = &eventHandler{} }()

handler = &eventHandler{
state: emptyState(latest.BuildConfig{}),
}

wait(t, func() bool { return handler.getState().FileSyncState.Status == NotStarted })
FileSyncFailed(5, "image", errors.New("BUG"))
wait(t, func() bool { return handler.getState().FileSyncState.Status == Failed })
}

func TestFileSyncSucceeded(t *testing.T) {
defer func() { handler = &eventHandler{} }()

handler = &eventHandler{
state: emptyState(latest.BuildConfig{}),
}

wait(t, func() bool { return handler.getState().FileSyncState.Status == NotStarted })
FileSyncSucceeded(5, "image")
wait(t, func() bool { return handler.getState().FileSyncState.Status == Succeeded })
}

func wait(t *testing.T, condition func() bool) {
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
Expand Down Expand Up @@ -278,6 +314,7 @@ func TestResetStateOnBuild(t *testing.T) {
},
DeployState: &proto.DeployState{Status: Complete},
StatusCheckState: &proto.StatusCheckState{Status: Complete},
FileSyncState: &proto.FileSyncState{Status: Succeeded},
},
}
ResetStateOnBuild()
Expand All @@ -289,6 +326,7 @@ func TestResetStateOnBuild(t *testing.T) {
},
DeployState: &proto.DeployState{Status: NotStarted},
StatusCheckState: &proto.StatusCheckState{Status: NotStarted},
FileSyncState: &proto.FileSyncState{Status: NotStarted},
}
testutil.CheckDeepEqual(t, expected, handler.getState())
}
Loading

0 comments on commit b4df5d9

Please sign in to comment.