@@ -18,12 +18,6 @@ package gameserverset
18
18
19
19
import (
20
20
"context"
21
- "k8s.io/apimachinery/pkg/labels"
22
- "k8s.io/client-go/tools/record"
23
- "sort"
24
- "sync"
25
- "time"
26
-
27
21
kruiseV1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
28
22
kruiseV1beta1 "github.com/openkruise/kruise-api/apps/v1beta1"
29
23
corev1 "k8s.io/api/core/v1"
@@ -32,9 +26,13 @@ import (
32
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
27
"k8s.io/apimachinery/pkg/types"
34
28
"k8s.io/apimachinery/pkg/util/json"
29
+ "k8s.io/client-go/tools/record"
35
30
"k8s.io/klog/v2"
36
31
"k8s.io/utils/pointer"
37
32
"sigs.k8s.io/controller-runtime/pkg/client"
33
+ "sort"
34
+ "strconv"
35
+ "sync"
38
36
39
37
gameKruiseV1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
40
38
"github.com/openkruise/kruise-game/pkg/util"
@@ -47,7 +45,6 @@ type Control interface {
47
45
IsNeedToScale () bool
48
46
IsNeedToUpdateWorkload () bool
49
47
SyncPodProbeMarker () error
50
- SyncGameServerReplicas () error
51
48
GetReplicasAfterKilling () * int32
52
49
}
53
50
@@ -129,7 +126,15 @@ func (manager *GameServerSetManager) GameServerScale() error {
129
126
klog .Infof ("GameServers %s/%s already has %d replicas, expect to have %d replicas." , gss .GetNamespace (), gss .GetName (), currentReplicas , expectedReplicas )
130
127
manager .eventRecorder .Eventf (gss , corev1 .EventTypeNormal , ScaleReason , "scale from %d to %d" , currentReplicas , expectedReplicas )
131
128
132
- newReserveIds := computeToScaleGs (gssReserveIds , reserveIds , notExistIds , expectedReplicas , podList , gss .Spec .ScaleStrategy .ScaleDownStrategyType )
129
+ newManageIds , newReserveIds := computeToScaleGs (gssReserveIds , reserveIds , notExistIds , expectedReplicas , podList , gss .Spec .ScaleStrategy .ScaleDownStrategyType )
130
+
131
+ if gss .Spec .GameServerTemplate .ReclaimPolicy == gameKruiseV1alpha1 .DeleteGameServerReclaimPolicy {
132
+ err := SyncGameServer (gss , c , newManageIds , util .GetIndexListFromPodList (podList ))
133
+ if err != nil {
134
+ return err
135
+ }
136
+ }
137
+
133
138
asts .Spec .ReserveOrdinals = newReserveIds
134
139
asts .Spec .Replicas = gss .Spec .Replicas
135
140
asts .Spec .ScaleStrategy = & kruiseV1beta1.StatefulSetScaleStrategy {
@@ -157,7 +162,7 @@ func (manager *GameServerSetManager) GameServerScale() error {
157
162
return nil
158
163
}
159
164
160
- func computeToScaleGs (gssReserveIds , reserveIds , notExistIds []int , expectedReplicas int , pods []corev1.Pod , scaleDownType gameKruiseV1alpha1.ScaleDownStrategyType ) []int {
165
+ func computeToScaleGs (gssReserveIds , reserveIds , notExistIds []int , expectedReplicas int , pods []corev1.Pod , scaleDownType gameKruiseV1alpha1.ScaleDownStrategyType ) ( []int , [] int ) {
161
166
workloadManageIds := util .GetIndexListFromPodList (pods )
162
167
163
168
var toAdd []int
@@ -215,12 +220,13 @@ func computeToScaleGs(gssReserveIds, reserveIds, notExistIds []int, expectedRepl
215
220
}
216
221
}
217
222
223
+ newManageIds := append (workloadManageIds , util .GetSliceInANotInB (toAdd , workloadManageIds )... )
224
+ newManageIds = util .GetSliceInANotInB (newManageIds , toDelete )
225
+
218
226
if scaleDownType == gameKruiseV1alpha1 .ReserveIdsScaleDownStrategyType {
219
- return append (gssReserveIds , util .GetSliceInANotInB (toDelete , gssReserveIds )... )
227
+ return newManageIds , append (gssReserveIds , util .GetSliceInANotInB (toDelete , gssReserveIds )... )
220
228
}
221
229
222
- newManageIds := append (workloadManageIds , util .GetSliceInANotInB (toAdd , workloadManageIds )... )
223
- newManageIds = util .GetSliceInANotInB (newManageIds , toDelete )
224
230
var newReserveIds []int
225
231
if len (newManageIds ) != 0 {
226
232
sort .Ints (newManageIds )
@@ -231,63 +237,80 @@ func computeToScaleGs(gssReserveIds, reserveIds, notExistIds []int, expectedRepl
231
237
}
232
238
}
233
239
234
- return newReserveIds
240
+ return newManageIds , newReserveIds
235
241
}
236
242
237
- func (manager * GameServerSetManager ) SyncGameServerReplicas () error {
238
- gss := manager .gameServerSet
239
- gsList := & gameKruiseV1alpha1.GameServerList {}
240
- err := manager .client .List (context .Background (), gsList , & client.ListOptions {
241
- Namespace : gss .GetNamespace (),
242
- LabelSelector : labels .SelectorFromSet (map [string ]string {
243
- gameKruiseV1alpha1 .GameServerOwnerGssKey : gss .GetName (),
244
- })})
245
- if err != nil {
246
- return err
247
- }
248
- podIds := util .GetIndexListFromPodList (manager .podList )
249
-
250
- gsMap := make (map [int ]int )
251
- deleteIds := make ([]int , 0 )
252
- for id , gs := range gsList .Items {
253
- gsId := util .GetIndexFromGsName (gs .Name )
254
- if ! util .IsNumInList (gsId , podIds ) {
255
- gsMap [gsId ] = id
256
- deleteIds = append (deleteIds , gsId )
257
- }
258
- }
259
-
260
- ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Second )
243
+ func SyncGameServer (gss * gameKruiseV1alpha1.GameServerSet , c client.Client , newManageIds , oldManageIds []int ) error {
244
+ ctx , cancel := context .WithCancel (context .Background ())
261
245
defer cancel ()
262
246
263
- errch := make (chan error , len (deleteIds ))
247
+ addIds := util .GetSliceInANotInB (newManageIds , oldManageIds )
248
+ deleteIds := util .GetSliceInANotInB (oldManageIds , newManageIds )
249
+
250
+ errch := make (chan error , len (addIds )+ len (deleteIds ))
264
251
var wg sync.WaitGroup
265
- for _ , gsId := range deleteIds {
252
+ for _ , gsId := range append ( addIds , deleteIds ... ) {
266
253
wg .Add (1 )
267
254
id := gsId
268
255
go func (ctx context.Context ) {
269
256
defer wg .Done ()
270
257
defer ctx .Done ()
271
258
272
- gs := gsList.Items [gsMap [id ]].DeepCopy ()
273
- gsLabels := make (map [string ]string )
274
- gsLabels [gameKruiseV1alpha1 .GameServerDeletingKey ] = "true"
275
- patchGs := map [string ]interface {}{"metadata" : map [string ]map [string ]string {"labels" : gsLabels }}
276
- patchBytes , err := json .Marshal (patchGs )
259
+ gs := & gameKruiseV1alpha1.GameServer {}
260
+ gsName := gss .Name + "-" + strconv .Itoa (id )
261
+ err := c .Get (ctx , types.NamespacedName {
262
+ Name : gsName ,
263
+ Namespace : gss .Namespace ,
264
+ }, gs )
277
265
if err != nil {
266
+ if errors .IsNotFound (err ) {
267
+ return
268
+ }
278
269
errch <- err
270
+ return
279
271
}
280
- err = manager .client .Patch (context .TODO (), gs , client .RawPatch (types .MergePatchType , patchBytes ))
281
- if err != nil && ! errors .IsNotFound (err ) {
282
- errch <- err
272
+
273
+ if util .IsNumInList (id , addIds ) && gs .GetLabels ()[gameKruiseV1alpha1 .GameServerDeletingKey ] == "true" {
274
+ gsLabels := make (map [string ]string )
275
+ gsLabels [gameKruiseV1alpha1 .GameServerDeletingKey ] = "false"
276
+ patchGs := map [string ]interface {}{"metadata" : map [string ]map [string ]string {"labels" : gsLabels }}
277
+ patchBytes , err := json .Marshal (patchGs )
278
+ if err != nil {
279
+ errch <- err
280
+ return
281
+ }
282
+ err = c .Patch (ctx , gs , client .RawPatch (types .MergePatchType , patchBytes ))
283
+ if err != nil && ! errors .IsNotFound (err ) {
284
+ errch <- err
285
+ return
286
+ }
287
+ klog .Infof ("GameServer %s/%s DeletingKey turn into false" , gss .Namespace , gsName )
283
288
}
289
+
290
+ if util .IsNumInList (id , deleteIds ) && gs .GetLabels ()[gameKruiseV1alpha1 .GameServerDeletingKey ] != "true" {
291
+ gsLabels := make (map [string ]string )
292
+ gsLabels [gameKruiseV1alpha1 .GameServerDeletingKey ] = "true"
293
+ patchGs := map [string ]interface {}{"metadata" : map [string ]map [string ]string {"labels" : gsLabels }}
294
+ patchBytes , err := json .Marshal (patchGs )
295
+ if err != nil {
296
+ errch <- err
297
+ return
298
+ }
299
+ err = c .Patch (ctx , gs , client .RawPatch (types .MergePatchType , patchBytes ))
300
+ if err != nil && ! errors .IsNotFound (err ) {
301
+ errch <- err
302
+ return
303
+ }
304
+ klog .Infof ("GameServer %s/%s DeletingKey turn into true, who will be deleted" , gss .Namespace , gsName )
305
+ }
306
+
284
307
}(ctx )
285
308
}
309
+
286
310
wg .Wait ()
287
311
close (errch )
288
- err = <- errch
312
+ err : = <- errch
289
313
if err != nil {
290
- klog .Errorf ("failed to delete GameServers %s in %s because of %s." , gss .GetName (), gss .GetNamespace (), err .Error ())
291
314
return err
292
315
}
293
316
0 commit comments