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 the ONLY_SEND_ERRORS option, to only forward Flux errors. #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Set the following environment variables in your chosen deployment:
* `WEBHOOK_URL`: if the exporter is "webhook", then the URL to use for the webhook.
* `EXPORTER_TYPE` (optional): The types of exporter to use in comma delimited form. (Ex: `slack,webhook`) (Choices: slack, msteams, webhook, Default: slack)
* `JAEGER_ENDPOINT` (optional): endpoint to report Jaeger traces to.
* `ONLY_SEND_ERRORS` (optional): if set to "1", only events indicating an error will be sent.

And then apply the configuration:

Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func HandleV6(config APIConfig) (err error) {
return
}

if config.Config.Optional("ONLY_SEND_ERRORS", "0") == "1" && !utils.IsErrorEvent(event) {
w.WriteHeader(200)
return
}

var sendError bool
for _, exporter := range config.Exporter {
message := config.Formatter.FormatEvent(event, exporter)
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/v6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestHandleV6(t *testing.T) {
Server: http.NewServeMux(),
Exporter: []exporters.Exporter{fakeExporter},
Formatter: formatter,
Config: config,
}

HandleV6(apiConfig)
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/test/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test_utils

import (
"github.com/justinbarrick/fluxcloud/pkg/utils"
"github.com/stretchr/testify/assert"
fluxevent "github.com/weaveworks/flux/event"
"testing"
Expand Down Expand Up @@ -55,3 +56,11 @@ func TestParseFluxEventSyncError(t *testing.T) {
assert.Equal(t, true, metadata.Includes["other"])
assert.Equal(t, "running kubectl: The PersistentVolumeClaim \"test\" is invalid: spec: Forbidden: field is immutable after creation", metadata.Errors[0].Error)
}

func TestIsErrorEvent(t *testing.T) {
assert.Equal(t, utils.IsErrorEvent(NewFluxSyncEvent()), false)
assert.Equal(t, utils.IsErrorEvent(NewFluxSyncErrorEvent()), true)
assert.Equal(t, utils.IsErrorEvent(NewFluxCommitEvent()), false)
assert.Equal(t, utils.IsErrorEvent(NewFluxAutoReleaseEvent()), false)
assert.Equal(t, utils.IsErrorEvent(NewFluxUpdatePolicyEvent()), false)
}
19 changes: 19 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@ func ParseFluxEvent(reader io.Reader) (event fluxevent.Event, err error) {
err = json.NewDecoder(reader).Decode(&event)
return
}

func IsErrorEvent(event fluxevent.Event) bool {
switch event.Type {
case fluxevent.EventRelease:
metadata := event.Metadata.(*fluxevent.ReleaseEventMetadata)
return len(metadata.Result.Error()) > 0
case fluxevent.EventAutoRelease:
metadata := event.Metadata.(*fluxevent.AutoReleaseEventMetadata)
return len(metadata.Result.Error()) > 0
case fluxevent.EventCommit:
metadata := event.Metadata.(*fluxevent.CommitEventMetadata)
return len(metadata.Result.Error()) > 0
case fluxevent.EventSync:
metadata := event.Metadata.(*fluxevent.SyncEventMetadata)
return len(metadata.Errors) > 0
default:
return false
}
}