From 51bb0a06aaba3b21fafe238b6299478f897f0182 Mon Sep 17 00:00:00 2001 From: Claire Chong <43993569+clairecng@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:41:55 +0100 Subject: [PATCH] [chaosplt 207] namespace tag gone (#936) * [chaosplt] get rid of bad bad log tags * woops * Apply suggestions from code review --- api/v1beta1/disruption_cron_webhook.go | 9 +++++---- api/v1beta1/disruption_webhook.go | 11 ++++++----- cli/injector/main.go | 6 +++--- controllers/cron_rollout_helpers.go | 5 +++-- controllers/disruption_controller.go | 3 ++- controllers/disruption_cron_controller.go | 5 +++-- controllers/disruption_rollout_controller.go | 3 ++- eventnotifier/datadog/datadog.go | 5 +++-- eventnotifier/http/http.go | 5 +++-- eventnotifier/slack/slack.go | 5 +++-- log/log.go | 13 +++++++++++++ watchers/chaos_pod_handler.go | 5 +++-- watchers/disruptions_watchers_manager.go | 13 +++++++------ watchers/target_pod_handler.go | 13 +++++++------ webhook/chaos_handler.go | 8 ++++---- webhook/span_context.go | 9 +++++---- webhook/userinfo_mutator.go | 5 +++-- 17 files changed, 75 insertions(+), 48 deletions(-) diff --git a/api/v1beta1/disruption_cron_webhook.go b/api/v1beta1/disruption_cron_webhook.go index 8a4b038aa..db75b12e5 100644 --- a/api/v1beta1/disruption_cron_webhook.go +++ b/api/v1beta1/disruption_cron_webhook.go @@ -12,6 +12,7 @@ import ( "strings" "time" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/chaos-controller/utils" "github.com/robfig/cron" "go.uber.org/zap" @@ -71,7 +72,7 @@ var _ webhook.Defaulter = &DisruptionCron{} // Default implements webhook.Defaulter so a webhook will be registered for the type func (d *DisruptionCron) Default() { if d.Spec.DelayedStartTolerance.Duration() == 0 { - logger.Infow(fmt.Sprintf("setting default delayedStartTolerance of %s in disruptionCron", defaultCronDelayedStartTolerance), "instance", d.Name, "namespace", d.Namespace) + logger.Infow(fmt.Sprintf("setting default delayedStartTolerance of %s in disruptionCron", defaultCronDelayedStartTolerance), cLog.DisruptionCronNameKey, d.Name, cLog.DisruptionCronNamespaceKey, d.Namespace) d.Spec.DelayedStartTolerance = DisruptionDuration(defaultCronDelayedStartTolerance.String()) } } @@ -82,7 +83,7 @@ var _ webhook.Validator = &DisruptionCron{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (d *DisruptionCron) ValidateCreate() (admission.Warnings, error) { - log := disruptionCronWebhookLogger.With("disruptionCronName", d.Name, "disruptionCronNamespace", d.Namespace) + log := disruptionCronWebhookLogger.With(cLog.DisruptionCronNameKey, d.Name, cLog.DisruptionCronNamespaceKey, d.Namespace) log.Infow("validating created disruption cron", "spec", d.Spec) @@ -114,7 +115,7 @@ func (d *DisruptionCron) ValidateCreate() (admission.Warnings, error) { } func (d *DisruptionCron) ValidateUpdate(oldObject runtime.Object) (admission.Warnings, error) { - log := logger.With("disruptionCronName", d.Name, "disruptionCronNamespace", d.Namespace) + log := logger.With(cLog.DisruptionCronNameKey, d.Name, cLog.DisruptionCronNamespaceKey, d.Namespace) log.Infow("validating updated disruption cron", "spec", d.Spec) @@ -146,7 +147,7 @@ func (d *DisruptionCron) ValidateUpdate(oldObject runtime.Object) (admission.War } func (d *DisruptionCron) ValidateDelete() (warnings admission.Warnings, err error) { - log := disruptionCronWebhookLogger.With("disruptionCronName", d.Name, "disruptionCronNamespace", d.Namespace) + log := disruptionCronWebhookLogger.With(cLog.DisruptionCronNameKey, d.Name, cLog.DisruptionCronNamespaceKey, d.Namespace) log.Infow("validating deleted disruption cron", "spec", d.Spec) diff --git a/api/v1beta1/disruption_webhook.go b/api/v1beta1/disruption_webhook.go index 7aa4deff3..85b101166 100644 --- a/api/v1beta1/disruption_webhook.go +++ b/api/v1beta1/disruption_webhook.go @@ -18,6 +18,7 @@ import ( "github.com/DataDog/chaos-controller/cloudservice" cloudtypes "github.com/DataDog/chaos-controller/cloudservice/types" "github.com/DataDog/chaos-controller/ddmark" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/chaos-controller/o11y/metrics" "github.com/DataDog/chaos-controller/o11y/tracer" chaostypes "github.com/DataDog/chaos-controller/types" @@ -121,7 +122,7 @@ var _ webhook.Validator = &Disruption{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *Disruption) ValidateCreate() (admission.Warnings, error) { - log := logger.With("disruptionName", r.Name, "disruptionNamespace", r.Namespace) + log := logger.With(cLog.DisruptionNameKey, r.Name, cLog.DisruptionNamespaceKey, r.Namespace) ctx, err := r.SpanContext(context.Background()) if err != nil { @@ -274,7 +275,7 @@ func (r *Disruption) ValidateCreate() (admission.Warnings, error) { // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *Disruption) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { - log := logger.With("disruptionName", r.Name, "disruptionNamespace", r.Namespace) + log := logger.With(cLog.DisruptionNameKey, r.Name, cLog.DisruptionNamespaceKey, r.Namespace) log.Debugw("validating updated disruption", "spec", r.Spec) var err error @@ -384,7 +385,7 @@ func (r *Disruption) getMetricsTags() []string { if userInfo, err := r.UserInfo(); !errors.Is(err, ErrNoUserInfo) { if err != nil { - logger.Errorw("error retrieving user info from disruption, using empty user info", "error", err, "disruptionName", r.Name, "disruptionNamespace", r.Namespace) + logger.Errorw("error retrieving user info from disruption, using empty user info", "error", err, cLog.DisruptionNameKey, r.Name, cLog.DisruptionNamespaceKey, r.Namespace) } tags = append(tags, "username:"+userInfo.Username) @@ -602,8 +603,8 @@ func safetyNetCountNotTooLarge(r *Disruption) (bool, string, error) { } logger.Debugw("comparing estimated target count to total existing targets", - "disruptionName", r.Name, - "disruptionNamespace", r.Namespace, + cLog.DisruptionNameKey, r.Name, + cLog.DisruptionNamespaceKey, r.Namespace, "namespaceThreshold", namespaceThreshold, "clusterThreshold", clusterThreshold, "estimatedEligibleTargetsCount", targetCount, diff --git a/cli/injector/main.go b/cli/injector/main.go index 538ce494c..6d0670a88 100644 --- a/cli/injector/main.go +++ b/cli/injector/main.go @@ -148,14 +148,14 @@ func initLogger() { } log = log.With( - "disruptionName", disruptionArgs.DisruptionName, - "disruptionNamespace", disruptionArgs.DisruptionNamespace, + logger.DisruptionNameKey, disruptionArgs.DisruptionName, + logger.DisruptionNamespaceKey, disruptionArgs.DisruptionNamespace, "targetName", disruptionArgs.TargetName, "targetNodeName", disruptionArgs.TargetNodeName, ) if parentPID != 0 { - log = log.With("parent_pid", parentPID) + log = log.With("parentPid", parentPID) } } diff --git a/controllers/cron_rollout_helpers.go b/controllers/cron_rollout_helpers.go index 97dadf4eb..de39070d9 100644 --- a/controllers/cron_rollout_helpers.go +++ b/controllers/cron_rollout_helpers.go @@ -12,6 +12,7 @@ import ( "time" chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "go.uber.org/zap" appsv1 "k8s.io/api/apps/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -163,7 +164,7 @@ func CreateDisruptionFromTemplate(ctx context.Context, cl client.Client, scheme disruption.Labels[ownerNameLabel] = owner.GetName() if err := setDisruptionAnnotations(disruption, owner, scheduledTime); err != nil { - log.Errorw("unable to set annotations for child disruption", "err", err, "disruptionName", disruption.Name) + log.Errorw("unable to set annotations for child disruption", "err", err, cLog.DisruptionNameKey, disruption.Name) } if err := overwriteDisruptionSelectors(ctx, cl, disruption, targetResource, owner.GetNamespace()); err != nil { @@ -181,7 +182,7 @@ func CreateDisruptionFromTemplate(ctx context.Context, cl client.Client, scheme func getScheduledTimeForDisruption(log *zap.SugaredLogger, disruption *chaosv1beta1.Disruption) time.Time { parsedTime, err := disruption.GetScheduledAtAnnotation() if err != nil { - log.Errorw("unable to parse schedule time for child disruption", "err", err, "disruptionName", disruption.Name) + log.Errorw("unable to parse schedule time for child disruption", "err", err, cLog.DisruptionNameKey, disruption.Name) return time.Time{} } diff --git a/controllers/disruption_controller.go b/controllers/disruption_controller.go index fddea11d7..7c02d7ffe 100644 --- a/controllers/disruption_controller.go +++ b/controllers/disruption_controller.go @@ -26,6 +26,7 @@ import ( chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1" "github.com/DataDog/chaos-controller/cloudservice" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/chaos-controller/o11y/metrics" "github.com/DataDog/chaos-controller/o11y/tracer" "github.com/DataDog/chaos-controller/safemode" @@ -96,7 +97,7 @@ func (r *DisruptionReconciler) Reconcile(ctx context.Context, req ctrl.Request) // because the logger instance is pointer, concurrent reconciling would create a race condition // where the logger context would change for all ongoing reconcile loops // in the case we enable concurrent reconciling, we should create one logger instance per reconciling call - r.log = r.BaseLog.With("disruptionName", req.Name, "disruptionNamespace", req.Namespace) + r.log = r.BaseLog.With(cLog.DisruptionNameKey, req.Name, cLog.DisruptionNamespaceKey, req.Namespace) // reconcile metrics r.handleMetricSinkError(r.MetricsSink.MetricReconcile()) diff --git a/controllers/disruption_cron_controller.go b/controllers/disruption_cron_controller.go index db3b74053..f18d92f16 100644 --- a/controllers/disruption_cron_controller.go +++ b/controllers/disruption_cron_controller.go @@ -11,6 +11,7 @@ import ( "time" chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/chaos-controller/o11y/metrics" chaostypes "github.com/DataDog/chaos-controller/types" @@ -35,7 +36,7 @@ type DisruptionCronReconciler struct { } func (r *DisruptionCronReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) { - r.log = r.BaseLog.With("disruptionCronNamespace", req.Namespace, "disruptionCronName", req.Name) + r.log = r.BaseLog.With(cLog.DisruptionCronNamespaceKey, req.Namespace, cLog.DisruptionCronNameKey, req.Name) r.log.Info("Reconciling DisruptionCron") // reconcile metrics @@ -192,7 +193,7 @@ func (r *DisruptionCronReconciler) Reconcile(ctx context.Context, req ctrl.Reque r.handleMetricSinkError(r.MetricsSink.MetricDisruptionScheduled(append(DisruptionCronTags, "disruptionName:"+disruption.Name))) - r.log.Infow("created Disruption for DisruptionCron run", "disruptionName", disruption.Name) + r.log.Infow("created Disruption for DisruptionCron run", cLog.DisruptionNameKey, disruption.Name) // ------------------------------------------------------------------ // // If this process restarts at this point (after posting a disruption, but diff --git a/controllers/disruption_rollout_controller.go b/controllers/disruption_rollout_controller.go index 7d08a6701..03f54a7de 100644 --- a/controllers/disruption_rollout_controller.go +++ b/controllers/disruption_rollout_controller.go @@ -10,6 +10,7 @@ import ( "math/rand" "time" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/chaos-controller/o11y/metrics" chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1" @@ -150,7 +151,7 @@ func (r *DisruptionRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Re r.handleMetricSinkError(r.MetricsSink.MetricDisruptionScheduled(append(DisruptionRolloutTags, "disruptionName:"+disruption.Name))) - r.log.Infow("created Disruption for DisruptionRollout run", "disruptionName", disruption.Name) + r.log.Infow("created Disruption for DisruptionRollout run", cLog.DisruptionNameKey, disruption.Name) // ------------------------------------------------------------------ // // If this process restarts at this point (after posting a disruption, but diff --git a/eventnotifier/datadog/datadog.go b/eventnotifier/datadog/datadog.go index d7540b7b4..f3ebf2633 100644 --- a/eventnotifier/datadog/datadog.go +++ b/eventnotifier/datadog/datadog.go @@ -12,6 +12,7 @@ import ( "github.com/DataDog/chaos-controller/api/v1beta1" "github.com/DataDog/chaos-controller/eventnotifier/types" "github.com/DataDog/chaos-controller/eventnotifier/utils" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/datadog-go/statsd" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -74,7 +75,7 @@ func (n *Notifier) Notify(obj client.Object, event corev1.Event, notifType types } func (n *Notifier) notifyDisruption(d *v1beta1.Disruption, event corev1.Event, notifType types.NotificationType) error { - n.logger.With("disruptionName", d.Name) + n.logger.With(cLog.DisruptionNameKey, d.Name, cLog.DisruptionNamespaceKey, d.Namespace) eventType := n.getEventAlertType(notifType) @@ -86,7 +87,7 @@ func (n *Notifier) notifyDisruption(d *v1beta1.Disruption, event corev1.Event, n } func (n *Notifier) notifyDisruptionCron(d *v1beta1.DisruptionCron, event corev1.Event, notifType types.NotificationType) error { - n.logger.With("disruptionCronName", d.Name, "disruptionCronNamespace", d.Namespace) + n.logger.With(cLog.DisruptionCronNameKey, d.Name, cLog.DisruptionCronNamespaceKey, d.Namespace) eventType := n.getEventAlertType(notifType) diff --git a/eventnotifier/http/http.go b/eventnotifier/http/http.go index 10011c113..b92ea0402 100644 --- a/eventnotifier/http/http.go +++ b/eventnotifier/http/http.go @@ -21,6 +21,7 @@ import ( "github.com/DataDog/chaos-controller/api/v1beta1" "github.com/DataDog/chaos-controller/eventnotifier/types" "github.com/DataDog/chaos-controller/eventnotifier/utils" + cLog "github.com/DataDog/chaos-controller/log" "github.com/DataDog/jsonapi" "go.uber.org/zap" authv1 "k8s.io/api/authentication/v1" @@ -248,7 +249,7 @@ func (n *Notifier) marshalUserGroups(groups []string) (string, error) { func (n *Notifier) notifyDisruption(dis v1beta1.Disruption, event corev1.Event, notifType types.NotificationType) error { logger := n.logger - logger.With("disruptionName", dis.Name, "disruptionNamespace", dis.Namespace) + logger.With(cLog.DisruptionNameKey, dis.Name, cLog.DisruptionNamespaceKey, dis.Namespace) if !n.disruptionConfig.Enabled { return nil @@ -292,7 +293,7 @@ func (n *Notifier) notifyDisruption(dis v1beta1.Disruption, event corev1.Event, func (n *Notifier) notifyDisruptionCron(disruptionCron v1beta1.DisruptionCron, event corev1.Event, notifType types.NotificationType) error { logger := n.logger - logger.With("disruptionCronName", disruptionCron.Name, "disruptionCronNamespace", disruptionCron.Namespace) + logger.With(cLog.DisruptionCronNameKey, disruptionCron.Name, cLog.DisruptionCronNamespaceKey, disruptionCron.Namespace) if !n.disruptionCronConfig.Enabled { return nil diff --git a/eventnotifier/slack/slack.go b/eventnotifier/slack/slack.go index d6937f9f5..b51175718 100644 --- a/eventnotifier/slack/slack.go +++ b/eventnotifier/slack/slack.go @@ -17,6 +17,7 @@ import ( "github.com/DataDog/chaos-controller/api/v1beta1" "github.com/DataDog/chaos-controller/eventnotifier/types" "github.com/DataDog/chaos-controller/eventnotifier/utils" + cLog "github.com/DataDog/chaos-controller/log" "github.com/slack-go/slack" "go.uber.org/zap" authv1 "k8s.io/api/authentication/v1" @@ -120,7 +121,7 @@ func (n *Notifier) Notify(obj client.Object, event corev1.Event, notifType types } func (n *Notifier) notifyForDisruption(dis *v1beta1.Disruption, event corev1.Event, notifType types.NotificationType) error { - logger := n.logger.With("disruptionName", dis.Name, "disruptionNamespace", dis.Namespace, "eventType", event.Type) + logger := n.logger.With(cLog.DisruptionNameKey, dis.Name, cLog.DisruptionNamespaceKey, dis.Namespace, "eventType", event.Type) slackMsg := n.buildSlackMessage(dis, event, notifType, dis.Spec.Reporting, logger) @@ -153,7 +154,7 @@ func (n *Notifier) notifyForDisruption(dis *v1beta1.Disruption, event corev1.Eve } func (n *Notifier) notifyForDisruptionCron(disruptionCron *v1beta1.DisruptionCron, event corev1.Event, notifType types.NotificationType) error { - logger := n.logger.With("disruptionCronName", disruptionCron.Name, "disruptionCronNamespace", disruptionCron.Namespace, "eventType", event.Type) + logger := n.logger.With(cLog.DisruptionCronNameKey, disruptionCron.Name, cLog.DisruptionCronNamespaceKey, disruptionCron.Namespace, "eventType", event.Type) slackMsg := n.buildSlackMessage(disruptionCron, event, notifType, disruptionCron.Spec.Reporting, logger) diff --git a/log/log.go b/log/log.go index 4857c9f70..8d6eacf2a 100644 --- a/log/log.go +++ b/log/log.go @@ -12,6 +12,19 @@ import ( "go.uber.org/zap/zapcore" ) +// logger's keys +const ( + // Disruption + DisruptionPrefixKey = "disruption" + DisruptionNameKey = DisruptionPrefixKey + "Name" + DisruptionNamespaceKey = DisruptionPrefixKey + "Namespace" + + // DisruptionCron + DisruptionCronPrefixKey = "disruptionCron" + DisruptionCronNameKey = DisruptionCronPrefixKey + "Name" + DisruptionCronNamespaceKey = DisruptionCronPrefixKey + "Namespace" +) + // NewZapLogger returns a zap production sugared logger with pre-configured encoder settings func NewZapLogger() (*zap.SugaredLogger, error) { // configure logger diff --git a/watchers/chaos_pod_handler.go b/watchers/chaos_pod_handler.go index 3fd99f27b..5bf7f4300 100644 --- a/watchers/chaos_pod_handler.go +++ b/watchers/chaos_pod_handler.go @@ -9,6 +9,7 @@ import ( "fmt" chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/record" @@ -98,8 +99,8 @@ func (w ChaosPodHandler) sendEvent(newPod *corev1.Pod) { w.log.Debugw("ChaosPodHandler UPDATE - Send event", "eventMessage", eventMessage, "eventType", eventType, - "disruptionName", w.disruption.Name, - "disruptionNamespace", w.disruption.Namespace, + cLog.DisruptionNameKey, w.disruption.Name, + cLog.DisruptionNamespaceKey, w.disruption.Namespace, "chaosPodName", newPod.Name, ) } diff --git a/watchers/disruptions_watchers_manager.go b/watchers/disruptions_watchers_manager.go index 3b6028c92..6bcb6f163 100644 --- a/watchers/disruptions_watchers_manager.go +++ b/watchers/disruptions_watchers_manager.go @@ -10,6 +10,7 @@ import ( "fmt" "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "go.uber.org/zap" "k8s.io/apimachinery/pkg/types" k8scontrollercache "sigs.k8s.io/controller-runtime/pkg/cache" @@ -99,7 +100,7 @@ func (d disruptionsWatchersManager) CreateAllWatchers(disruption *v1beta1.Disrup return err } - d.log.Debugw("Watcher created", "watcherName", watcherName, "disruptionName", disruption.Name, "disruptionNamespace", disruption.Namespace) + d.log.Debugw("Watcher created", "watcherName", watcherName, cLog.DisruptionNameKey, disruption.Name, cLog.DisruptionNamespaceKey, disruption.Namespace) } return nil @@ -114,7 +115,7 @@ func (d disruptionsWatchersManager) RemoveAllWatchers(disruption *v1beta1.Disrup // If the Watcher Manager does not exist just do nothing. if watcherManager == nil { - d.log.Debugw("could not remove all watchers", "disruptionName", disruption.Name, "disruptionNamespace", disruption.Namespace) + d.log.Debugw("could not remove all watchers", cLog.DisruptionNameKey, disruption.Name, cLog.DisruptionNamespaceKey, disruption.Namespace) return } @@ -123,7 +124,7 @@ func (d disruptionsWatchersManager) RemoveAllWatchers(disruption *v1beta1.Disrup // Remove the Watcher Manager from the map. delete(d.watchersManagers, namespacedName) - d.log.Infow("all watchers have been removed", "disruptionName", disruption.Name, "disruptionNamespace", disruption.Namespace) + d.log.Infow("all watchers have been removed", cLog.DisruptionNameKey, disruption.Name, cLog.DisruptionNamespaceKey, disruption.Namespace) } // RemoveAllOrphanWatchers removes all Watchers associated with a none existing Disruption. @@ -143,7 +144,7 @@ func (d disruptionsWatchersManager) RemoveAllOrphanWatchers() error { // Remove the watcher manager from the stored managers delete(d.watchersManagers, namespacedName) - d.log.Infow("all watchers have been removed", "disruptionName", namespacedName.Name, "disruptionNamespace", namespacedName.Namespace) + d.log.Infow("all watchers have been removed", cLog.DisruptionNameKey, namespacedName.Name, cLog.DisruptionNamespaceKey, namespacedName.Namespace) } } @@ -206,12 +207,12 @@ func getDisruptionNamespacedName(disruption *v1beta1.Disruption) types.Namespace func (d disruptionsWatchersManager) getWatcherManager(disruptionNamespacedName types.NamespacedName) Manager { // If we have already created a watcher manager for this disruption, use it if cachedWatcherManager := d.watchersManagers[disruptionNamespacedName]; cachedWatcherManager != nil { - d.log.Debugw("Load watcher manager from the cache", "disruptionName", disruptionNamespacedName.Name, "disruptionNamespace", disruptionNamespacedName.Namespace) + d.log.Debugw("Load watcher manager from the cache", cLog.DisruptionNameKey, disruptionNamespacedName.Name, cLog.DisruptionNamespaceKey, disruptionNamespacedName.Namespace) return cachedWatcherManager } - d.log.Debugw("Creating a new watcher manager", "disruptionName", disruptionNamespacedName.Name, "disruptionNamespace", disruptionNamespacedName.Namespace) + d.log.Debugw("Creating a new watcher manager", cLog.DisruptionNameKey, disruptionNamespacedName.Name, cLog.DisruptionNamespaceKey, disruptionNamespacedName.Namespace) // Otherwise, create a new watcher manager return NewManager(d.reader, d.controller) diff --git a/watchers/target_pod_handler.go b/watchers/target_pod_handler.go index 8e214f8f7..97a97bab3 100644 --- a/watchers/target_pod_handler.go +++ b/watchers/target_pod_handler.go @@ -13,6 +13,7 @@ import ( "time" "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -42,8 +43,8 @@ func (d DisruptionTargetHandler) OnAdd(obj interface{}, _ bool) { targetName, targetKind := getTargetNameAndKind(obj) d.log.Debugw("DisruptionTargetHandler ADD", - "disruptionName", d.disruption.Name, - "disruptionNamespace", d.disruption.Namespace, + cLog.DisruptionNameKey, d.disruption.Name, + cLog.DisruptionNamespaceKey, d.disruption.Namespace, "targetName", targetName, "targetKind", targetKind, ) @@ -59,8 +60,8 @@ func (d DisruptionTargetHandler) OnDelete(obj interface{}) { targetName, targetKind := getTargetNameAndKind(obj) d.log.Debugw("DisruptionTargetHandler DELETE", - "disruptionName", d.disruption.Name, - "disruptionNamespace", d.disruption.Namespace, + cLog.DisruptionNameKey, d.disruption.Name, + cLog.DisruptionNamespaceKey, d.disruption.Namespace, "targetName", targetName, "targetKind", targetKind, ) @@ -79,8 +80,8 @@ func (d DisruptionTargetHandler) OnUpdate(oldObj, newObj interface{}) { newTargetName, newTargetKind := getTargetNameAndKind(newObj) d.log.Debugw("DisruptionTargetHandler UPDATE", - "disruptionName", d.disruption.Name, - "disruptionNamespace", d.disruption.Namespace, + cLog.DisruptionNameKey, d.disruption.Name, + cLog.DisruptionNamespaceKey, d.disruption.Namespace, "oldTargetName", oldTargetName, "oldTargetKind", oldTargetKind, "newTargetName", newTargetName, diff --git a/webhook/chaos_handler.go b/webhook/chaos_handler.go index 39abccf4f..fa9a36fd7 100644 --- a/webhook/chaos_handler.go +++ b/webhook/chaos_handler.go @@ -41,7 +41,7 @@ func (m *ChaosHandlerMutator) Handle(ctx context.Context, req admission.Request) // decode pod object err := m.Decoder.Decode(req, pod) if err != nil { - m.Log.Errorw("error decoding pod object", "error", err, "pod", pod.Name, "namespace", pod.Namespace) + m.Log.Errorw("error decoding pod object", "error", err, "podName", pod.Name, "podNamespace", pod.Namespace) return admission.Errored(http.StatusBadRequest, err) } @@ -70,7 +70,7 @@ func (m *ChaosHandlerMutator) Handle(ctx context.Context, req admission.Request) handlerTimeout = timeoutOverride.String() } else if err != nil { - m.Log.Warnw("could not parse user's disrupt-on-init-timeout annotation", "err", err, "pod", podName, "namespace", req.Namespace) + m.Log.Warnw("could not parse user's disrupt-on-init-timeout annotation", "err", err, "podName", podName, "podNamespace", req.Namespace) } } @@ -79,7 +79,7 @@ func (m *ChaosHandlerMutator) Handle(ctx context.Context, req admission.Request) succeedOnTimeout = "--succeed-on-timeout" } - m.Log.Infow("injecting chaos handler init container into targeted pod", "pod", podName, "namespace", req.Namespace) + m.Log.Infow("injecting chaos handler init container into targeted pod", "podName", podName, "podNamespace", req.Namespace) // build chaos handler init container init := corev1.Container{ @@ -102,7 +102,7 @@ func (m *ChaosHandlerMutator) Handle(ctx context.Context, req admission.Request) marshaledPod, err := json.Marshal(pod) if err != nil { - m.Log.Errorw("error encoding modified pod object", "error", err, "pod", pod.Name, "namespace", pod.Namespace) + m.Log.Errorw("error encoding modified pod object", "error", err, "podName", pod.Name, "podNamespace", pod.Namespace) return admission.Errored(http.StatusInternalServerError, err) } diff --git a/webhook/span_context.go b/webhook/span_context.go index c5636771c..0df7d9dda 100644 --- a/webhook/span_context.go +++ b/webhook/span_context.go @@ -11,6 +11,7 @@ import ( "net/http" "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" @@ -38,7 +39,7 @@ func (m *SpanContextMutator) Handle(ctx context.Context, req admission.Request) // decode object if err := m.Decoder.Decode(req, dis); err != nil { - m.Log.Errorw("error decoding disruption object", "error", err, "disruptionName", req.Name, "disruptionNamespace", req.Namespace) + m.Log.Errorw("error decoding disruption object", "error", err, cLog.DisruptionNameKey, req.Name, cLog.DisruptionNamespaceKey, req.Namespace) return admission.Errored(http.StatusBadRequest, err) } @@ -51,7 +52,7 @@ func (m *SpanContextMutator) Handle(ctx context.Context, req admission.Request) defer disruptionSpan.End() // retrieve span context - m.Log.Infow("storing span context in annotations", "disruptionName", dis.Name, "disruptionNamespace", dis.Namespace) + m.Log.Infow("storing span context in annotations", cLog.DisruptionNameKey, dis.Name, cLog.DisruptionNamespaceKey, dis.Namespace) annotations := make(map[string]string) @@ -64,12 +65,12 @@ func (m *SpanContextMutator) Handle(ctx context.Context, req admission.Request) // writes the traceID and spanID in the annotations of the disruption err := dis.SetSpanContext(ctx) if err != nil { - m.Log.Errorw("error defining SpanContext", "error", err, "disruptionName", dis.Name, "disruptionNamespace", dis.Namespace) + m.Log.Errorw("error defining SpanContext", "error", err, cLog.DisruptionNameKey, dis.Name, cLog.DisruptionNamespaceKey, dis.Namespace) } marshaled, err := json.Marshal(dis) if err != nil { - m.Log.Errorw("error encoding modified annotations", "error", err, "disruptionName", dis.Name, "disruptionNamespace", dis.Namespace) + m.Log.Errorw("error encoding modified annotations", "error", err, cLog.DisruptionNameKey, dis.Name, cLog.DisruptionNamespaceKey, dis.Namespace) return admission.Errored(http.StatusInternalServerError, err) } diff --git a/webhook/userinfo_mutator.go b/webhook/userinfo_mutator.go index 9718cee95..cfad91c9d 100644 --- a/webhook/userinfo_mutator.go +++ b/webhook/userinfo_mutator.go @@ -12,6 +12,7 @@ import ( "net/http" "github.com/DataDog/chaos-controller/api/v1beta1" + cLog "github.com/DataDog/chaos-controller/log" "go.uber.org/zap" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" @@ -88,9 +89,9 @@ func (m UserInfoMutator) getObject(request admission.Request) (client.Object, er func (m UserInfoMutator) getLogger(request admission.Request) (*zap.SugaredLogger, error) { switch request.Kind.Kind { case v1beta1.DisruptionKind: - return m.Log.With("disruptionName", request.Name, "disruptionNamespace", request.Namespace), nil + return m.Log.With(cLog.DisruptionNameKey, request.Name, cLog.DisruptionNamespaceKey, request.Namespace), nil case v1beta1.DisruptionCronKind: - return m.Log.With("disruptionCronName", request.Name, "disruptionCronNamespace", request.Namespace), nil + return m.Log.With(cLog.DisruptionCronNameKey, request.Name, cLog.DisruptionCronNamespaceKey, request.Namespace), nil } return nil, fmt.Errorf("not a valid kind: %s", request.Kind.Kind)