Skip to content

Commit e438ca1

Browse files
committed
Rework log messages.
Signed-off-by: Kevin McDermott <[email protected]>
1 parent bae8794 commit e438ca1

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

internal/server/receiver_handlers.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ import (
4747
apiv1 "github.com/fluxcd/notification-controller/api/v1"
4848
)
4949

50-
var (
51-
WebhookPathIndexKey = ".metadata.webhookPath"
50+
const (
51+
WebhookPathIndexKey string = ".metadata.webhookPath"
52+
53+
// maxRequestSizeBytes is the maximum size of a request to the API server
54+
maxRequestSizeBytes int64 = 3 * 1024 * 1024
5255
)
5356

5457
// defaultFluxAPIVersions is a map of Flux API kinds to their API versions.
@@ -142,7 +145,7 @@ func (s *ReceiverServer) handlePayload(w http.ResponseWriter, r *http.Request) {
142145
func (s *ReceiverServer) notifySingleResource(ctx context.Context, logger logr.Logger, resource *metav1.PartialObjectMetadata, resourcePredicate resourcePredicate) error {
143146
objectKey := client.ObjectKeyFromObject(resource)
144147
if err := s.kubeClient.Get(ctx, objectKey, resource); err != nil {
145-
return fmt.Errorf("unable to read %s '%s' error: %w", resource.Kind, objectKey, err)
148+
return fmt.Errorf("unable to read %s %q error: %w", resource.Kind, objectKey, err)
146149
}
147150

148151
return s.notifyResource(ctx, logger, resource, resourcePredicate)
@@ -175,7 +178,7 @@ func (s *ReceiverServer) notifyDynamicResources(ctx context.Context, logger logr
175178
return fmt.Errorf("matchLabels field not set when using wildcard '*' as name")
176179
}
177180

178-
logger.V(1).Info(fmt.Sprintf("annotate resources by matchLabel for kind '%s' in '%s'",
181+
logger.V(1).Info(fmt.Sprintf("annotate resources by matchLabel for kind %q in %q",
179182
resource.Kind, namespace), "matchLabels", resource.MatchLabels)
180183

181184
var resources metav1.PartialObjectMetadataList
@@ -193,7 +196,7 @@ func (s *ReceiverServer) notifyDynamicResources(ctx context.Context, logger logr
193196
}
194197

195198
if len(resources.Items) == 0 {
196-
noObjectsFoundErr := fmt.Errorf("no '%s' resources found with matching labels '%s' in '%s' namespace", resource.Kind, resource.MatchLabels, namespace)
199+
noObjectsFoundErr := fmt.Errorf("no %q resources found with matching labels %q' in %q namespace", resource.Kind, resource.MatchLabels, namespace)
197200
logger.Error(noObjectsFoundErr, "error annotating resources")
198201
return nil
199202
}
@@ -218,6 +221,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
218221
"name", receiver.Name,
219222
"namespace", receiver.Namespace)
220223

224+
r.Body = io.NopCloser(io.LimitReader(r.Body, maxRequestSizeBytes))
221225
switch receiver.Spec.Type {
222226
case apiv1.GenericReceiver:
223227
return nil
@@ -254,7 +258,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
254258
}
255259
}
256260
if !allowed {
257-
return fmt.Errorf("the GitHub event '%s' is not authorised", event)
261+
return fmt.Errorf("the GitHub event %q is not authorised", event)
258262
}
259263
}
260264

@@ -276,7 +280,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
276280
}
277281
}
278282
if !allowed {
279-
return fmt.Errorf("the GitLab event '%s' is not authorised", event)
283+
return fmt.Errorf("the GitLab event %q is not authorised", event)
280284
}
281285
}
282286

@@ -308,7 +312,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
308312
}
309313
}
310314
if !allowed {
311-
return fmt.Errorf("the CDEvent '%s' is not authorised", event)
315+
return fmt.Errorf("the CDEvent %q is not authorised", event)
312316
}
313317
}
314318

@@ -336,7 +340,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
336340
}
337341
}
338342
if !allowed {
339-
return fmt.Errorf("the Bitbucket server event '%s' is not authorised", event)
343+
return fmt.Errorf("the Bitbucket server event %q is not authorised", event)
340344
}
341345
}
342346

@@ -489,7 +493,7 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver apiv1.Receiver,
489493
return nil
490494
}
491495

492-
return fmt.Errorf("recevier type '%s' not supported", receiver.Spec.Type)
496+
return fmt.Errorf("recevier type %q not supported", receiver.Spec.Type)
493497
}
494498

495499
func (s *ReceiverServer) token(ctx context.Context, receiver apiv1.Receiver) (string, error) {
@@ -502,13 +506,13 @@ func (s *ReceiverServer) token(ctx context.Context, receiver apiv1.Receiver) (st
502506
var secret corev1.Secret
503507
err := s.kubeClient.Get(ctx, secretName, &secret)
504508
if err != nil {
505-
return "", fmt.Errorf("unable to read token from secret '%s' error: %w", secretName, err)
509+
return "", fmt.Errorf("unable to read token from secret %q error: %w", secretName, err)
506510
}
507511

508512
if val, ok := secret.Data["token"]; ok {
509513
token = string(val)
510514
} else {
511-
return "", fmt.Errorf("invalid '%s' secret data: required field 'token'", secretName)
515+
return "", fmt.Errorf("invalid %q secret data: required field 'token'", secretName)
512516
}
513517

514518
return token, nil
@@ -526,7 +530,7 @@ func (s *ReceiverServer) requestReconciliation(ctx context.Context, logger logr.
526530
apiVersion := resource.APIVersion
527531
if apiVersion == "" {
528532
if defaultFluxAPIVersions[resource.Kind] == "" {
529-
return fmt.Errorf("apiVersion must be specified for kind '%s'", resource.Kind)
533+
return fmt.Errorf("apiVersion must be specified for kind %q", resource.Kind)
530534
}
531535
apiVersion = defaultFluxAPIVersions[resource.Kind]
532536
}
@@ -561,7 +565,7 @@ func (s *ReceiverServer) annotate(ctx context.Context, resource *metav1.PartialO
561565
resource.SetAnnotations(sourceAnnotations)
562566

563567
if err := s.kubeClient.Patch(ctx, resource, patch); err != nil {
564-
return fmt.Errorf("unable to annotate %s '%s' error: %w", resource.Kind, client.ObjectKey{
568+
return fmt.Errorf("unable to annotate %s %q error: %w", resource.Kind, client.ObjectKey{
565569
Namespace: resource.Namespace,
566570
Name: resource.Name,
567571
}, err)

0 commit comments

Comments
 (0)