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 option to wait for connection to /v2/events on gRPC/HTTP server #6545

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ func isQuietMode() bool {
}

func apiServerShutdownHook(err error) error {
// clean up server at end of the execution since cobra post run hooks
// Clean up server at end of the execution since cobra post run hooks
// are only executed if RunE is successful.
// Also sends out error message on event stream before shutting down server.
if shutdownAPIServer != nil {
event.SendErrorMessageOnce(constants.DevLoop, constants.SubtaskIDNone, err)
shutdownAPIServer()
}
return err
Expand Down
9 changes: 9 additions & 0 deletions cmd/skaffold/app/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ var flagRegistry = []Flag{
DefinedOn: []string{"dev", "build", "run", "debug", "deploy", "render", "apply", "test"},
IsEnum: true,
},
{
Name: "wait-for-connection",
Usage: "Blocks execution until the /v2/events gRPC/HTTP endpoint is hit",
Value: &opts.WaitForConnection,
DefValue: false,
FlagAddMethod: "BoolVar",
DefinedOn: []string{"dev", "build", "run", "debug", "deploy", "render", "apply", "test"},
IsEnum: true,
},
{
Name: "event-log-file",
Usage: "Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true",
Expand Down
3 changes: 0 additions & 3 deletions cmd/skaffold/skaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"cloud.google.com/go/profiler"

"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
Expand Down Expand Up @@ -57,7 +55,6 @@ func main() {
// for the entire skaffold run here. It's possible SetupColors() was never called, so call it again
// before we print an error to get the right coloring.
errOut := output.SetupColors(context.Background(), os.Stderr, output.DefaultColorCode, false)
eventV2.SendErrorMessageOnce(constants.DevLoop, constants.SubtaskIDNone, err)
output.Red.Fprintln(errOut, err)
code = app.ExitCode(err)
}
Expand Down
4 changes: 4 additions & 0 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ Options:
--tail=true: Stream logs from deployed objects
--toot=false: Emit a terminal beep after the deploy is complete
--trigger='notify': How is change detection triggered? (polling, notify, or manual)
--wait-for-connection=false: Blocks execution until the /v2/events gRPC/HTTP endpoint is hit
--wait-for-deletions=true: Wait for pending deletions to complete before a deployment
--wait-for-deletions-delay=2s: Delay between two checks for pending deletions
--wait-for-deletions-max=1m0s: Max duration to wait for pending deletions
Expand Down Expand Up @@ -481,6 +482,7 @@ Env vars:
* `SKAFFOLD_TAIL` (same as `--tail`)
* `SKAFFOLD_TOOT` (same as `--toot`)
* `SKAFFOLD_TRIGGER` (same as `--trigger`)
* `SKAFFOLD_WAIT_FOR_CONNECTION` (same as `--wait-for-connection`)
* `SKAFFOLD_WAIT_FOR_DELETIONS` (same as `--wait-for-deletions`)
* `SKAFFOLD_WAIT_FOR_DELETIONS_DELAY` (same as `--wait-for-deletions-delay`)
* `SKAFFOLD_WAIT_FOR_DELETIONS_MAX` (same as `--wait-for-deletions-max`)
Expand Down Expand Up @@ -678,6 +680,7 @@ Options:
--tail=true: Stream logs from deployed objects
--toot=false: Emit a terminal beep after the deploy is complete
--trigger='notify': How is change detection triggered? (polling, notify, or manual)
--wait-for-connection=false: Blocks execution until the /v2/events gRPC/HTTP endpoint is hit
--wait-for-deletions=true: Wait for pending deletions to complete before a deployment
--wait-for-deletions-delay=2s: Delay between two checks for pending deletions
--wait-for-deletions-max=1m0s: Max duration to wait for pending deletions
Expand Down Expand Up @@ -733,6 +736,7 @@ Env vars:
* `SKAFFOLD_TAIL` (same as `--tail`)
* `SKAFFOLD_TOOT` (same as `--toot`)
* `SKAFFOLD_TRIGGER` (same as `--trigger`)
* `SKAFFOLD_WAIT_FOR_CONNECTION` (same as `--wait-for-connection`)
* `SKAFFOLD_WAIT_FOR_DELETIONS` (same as `--wait-for-deletions`)
* `SKAFFOLD_WAIT_FOR_DELETIONS_DELAY` (same as `--wait-for-deletions-delay`)
* `SKAFFOLD_WAIT_FOR_DELETIONS_MAX` (same as `--wait-for-deletions-max`)
Expand Down
11 changes: 6 additions & 5 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ type SkaffoldOptions struct {
// TODO(https://github.com/GoogleContainerTools/skaffold/issues/3668):
// remove minikubeProfile from here and instead detect it by matching the
// kubecontext API Server to minikube profiles
MinikubeProfile string
RepoCacheDir string
SyncRemoteCache SyncRemoteCacheOption
WaitForDeletions WaitForDeletions
ForceLoadImages bool
MinikubeProfile string
RepoCacheDir string
SyncRemoteCache SyncRemoteCacheOption
WaitForDeletions WaitForDeletions
ForceLoadImages bool
WaitForConnection bool
}

type RunMode string
Expand Down
10 changes: 10 additions & 0 deletions pkg/skaffold/event/v2/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var handler = newHandler()
func newHandler() *eventHandler {
h := &eventHandler{
eventChan: make(chan *proto.Event),
wait: make(chan bool, 1),
}
go func() {
for {
Expand All @@ -73,6 +74,7 @@ type eventHandler struct {

iteration int
errorOnce sync.Once
wait chan bool
state proto.State
stateLock sync.Mutex
eventChan chan *proto.Event
Expand Down Expand Up @@ -110,6 +112,11 @@ func Handle(event *proto.Event) error {
return nil
}

// WaitForConnection will block execution until the server receives a connection
func WaitForConnection() {
<-handler.wait
}

func (ev *eventHandler) getState() proto.State {
ev.stateLock.Lock()
// Deep copy
Expand Down Expand Up @@ -173,6 +180,9 @@ func (ev *eventHandler) forEach(listeners *[]*listener, log *[]proto.Event, lock
}

func (ev *eventHandler) forEachEvent(callback func(*proto.Event) error) error {
if len(handler.wait) != cap(handler.wait) {
MarlonGamez marked this conversation as resolved.
Show resolved Hide resolved
handler.wait <- true
}
return ev.forEach(&ev.eventListeners, &ev.eventLog, &ev.logLock, callback)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/skaffold/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
v2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/server/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand Down Expand Up @@ -130,6 +131,11 @@ func Initialize(opts config.SkaffoldOptions) (func() error, error) {
return callback, fmt.Errorf("starting HTTP server: %w", err)
}

// Optionally pause execution until endpoint hit
if opts.WaitForConnection {
eventV2.WaitForConnection()
}

return callback, nil
}

Expand Down