Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sync annotations from gs to pod #140

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/controllers/gameserver/gameserver_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions pkg/controllers/gameserver/gameserver_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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])
}
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/util/gameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
}
Expand Down
Loading