Skip to content

Commit

Permalink
fix: refuse to reconcile logging with non-unique loggingRef
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Wilcsinszky <[email protected]>
  • Loading branch information
pepov committed Jul 24, 2023
1 parent c804173 commit 891d0ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions config/samples/loggingref-check/loggingref-multi.yaml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions controllers/logging/logging_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 891d0ce

Please sign in to comment.