Skip to content

Commit

Permalink
fix(runtime): let clientSet infere object namespace
Browse files Browse the repository at this point in the history
When sending Kubernetes events, let the kubernetes clientSet infere
what is the namespace of the object (Pod) instead of having one
recorder per namespace.
  • Loading branch information
hspedro committed Sep 2, 2024
1 parent 785a118 commit 1788e34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
17 changes: 1 addition & 16 deletions internal/adapters/runtime/kubernetes/game_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
)

func (k *kubernetes) CreateGameRoomInstance(ctx context.Context, scheduler *entities.Scheduler, gameRoomName string, gameRoomSpec game_room.Spec) (*game_room.Instance, error) {
Expand Down Expand Up @@ -101,19 +98,7 @@ 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.eventRecorder.Event(pod, corev1.EventTypeNormal, reason, message)

return nil
}

func (k *kubernetes) getEventRecorder(pod *corev1.Pod) record.EventRecorder {
if recorder, ok := k.eventRecorders[pod.Namespace]; ok {
return recorder
}

k.eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: k.clientSet.CoreV1().Events(pod.Namespace)})
recorder := k.eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "maestro-next"})
k.eventRecorders[pod.Namespace] = recorder

return recorder
}
23 changes: 14 additions & 9 deletions internal/adapters/runtime/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,29 @@ import (
"github.com/topfreegames/maestro/internal/core/logs"
"github.com/topfreegames/maestro/internal/core/ports"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
kube "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
)

var _ ports.Runtime = (*kubernetes)(nil)

type kubernetes struct {
clientSet kube.Interface
logger *zap.Logger
eventBroadcaster record.EventBroadcaster
eventRecorders map[string]record.EventRecorder
clientSet kube.Interface
logger *zap.Logger
eventRecorder record.EventRecorder
}

func New(clientSet kube.Interface) *kubernetes {
return &kubernetes{
clientSet: clientSet,
logger: zap.L().With(zap.String(logs.LogFieldRuntime, "kubernetes")),
eventBroadcaster: record.NewBroadcaster(),
eventRecorders: make(map[string]record.EventRecorder, 0),
k := &kubernetes{
clientSet: clientSet,
logger: zap.L().With(zap.String(logs.LogFieldRuntime, "kubernetes")),
}

eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: k.clientSet.CoreV1().Events("")})
k.eventRecorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "maestro-next"})
return k
}

0 comments on commit 1788e34

Please sign in to comment.