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 file sync to Event and State APIs #2978

Merged
merged 1 commit into from
Oct 9, 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
42 changes: 42 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,22 @@ 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)
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 @@ -285,6 +321,7 @@ func TestResetStateOnBuild(t *testing.T) {
},
},
StatusCheckState: &proto.StatusCheckState{Status: Complete},
FileSyncState: &proto.FileSyncState{Status: Succeeded},
},
}
ResetStateOnBuild()
Expand All @@ -296,6 +333,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())
}
Expand Down
Loading