Skip to content

Commit

Permalink
feat: user-defined GameServers' lifecycle
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 committed Dec 26, 2023
1 parent 250bd86 commit bb178b3
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 161 deletions.
14 changes: 14 additions & 0 deletions apis/v1alpha1/gameserverset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,22 @@ type GameServerTemplate struct {
// +kubebuilder:validation:Schemaless
corev1.PodTemplateSpec `json:",inline"`
VolumeClaimTemplates []corev1.PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
// LifecyclePolicyType indicates the policy for GameServer creation and deletion.
LifecyclePolicyType LifecyclePolicyType `json:"lifecyclePolicyType,omitempty"`
}

type LifecyclePolicyType string

const (
// FollowByPodLifecyclePolicyType indicates that GameServer is created when the pod is created
// and deleted when the pod is deleted. The age of GameServer is exactly the same as that of the pod.
FollowByPodLifecyclePolicyType LifecyclePolicyType = "FollowByPod"
// WhenScaledLifecyclePolicyType indicates that GameServers will be created when replicas of GameServerSet
// increases and will be deleted when replicas of GameServerSet decreases. The GameServer will not be deleted
// when the corresponding pod is deleted due to manual deletion, update, eviction, etc.
WhenScaledLifecyclePolicyType LifecyclePolicyType = "WhenScaled"
)

type Network struct {
NetworkType string `json:"networkType,omitempty"`
NetworkConf []NetworkConfParams `json:"networkConf,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/game.kruise.io_gameserversets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ spec:
description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Important: Run "make" to regenerate code after modifying this file'
properties:
lifecyclePolicyType:
description: LifecyclePolicyType indicates the policy for GameServer
creation and deletion.
type: string
volumeClaimTemplates:
items:
description: PersistentVolumeClaim is a user's request for and
Expand Down
14 changes: 14 additions & 0 deletions docs/en/user_manuals/CRD_field_description.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,22 @@ type GameServerTemplate struct {

// Requests and claims for persistent volumes.
VolumeClaimTemplates []corev1.PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`

// LifecyclePolicyType indicates the policy for GameServer creation and deletion.
LifecyclePolicyType LifecyclePolicyType `json:"lifecyclePolicyType,omitempty"`
}

type LifecyclePolicyType string

// FollowByPodLifecyclePolicyType indicates that GameServer is created when the pod is created
// and deleted when the pod is deleted. The age of GameServer is exactly the same as that of the pod.
FollowByPodLifecyclePolicyType LifecyclePolicyType = "FollowByPod"

// WhenScaledLifecyclePolicyType indicates that GameServers will be created when replicas of GameServerSet
// increases and will be deleted when replicas of GameServerSet decreases. The GameServer will not be deleted
// when the corresponding pod is deleted due to manual deletion, update, eviction, etc.
WhenScaledLifecyclePolicyType LifecyclePolicyType = "WhenScaled"

```

#### UpdateStrategy
Expand Down
14 changes: 14 additions & 0 deletions docs/中文/用户手册/CRD字段说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,21 @@ type GameServerTemplate struct {
// 对持久卷的请求和声明
VolumeClaimTemplates []corev1.PersistentVolumeClaim `json:"volumeClaimTemplates,omitempty"`
// LifecyclePolicyType 表明GameServer创建与删除的策略
LifecyclePolicyType LifecyclePolicyType `json:"lifecyclePolicyType,omitempty"`
}
type LifecyclePolicyType string
// FollowByPodLifecyclePolicyType 表示创建 pod 时创建 GameServer, 并在删除 pod 时删除。
// 此策略类型下,GameServer 的年龄与 pod 的年龄完全相同。
FollowByPodLifecyclePolicyType LifecyclePolicyType = "FollowByPod"
// WhenScaledLifecyclePolicyType 表示当 GameServerSet 的副本增加时将创建 GameServers,当 GameServerSet 的副本减少时将被删除。
// 由于手动删除、更新、驱逐等原因删除相应的 pod 时,GameServer 不会被删除。
WhenScaledLifecyclePolicyType LifecyclePolicyType = "WhenScaled"
```

### UpdateStrategy
Expand Down
79 changes: 26 additions & 53 deletions pkg/controllers/gameserver/gameserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -173,8 +172,15 @@ func (r *GameServerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

if podFound && !gsFound {
err := r.initGameServer(pod)
if err != nil && !errors.IsAlreadyExists(err) && !errors.IsNotFound(err) {
gss, err := r.getGameServerSet(pod)
if err != nil {
if errors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err

Check warning on line 180 in pkg/controllers/gameserver/gameserver_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_controller.go#L175-L180

Added lines #L175 - L180 were not covered by tests
}
err = r.initGameServerByPod(gss, pod)
if err != nil && !errors.IsAlreadyExists(err) {

Check warning on line 183 in pkg/controllers/gameserver/gameserver_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_controller.go#L182-L183

Added lines #L182 - L183 were not covered by tests
klog.Errorf("failed to create GameServer %s in %s, because of %s.", namespacedName.Name, namespacedName.Namespace, err.Error())
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -237,57 +243,24 @@ func (r *GameServerReconciler) getGameServerSet(pod *corev1.Pod) (*gamekruiseiov
return gss, err
}

func (r *GameServerReconciler) initGameServer(pod *corev1.Pod) error {
gs := &gamekruiseiov1alpha1.GameServer{}
gs.Name = pod.GetName()
gs.Namespace = pod.GetNamespace()

// set owner reference
gss, err := r.getGameServerSet(pod)
if err != nil {
return err
}
ors := make([]metav1.OwnerReference, 0)
or := metav1.OwnerReference{
APIVersion: gss.APIVersion,
Kind: gss.Kind,
Name: gss.GetName(),
UID: gss.GetUID(),
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(true),
}
ors = append(ors, or)
gs.OwnerReferences = ors

// set Labels
gsLabels := gss.Spec.GameServerTemplate.GetLabels()
if gsLabels == nil {
gsLabels = make(map[string]string)
}
gsLabels[gamekruiseiov1alpha1.GameServerOwnerGssKey] = gss.GetName()
gs.SetLabels(gsLabels)

// set Annotations
gsAnnotations := gss.Spec.GameServerTemplate.GetAnnotations()
if gsAnnotations == nil {
gsAnnotations = make(map[string]string)
func (r *GameServerReconciler) initGameServerByPod(gss *gamekruiseiov1alpha1.GameServerSet, pod *corev1.Pod) error {
// default fields
gs := util.InitGameServer(gss, pod.Name)

if gss.Spec.GameServerTemplate.LifecyclePolicyType == gamekruiseiov1alpha1.FollowByPodLifecyclePolicyType || gss.Spec.GameServerTemplate.LifecyclePolicyType == "" {
// rewrite ownerReferences
ors := make([]metav1.OwnerReference, 0)
or := metav1.OwnerReference{
APIVersion: pod.APIVersion,
Kind: pod.Kind,
Name: pod.GetName(),
UID: pod.GetUID(),
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(true),
}
ors = append(ors, or)
gs.OwnerReferences = ors

Check warning on line 262 in pkg/controllers/gameserver/gameserver_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_controller.go#L246-L262

Added lines #L246 - L262 were not covered by tests
}
gsAnnotations[gamekruiseiov1alpha1.GsTemplateMetadataHashKey] = util.GetGsTemplateMetadataHash(gss)
gs.SetAnnotations(gsAnnotations)

// set NetWork
gs.Spec.NetworkDisabled = false

// set OpsState
gs.Spec.OpsState = gamekruiseiov1alpha1.None

// set UpdatePriority
updatePriority := intstr.FromInt(0)
gs.Spec.UpdatePriority = &updatePriority

// set deletionPriority
deletionPriority := intstr.FromInt(0)
gs.Spec.DeletionPriority = &deletionPriority

return r.Client.Create(context.Background(), gs)
}
15 changes: 9 additions & 6 deletions pkg/controllers/gameserverset/gameserverset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ func (r *GameServerSetReconciler) Reconcile(ctx context.Context, req ctrl.Reques
err = r.Get(ctx, namespacedName, asts)
if err != nil {
if errors.IsNotFound(err) {
// init GameServers when owner is GameServerSet
if gss.Spec.GameServerTemplate.LifecyclePolicyType == gamekruiseiov1alpha1.WhenScaledLifecyclePolicyType {
newManageIds, _ := computeToScaleGs(gss.Spec.ReserveGameServerIds, []int{}, []int{}, int(*gss.Spec.Replicas), []corev1.Pod{}, gamekruiseiov1alpha1.GeneralScaleDownStrategyType)
err := SyncGameServer(gss, r.Client, newManageIds, []int{})
if err != nil {
klog.Errorf("failed to sync GameServers %s in %s,because of %s.", namespacedName.Name, namespacedName.Namespace, err.Error())
return reconcile.Result{}, err
}

Check warning on line 190 in pkg/controllers/gameserverset/gameserverset_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserverset/gameserverset_controller.go#L183-L190

Added lines #L183 - L190 were not covered by tests
}
err = r.initAsts(gss)
if err != nil {
klog.Errorf("failed to create advanced statefulset %s in %s,because of %s.", namespacedName.Name, namespacedName.Namespace, err.Error())
Expand Down Expand Up @@ -252,12 +261,6 @@ func (r *GameServerSetReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return reconcile.Result{}, err
}

err = gsm.SyncGameServerReplicas()
if err != nil {
klog.Errorf("GameServerSet %s failed to adjust the replicas of GameServers to match that of Pods in %s, because of %s.", namespacedName.Name, namespacedName.Namespace, err.Error())
return reconcile.Result{}, err
}

return ctrl.Result{}, nil
}

Expand Down
Loading

0 comments on commit bb178b3

Please sign in to comment.