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

Address miscellaneous issues throughout code base #627

Merged
merged 6 commits into from
Oct 11, 2023
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/whilp/git-urls v1.0.0
github.com/xanzy/go-gitlab v0.90.0
golang.org/x/oauth2 v0.11.0
golang.org/x/text v0.12.0
google.golang.org/api v0.138.0
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
Expand Down Expand Up @@ -140,7 +141,6 @@ require (
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func (r *AlertReconciler) requestsForProviderChange(ctx context.Context, o clien
}

var reqs []reconcile.Request
for _, i := range list.Items {
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
for i := range list.Items {
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&list.Items[i])})
}

return reqs
Expand Down
6 changes: 4 additions & 2 deletions internal/notifier/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"crypto/x509"
"fmt"
"net/url"
"strings"

"golang.org/x/text/cases"
"golang.org/x/text/language"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
)
Expand Down Expand Up @@ -70,7 +72,7 @@ func (s *Alertmanager) Post(ctx context.Context, event eventv1.Event) error {
if event.Metadata != nil {
labels = event.Metadata
}
labels["alertname"] = "Flux" + event.InvolvedObject.Kind + strings.Title(event.Reason)
labels["alertname"] = "Flux" + event.InvolvedObject.Kind + cases.Title(language.Und).String(event.Reason)
labels["severity"] = event.Severity
labels["reason"] = event.Reason
labels["timestamp"] = event.Timestamp.String()
Expand Down
6 changes: 3 additions & 3 deletions internal/notifier/azure_eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ func (e *AzureEventHub) Post(ctx context.Context, event eventv1.Event) error {

eventBytes, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("Unable to marshall event: %w", err)
return fmt.Errorf("unable to marshall event: %w", err)
}

err = e.Hub.Send(ctx, eventhub.NewEvent(eventBytes))
if err != nil {
return fmt.Errorf("Failed to send msg: %w", err)
return fmt.Errorf("failed to send msg: %w", err)
}

err = e.Hub.Close(ctx)
if err != nil {
return fmt.Errorf("Unable to close connection: %w", err)
return fmt.Errorf("unable to close connection: %w", err)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/notifier/github_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (g *GitHubDispatch) Post(ctx context.Context, event eventv1.Event) error {

eventData, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("Failed to marshal object into json: %w", err)
return fmt.Errorf("failed to marshal object into json: %w", err)
}
eventDataRaw := json.RawMessage(eventData)

Expand All @@ -110,7 +110,7 @@ func (g *GitHubDispatch) Post(ctx context.Context, event eventv1.Event) error {
_, _, err = g.Client.Repositories.Dispatch(ctx, g.Owner, g.Repo, opts)

if err != nil {
return fmt.Errorf("Could not send github repository dispatch webhook: %v", err)
return fmt.Errorf("could not send github repository dispatch webhook: %v", err)
}

return nil
Expand Down
2 changes: 0 additions & 2 deletions internal/notifier/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ func (s *Sentry) Post(ctx context.Context, event eventv1.Event) error {
case eventv1.EventSeverityInfo:
// Info is sent as a trace
sev = eventToSpan(event)
break
case eventv1.EventSeverityError:
// Errors are sent as normal events
sev = toSentryEvent(event)
break
}
s.Client.CaptureEvent(sev, nil, nil)

Expand Down
10 changes: 5 additions & 5 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
// find matching alerts
alerts := make([]apiv1beta2.Alert, 0)
each_alert:
for _, alert := range allAlerts.Items {
alertLogger := eventLogger.WithValues("alert", client.ObjectKeyFromObject(&alert))
for i, alert := range allAlerts.Items {
alertLogger := eventLogger.WithValues("alert", client.ObjectKeyFromObject(&allAlerts.Items[i]))
ctx := log.IntoContext(ctx, alertLogger)

// skip suspended and not ready alerts
isReady := conditions.IsReady(&alert)
isReady := conditions.IsReady(&allAlerts.Items[i])
if alert.Spec.Suspend || !isReady {
continue each_alert
}
Expand Down Expand Up @@ -123,8 +123,8 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
eventLogger.Info(fmt.Sprintf("Dispatching event: %s", event.Message))

// dispatch notifications
for _, alert := range alerts {
alertLogger := eventLogger.WithValues("alert", client.ObjectKeyFromObject(&alert))
for i, alert := range alerts {
alertLogger := eventLogger.WithValues("alert", client.ObjectKeyFromObject(&alerts[i]))
ctx := log.IntoContext(ctx, alertLogger)

// verify if event comes from a different namespace
Expand Down
6 changes: 5 additions & 1 deletion internal/server/event_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func (s *EventServer) eventMiddleware(h http.Handler) http.Handler {
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body.Close()
if err := r.Body.Close(); err != nil {
s.logger.Error(err, "closing the request body failed")
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body = io.NopCloser(bytes.NewBuffer(body))

event := &eventv1.Event{}
Expand Down
2 changes: 1 addition & 1 deletion internal/server/receiver_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func Test_handlePayload(t *testing.T) {
g.Expect(rr.Result().StatusCode).To(gomega.Equal(tt.expectedResponseCode))

var allReceivers apiv1.ReceiverList
err = client.List(context.TODO(), &allReceivers)
g.Expect(client.List(context.TODO(), &allReceivers)).To(gomega.Succeed())

var annotatedResources int
for _, obj := range allReceivers.Items {
Expand Down
14 changes: 7 additions & 7 deletions internal/server/receiver_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
if len(receiver.Spec.Events) > 0 {
allowed := false
for _, e := range receiver.Spec.Events {
if strings.ToLower(event) == strings.ToLower(e) {
if strings.EqualFold(event, e) {
allowed = true
break
}
Expand All @@ -185,7 +185,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
if len(receiver.Spec.Events) > 0 {
allowed := false
for _, e := range receiver.Spec.Events {
if strings.ToLower(event) == strings.ToLower(e) {
if strings.EqualFold(event, e) {
allowed = true
break
}
Expand All @@ -207,7 +207,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
if len(receiver.Spec.Events) > 0 {
allowed := false
for _, e := range receiver.Spec.Events {
if strings.ToLower(event) == strings.ToLower(e) {
if strings.EqualFold(event, e) {
allowed = true
break
}
Expand Down Expand Up @@ -408,8 +408,8 @@ func (s *ReceiverServer) requestReconciliation(ctx context.Context, logger logr.
return nil
}

for _, resource := range resources.Items {
if err := s.annotate(ctx, &resource); err != nil {
for i, resource := range resources.Items {
if err := s.annotate(ctx, &resources.Items[i]); err != nil {
return fmt.Errorf("failed to annotate resource: '%s/%s.%s': %w", resource.Kind, resource.Name, namespace, err)
} else {
logger.Info(fmt.Sprintf("resource '%s/%s.%s' annotated",
Expand Down Expand Up @@ -482,12 +482,12 @@ func authenticateGCRRequest(c *http.Client, bearer string, tokenIndex int) (err

resp, err := c.Get(url)
if err != nil {
return fmt.Errorf("Cannot verify authenticity of payload: %w", err)
return fmt.Errorf("cannot verify authenticity of payload: %w", err)
}

var p auth
if err := json.NewDecoder(resp.Body).Decode(&p); err != nil {
return fmt.Errorf("Cannot decode auth payload: %w", err)
return fmt.Errorf("cannot decode auth payload: %w", err)
}

return nil
Expand Down