Skip to content

Commit

Permalink
add managed controller logging format (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverMKing authored Aug 25, 2023
1 parent bff71e0 commit e4e5027
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 33 deletions.
19 changes: 19 additions & 0 deletions pkg/controller/controllername/controllername.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"unicode"

"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
Expand All @@ -22,6 +24,8 @@ type ControllerNamer interface {
LoggerName() string
// AddToLogger adds controller name fields to the logger then returns the logger with the added fields
AddToLogger(l logr.Logger) logr.Logger
// AddToController adds the controller name to the controller builder then returns the builder with the added name. This is useful for naming managed controllers from controller-runtime
AddToController(blder *builder.Builder, l logr.Logger) *builder.Builder
}

// controllerName ex. {"My","Controller", "Name"} -> MyControllerName
Expand Down Expand Up @@ -69,3 +73,18 @@ func (c controllerName) AddToLogger(l logr.Logger) logr.Logger {
WithValues("controller", c.String()).
WithValues("controllerMetricsName", c.MetricsName()) // include metrics name, so we can automate creating queries that check Logs based on alerts
}

func (c controllerName) AddToController(blder *builder.Builder, l logr.Logger) *builder.Builder {
return blder.
Named(c.MetricsName()).
WithLogConstructor(func(req *reconcile.Request) logr.Logger {
logger := c.AddToLogger(l)
if req != nil {
logger.WithValues(
"namespace", req.Namespace,
"name", req.Name,
)
}
return logger
})
}
11 changes: 6 additions & 5 deletions pkg/controller/keyvault/event_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func NewEventMirror(manager ctrl.Manager, conf *config.Config) error {
client: manager.GetClient(),
events: manager.GetEventRecorderFor("aks-app-routing-operator"),
}
return ctrl.
NewControllerManagedBy(manager).
For(&corev1.Event{}).
WithEventFilter(e.newPredicates()).
Complete(e)
return eventMirrorControllerName.AddToController(
ctrl.
NewControllerManagedBy(manager).
For(&corev1.Event{}).
WithEventFilter(e.newPredicates()), manager.GetLogger(),
).Complete(e)
}

func (e *EventMirror) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
19 changes: 10 additions & 9 deletions pkg/controller/keyvault/ingress_secret_provider_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ func NewIngressSecretProviderClassReconciler(manager ctrl.Manager, conf *config.
if conf.DisableKeyvault {
return nil
}
return ctrl.
NewControllerManagedBy(manager).
For(&netv1.Ingress{}).
Complete(&IngressSecretProviderClassReconciler{
client: manager.GetClient(),
events: manager.GetEventRecorderFor("aks-app-routing-operator"),
config: conf,
ingressManager: ingressManager,
})
return ingressSecretProviderControllerName.AddToController(
ctrl.
NewControllerManagedBy(manager).
For(&netv1.Ingress{}), manager.GetLogger(),
).Complete(&IngressSecretProviderClassReconciler{
client: manager.GetClient(),
events: manager.GetEventRecorderFor("aks-app-routing-operator"),
config: conf,
ingressManager: ingressManager,
})
}

func (i *IngressSecretProviderClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/keyvault/placeholder_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ func NewPlaceholderPodController(manager ctrl.Manager, conf *config.Config, ingr
if conf.DisableKeyvault {
return nil
}
return ctrl.
NewControllerManagedBy(manager).
For(&secv1.SecretProviderClass{}).
Complete(&PlaceholderPodController{client: manager.GetClient(), config: conf, ingressManager: ingressManager})
return placeholderPodControllerName.AddToController(
ctrl.
NewControllerManagedBy(manager).
For(&secv1.SecretProviderClass{}), manager.GetLogger(),
).Complete(&PlaceholderPodController{client: manager.GetClient(), config: conf, ingressManager: ingressManager})
}

func (p *PlaceholderPodController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/osm/ingress_backend_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ func NewIngressBackendReconciler(manager ctrl.Manager, conf *config.Config, ingr
if conf.DisableOSM {
return nil
}
return ctrl.
NewControllerManagedBy(manager).
For(&netv1.Ingress{}).
Complete(&IngressBackendReconciler{client: manager.GetClient(), config: conf, ingressControllerNamer: ingressControllerNamer})
return ingressBackendControllerName.AddToController(
ctrl.
NewControllerManagedBy(manager).
For(&netv1.Ingress{}),
manager.GetLogger(),
).Complete(&IngressBackendReconciler{client: manager.GetClient(), config: conf, ingressControllerNamer: ingressControllerNamer})
}

func (i *IngressBackendReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/osm/ingress_cert_config_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func NewIngressCertConfigReconciler(manager ctrl.Manager, conf *config.Config) e
if conf.DisableOSM {
return nil
}
return ctrl.
NewControllerManagedBy(manager).
For(&cfgv1alpha2.MeshConfig{}).
Complete(&IngressCertConfigReconciler{client: manager.GetClient()})
return ingressCertConfigControllerName.AddToController(
ctrl.
NewControllerManagedBy(manager).
For(&cfgv1alpha2.MeshConfig{}), manager.GetLogger(),
).Complete(&IngressCertConfigReconciler{client: manager.GetClient()})
}

func (i *IngressCertConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
15 changes: 8 additions & 7 deletions pkg/controller/service/ingress_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import (
"fmt"

"github.com/Azure/aks-app-routing-operator/pkg/controller/controllername"
"github.com/Azure/aks-app-routing-operator/pkg/controller/metrics"
"github.com/Azure/aks-app-routing-operator/pkg/manifests"
"github.com/Azure/aks-app-routing-operator/pkg/util"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/Azure/aks-app-routing-operator/pkg/controller/metrics"
"github.com/Azure/aks-app-routing-operator/pkg/util"
)

var (
Expand All @@ -45,10 +44,12 @@ type NginxIngressReconciler struct {
func NewNginxIngressReconciler(manager ctrl.Manager, ingConfig *manifests.NginxIngressConfig) error {
metrics.InitControllerMetrics(ingressControllerName)

return ctrl.
NewControllerManagedBy(manager).
For(&corev1.Service{}).
Complete(&NginxIngressReconciler{client: manager.GetClient(), ingConfig: ingConfig})
return ingressControllerName.AddToController(
ctrl.
NewControllerManagedBy(manager).
For(&corev1.Service{}),
manager.GetLogger(),
).Complete(&NginxIngressReconciler{client: manager.GetClient(), ingConfig: ingConfig})
}

func (i *NginxIngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down

0 comments on commit e4e5027

Please sign in to comment.