diff --git a/pkg/apiutil/core/v1alpha1/group.go b/pkg/apiutil/core/v1alpha1/group.go index 2a3a779409..25a70116a7 100644 --- a/pkg/apiutil/core/v1alpha1/group.go +++ b/pkg/apiutil/core/v1alpha1/group.go @@ -20,14 +20,6 @@ import ( "github.com/pingcap/tidb-operator/pkg/runtime/scope" ) -func Cluster[ - S scope.Object[F, T], - F client.Object, - T runtime.Object, -](f F) string { - return scope.From[S](f).Cluster() -} - func Version[ S scope.Group[F, T], F client.Object, diff --git a/pkg/apiutil/core/v1alpha1/object.go b/pkg/apiutil/core/v1alpha1/object.go new file mode 100644 index 0000000000..2c6022322e --- /dev/null +++ b/pkg/apiutil/core/v1alpha1/object.go @@ -0,0 +1,29 @@ +// Copyright 2024 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package coreutil + +import ( + "github.com/pingcap/tidb-operator/pkg/client" + "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" +) + +func Cluster[ + S scope.Object[F, T], + F client.Object, + T runtime.Object, +](f F) string { + return scope.From[S](f).Cluster() +} diff --git a/pkg/controllers/common/resource_test.go b/pkg/controllers/common/resource_test.go index 8a4129b76d..6918544dd7 100644 --- a/pkg/controllers/common/resource_test.go +++ b/pkg/controllers/common/resource_test.go @@ -33,19 +33,19 @@ func TestResource(t *testing.T) { }{ { desc: "normal", - ns: Namespace("aaa"), + ns: Namespace("xxx"), name: Name("bbb"), obj: 42, - expectedNs: "aaa", + expectedNs: "xxx", expectedName: "bbb", expectedObj: 42, }, { desc: "use name func", - ns: Lazy[string](func() string { return "aaa" }), + ns: Lazy[string](func() string { return "xxx" }), name: Lazy[string](func() string { return "bbb" }), obj: 42, - expectedNs: "aaa", + expectedNs: "xxx", expectedName: "bbb", expectedObj: 42, }, @@ -85,10 +85,10 @@ func TestResourceSlice(t *testing.T) { }{ { desc: "normal", - ns: Namespace("aaa"), + ns: Namespace("nnn"), labels: Labels(map[string]string{"xxx": "yyy"}), objs: []*int{ptr.To(42)}, - expectedNs: "aaa", + expectedNs: "nnn", expectedLabels: map[string]string{ "xxx": "yyy", }, @@ -96,10 +96,10 @@ func TestResourceSlice(t *testing.T) { }, { desc: "use func", - ns: Lazy[string](func() string { return "aaa" }), + ns: Lazy[string](func() string { return "nnn" }), labels: LabelsFunc(func() map[string]string { return map[string]string{"xxx": "yyy"} }), objs: []*int{ptr.To(42)}, - expectedNs: "aaa", + expectedNs: "nnn", expectedLabels: map[string]string{ "xxx": "yyy", }, diff --git a/pkg/controllers/common/task.go b/pkg/controllers/common/task.go index 97608b035f..1c851e328e 100644 --- a/pkg/controllers/common/task.go +++ b/pkg/controllers/common/task.go @@ -27,8 +27,11 @@ import ( "k8s.io/apimachinery/pkg/types" "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" + coreutil "github.com/pingcap/tidb-operator/pkg/apiutil/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/features" + "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -114,11 +117,6 @@ func TaskContextTiFlash(state TiFlashStateInitializer, c client.Client) task.Tas return taskContextResource("TiFlash", w, c, false) } -func TaskContextCluster(state ClusterStateInitializer, c client.Client) task.Task { - w := state.ClusterInitializer() - return taskContextResource("Cluster", w, c, true) -} - func TaskContextPod(state PodStateInitializer, c client.Client) task.Task { w := state.PodInitializer() return taskContextResource("Pod", w, c, false) @@ -193,3 +191,35 @@ func TaskFeatureGates(state ClusterState) task.Task { return task.Complete().With("feature gates are initialized") }) } + +type ContextClusterNewer[ + F client.Object, +] interface { + Object() F + SetCluster(c *v1alpha1.Cluster) +} + +func TaskContextCluster[ + S scope.Object[F, T], + F client.Object, + T runtime.Object, +](state ContextClusterNewer[F], c client.Client) task.Task { + return task.NameTaskFunc("ContextCluster", func(ctx context.Context) task.Result { + cluster := v1alpha1.Cluster{} + obj := state.Object() + + key := types.NamespacedName{ + Namespace: obj.GetNamespace(), + Name: coreutil.Cluster[S](obj), + } + if err := c.Get(ctx, key, &cluster); err != nil { + if !errors.IsNotFound(err) { + return task.Fail().With("can't get %s: %v", key, err) + } + + return task.Fail().With("cannot find %s: %v", key, err) + } + state.SetCluster(&cluster) + return task.Complete().With("cluster is set") + }) +} diff --git a/pkg/controllers/common/task_test.go b/pkg/controllers/common/task_test.go index 71b18b7c26..e2a84b209e 100644 --- a/pkg/controllers/common/task_test.go +++ b/pkg/controllers/common/task_test.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/features" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -95,10 +96,35 @@ func TestTaskContextPD(t *testing.T) { } } +type fakeObjectState[ + F client.Object, +] struct { + obj F + cluster *v1alpha1.Cluster +} + +func (s *fakeObjectState[F]) Object() F { + return s.obj +} + +func (s *fakeObjectState[F]) SetCluster(c *v1alpha1.Cluster) { + s.cluster = c +} + +func newFakeObjectState[ + F client.Object, +](f F) *fakeObjectState[F] { + return &fakeObjectState[F]{ + obj: f, + } +} + func TestTaskContextCluster(t *testing.T) { + const ns = "aaa" + const name = "bbb" cases := []struct { desc string - state *fakeState[v1alpha1.Cluster] + state *fakeObjectState[*v1alpha1.PD] objs []client.Object unexpectedErr bool @@ -107,32 +133,35 @@ func TestTaskContextCluster(t *testing.T) { }{ { desc: "success", - state: &fakeState[v1alpha1.Cluster]{ - ns: "aaa", - name: "aaa", - }, + state: newFakeObjectState(fake.FakeObj(name, func(obj *v1alpha1.PD) *v1alpha1.PD { + obj.Namespace = ns + obj.Spec.Cluster.Name = name + return obj + })), objs: []client.Object{ - fake.FakeObj("aaa", fake.SetNamespace[v1alpha1.Cluster]("aaa")), + fake.FakeObj(name, fake.SetNamespace[v1alpha1.Cluster](ns)), }, expectedResult: task.SComplete, - expectedObj: fake.FakeObj("aaa", fake.SetNamespace[v1alpha1.Cluster]("aaa")), + expectedObj: fake.FakeObj(name, fake.SetNamespace[v1alpha1.Cluster](ns)), }, { desc: "not found", - state: &fakeState[v1alpha1.Cluster]{ - ns: "aaa", - name: "aaa", - }, + state: newFakeObjectState(fake.FakeObj(name, func(obj *v1alpha1.PD) *v1alpha1.PD { + obj.Namespace = ns + obj.Spec.Cluster.Name = name + return obj + })), expectedResult: task.SFail, }, { desc: "has unexpected error", - state: &fakeState[v1alpha1.Cluster]{ - ns: "aaa", - name: "aaa", - }, + state: newFakeObjectState(fake.FakeObj(name, func(obj *v1alpha1.PD) *v1alpha1.PD { + obj.Namespace = ns + obj.Spec.Cluster.Name = name + return obj + })), objs: []client.Object{ - fake.FakeObj("aaa", fake.SetNamespace[v1alpha1.Cluster]("aaa")), + fake.FakeObj(name, fake.SetNamespace[v1alpha1.Cluster](ns)), }, unexpectedErr: true, expectedResult: task.SFail, @@ -149,12 +178,10 @@ func TestTaskContextCluster(t *testing.T) { if c.unexpectedErr { fc.WithError("*", "*", errors.NewInternalError(fmt.Errorf("fake internal err"))) } - s := &fakeClusterState{s: c.state} - - res, done := task.RunTask(context.Background(), TaskContextCluster(s, fc)) + res, done := task.RunTask(context.Background(), TaskContextCluster[scope.PD](c.state, fc)) assert.Equal(tt, c.expectedResult, res.Status(), c.desc) assert.False(tt, done, c.desc) - assert.Equal(tt, c.expectedObj, c.state.obj, c.desc) + assert.Equal(tt, c.expectedObj, c.state.cluster, c.desc) }) } } diff --git a/pkg/controllers/pd/builder.go b/pkg/controllers/pd/builder.go index 63a7b6829d..86e1dc4bad 100644 --- a/pkg/controllers/pd/builder.go +++ b/pkg/controllers/pd/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/pd/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondInstanceHasBeenDeleted(state)), // get cluster - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.PD](state, r.Client), common.TaskFeatureGates(state), // if it's paused just return task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/pd/tasks/state.go b/pkg/controllers/pd/tasks/state.go index 649ce1c500..5342bc76b2 100644 --- a/pkg/controllers/pd/tasks/state.go +++ b/pkg/controllers/pd/tasks/state.go @@ -36,7 +36,6 @@ type state struct { type State interface { common.PDStateInitializer - common.ClusterStateInitializer common.PodStateInitializer common.PDSliceStateInitializer @@ -46,6 +45,9 @@ type State interface { common.PDSliceState common.InstanceState[*runtime.PD] + + common.ContextClusterNewer[*v1alpha1.PD] + SetPod(*corev1.Pod) } @@ -56,6 +58,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.PD { + return s.pd +} + func (s *state) PD() *v1alpha1.PD { return s.pd } @@ -76,6 +82,10 @@ func (s *state) SetPod(pod *corev1.Pod) { s.pod = pod } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) PDSlice() []*v1alpha1.PD { return s.pds } @@ -87,15 +97,6 @@ func (s *state) PDInitializer() common.PDInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.pd.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) PodInitializer() common.PodInitializer { return common.NewResource(s.SetPod). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/pd/tasks/state_test.go b/pkg/controllers/pd/tasks/state_test.go index e556647424..255d6d9a83 100644 --- a/pkg/controllers/pd/tasks/state_test.go +++ b/pkg/controllers/pd/tasks/state_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -120,7 +121,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextPD(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.PD](s, fc), common.TaskContextPDSlice(s, fc), common.TaskContextPod(s, fc), )) diff --git a/pkg/controllers/pdgroup/builder.go b/pkg/controllers/pdgroup/builder.go index 6e0ff43831..8ca9394586 100644 --- a/pkg/controllers/pdgroup/builder.go +++ b/pkg/controllers/pdgroup/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/pdgroup/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondGroupHasBeenDeleted(state)), // get cluster - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.PDGroup](state, r.Client), common.TaskFeatureGates(state), // if it's paused just return task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/pdgroup/tasks/state.go b/pkg/controllers/pdgroup/tasks/state.go index fbfde7d7df..c9234fe495 100644 --- a/pkg/controllers/pdgroup/tasks/state.go +++ b/pkg/controllers/pdgroup/tasks/state.go @@ -36,7 +36,6 @@ type state struct { type State interface { common.PDGroupStateInitializer - common.ClusterStateInitializer common.PDSliceStateInitializer common.RevisionStateInitializer[*runtime.PDGroup] @@ -46,6 +45,9 @@ type State interface { common.RevisionState common.GroupState[*runtime.PDGroup] + + common.ContextClusterNewer[*v1alpha1.PDGroup] + common.InstanceSliceState[*runtime.PD] } @@ -56,6 +58,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.PDGroup { + return s.pdg +} + func (s *state) PDGroup() *v1alpha1.PDGroup { return s.pdg } @@ -76,6 +82,10 @@ func (s *state) Slice() []*runtime.PD { return runtime.FromPDSlice(s.pds) } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) PDGroupInitializer() common.PDGroupInitializer { return common.NewResource(func(pdg *v1alpha1.PDGroup) { s.pdg = pdg }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -83,15 +93,6 @@ func (s *state) PDGroupInitializer() common.PDGroupInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.pdg.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) PDSliceInitializer() common.PDSliceInitializer { return common.NewResourceSlice(func(pds []*v1alpha1.PD) { s.pds = pds }). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/pdgroup/tasks/state_test.go b/pkg/controllers/pdgroup/tasks/state_test.go index 26c78c7913..3151452a74 100644 --- a/pkg/controllers/pdgroup/tasks/state_test.go +++ b/pkg/controllers/pdgroup/tasks/state_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -94,7 +95,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextPDGroup(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.PDGroup](s, fc), common.TaskContextPDSlice(s, fc), )) assert.Equal(tt, task.SComplete, res.Status(), c.desc) diff --git a/pkg/controllers/tidb/builder.go b/pkg/controllers/tidb/builder.go index 67eb15287e..586035427b 100644 --- a/pkg/controllers/tidb/builder.go +++ b/pkg/controllers/tidb/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/tidb/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondInstanceHasBeenDeleted(state)), // get cluster info, FinalizerDel will use it - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.TiDB](state, r.Client), common.TaskFeatureGates(state), // check whether it's paused task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/tidb/tasks/state.go b/pkg/controllers/tidb/tasks/state.go index 8f9e195464..f52e3328a1 100644 --- a/pkg/controllers/tidb/tasks/state.go +++ b/pkg/controllers/tidb/tasks/state.go @@ -31,11 +31,12 @@ type state struct { cluster *v1alpha1.Cluster tidb *v1alpha1.TiDB pod *corev1.Pod + + statusChanged bool } type State interface { common.TiDBStateInitializer - common.ClusterStateInitializer common.PodStateInitializer common.TiDBState @@ -44,6 +45,8 @@ type State interface { common.InstanceState[*runtime.TiDB] + common.ContextClusterNewer[*v1alpha1.TiDB] + SetPod(*corev1.Pod) } @@ -70,10 +73,26 @@ func (s *state) Instance() *runtime.TiDB { return runtime.FromTiDB(s.tidb) } +func (s *state) Object() *v1alpha1.TiDB { + return s.tidb +} + +func (s *state) IsStatusChanged() bool { + return s.statusChanged +} + func (s *state) SetPod(pod *corev1.Pod) { s.pod = pod } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + +func (s *state) SetStatusChanged() { + s.statusChanged = true +} + func (s *state) TiDBInitializer() common.TiDBInitializer { return common.NewResource(func(tidb *v1alpha1.TiDB) { s.tidb = tidb }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -81,15 +100,6 @@ func (s *state) TiDBInitializer() common.TiDBInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.tidb.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) PodInitializer() common.PodInitializer { return common.NewResource(s.SetPod). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/tidb/tasks/state_test.go b/pkg/controllers/tidb/tasks/state_test.go index f2ea5bd24e..7e927569e6 100644 --- a/pkg/controllers/tidb/tasks/state_test.go +++ b/pkg/controllers/tidb/tasks/state_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -77,7 +78,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextTiDB(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.TiDB](s, fc), common.TaskContextPod(s, fc), )) assert.Equal(tt, task.SComplete, res.Status(), c.desc) diff --git a/pkg/controllers/tidbgroup/builder.go b/pkg/controllers/tidbgroup/builder.go index 9edf45351d..405642376e 100644 --- a/pkg/controllers/tidbgroup/builder.go +++ b/pkg/controllers/tidbgroup/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/tidbgroup/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondGroupHasBeenDeleted(state)), // get cluster - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.TiDBGroup](state, r.Client), common.TaskFeatureGates(state), // if it's paused just return task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/tidbgroup/tasks/state.go b/pkg/controllers/tidbgroup/tasks/state.go index 16d4507e37..941cb27691 100644 --- a/pkg/controllers/tidbgroup/tasks/state.go +++ b/pkg/controllers/tidbgroup/tasks/state.go @@ -36,7 +36,6 @@ type state struct { type State interface { common.TiDBGroupStateInitializer - common.ClusterStateInitializer common.TiDBSliceStateInitializer common.RevisionStateInitializer[*runtime.TiDBGroup] @@ -46,6 +45,9 @@ type State interface { common.RevisionState common.GroupState[*runtime.TiDBGroup] + + common.ContextClusterNewer[*v1alpha1.TiDBGroup] + common.InstanceSliceState[*runtime.TiDB] } @@ -56,6 +58,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.TiDBGroup { + return s.dbg +} + func (s *state) TiDBGroup() *v1alpha1.TiDBGroup { return s.dbg } @@ -76,6 +82,10 @@ func (s *state) Slice() []*runtime.TiDB { return runtime.FromTiDBSlice(s.dbs) } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) TiDBGroupInitializer() common.TiDBGroupInitializer { return common.NewResource(func(dbg *v1alpha1.TiDBGroup) { s.dbg = dbg }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -83,15 +93,6 @@ func (s *state) TiDBGroupInitializer() common.TiDBGroupInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.dbg.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) TiDBSliceInitializer() common.TiDBSliceInitializer { return common.NewResourceSlice(func(dbs []*v1alpha1.TiDB) { s.dbs = dbs }). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/tidbgroup/tasks/state_test.go b/pkg/controllers/tidbgroup/tasks/state_test.go index 66d340de20..1fc7f5bad3 100644 --- a/pkg/controllers/tidbgroup/tasks/state_test.go +++ b/pkg/controllers/tidbgroup/tasks/state_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -97,7 +98,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextTiDBGroup(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.TiDBGroup](s, fc), common.TaskContextTiDBSlice(s, fc), common.TaskRevision[runtime.TiDBGroupTuple](s, fc), )) diff --git a/pkg/controllers/tiflash/builder.go b/pkg/controllers/tiflash/builder.go index 0799b53508..dc1a979852 100644 --- a/pkg/controllers/tiflash/builder.go +++ b/pkg/controllers/tiflash/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/tiflash/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondInstanceHasBeenDeleted(state)), // get cluster info, FinalizerDel will use it - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.TiFlash](state, r.Client), common.TaskFeatureGates(state), // check whether it's paused task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/tiflash/tasks/state.go b/pkg/controllers/tiflash/tasks/state.go index 832fc2e2f7..4eddea5dae 100644 --- a/pkg/controllers/tiflash/tasks/state.go +++ b/pkg/controllers/tiflash/tasks/state.go @@ -35,7 +35,6 @@ type state struct { type State interface { common.TiFlashStateInitializer - common.ClusterStateInitializer common.PodStateInitializer common.TiFlashState @@ -44,6 +43,8 @@ type State interface { common.InstanceState[*runtime.TiFlash] + common.ContextClusterNewer[*v1alpha1.TiFlash] + SetPod(*corev1.Pod) } @@ -54,6 +55,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.TiFlash { + return s.tiflash +} + func (s *state) TiFlash() *v1alpha1.TiFlash { return s.tiflash } @@ -74,6 +79,10 @@ func (s *state) SetPod(pod *corev1.Pod) { s.pod = pod } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) TiFlashInitializer() common.TiFlashInitializer { return common.NewResource(func(tiflash *v1alpha1.TiFlash) { s.tiflash = tiflash }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -81,15 +90,6 @@ func (s *state) TiFlashInitializer() common.TiFlashInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.tiflash.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) PodInitializer() common.PodInitializer { return common.NewResource(s.SetPod). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/tiflash/tasks/state_test.go b/pkg/controllers/tiflash/tasks/state_test.go index dd706b1f72..dc76863508 100644 --- a/pkg/controllers/tiflash/tasks/state_test.go +++ b/pkg/controllers/tiflash/tasks/state_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -77,7 +78,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextTiFlash(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.TiFlash](s, fc), common.TaskContextPod(s, fc), )) assert.Equal(tt, task.SComplete, res.Status(), c.desc) diff --git a/pkg/controllers/tiflashgroup/builder.go b/pkg/controllers/tiflashgroup/builder.go index 202644f6c3..018555f252 100644 --- a/pkg/controllers/tiflashgroup/builder.go +++ b/pkg/controllers/tiflashgroup/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/tiflashgroup/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondGroupHasBeenDeleted(state)), // get cluster - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.TiFlashGroup](state, r.Client), common.TaskFeatureGates(state), // if it's paused just return task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/tiflashgroup/tasks/state.go b/pkg/controllers/tiflashgroup/tasks/state.go index 43397ce9b5..e64858ffd8 100644 --- a/pkg/controllers/tiflashgroup/tasks/state.go +++ b/pkg/controllers/tiflashgroup/tasks/state.go @@ -36,7 +36,6 @@ type state struct { type State interface { common.TiFlashGroupStateInitializer - common.ClusterStateInitializer common.TiFlashSliceStateInitializer common.RevisionStateInitializer[*runtime.TiFlashGroup] @@ -46,6 +45,9 @@ type State interface { common.RevisionState common.GroupState[*runtime.TiFlashGroup] + + common.ContextClusterNewer[*v1alpha1.TiFlashGroup] + common.InstanceSliceState[*runtime.TiFlash] } @@ -56,6 +58,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.TiFlashGroup { + return s.fg +} + func (s *state) TiFlashGroup() *v1alpha1.TiFlashGroup { return s.fg } @@ -76,6 +82,10 @@ func (s *state) Slice() []*runtime.TiFlash { return runtime.FromTiFlashSlice(s.fs) } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) TiFlashGroupInitializer() common.TiFlashGroupInitializer { return common.NewResource(func(fg *v1alpha1.TiFlashGroup) { s.fg = fg }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -83,15 +93,6 @@ func (s *state) TiFlashGroupInitializer() common.TiFlashGroupInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.fg.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) TiFlashSliceInitializer() common.TiFlashSliceInitializer { return common.NewResourceSlice(func(fs []*v1alpha1.TiFlash) { s.fs = fs }). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/tiflashgroup/tasks/state_test.go b/pkg/controllers/tiflashgroup/tasks/state_test.go index 5686d172fe..5b4842924a 100644 --- a/pkg/controllers/tiflashgroup/tasks/state_test.go +++ b/pkg/controllers/tiflashgroup/tasks/state_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -97,7 +98,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextTiFlashGroup(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.TiFlashGroup](s, fc), common.TaskContextTiFlashSlice(s, fc), common.TaskRevision[runtime.TiFlashGroupTuple](s, fc), )) diff --git a/pkg/controllers/tikv/builder.go b/pkg/controllers/tikv/builder.go index 404edf91fc..393e7f1d2c 100644 --- a/pkg/controllers/tikv/builder.go +++ b/pkg/controllers/tikv/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/tikv/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondInstanceHasBeenDeleted(state)), // get cluster info, FinalizerDel will use it - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.TiKV](state, r.Client), common.TaskFeatureGates(state), // check whether it's paused diff --git a/pkg/controllers/tikv/tasks/state.go b/pkg/controllers/tikv/tasks/state.go index 3f04a6d71b..d377ff9ae2 100644 --- a/pkg/controllers/tikv/tasks/state.go +++ b/pkg/controllers/tikv/tasks/state.go @@ -35,7 +35,6 @@ type state struct { type State interface { common.TiKVStateInitializer - common.ClusterStateInitializer common.PodStateInitializer common.TiKVState @@ -44,6 +43,8 @@ type State interface { common.InstanceState[*runtime.TiKV] + common.ContextClusterNewer[*v1alpha1.TiKV] + SetPod(*corev1.Pod) } @@ -54,6 +55,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.TiKV { + return s.tikv +} + func (s *state) TiKV() *v1alpha1.TiKV { return s.tikv } @@ -74,6 +79,10 @@ func (s *state) SetPod(pod *corev1.Pod) { s.pod = pod } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) TiKVInitializer() common.TiKVInitializer { return common.NewResource(func(tikv *v1alpha1.TiKV) { s.tikv = tikv }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -81,15 +90,6 @@ func (s *state) TiKVInitializer() common.TiKVInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.tikv.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) PodInitializer() common.PodInitializer { return common.NewResource(s.SetPod). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/tikv/tasks/state_test.go b/pkg/controllers/tikv/tasks/state_test.go index 70255fe0ba..0fba6d2ac2 100644 --- a/pkg/controllers/tikv/tasks/state_test.go +++ b/pkg/controllers/tikv/tasks/state_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -77,7 +78,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextTiKV(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.TiKV](s, fc), common.TaskContextPod(s, fc), )) assert.Equal(tt, task.SComplete, res.Status(), c.desc) diff --git a/pkg/controllers/tikvgroup/builder.go b/pkg/controllers/tikvgroup/builder.go index c060f6415d..7805bbc62a 100644 --- a/pkg/controllers/tikvgroup/builder.go +++ b/pkg/controllers/tikvgroup/builder.go @@ -18,6 +18,7 @@ import ( "github.com/pingcap/tidb-operator/pkg/controllers/common" "github.com/pingcap/tidb-operator/pkg/controllers/tikvgroup/tasks" "github.com/pingcap/tidb-operator/pkg/runtime" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -29,7 +30,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task task.IfBreak(common.CondGroupHasBeenDeleted(state)), // get cluster - common.TaskContextCluster(state, r.Client), + common.TaskContextCluster[scope.TiKVGroup](state, r.Client), common.TaskFeatureGates(state), // if it's paused just return task.IfBreak(common.CondClusterIsPaused(state)), diff --git a/pkg/controllers/tikvgroup/tasks/state.go b/pkg/controllers/tikvgroup/tasks/state.go index d5bc1b6fce..e6c03682a0 100644 --- a/pkg/controllers/tikvgroup/tasks/state.go +++ b/pkg/controllers/tikvgroup/tasks/state.go @@ -36,7 +36,6 @@ type state struct { type State interface { common.TiKVGroupStateInitializer - common.ClusterStateInitializer common.TiKVSliceStateInitializer common.RevisionStateInitializer[*runtime.TiKVGroup] @@ -46,6 +45,9 @@ type State interface { common.RevisionState common.GroupState[*runtime.TiKVGroup] + + common.ContextClusterNewer[*v1alpha1.TiKVGroup] + common.InstanceSliceState[*runtime.TiKV] } @@ -56,6 +58,10 @@ func NewState(key types.NamespacedName) State { return s } +func (s *state) Object() *v1alpha1.TiKVGroup { + return s.kvg +} + func (s *state) TiKVGroup() *v1alpha1.TiKVGroup { return s.kvg } @@ -76,6 +82,10 @@ func (s *state) Slice() []*runtime.TiKV { return runtime.FromTiKVSlice(s.kvs) } +func (s *state) SetCluster(cluster *v1alpha1.Cluster) { + s.cluster = cluster +} + func (s *state) TiKVGroupInitializer() common.TiKVGroupInitializer { return common.NewResource(func(kvg *v1alpha1.TiKVGroup) { s.kvg = kvg }). WithNamespace(common.Namespace(s.key.Namespace)). @@ -83,15 +93,6 @@ func (s *state) TiKVGroupInitializer() common.TiKVGroupInitializer { Initializer() } -func (s *state) ClusterInitializer() common.ClusterInitializer { - return common.NewResource(func(cluster *v1alpha1.Cluster) { s.cluster = cluster }). - WithNamespace(common.Namespace(s.key.Namespace)). - WithName(common.Lazy[string](func() string { - return s.kvg.Spec.Cluster.Name - })). - Initializer() -} - func (s *state) TiKVSliceInitializer() common.TiKVSliceInitializer { return common.NewResourceSlice(func(kvs []*v1alpha1.TiKV) { s.kvs = kvs }). WithNamespace(common.Namespace(s.key.Namespace)). diff --git a/pkg/controllers/tikvgroup/tasks/state_test.go b/pkg/controllers/tikvgroup/tasks/state_test.go index 1418f77586..857b69c6ed 100644 --- a/pkg/controllers/tikvgroup/tasks/state_test.go +++ b/pkg/controllers/tikvgroup/tasks/state_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client" "github.com/pingcap/tidb-operator/pkg/controllers/common" + "github.com/pingcap/tidb-operator/pkg/runtime/scope" "github.com/pingcap/tidb-operator/pkg/utils/fake" "github.com/pingcap/tidb-operator/pkg/utils/task/v3" ) @@ -94,7 +95,7 @@ func TestState(t *testing.T) { ctx := context.Background() res, done := task.RunTask(ctx, task.Block( common.TaskContextTiKVGroup(s, fc), - common.TaskContextCluster(s, fc), + common.TaskContextCluster[scope.TiKVGroup](s, fc), common.TaskContextTiKVSlice(s, fc), )) assert.Equal(tt, task.SComplete, res.Status(), c.desc)