Skip to content

Commit 02d26c4

Browse files
committed
feat: add Resource Manager client functionality and integrate into reconciler
Signed-off-by: okjiang <819421878@qq.com>
1 parent 2633cb1 commit 02d26c4

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

cmd/tidb-operator/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import (
7878
"github.com/pingcap/tidb-operator/v2/pkg/metrics"
7979
"github.com/pingcap/tidb-operator/v2/pkg/scheme"
8080
pdm "github.com/pingcap/tidb-operator/v2/pkg/timanager/pd"
81+
rmm "github.com/pingcap/tidb-operator/v2/pkg/timanager/resourcemanager"
8182
fm "github.com/pingcap/tidb-operator/v2/pkg/timanager/tiflash"
8283
tsom "github.com/pingcap/tidb-operator/v2/pkg/timanager/tso"
8384
"github.com/pingcap/tidb-operator/v2/pkg/utils/informertest"
@@ -242,6 +243,7 @@ func setup(ctx context.Context, mgr ctrl.Manager) error {
242243
logger.Info("setup client manager")
243244
pdcm := pdm.NewPDClientManager(mgr.GetLogger(), c)
244245
tsocm := tsom.NewTSOClientManager(mgr.GetLogger(), c)
246+
rmcm := rmm.NewResourceManagerClientManager(mgr.GetLogger(), c)
245247
fcm := fm.NewTiFlashClientManager(mgr.GetLogger(), c)
246248

247249
logger.Info("setup volume modifier")
@@ -250,7 +252,7 @@ func setup(ctx context.Context, mgr ctrl.Manager) error {
250252
am := adoption.New(ctrl.Log.WithName("adoption"))
251253
tf := tracker.New()
252254
setupLog.Info("setup controllers")
253-
if err := setupControllers(mgr, c, pdcm, tsocm, fcm, vm, tf, am); err != nil {
255+
if err := setupControllers(mgr, c, pdcm, tsocm, rmcm, fcm, vm, tf, am); err != nil {
254256
setupLog.Error(err, "unable to setup controllers")
255257
os.Exit(1)
256258
}
@@ -262,6 +264,7 @@ func setup(ctx context.Context, mgr ctrl.Manager) error {
262264
logger.Info("start client manager")
263265
pdcm.Start(ctx)
264266
tsocm.Start(ctx)
267+
rmcm.Start(ctx)
265268

266269
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
267270
return fmt.Errorf("unable to set up health check: %w", err)
@@ -380,6 +383,7 @@ func setupControllers(
380383
c client.Client,
381384
pdcm pdm.PDClientManager,
382385
tsocm tsom.TSOClientManager,
386+
rmcm rmm.ResourceManagerClientManager,
383387
fcm fm.TiFlashClientManager,
384388
vm volumes.ModifierFactory,
385389
tf tracker.Factory,
@@ -479,7 +483,7 @@ func setupControllers(
479483
{
480484
name: "ResourceManagerGroup",
481485
setupFunc: func() error {
482-
return resourcemanagergroup.Setup(mgr, c, tf.AllocateFactory("resourcemanager"))
486+
return resourcemanagergroup.Setup(mgr, c, rmcm, tf.AllocateFactory("resourcemanager"))
483487
},
484488
},
485489
{

pkg/controllers/resourcemanagergroup/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task
5252
common.TaskStatusPersister[scope.ResourceManagerGroup](state, r.Client),
5353
),
5454

55+
tasks.TaskContextRMClient(state, r.RMClientManager),
56+
5557
common.TaskRevision[runtime.ResourceManagerGroupTuple](state, r.Client),
5658
tasks.TaskService(state, r.Client),
5759
tasks.TaskUpdater(state, r.Client, r.AllocateFactory),

pkg/controllers/resourcemanagergroup/controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
3737
"github.com/pingcap/tidb-operator/v2/pkg/client"
3838
"github.com/pingcap/tidb-operator/v2/pkg/controllers/resourcemanagergroup/tasks"
39+
rmm "github.com/pingcap/tidb-operator/v2/pkg/timanager/resourcemanager"
3940
"github.com/pingcap/tidb-operator/v2/pkg/utils/k8s"
4041
"github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3"
4142
"github.com/pingcap/tidb-operator/v2/pkg/utils/tracker"
@@ -45,13 +46,15 @@ type Reconciler struct {
4546
Logger logr.Logger
4647
Client client.Client
4748
AllocateFactory tracker.AllocateFactory
49+
RMClientManager rmm.ResourceManagerClientManager
4850
}
4951

50-
func Setup(mgr manager.Manager, c client.Client, af tracker.AllocateFactory) error {
52+
func Setup(mgr manager.Manager, c client.Client, rmcm rmm.ResourceManagerClientManager, af tracker.AllocateFactory) error {
5153
r := &Reconciler{
5254
Logger: mgr.GetLogger().WithName("ResourceManagerGroup"),
5355
Client: c,
5456
AllocateFactory: af,
57+
RMClientManager: rmcm,
5558
}
5659
return ctrl.NewControllerManagedBy(mgr).
5760
For(&v1alpha1.ResourceManagerGroup{}).

pkg/controllers/resourcemanagergroup/tasks/ctx.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414

1515
package tasks
1616

17+
import (
18+
"context"
19+
20+
"github.com/pingcap/tidb-operator/v2/pkg/timanager/resourcemanager"
21+
"github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3"
22+
)
23+
1724
type ReconcileContext struct {
1825
State
1926
}
27+
28+
func TaskContextRMClient(state *ReconcileContext, m resourcemanager.ResourceManagerClientManager) task.Task {
29+
return task.NameTaskFunc("ContextRMClient", func(_ context.Context) task.Result {
30+
if err := m.Register(state.Object()); err != nil {
31+
return task.Fail().With("cannot register resource manager client: %v", err)
32+
}
33+
return task.Complete().With("resource manager client is registered")
34+
})
35+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package resourcemanager
16+
17+
import (
18+
"context"
19+
"crypto/tls"
20+
"fmt"
21+
"time"
22+
23+
"github.com/go-logr/logr"
24+
25+
"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
26+
"github.com/pingcap/tidb-operator/v2/pkg/apicall"
27+
coreutil "github.com/pingcap/tidb-operator/v2/pkg/apiutil/core/v1alpha1"
28+
"github.com/pingcap/tidb-operator/v2/pkg/client"
29+
"github.com/pingcap/tidb-operator/v2/pkg/resourcemanagerapi"
30+
"github.com/pingcap/tidb-operator/v2/pkg/runtime/scope"
31+
"github.com/pingcap/tidb-operator/v2/pkg/timanager"
32+
)
33+
34+
const (
35+
resourceManagerRequestTimeout = 10 * time.Second
36+
)
37+
38+
type ResourceManagerClientManager = timanager.Manager[*v1alpha1.ResourceManagerGroup, ResourceManagerClient]
39+
40+
type ResourceManagerClient interface {
41+
Underlay() resourcemanagerapi.Client
42+
}
43+
44+
type resourceManagerClient struct {
45+
underlay resourcemanagerapi.Client
46+
}
47+
48+
func (c *resourceManagerClient) Underlay() resourcemanagerapi.Client {
49+
return c.underlay
50+
}
51+
52+
func NewClient(
53+
_ *v1alpha1.ResourceManagerGroup,
54+
underlay resourcemanagerapi.Client,
55+
_ timanager.SharedInformerFactory[resourcemanagerapi.Client],
56+
) (ResourceManagerClient, error) {
57+
return &resourceManagerClient{underlay: underlay}, nil
58+
}
59+
60+
// CacheKeys returns the keys of the ResourceManagerGroup.
61+
// If any keys are changed, client will be renewed.
62+
// The first key is primary key to get client from manager.
63+
func CacheKeys(rmg *v1alpha1.ResourceManagerGroup) ([]string, error) {
64+
keys := []string{
65+
// cluster name as primary key
66+
timanager.PrimaryKey(rmg.Namespace, rmg.Spec.Cluster.Name),
67+
rmg.Name,
68+
string(rmg.GetUID()),
69+
}
70+
71+
return keys, nil
72+
}
73+
74+
func NewUnderlayClientFunc(c client.Client) timanager.NewUnderlayClientFunc[*v1alpha1.ResourceManagerGroup, resourcemanagerapi.Client] {
75+
return func(rmg *v1alpha1.ResourceManagerGroup) (resourcemanagerapi.Client, error) {
76+
ctx, cancel := context.WithTimeout(context.Background(), resourceManagerRequestTimeout)
77+
defer cancel()
78+
79+
var cluster v1alpha1.Cluster
80+
if err := c.Get(ctx, client.ObjectKey{
81+
Name: rmg.Spec.Cluster.Name,
82+
Namespace: rmg.Namespace,
83+
}, &cluster); err != nil {
84+
return nil, fmt.Errorf("cannot find cluster %s: %w", rmg.Spec.Cluster.Name, err)
85+
}
86+
87+
scheme := "http"
88+
var tlsConfig *tls.Config
89+
if coreutil.IsTLSClusterEnabled(&cluster) {
90+
cfg, err := apicall.GetClientTLSConfig(ctx, c, &cluster)
91+
if err != nil {
92+
return nil, fmt.Errorf("cannot get tls config from secret: %w", err)
93+
}
94+
tlsConfig = cfg
95+
scheme = "https"
96+
}
97+
98+
svcName := coreutil.HeadlessServiceName[scope.ResourceManagerGroup](rmg)
99+
url := fmt.Sprintf("%s://%s.%s.svc:%d", scheme, svcName, rmg.Namespace, coreutil.ResourceManagerGroupClientPort(rmg))
100+
return resourcemanagerapi.NewClient(url, resourceManagerRequestTimeout, tlsConfig), nil
101+
}
102+
}
103+
104+
func NewResourceManagerClientManager(logger logr.Logger, c client.Client) ResourceManagerClientManager {
105+
m := timanager.NewManagerBuilder[*v1alpha1.ResourceManagerGroup, resourcemanagerapi.Client, ResourceManagerClient]().
106+
WithLogger(logger).
107+
WithNewUnderlayClientFunc(NewUnderlayClientFunc(c)).
108+
WithNewClientFunc(NewClient).
109+
WithCacheKeysFunc(CacheKeys).
110+
Build()
111+
112+
return m
113+
}

0 commit comments

Comments
 (0)