feat: sync annotations from gs to pod (#140)

Signed-off-by: ChrisLiu <chrisliu1995@163.com>
This commit is contained in:
ChrisLiu 2024-04-26 11:23:13 +08:00 committed by GitHub
parent d547f61323
commit d67e058e0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 0 deletions

View File

@ -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)

View File

@ -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])
}
}
}
}

View File

@ -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/")
}