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 flag to save events to a file #5125

Merged
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
8 changes: 8 additions & 0 deletions cmd/skaffold/app/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ var flagRegistry = []Flag{
FlagAddMethod: "BoolVar",
DefinedOn: []string{"dev", "build", "run", "debug", "deploy"},
},
{
Name: "event-log-file",
Usage: "Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true",
Value: &opts.EventLogFile,
DefValue: "",
FlagAddMethod: "StringVar",
DefinedOn: []string{"dev", "build", "run", "debug", "deploy"},
},
{
Name: "rpc-port",
Usage: "tcp port to expose event API",
Expand Down
10 changes: 10 additions & 0 deletions docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Options:
--detect-minikube=false: Use heuristics to detect a minikube cluster
--dry-run=false: Don't build images, just compute the tag for each artifact.
--enable-rpc=false: Enable gRPC for exposing Skaffold events (true by default for `skaffold dev`)
--event-log-file='': Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true
--file-output='': Filename to write build images to
-f, --filename='skaffold.yaml': Path or URL to the Skaffold config file
--insecure-registry=[]: Target registries for built images which are not secure
Expand Down Expand Up @@ -171,6 +172,7 @@ Env vars:
* `SKAFFOLD_DETECT_MINIKUBE` (same as `--detect-minikube`)
* `SKAFFOLD_DRY_RUN` (same as `--dry-run`)
* `SKAFFOLD_ENABLE_RPC` (same as `--enable-rpc`)
* `SKAFFOLD_EVENT_LOG_FILE` (same as `--event-log-file`)
* `SKAFFOLD_FILE_OUTPUT` (same as `--file-output`)
* `SKAFFOLD_FILENAME` (same as `--filename`)
* `SKAFFOLD_INSECURE_REGISTRY` (same as `--insecure-registry`)
Expand Down Expand Up @@ -345,6 +347,7 @@ Options:
-d, --default-repo='': Default repository value (overrides global config)
--detect-minikube=false: Use heuristics to detect a minikube cluster
--enable-rpc=false: Enable gRPC for exposing Skaffold events (true by default for `skaffold dev`)
--event-log-file='': Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true
-f, --filename='skaffold.yaml': Path or URL to the Skaffold config file
--force=false: Recreate Kubernetes resources if necessary for deployment, warning: might cause downtime!
--insecure-registry=[]: Target registries for built images which are not secure
Expand Down Expand Up @@ -388,6 +391,7 @@ Env vars:
* `SKAFFOLD_DEFAULT_REPO` (same as `--default-repo`)
* `SKAFFOLD_DETECT_MINIKUBE` (same as `--detect-minikube`)
* `SKAFFOLD_ENABLE_RPC` (same as `--enable-rpc`)
* `SKAFFOLD_EVENT_LOG_FILE` (same as `--event-log-file`)
* `SKAFFOLD_FILENAME` (same as `--filename`)
* `SKAFFOLD_FORCE` (same as `--force`)
* `SKAFFOLD_INSECURE_REGISTRY` (same as `--insecure-registry`)
Expand Down Expand Up @@ -478,6 +482,7 @@ Options:
-d, --default-repo='': Default repository value (overrides global config)
--detect-minikube=false: Use heuristics to detect a minikube cluster
--enable-rpc=false: Enable gRPC for exposing Skaffold events (true by default for `skaffold dev`)
--event-log-file='': Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true
-f, --filename='skaffold.yaml': Path or URL to the Skaffold config file
--force=false: Recreate Kubernetes resources if necessary for deployment, warning: might cause downtime!
-i, --images=: A list of pre-built images to deploy
Expand Down Expand Up @@ -514,6 +519,7 @@ Env vars:
* `SKAFFOLD_DEFAULT_REPO` (same as `--default-repo`)
* `SKAFFOLD_DETECT_MINIKUBE` (same as `--detect-minikube`)
* `SKAFFOLD_ENABLE_RPC` (same as `--enable-rpc`)
* `SKAFFOLD_EVENT_LOG_FILE` (same as `--event-log-file`)
* `SKAFFOLD_FILENAME` (same as `--filename`)
* `SKAFFOLD_FORCE` (same as `--force`)
* `SKAFFOLD_IMAGES` (same as `--images`)
Expand Down Expand Up @@ -551,6 +557,7 @@ Options:
-d, --default-repo='': Default repository value (overrides global config)
--detect-minikube=false: Use heuristics to detect a minikube cluster
--enable-rpc=false: Enable gRPC for exposing Skaffold events (true by default for `skaffold dev`)
--event-log-file='': Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true
-f, --filename='skaffold.yaml': Path or URL to the Skaffold config file
--force=false: Recreate Kubernetes resources if necessary for deployment, warning: might cause downtime!
--insecure-registry=[]: Target registries for built images which are not secure
Expand Down Expand Up @@ -595,6 +602,7 @@ Env vars:
* `SKAFFOLD_DEFAULT_REPO` (same as `--default-repo`)
* `SKAFFOLD_DETECT_MINIKUBE` (same as `--detect-minikube`)
* `SKAFFOLD_ENABLE_RPC` (same as `--enable-rpc`)
* `SKAFFOLD_EVENT_LOG_FILE` (same as `--event-log-file`)
* `SKAFFOLD_FILENAME` (same as `--filename`)
* `SKAFFOLD_FORCE` (same as `--force`)
* `SKAFFOLD_INSECURE_REGISTRY` (same as `--insecure-registry`)
Expand Down Expand Up @@ -812,6 +820,7 @@ Options:
-d, --default-repo='': Default repository value (overrides global config)
--detect-minikube=false: Use heuristics to detect a minikube cluster
--enable-rpc=false: Enable gRPC for exposing Skaffold events (true by default for `skaffold dev`)
--event-log-file='': Save Skaffold events to the provided file after skaffold has finished executing, requires --enable-rpc=true
-f, --filename='skaffold.yaml': Path or URL to the Skaffold config file
--force=false: Recreate Kubernetes resources if necessary for deployment, warning: might cause downtime!
--insecure-registry=[]: Target registries for built images which are not secure
Expand Down Expand Up @@ -855,6 +864,7 @@ Env vars:
* `SKAFFOLD_DEFAULT_REPO` (same as `--default-repo`)
* `SKAFFOLD_DETECT_MINIKUBE` (same as `--detect-minikube`)
* `SKAFFOLD_ENABLE_RPC` (same as `--enable-rpc`)
* `SKAFFOLD_EVENT_LOG_FILE` (same as `--event-log-file`)
* `SKAFFOLD_FILENAME` (same as `--filename`)
* `SKAFFOLD_FORCE` (same as `--force`)
* `SKAFFOLD_INSECURE_REGISTRY` (same as `--insecure-registry`)
Expand Down
1 change: 1 addition & 0 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type WaitForDeletions struct {
type SkaffoldOptions struct {
ConfigurationFile string
GlobalConfig string
EventLogFile string
Cleanup bool
Notification bool
Tail bool
Expand Down
26 changes: 26 additions & 0 deletions pkg/skaffold/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ limitations under the License.
package event

import (
"bytes"
"encoding/json"
"fmt"
"os"
"sync"

//nolint:golint,staticcheck
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"

Expand Down Expand Up @@ -734,3 +738,25 @@ func InititializationFailed(err error) {
},
})
}

// SaveEventsToFile saves the current event log to the filepath provided
func SaveEventsToFile(fp string) error {
handler.logLock.Lock()
f, err := os.OpenFile(fp, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("opening %s: %w", fp, err)
}
defer f.Close()
marshaller := jsonpb.Marshaler{}
for _, ev := range handler.eventLog {
contents := bytes.NewBuffer([]byte{})
if err := marshaller.Marshal(contents, &ev); err != nil {
return fmt.Errorf("marshalling event: %w", err)
}
if _, err := f.WriteString(contents.String() + "\n"); err != nil {
return fmt.Errorf("writing string: %w", err)
}
}
handler.logLock.Unlock()
return nil
}
69 changes: 69 additions & 0 deletions pkg/skaffold/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ package event
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"sync/atomic"
"testing"
"time"

//nolint:golint,staticcheck
"github.com/golang/protobuf/jsonpb"
"github.com/google/go-cmp/cmp/cmpopts"

sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
Expand Down Expand Up @@ -529,3 +534,67 @@ func TestDevLoopFailedInPhase(t *testing.T) {
})
}
}

func TestSaveEventsToFile(t *testing.T) {
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatalf("getting temp file: %v", err)
}
t.Cleanup(func() { os.Remove(f.Name()) })
if err := f.Close(); err != nil {
t.Fatalf("error closing tmp file: %v", err)
}

// add some events to the event log
handler.eventLog = []proto.LogEntry{
{
Event: &proto.Event{EventType: &proto.Event_BuildEvent{}},
}, {
Event: &proto.Event{EventType: &proto.Event_DevLoopEvent{}},
},
}

// save events to file
if err := SaveEventsToFile(f.Name()); err != nil {
t.Fatalf("error saving events to file: %v", err)
}

// ensure that the events in the file match the event log
contents, err := ioutil.ReadFile(f.Name())
if err != nil {
t.Fatalf("reading tmp file: %v", err)
}

var logEntries []proto.LogEntry
entries := strings.Split(string(contents), "\n")
for _, e := range entries {
if e == "" {
continue
}
var logEntry proto.LogEntry
if err := jsonpb.UnmarshalString(e, &logEntry); err != nil {
t.Errorf("error converting http response %s to proto: %s", e, err.Error())
}
logEntries = append(logEntries, logEntry)
}

buildCompleteEvent, devLoopCompleteEvent := 0, 0
for _, entry := range logEntries {
t.Log(entry.Event.GetEventType())
switch entry.Event.GetEventType().(type) {
case *proto.Event_BuildEvent:
buildCompleteEvent++
t.Logf("build event %d: %v", buildCompleteEvent, entry.Event)
case *proto.Event_DevLoopEvent:
devLoopCompleteEvent++
t.Logf("dev loop event %d: %v", devLoopCompleteEvent, entry.Event)
default:
t.Logf("unknown event: %v", entry.Event)
}
}

// make sure we have exactly 1 build entry and 1 dev loop complete entry
testutil.CheckDeepEqual(t, 2, len(logEntries))
testutil.CheckDeepEqual(t, 1, buildCompleteEvent)
testutil.CheckDeepEqual(t, 1, devLoopCompleteEvent)
}
7 changes: 7 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 (
"google.golang.org/grpc/status"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/proto"
)
Expand Down Expand Up @@ -115,6 +116,12 @@ func Initialize(opts config.SkaffoldOptions) (func() error, error) {
if httpErr != nil {
errStr += fmt.Sprintf("http callback error: %s\n", httpErr.Error())
}
if opts.EventLogFile != "" {
logFileErr := event.SaveEventsToFile(opts.EventLogFile)
if logFileErr != nil {
errStr += fmt.Sprintf("event log file error: %s\n", logFileErr.Error())
}
}
return errors.New(errStr)
}
if err != nil {
Expand Down