@@ -40,13 +40,36 @@ type GameServerSetReconciler struct {
4040 Scheme * runtime.Scheme
4141}
4242
43- // +kubebuilder:rbac:groups=game.kruise.io,resources=gameserversets,verbs=get;list;watch
43+ // +kubebuilder:rbac:groups=game.kruise.io,resources=gameserversets,verbs=get;list;watch;update;patch
4444// +kubebuilder:rbac:groups=game.kruise.io,resources=gameserversets/status,verbs=get
4545
4646// Reconcile is part of the main kubernetes reconciliation loop which aims to
4747// move the current state of the cluster closer to the desired state.
4848func (r * GameServerSetReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
49- return Reconcile (ctx , req , r .Client , & gamekruiseiov1alpha1.GameServerSet {}, r .sync )
49+ return ReconcileWithFinalizer (ctx , req , r .Client , & gamekruiseiov1alpha1.GameServerSet {}, r .sync , r .cleanup )
50+ }
51+
52+ func (r * GameServerSetReconciler ) cleanup (ctx context.Context , gss * gamekruiseiov1alpha1.GameServerSet ) (ctrl.Result , error ) {
53+ ppName := getPortPoolName (gss )
54+ pp := & networkingv1alpha1.CLBPortPool {}
55+ if err := r .Get (ctx , client.ObjectKey {Name : ppName }, pp ); err != nil {
56+ if apierrors .IsNotFound (err ) { // 已经删除,忽略
57+ return ctrl.Result {}, nil
58+ }
59+ return ctrl.Result {}, errors .WithStack (err )
60+ }
61+ if ! pp .DeletionTimestamp .IsZero () { // 正在删除,忽略
62+ return ctrl.Result {}, nil
63+ }
64+ // 还未删除,发起删除请求
65+ if err := r .Delete (ctx , pp ); err != nil {
66+ return ctrl.Result {}, errors .WithStack (err )
67+ }
68+ return ctrl.Result {}, nil
69+ }
70+
71+ func getPortPoolName (gss * gamekruiseiov1alpha1.GameServerSet ) string {
72+ return fmt .Sprintf ("%s-%s" , gss .Namespace , gss .Name )
5073}
5174
5275func (r * GameServerSetReconciler ) sync (ctx context.Context , gss * gamekruiseiov1alpha1.GameServerSet ) (ctrl.Result , error ) {
@@ -87,7 +110,7 @@ func (r *GameServerSetReconciler) sync(ctx context.Context, gss *gamekruiseiov1a
87110 }
88111
89112 // 确保 CLBPortPool 存在并符合预期
90- ppName := fmt . Sprintf ( "%s-%s" , gss . Namespace , gss . Name )
113+ ppName := getPortPoolName ( gss )
91114 pp := & networkingv1alpha1.CLBPortPool {}
92115 if err := r .Get (ctx , client.ObjectKey {Name : ppName }, pp ); err != nil {
93116 if apierrors .IsNotFound (err ) { // 不存在,创建一个
0 commit comments