From 26fcaf7889389eb253565ee74ff5d581ab7f19e7 Mon Sep 17 00:00:00 2001 From: ChrisLiu <70144550+chrisliu1995@users.noreply.github.com> Date: Fri, 16 Aug 2024 10:44:26 +0800 Subject: [PATCH] feat: add lifecycle field for gameserverset (#162) Signed-off-by: ChrisLiu --- apis/v1alpha1/gameserver_types.go | 16 ++++--- apis/v1alpha1/gameserverset_types.go | 1 + apis/v1alpha1/zz_generated.deepcopy.go | 5 +++ .../bases/game.kruise.io_gameserversets.yaml | 43 +++++++++++++++++++ .../gameserver/gameserver_manager.go | 13 +++++- pkg/util/gameserver.go | 16 ++++--- 6 files changed, 81 insertions(+), 13 deletions(-) diff --git a/apis/v1alpha1/gameserver_types.go b/apis/v1alpha1/gameserver_types.go index 2befbf26..eb996f87 100644 --- a/apis/v1alpha1/gameserver_types.go +++ b/apis/v1alpha1/gameserver_types.go @@ -61,13 +61,15 @@ type GameServerContainer struct { type GameServerState string const ( - Unknown GameServerState = "Unknown" - Creating GameServerState = "Creating" - Ready GameServerState = "Ready" - NotReady GameServerState = "NotReady" - Crash GameServerState = "Crash" - Updating GameServerState = "Updating" - Deleting GameServerState = "Deleting" + Unknown GameServerState = "Unknown" + Creating GameServerState = "Creating" + Ready GameServerState = "Ready" + NotReady GameServerState = "NotReady" + Crash GameServerState = "Crash" + Updating GameServerState = "Updating" + Deleting GameServerState = "Deleting" + PreDelete GameServerState = "PreDelete" + PreUpdate GameServerState = "PreUpdate" ) type OpsState string diff --git a/apis/v1alpha1/gameserverset_types.go b/apis/v1alpha1/gameserverset_types.go index a863c757..052d397f 100644 --- a/apis/v1alpha1/gameserverset_types.go +++ b/apis/v1alpha1/gameserverset_types.go @@ -57,6 +57,7 @@ type GameServerSetSpec struct { UpdateStrategy UpdateStrategy `json:"updateStrategy,omitempty"` ScaleStrategy ScaleStrategy `json:"scaleStrategy,omitempty"` Network *Network `json:"network,omitempty"` + Lifecycle *appspub.Lifecycle `json:"lifecycle,omitempty"` } type GameServerTemplate struct { diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index fa43487d..21b29499 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -207,6 +207,11 @@ func (in *GameServerSetSpec) DeepCopyInto(out *GameServerSetSpec) { *out = new(Network) (*in).DeepCopyInto(*out) } + if in.Lifecycle != nil { + in, out := &in.Lifecycle, &out.Lifecycle + *out = new(pub.Lifecycle) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSetSpec. diff --git a/config/crd/bases/game.kruise.io_gameserversets.yaml b/config/crd/bases/game.kruise.io_gameserversets.yaml index fb8dddac..391c8262 100644 --- a/config/crd/bases/game.kruise.io_gameserversets.yaml +++ b/config/crd/bases/game.kruise.io_gameserversets.yaml @@ -383,6 +383,49 @@ spec: type: array type: object x-kubernetes-preserve-unknown-fields: true + lifecycle: + description: Lifecycle contains the hooks for Pod lifecycle. + properties: + inPlaceUpdate: + description: InPlaceUpdate is the hook before Pod to update and + after Pod has been updated. + properties: + finalizersHandler: + items: + type: string + type: array + labelsHandler: + additionalProperties: + type: string + type: object + markPodNotReady: + description: 'MarkPodNotReady = true means: - Pod will be + set to ''NotReady'' at preparingDelete/preparingUpdate state. + - Pod will be restored to ''Ready'' at Updated state if + it was set to ''NotReady'' at preparingUpdate state. Default + to false.' + type: boolean + type: object + preDelete: + description: PreDelete is the hook before Pod to be deleted. + properties: + finalizersHandler: + items: + type: string + type: array + labelsHandler: + additionalProperties: + type: string + type: object + markPodNotReady: + description: 'MarkPodNotReady = true means: - Pod will be + set to ''NotReady'' at preparingDelete/preparingUpdate state. + - Pod will be restored to ''Ready'' at Updated state if + it was set to ''NotReady'' at preparingUpdate state. Default + to false.' + type: boolean + type: object + type: object network: properties: networkConf: diff --git a/pkg/controllers/gameserver/gameserver_manager.go b/pkg/controllers/gameserver/gameserver_manager.go index 44f6e88a..3e0bfe00 100644 --- a/pkg/controllers/gameserver/gameserver_manager.go +++ b/pkg/controllers/gameserver/gameserver_manager.go @@ -130,10 +130,21 @@ func (manager GameServerManager) SyncGsToPod() error { case corev1.PodRunning: // GameServer Updating lifecycleState, exist := pod.GetLabels()[kruisePub.LifecycleStateKey] - if exist && (lifecycleState == string(kruisePub.LifecycleStateUpdating) || lifecycleState == string(kruisePub.LifecycleStatePreparingUpdate)) { + if exist && lifecycleState == string(kruisePub.LifecycleStateUpdating) { gsState = gameKruiseV1alpha1.Updating break } + // GameServer PreUpdate + if exist && lifecycleState == string(kruisePub.LifecycleStatePreparingUpdate) { + gsState = gameKruiseV1alpha1.PreUpdate + break + } + // GameServer PreDelete + if exist && lifecycleState == string(kruisePub.LifecycleStatePreparingDelete) { + gsState = gameKruiseV1alpha1.PreDelete + break + } + // GameServer Deleting if !pod.DeletionTimestamp.IsZero() { gsState = gameKruiseV1alpha1.Deleting diff --git a/pkg/util/gameserver.go b/pkg/util/gameserver.go index f72eb649..2f8e013f 100644 --- a/pkg/util/gameserver.go +++ b/pkg/util/gameserver.go @@ -136,14 +136,20 @@ func GetNewAstsFromGss(gss *gameKruiseV1alpha1.GameServerSet, asts *kruiseV1beta readinessGates = append(readinessGates, corev1.PodReadinessGate{ConditionType: appspub.InPlaceUpdateReady}) asts.Spec.Template.Spec.ReadinessGates = readinessGates + // set Lifecycle + asts.Spec.Lifecycle = gss.Spec.Lifecycle // AllowNotReadyContainers if gss.Spec.Network != nil && IsAllowNotReadyContainers(gss.Spec.Network.NetworkConf) { - // set lifecycle - asts.Spec.Lifecycle = &appspub.Lifecycle{ - InPlaceUpdate: &appspub.LifecycleHook{ - LabelsHandler: map[string]string{gameKruiseV1alpha1.InplaceUpdateNotReadyBlocker: "true"}, - }, + if asts.Spec.Lifecycle == nil { + asts.Spec.Lifecycle = &appspub.Lifecycle{} + } + if asts.Spec.Lifecycle.InPlaceUpdate == nil { + asts.Spec.Lifecycle.InPlaceUpdate = &appspub.LifecycleHook{} + } + if asts.Spec.Lifecycle.InPlaceUpdate.LabelsHandler == nil { + asts.Spec.Lifecycle.InPlaceUpdate.LabelsHandler = make(map[string]string) } + asts.Spec.Lifecycle.InPlaceUpdate.LabelsHandler[gameKruiseV1alpha1.InplaceUpdateNotReadyBlocker] = "true" } // set VolumeClaimTemplates