From 891d0cef66b1c3298c2476776b190b4286fc648f Mon Sep 17 00:00:00 2001 From: Peter Wilcsinszky Date: Mon, 24 Jul 2023 17:24:39 +0200 Subject: [PATCH] fix: refuse to reconcile logging with non-unique loggingRef Signed-off-by: Peter Wilcsinszky --- .../loggingref-check/loggingref-multi.yaml | 24 +++++++++++++++++++ controllers/logging/logging_controller.go | 22 +++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 config/samples/loggingref-check/loggingref-multi.yaml diff --git a/config/samples/loggingref-check/loggingref-multi.yaml b/config/samples/loggingref-check/loggingref-multi.yaml new file mode 100644 index 0000000000..b53ce59be6 --- /dev/null +++ b/config/samples/loggingref-check/loggingref-multi.yaml @@ -0,0 +1,24 @@ +apiVersion: logging.banzaicloud.io/v1beta1 +kind: Logging +metadata: + name: one +spec: + controlNamespace: default +--- +apiVersion: logging.banzaicloud.io/v1beta1 +kind: Logging +metadata: + name: two +spec: + controlNamespace: default +# this is invalid, as implicitly this will have the same loggingRef as the first one +--- +apiVersion: logging.banzaicloud.io/v1beta1 +kind: Logging +metadata: + name: three +spec: + loggingRef: custom + controlNamespace: default + fluentd: {} +# this must be processed regardless of the above as it has a unique loggingRef diff --git a/controllers/logging/logging_controller.go b/controllers/logging/logging_controller.go index 217cb0e269..53e89002dc 100644 --- a/controllers/logging/logging_controller.go +++ b/controllers/logging/logging_controller.go @@ -93,6 +93,28 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return reconcile.Result{}, client.IgnoreNotFound(err) } + allLoggings := &loggingv1beta1.LoggingList{} + if err := r.Client.List(ctx, allLoggings); err != nil { + return reconcile.Result{}, errors.WrapIf(err, "listing logging resources") + } + + loggingsForTheSameRef := make([]string, 0) + for _, l := range allLoggings.Items { + if l.Name == logging.Name { + continue + } + if l.Spec.LoggingRef == logging.Spec.LoggingRef { + loggingsForTheSameRef = append(loggingsForTheSameRef, l.Name) + } + } + + if len(loggingsForTheSameRef) > 0 { + problem := fmt.Sprintf("multiple other logging resources exist with the same loggingRef: %s", + strings.Join(loggingsForTheSameRef, ",")) + logging.Status.Problems = []string{problem} + return reconcile.Result{}, errors.New(problem) + } + if err := r.Client.List(ctx, &v1.ServiceMonitorList{}); err == nil { //nolint:staticcheck ctx = context.WithValue(ctx, resources.ServiceMonitorKey, true)