-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Webhook to release-daemon for flux2-notification alerts (#365)
* lunarway/http * http endpoint /flux2-alerts for flux2-notification controller * tidy * zlog -> internal/log * rm zlog * log from the get go * http server started in RunE func * Update cmd/daemon/flux-notification-controller/server.go Co-authored-by: Bjørn <bso@lunar.app> * Update cmd/daemon/flux-notification-controller/api.go Co-authored-by: Bjørn <bso@lunar.app> * renmae flux-notification-controller -> flux2notifications * test of webhook * rename * small refactor * statuscode Co-authored-by: Bjørn <bso@lunar.app>
- Loading branch information
1 parent
819dfbd
commit dfb3e4a
Showing
6 changed files
with
122 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package flux2notifications | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/lunarway/release-manager/internal/log" | ||
) | ||
|
||
type Event struct { | ||
InvolvedObject struct { | ||
Kind string `json:"kind"` | ||
Namespace string `json:"namespace"` | ||
Name string `json:"name"` | ||
UID string `json:"uid"` | ||
APIVersion string `json:"apiVersion"` | ||
ResourceVersion string `json:"resourceVersion"` | ||
} `json:"involvedObject"` | ||
Severity string `json:"severity"` | ||
Timestamp time.Time `json:"timestamp"` | ||
Message string `json:"message"` | ||
Reason string `json:"reason"` | ||
ReportingController string `json:"reportingController"` | ||
ReportingInstance string `json:"reportingInstance"` | ||
} | ||
|
||
func HandleEventFromFlux2(w http.ResponseWriter, r *http.Request) { | ||
defer r.Body.Close() | ||
body, err := ioutil.ReadAll(r.Body) //TODO: log something | ||
if err != nil { | ||
log.Errorf("Failed to unmarshal alert from flux2-notification-controller: %v", err) | ||
http.Error(w, "unknown error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
var event Event | ||
err = json.Unmarshal(body, &event) | ||
if err != nil { | ||
log.Errorf("Failed to unmarshal alert from flux2-notification-controller: %v", err) | ||
http.Error(w, "unknown error", http.StatusInternalServerError) | ||
return | ||
} | ||
log.Infof("Received alert from flux2-notification-controller: %s with msg: %s", event.InvolvedObject.Name, event.Message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package flux2notifications | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/lunarway/release-manager/internal/log" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// TestWebhookForAlerts tests if /flux2-alerts can handle requests from flux2-notification-controller | ||
func TestWebhookForAlerts(t *testing.T) { | ||
//arrange | ||
setupTest() | ||
var ( | ||
eventAsJson = "{\"involvedObject\": {\"kind\":\"GitRepository\",\"namespace\":\"flux-system\",\"name\":\"flux-system\",\"uid\":\"cc4d0095-83f4-4f08-98f2-d2e9f3731fb9\",\"apiVersion\":\"source.toolkit.fluxcd.io/v1beta2\", \"resourceVersion\":\"56921\"},\"severity\":\"info\",\"timestamp\":\"2006-01-02T15:04:05Z\",\"message\":\"Fetched revision: main/731f7eaddfb6af01cb2173e18f0f75b0ba780ef1\",\"reason\":\"info\",\"reportingController\":\"source-controller\",\"reportingInstance\":\"source-controller-7c7b47f5f-8bhrp\"}" | ||
request = httptest.NewRequest(http.MethodPost, "/webhook/flux2-alerts", strings.NewReader(eventAsJson)) | ||
w = httptest.NewRecorder() | ||
) | ||
|
||
//act | ||
HandleEventFromFlux2(w, request) | ||
response := w.Result() | ||
defer response.Body.Close() | ||
_, err := ioutil.ReadAll(response.Body) | ||
|
||
//assert | ||
assert.NoError(t, err, "HandleEventFromFlux2 could not handle request") | ||
assert.Equal(t, http.StatusOK, response.StatusCode) | ||
} | ||
|
||
func setupTest() { | ||
log.Init(&log.Configuration{ | ||
Level: log.Level{ | ||
Level: zapcore.DebugLevel, | ||
}, | ||
Development: true, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package flux2notifications | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/lunarway/release-manager/internal/log" | ||
) | ||
|
||
func StartHttpServer() { | ||
router := mux.NewRouter() | ||
router.HandleFunc("/webhook/flux2-alerts", HandleEventFromFlux2).Methods(http.MethodPost) | ||
err := http.ListenAndServe(":3001", router) | ||
if err != nil { | ||
log.Errorf("Failed to start daemon's HTTP server: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters