Skip to content

Commit

Permalink
cleanup: show prometheus crd warnings only once
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Wilcsinszky <[email protected]>
  • Loading branch information
pepov committed Jul 16, 2024
1 parent de01bdd commit 34e0338
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
26 changes: 10 additions & 16 deletions controllers/logging/logging_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import (
"os"
"regexp"
"strings"
"sync"

"emperror.dev/errors"
"github.com/cisco-open/operator-tools/pkg/reconciler"
"github.com/cisco-open/operator-tools/pkg/secret"
"github.com/cisco-open/operator-tools/pkg/utils"
"github.com/go-logr/logr"
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -53,6 +53,12 @@ import (
loggingv1beta1 "github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1"
)

var fluentbitWarning sync.Once

func init() {
fluentbitWarning = sync.Once{}
}

// NewLoggingReconciler returns a new LoggingReconciler instance
func NewLoggingReconciler(client client.Client, eventRecorder record.EventRecorder, log logr.Logger) *LoggingReconciler {
return &LoggingReconciler{
Expand Down Expand Up @@ -100,20 +106,6 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return reconcile.Result{}, client.IgnoreNotFound(err)
}

if err := r.Client.List(ctx, &v1.ServiceMonitorList{}); err == nil {
//nolint:staticcheck
ctx = context.WithValue(ctx, resources.ServiceMonitorKey, true)
} else {
log.Info("WARNING ServiceMonitor is not supported in the cluster")
}

if err := r.Client.List(ctx, &v1.PrometheusRuleList{}); err == nil {
//nolint:staticcheck
ctx = context.WithValue(ctx, resources.PrometheusRuleKey, true)
} else {
log.Info("WARNING PrometheusRule is not supported in the cluster")
}

if err := logging.SetDefaults(); err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -225,7 +217,9 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
case 0:
// check for legacy definition
if logging.Spec.FluentbitSpec != nil {
log.Info("WARNING fluentbit definition inside the Logging resource is deprecated and will be removed in the next major release")
fluentbitWarning.Do(func() {
log.Info("WARNING fluentbit definition inside the Logging resource is deprecated and will be removed in the next major release")
})
nameProvider := fluentbit.NewLegacyFluentbitNameProvider(&logging)
reconcilers = append(reconcilers, fluentbit.New(
r.Client,
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (

extensionsControllers "github.com/kube-logging/logging-operator/controllers/extensions"
loggingControllers "github.com/kube-logging/logging-operator/controllers/logging"
"github.com/kube-logging/logging-operator/pkg/resources"
extensionsv1alpha1 "github.com/kube-logging/logging-operator/pkg/sdk/extensions/api/v1alpha1"
config "github.com/kube-logging/logging-operator/pkg/sdk/extensions/extensionsconfig"
loggingv1alpha1 "github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1alpha1"
Expand Down Expand Up @@ -214,13 +215,28 @@ func main() {
webhookServer.Register(config.TailerWebhook.ServerPath, &webhook.Admission{Handler: webhookHandler})
}

if err := mgr.GetClient().List(ctx, &prometheusOperator.ServiceMonitorList{}); err == nil {
//nolint:staticcheck
ctx = context.WithValue(ctx, resources.ServiceMonitorKey, true)
} else {
setupLog.Info("WARNING ServiceMonitor is not supported in the cluster")
}

if err := mgr.GetClient().List(ctx, &prometheusOperator.PrometheusRuleList{}); err == nil {
//nolint:staticcheck
ctx = context.WithValue(ctx, resources.PrometheusRuleKey, true)
} else {
setupLog.Info("WARNING PrometheusRule is not supported in the cluster")
}

// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")

if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}

}

func detectContainerRuntime(ctx context.Context, c client.Reader) error {
Expand Down

0 comments on commit 34e0338

Please sign in to comment.