Skip to content

Commit 1868ebe

Browse files
committed
support gss TencentCloud-CLB plugin
Signed-off-by: roc <[email protected]>
1 parent 138063a commit 1868ebe

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

cmd/app/setup_controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,13 @@ func SetupControllers(mgr ctrl.Manager) {
9494
setupLog.Error(err, "unable to create controller", "controller", "Node")
9595
os.Exit(1)
9696
}
97+
98+
// GameServerSet controller
99+
if err := (&controller.GameServerSetReconciler{
100+
Client: mgr.GetClient(),
101+
Scheme: mgr.GetScheme(),
102+
}).SetupWithManager(mgr); err != nil {
103+
setupLog.Error(err, "unable to create controller", "controller", "GameServerSet")
104+
os.Exit(1)
105+
}
97106
}

internal/constant/constant.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ const (
1414
ProtocolTCP = "TCP"
1515
ProtocolUDP = "UDP"
1616
ProtocolTCPUDP = "TCPUDP"
17+
OKGNetworkType = "TencentCloud-CLB"
1718
)

internal/controller/gameserverset_controller.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ package controller
1818

1919
import (
2020
"context"
21+
"fmt"
22+
"reflect"
23+
"strconv"
24+
"strings"
2125

26+
networkingv1alpha1 "github.com/imroc/tke-extend-network-controller/api/v1alpha1"
27+
"github.com/imroc/tke-extend-network-controller/internal/constant"
28+
"github.com/imroc/tke-extend-network-controller/pkg/util"
2229
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
30+
"github.com/pkg/errors"
31+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2332
"k8s.io/apimachinery/pkg/runtime"
2433
ctrl "sigs.k8s.io/controller-runtime"
2534
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -41,6 +50,64 @@ func (r *GameServerSetReconciler) Reconcile(ctx context.Context, req ctrl.Reques
4150
}
4251

4352
func (r *GameServerSetReconciler) sync(ctx context.Context, gss *gamekruiseiov1alpha1.GameServerSet) (ctrl.Result, error) {
53+
network := gss.Spec.Network
54+
if network == nil {
55+
return ctrl.Result{}, nil
56+
}
57+
if network.NetworkType != constant.OKGNetworkType {
58+
return ctrl.Result{}, nil
59+
}
60+
var clbIds []string
61+
var startPort uint16 = 30000
62+
var listenerQuota *uint16
63+
for _, param := range network.NetworkConf {
64+
switch param.Name {
65+
case "ClbIds":
66+
clbIds = strings.Split(param.Value, ",")
67+
case "MinPort":
68+
if minPort, err := strconv.Atoi(param.Value); err != nil {
69+
return ctrl.Result{}, errors.WithStack(err)
70+
} else {
71+
startPort = uint16(minPort)
72+
}
73+
case "ListenerQuota":
74+
if quota, err := strconv.Atoi(param.Value); err != nil {
75+
return ctrl.Result{}, errors.WithStack(err)
76+
} else {
77+
listenerQuota = util.GetPtr(uint16(quota))
78+
}
79+
}
80+
}
81+
82+
// 构造期望的 CLBPortPool Spec
83+
ppSpec := networkingv1alpha1.CLBPortPoolSpec{
84+
StartPort: startPort,
85+
ListenerQuota: listenerQuota,
86+
ExsistedLoadBalancerIDs: clbIds,
87+
}
88+
89+
// 确保 CLBPortPool 存在并符合预期
90+
ppName := fmt.Sprintf("%s-%s", gss.Namespace, gss.Name)
91+
pp := &networkingv1alpha1.CLBPortPool{}
92+
if err := r.Get(ctx, client.ObjectKey{Name: ppName}, pp); err != nil {
93+
if apierrors.IsNotFound(err) { // 不存在,创建一个
94+
pp.Name = ppName
95+
pp.Spec = ppSpec
96+
if err := r.Create(ctx, pp); err != nil {
97+
return ctrl.Result{}, errors.WithStack(err)
98+
}
99+
return ctrl.Result{}, nil
100+
}
101+
return ctrl.Result{}, errors.WithStack(err)
102+
}
103+
// 存在,检查是否符合预期
104+
if !reflect.DeepEqual(pp.Spec, ppSpec) { // 不符合预期,更新
105+
pp.Spec = ppSpec
106+
if err := r.Update(ctx, pp); err != nil {
107+
return ctrl.Result{}, errors.WithStack(err)
108+
}
109+
}
110+
// 全部符合预期,忽略
44111
return ctrl.Result{}, nil
45112
}
46113

0 commit comments

Comments
 (0)