Skip to content

Commit a6d0852

Browse files
committed
Tidy up the handler code.
1 parent ba71388 commit a6d0852

File tree

3 files changed

+76
-62
lines changed

3 files changed

+76
-62
lines changed

internal/server/receiver_handler_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,7 @@ func Test_handlePayload(t *testing.T) {
943943
}
944944

945945
rr := httptest.NewRecorder()
946-
handler := s.handlePayload()
947-
handler(rr, req)
946+
s.handlePayload(rr, req)
948947
g.Expect(rr.Result().StatusCode).To(gomega.Equal(tt.expectedResponseCode))
949948

950949
var allReceivers apiv1.ReceiverList
@@ -961,3 +960,20 @@ func Test_handlePayload(t *testing.T) {
961960
})
962961
}
963962
}
963+
964+
func TestReceiverServer(t *testing.T) {
965+
// k8sClient := buildTestClient()
966+
// rs := NewReceiverServer(":0", logr.Discard(), k8sClient, false)
967+
968+
}
969+
970+
func buildTestClient(objs ...client.Object) client.Client {
971+
scheme := runtime.NewScheme()
972+
apiv1.AddToScheme(scheme)
973+
corev1.AddToScheme(scheme)
974+
975+
return fake.NewClientBuilder().
976+
WithScheme(scheme).
977+
WithObjects(objs...).
978+
WithIndex(&apiv1.Receiver{}, WebhookPathIndexKey, IndexReceiverWebhookPath).Build()
979+
}

internal/server/receiver_handlers.go

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,78 +77,76 @@ func IndexReceiverWebhookPath(o client.Object) []string {
7777
return nil
7878
}
7979

80-
func (s *ReceiverServer) handlePayload() func(w http.ResponseWriter, r *http.Request) {
81-
return func(w http.ResponseWriter, r *http.Request) {
82-
ctx := context.Background()
83-
digest := url.PathEscape(strings.TrimPrefix(r.RequestURI, apiv1.ReceiverWebhookPath))
80+
func (s *ReceiverServer) handlePayload(w http.ResponseWriter, r *http.Request) {
81+
ctx := context.Background()
82+
digest := url.PathEscape(strings.TrimPrefix(r.RequestURI, apiv1.ReceiverWebhookPath))
8483

85-
s.logger.Info(fmt.Sprintf("handling request: %s", digest))
84+
s.logger.Info(fmt.Sprintf("handling request: %s", digest))
8685

87-
var allReceivers apiv1.ReceiverList
88-
err := s.kubeClient.List(ctx, &allReceivers, client.MatchingFields{
89-
WebhookPathIndexKey: r.RequestURI,
90-
}, client.Limit(1))
91-
if err != nil {
92-
s.logger.Error(err, "unable to list receivers")
93-
w.WriteHeader(http.StatusInternalServerError)
94-
return
95-
}
86+
var allReceivers apiv1.ReceiverList
87+
err := s.kubeClient.List(ctx, &allReceivers, client.MatchingFields{
88+
WebhookPathIndexKey: r.RequestURI,
89+
}, client.Limit(1))
90+
if err != nil {
91+
s.logger.Error(err, "unable to list receivers")
92+
w.WriteHeader(http.StatusInternalServerError)
93+
return
94+
}
9695

97-
if len(allReceivers.Items) == 0 {
98-
w.WriteHeader(http.StatusNotFound)
99-
return
100-
}
96+
if len(allReceivers.Items) == 0 {
97+
w.WriteHeader(http.StatusNotFound)
98+
return
99+
}
101100

102-
receiver := allReceivers.Items[0]
103-
logger := s.logger.WithValues(
104-
"reconciler kind", apiv1.ReceiverKind,
105-
"name", receiver.Name,
106-
"namespace", receiver.Namespace)
101+
receiver := allReceivers.Items[0]
102+
logger := s.logger.WithValues(
103+
"reconciler kind", apiv1.ReceiverKind,
104+
"name", receiver.Name,
105+
"namespace", receiver.Namespace)
107106

108-
if receiver.Spec.Suspend || !conditions.IsReady(&receiver) {
109-
err := errors.New("unable to process request")
110-
if receiver.Spec.Suspend {
111-
logger.Error(err, "receiver is suspended")
112-
} else {
113-
logger.Error(err, "receiver is not ready")
114-
}
115-
w.WriteHeader(http.StatusServiceUnavailable)
116-
return
107+
if receiver.Spec.Suspend || !conditions.IsReady(&receiver) {
108+
err := errors.New("unable to process request")
109+
if receiver.Spec.Suspend {
110+
logger.Error(err, "receiver is suspended")
111+
} else {
112+
logger.Error(err, "receiver is not ready")
117113
}
114+
w.WriteHeader(http.StatusServiceUnavailable)
115+
return
116+
}
118117

119-
if err := s.validate(ctx, receiver, r); err != nil {
120-
logger.Error(err, "unable to validate payload")
121-
w.WriteHeader(http.StatusBadRequest)
122-
return
123-
}
118+
if err := s.validate(ctx, receiver, r); err != nil {
119+
logger.Error(err, "unable to validate payload")
120+
w.WriteHeader(http.StatusBadRequest)
121+
return
122+
}
124123

125-
var withErrors bool
126-
for _, resource := range receiver.Spec.Resources {
127-
if err := s.requestReconciliation(ctx, logger, resource, receiver.Namespace); err != nil {
128-
logger.Error(err, "unable to request reconciliation")
129-
withErrors = true
130-
}
124+
var withErrors bool
125+
for _, resource := range receiver.Spec.Resources {
126+
if err := s.requestReconciliation(ctx, logger, resource, receiver.Namespace); err != nil {
127+
logger.Error(err, "unable to request reconciliation")
128+
withErrors = true
131129
}
130+
}
132131

133-
evaluatedResources, err := s.evaluateResourceExpressions(r, receiver)
134-
if err != nil {
135-
logger.Error(err, "unable to evaluate resource expressions")
136-
w.WriteHeader(http.StatusBadRequest)
137-
return
138-
}
132+
evaluatedResources, err := s.evaluateResourceExpressions(r, receiver)
133+
if err != nil {
134+
logger.Error(err, "unable to evaluate resource expressions")
135+
w.WriteHeader(http.StatusBadRequest)
136+
return
137+
}
139138

140-
for _, resource := range evaluatedResources {
141-
if err := s.requestReconciliation(ctx, logger, resource, receiver.Namespace); err != nil {
142-
logger.Error(err, "unable to request reconciliation")
143-
withErrors = true
144-
}
139+
for _, resource := range evaluatedResources {
140+
if err := s.requestReconciliation(ctx, logger, resource, receiver.Namespace); err != nil {
141+
logger.Error(err, "unable to request reconciliation")
142+
withErrors = true
145143
}
144+
}
146145

147-
if withErrors {
148-
w.WriteHeader(http.StatusInternalServerError)
149-
} else {
150-
w.WriteHeader(http.StatusOK)
151-
}
146+
if withErrors {
147+
w.WriteHeader(http.StatusInternalServerError)
148+
} else {
149+
w.WriteHeader(http.StatusOK)
152150
}
153151
}
154152

internal/server/receiver_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewReceiverServer(port string, logger logr.Logger, kubeClient client.Client
5151
// ListenAndServe starts the HTTP server on the specified port
5252
func (s *ReceiverServer) ListenAndServe(stopCh <-chan struct{}, mdlw middleware.Middleware) {
5353
mux := http.NewServeMux()
54-
mux.Handle(apiv1.ReceiverWebhookPath, http.HandlerFunc(s.handlePayload()))
54+
mux.Handle(apiv1.ReceiverWebhookPath, http.HandlerFunc(s.handlePayload))
5555
handlerID := apiv1.ReceiverWebhookPath
5656
if s.exportHTTPPathMetrics {
5757
handlerID = ""

0 commit comments

Comments
 (0)