Skip to content

Commit

Permalink
replace patch asts with update (openkruise#131)
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 authored Mar 27, 2024
1 parent 2dd97c2 commit 69babe6
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pkg/controllers/gameserverset/gameserverset_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -326,16 +327,16 @@ func (manager *GameServerSetManager) UpdateWorkload() error {
asts := manager.asts

// sync with Advanced StatefulSet
asts = util.GetNewAstsFromGss(gss.DeepCopy(), asts)
astsAns := asts.GetAnnotations()
astsAns[gameKruiseV1alpha1.AstsHashKey] = util.GetAstsHash(manager.gameServerSet)
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
asts = util.GetNewAstsFromGss(gss.DeepCopy(), asts)
astsAns := asts.GetAnnotations()
astsAns[gameKruiseV1alpha1.AstsHashKey] = util.GetAstsHash(manager.gameServerSet)
asts.SetAnnotations(astsAns)

patchAsts := map[string]interface{}{"metadata": map[string]map[string]string{"annotations": astsAns}, "spec": asts.Spec}
patchAstsBytes, err := json.Marshal(patchAsts)
if err != nil {
return err
}
return manager.client.Patch(context.TODO(), asts, client.RawPatch(types.MergePatchType, patchAstsBytes))
return manager.client.Update(context.TODO(), asts)
})

return retryErr
}

func (manager *GameServerSetManager) SyncPodProbeMarker() error {
Expand Down
107 changes: 107 additions & 0 deletions pkg/controllers/gameserverset/gameserverset_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package gameserverset

import (
"context"
appspub "github.com/openkruise/kruise-api/apps/pub"
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"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"strconv"
Expand Down Expand Up @@ -1060,3 +1063,107 @@ func TestNumberToKill(t *testing.T) {
}
}
}

func TestGameServerSetManager_UpdateWorkload(t *testing.T) {
tests := []struct {
gss *gameKruiseV1alpha1.GameServerSet
asts *kruiseV1beta1.StatefulSet
newAsts *kruiseV1beta1.StatefulSet
}{
{
gss: &gameKruiseV1alpha1.GameServerSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "case0",
},
Spec: gameKruiseV1alpha1.GameServerSetSpec{
GameServerTemplate: gameKruiseV1alpha1.GameServerTemplate{},
},
},
asts: &kruiseV1beta1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "case0",
Annotations: map[string]string{gameKruiseV1alpha1.AstsHashKey: "xx"},
},
Spec: kruiseV1beta1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.PreferredSchedulingTerm{
{
Weight: 1,
Preference: corev1.NodeSelectorTerm{
MatchFields: []corev1.NodeSelectorRequirement{
{
Key: "role",
Operator: corev1.NodeSelectorOpIn,
Values: []string{"test"},
},
},
},
},
},
},
},
},
},
},
},
newAsts: &kruiseV1beta1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "xxx",
Name: "case0",
Annotations: map[string]string{gameKruiseV1alpha1.AstsHashKey: "xxx"},
},
Spec: kruiseV1beta1.StatefulSetSpec{
ScaleStrategy: &kruiseV1beta1.StatefulSetScaleStrategy{
MaxUnavailable: nil,
},
PodManagementPolicy: apps.ParallelPodManagement,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{gameKruiseV1alpha1.GameServerOwnerGssKey: "case0"},
},
Spec: corev1.PodSpec{
ReadinessGates: []corev1.PodReadinessGate{
{
ConditionType: appspub.InPlaceUpdateReady,
},
},
},
},
},
},
},
}
recorder := record.NewFakeRecorder(100)

for _, test := range tests {
objs := []client.Object{test.asts, test.gss}
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
manager := &GameServerSetManager{
gameServerSet: test.gss,
asts: test.asts,
eventRecorder: recorder,
client: c,
}

if err := manager.UpdateWorkload(); err != nil {
t.Error(err)
}

updateAsts := &kruiseV1beta1.StatefulSet{}
if err := manager.client.Get(context.TODO(), types.NamespacedName{
Namespace: test.asts.Namespace,
Name: test.asts.Name,
}, updateAsts); err != nil {
t.Error(err)
}

if !reflect.DeepEqual(updateAsts.Spec, test.newAsts.Spec) {
t.Errorf("expect new asts spec %v but got %v", test.newAsts.Spec, updateAsts.Spec)
}
}
}

0 comments on commit 69babe6

Please sign in to comment.