diff --git a/internal/adapters/runtime/kubernetes/game_room.go b/internal/adapters/runtime/kubernetes/game_room.go index 52cb0baa3..577c738ea 100644 --- a/internal/adapters/runtime/kubernetes/game_room.go +++ b/internal/adapters/runtime/kubernetes/game_room.go @@ -29,6 +29,8 @@ import ( "github.com/topfreegames/maestro/internal/core/entities" "github.com/topfreegames/maestro/internal/core/entities/game_room" "github.com/topfreegames/maestro/internal/core/ports/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -64,6 +66,7 @@ func (k *kubernetes) CreateGameRoomInstance(ctx context.Context, scheduler *enti } func (k *kubernetes) DeleteGameRoomInstance(ctx context.Context, gameRoomInstance *game_room.Instance, reason string) error { + k.logger.Debug("deleting game room instance", zap.String("gameRoomID", gameRoomInstance.ID), zap.String("schedulerID", gameRoomInstance.SchedulerID), zap.String("reason", reason)) _ = k.createKubernetesEvent(ctx, gameRoomInstance.SchedulerID, gameRoomInstance.ID, reason, "GameRoomDeleted") err := k.clientSet.CoreV1().Pods(gameRoomInstance.SchedulerID).Delete(ctx, gameRoomInstance.ID, metav1.DeleteOptions{}) @@ -101,19 +104,36 @@ func (k *kubernetes) createKubernetesEvent(ctx context.Context, schedulerID stri return errors.NewErrUnexpected("error getting game room instance: %s", err) } - k.getEventRecorder(pod).Event(pod, corev1.EventTypeNormal, reason, message) + k.logger.Debug("Getting event recorder", zap.String("pod", pod.Name), zap.String("namespace", pod.Namespace)) + recorder := k.getEventRecorder(pod) + k.logger.Debug("Recording event", zap.String("pod", pod.Name), zap.String("namespace", pod.Namespace)) + recorder.Event(pod, corev1.EventTypeNormal, reason, message) return nil } func (k *kubernetes) getEventRecorder(pod *corev1.Pod) record.EventRecorder { + k.logger.Debug( + "deleting game room instance", + zap.String("pod", pod.Name), + zap.String("namespace", pod.Namespace), + zap.Array("event recorders", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error { + for namespace, recorder := range k.eventRecorders { + enc.AppendString(fmt.Sprintf("%s: %v", namespace, recorder)) + } + return nil + })), + ) + if recorder, ok := k.eventRecorders[pod.Namespace]; ok { + k.logger.Debug("returning existing event recorder", zap.String("namespace", pod.Namespace), zap.Any("recorder", recorder)) return recorder } - k.eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: k.clientSet.CoreV1().Events(pod.Namespace)}) + k.eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: k.clientSet.CoreV1().Events("")}) recorder := k.eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "maestro-next"}) k.eventRecorders[pod.Namespace] = recorder + k.logger.Debug("returning newly created event recorder", zap.String("namespace", pod.Namespace), zap.Any("recorder", recorder)) return recorder }