Skip to content

Commit

Permalink
bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkanatsios committed Mar 9, 2022
1 parent a922f93 commit 2f435c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pkg/operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ kind: Kustomization
images:
- name: controller
newName: thundernetes-operator
newTag: ac1c91a
newTag: 8047add
66 changes: 33 additions & 33 deletions pkg/operator/controllers/gameserverbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,45 +167,20 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// user has decreased standingBy numbers
if nonActiveGameServersCount > gsb.Spec.StandingBy {
totalNumberOfGameServersToDelete := int(math.Min(float64(nonActiveGameServersCount-gsb.Spec.StandingBy), maxNumberOfGameServersToDelete))
deletedGameServersCount := 0
for i := 0; i < int(totalNumberOfGameServersToDelete); i++ {
gs := gameServers.Items[i]
// we're deleting only initializing/pending/standingBy servers, never touching active
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateInitializing || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
if err := r.deleteGameServer(ctx, &gs); err != nil {
if apierrors.IsConflict(err) { // this GameServer has been updated, skip it
continue
}
return ctrl.Result{}, err
}
deletedGameServersCount = deletedGameServersCount + 1
GameServersDeletedCounter.WithLabelValues(gsb.Name).Inc()
addGameServerToUnderDeletionMap(gsb.Name, gs.Name)
r.Recorder.Eventf(&gsb, corev1.EventTypeNormal, "GameServer deleted", "GameServer %s deleted", gs.Name)
}
}
if deletedGameServersCount < totalNumberOfGameServersToDelete || nonActiveGameServersCount-gsb.Spec.StandingBy > maxNumberOfGameServersToDelete {
log.Info("unable to delete enough gameServers, requeuing", "nonActiveGameServersCount", nonActiveGameServersCount, "gsb.Spec.StandingBy", gsb.Spec.StandingBy, "maxNumberOfGameServersToDelete", maxNumberOfGameServersToDelete)
return ctrl.Result{Requeue: true}, nil
err := r.deleteNonActiveGameServers(ctx, &gsb, &gameServers, totalNumberOfGameServersToDelete)
if err != nil {
return ctrl.Result{}, err
}
}

// we need to check if we are above the max
// this can happen if the user modifies the spec.Max during the GameServerBuild's lifetime
if nonActiveGameServersCount+activeCount > gsb.Spec.Max {
for i := 0; i < nonActiveGameServersCount+activeCount-gsb.Spec.Max && i < maxNumberOfGameServersToDelete; i++ {
gs := gameServers.Items[i]
// we're deleting only standingBy or initializing servers
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateInitializing || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
if err := r.deleteGameServer(ctx, &gs); err != nil {
if apierrors.IsConflict(err) { // this GameServer has been updated, skip it
continue
}
return ctrl.Result{}, err
}
GameServersDeletedCounter.WithLabelValues(gsb.Name).Inc()
addGameServerToUnderDeletionMap(gsb.Name, gs.Name)
}
totalNumberOfGameServersToDelete := int(math.Min(float64(nonActiveGameServersCount+activeCount-gsb.Spec.Max), maxNumberOfGameServersToDelete))
log.Info("Deleting non-active game servers", "totalNumberOfGameServersToDelete", totalNumberOfGameServersToDelete)
err := r.deleteNonActiveGameServers(ctx, &gsb, &gameServers, totalNumberOfGameServersToDelete)
if err != nil {
return ctrl.Result{}, err
}
}

Expand Down Expand Up @@ -380,6 +355,31 @@ func getKeyForCrashesPerBuildMap(gsb *mpsv1alpha1.GameServerBuild) string {
return fmt.Sprintf("%s/%s", gsb.Namespace, gsb.Name)
}

// deleteNonActiveGameServers loops through all the GameServers CRs and deletes non-Active ones
func (r *GameServerBuildReconciler) deleteNonActiveGameServers(ctx context.Context,
gsb *mpsv1alpha1.GameServerBuild,
gameServers *mpsv1alpha1.GameServerList,
totalNumberOfGameServersToDelete int) error {
deletedGameServersCount := 0
for i := 0; i < len(gameServers.Items) && deletedGameServersCount < totalNumberOfGameServersToDelete; i++ {
gs := gameServers.Items[i]
// we're deleting only initializing/pending/standingBy servers, never touching active
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateInitializing || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
if err := r.deleteGameServer(ctx, &gs); err != nil {
if apierrors.IsConflict(err) { // this GameServer has been updated, skip it
continue
}
return err
}
deletedGameServersCount = deletedGameServersCount + 1
GameServersDeletedCounter.WithLabelValues(gsb.Name).Inc()
addGameServerToUnderDeletionMap(gsb.Name, gs.Name)
r.Recorder.Eventf(gsb, corev1.EventTypeNormal, "GameServer deleted", "GameServer %s deleted", gs.Name)
}
}
return nil
}

// deleteGameServer deletes the provided GameServer
func (r *GameServerBuildReconciler) deleteGameServer(ctx context.Context, gs *mpsv1alpha1.GameServer) error {
// we're requesting the GameServer to be deleted to have the same ResourceVersion
Expand Down

0 comments on commit 2f435c7

Please sign in to comment.