Skip to content

Commit 23c2b27

Browse files
committed
cleanup CLBPortPool when gss was deleted
Signed-off-by: roc <roc@imroc.cc>
1 parent c5c59ff commit 23c2b27

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

charts/tke-extend-network-controller/templates/role.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ rules:
164164
verbs:
165165
- get
166166
- list
167+
- patch
168+
- update
167169
- watch
168170
- apiGroups:
169171
- game.kruise.io

config/rbac/role.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ rules:
9393
verbs:
9494
- get
9595
- list
96+
- patch
97+
- update
9698
- watch
9799
- apiGroups:
98100
- game.kruise.io

internal/controller/gameserverset_controller.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4848
func (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

5275
func (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

Comments
 (0)