diff --git a/pkg/controllers/gameserver/gameserver_manager.go b/pkg/controllers/gameserver/gameserver_manager.go index 3dc2e2e..44f6e88 100644 --- a/pkg/controllers/gameserver/gameserver_manager.go +++ b/pkg/controllers/gameserver/gameserver_manager.go @@ -174,6 +174,17 @@ func (manager GameServerManager) SyncGsToPod() error { } } + // sync annotations from gs to pod + for gsKey, gsValue := range gs.GetAnnotations() { + if util.IsHasPrefixGsSyncToPod(gsKey) { + podValue, exist := pod.GetAnnotations()[gsKey] + if exist && (podValue == gsValue) { + continue + } + newAnnotations[gsKey] = gsValue + } + } + // sync pod containers when the containers(images) in GameServer are different from that in pod. containers := manager.syncPodContainers(gs.Spec.Containers, pod.DeepCopy().Spec.Containers) diff --git a/pkg/controllers/gameserver/gameserver_manager_test.go b/pkg/controllers/gameserver/gameserver_manager_test.go index 205f1a3..9ed3312 100644 --- a/pkg/controllers/gameserver/gameserver_manager_test.go +++ b/pkg/controllers/gameserver/gameserver_manager_test.go @@ -5,6 +5,7 @@ import ( kruiseV1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1" kruiseV1beta1 "github.com/openkruise/kruise-api/apps/v1beta1" gameKruiseV1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1" + "github.com/openkruise/kruise-game/pkg/util" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -435,6 +436,9 @@ func TestSyncGsToPod(t *testing.T) { Labels: map[string]string{ gameKruiseV1alpha1.GameServerOwnerGssKey: "xxx", }, + Annotations: map[string]string{ + "gs-sync/match-id": "xxx-xxx-xxx", + }, }, Spec: gameKruiseV1alpha1.GameServerSpec{ UpdatePriority: &up, @@ -461,6 +465,10 @@ func TestSyncGsToPod(t *testing.T) { Labels: map[string]string{ gameKruiseV1alpha1.GameServerOwnerGssKey: "xxx", }, + Annotations: map[string]string{ + "meaningless-key": "meaningless-value", + "gs-sync/match-id": "xxx-xxx-xxx", + }, }, Spec: gameKruiseV1alpha1.GameServerSpec{ UpdatePriority: &up, @@ -481,6 +489,9 @@ func TestSyncGsToPod(t *testing.T) { gameKruiseV1alpha1.GameServerUpdatePriorityKey: up.String(), gameKruiseV1alpha1.GameServerStateKey: string(gameKruiseV1alpha1.Creating), }, + Annotations: map[string]string{ + "gs-sync/match-id": "xxx-xxx-xx2", + }, }, Status: corev1.PodStatus{ Phase: corev1.PodPending, @@ -525,6 +536,12 @@ func TestSyncGsToPod(t *testing.T) { if pod.Labels[gameKruiseV1alpha1.GameServerNetworkDisabled] != strconv.FormatBool(test.gs.Spec.NetworkDisabled) { t.Errorf("expect NetworkDisabled is %s ,but actually is %s", strconv.FormatBool(test.gs.Spec.NetworkDisabled), pod.Labels[gameKruiseV1alpha1.GameServerNetworkDisabled]) } + + for gsKey, gsValue := range test.gs.GetAnnotations() { + if util.IsHasPrefixGsSyncToPod(gsKey) && pod.Annotations[gsKey] != gsValue { + t.Errorf("expect gs annotation %s is %s ,but actually is %s", gsKey, gsValue, pod.Annotations[gsKey]) + } + } } } diff --git a/pkg/util/gameserver.go b/pkg/util/gameserver.go index 56c7bf1..05a2e53 100644 --- a/pkg/util/gameserver.go +++ b/pkg/util/gameserver.go @@ -209,6 +209,14 @@ func AddPrefixGameKruise(s string) string { return "game.kruise.io/" + s } +func AddPrefixGsSyncToPod(s string) string { + return "gs-sync/" + s +} + +func IsHasPrefixGsSyncToPod(s string) bool { + return strings.HasPrefix(s, "gs-sync/") +} + func RemovePrefixGameKruise(s string) string { return strings.TrimPrefix(s, "game.kruise.io/") }