Skip to content

Commit

Permalink
feat(group): move tiflashgroup to v3 and extract some common tasks (#…
Browse files Browse the repository at this point in the history
…6021)

Signed-off-by: liubo02 <[email protected]>
  • Loading branch information
liubog2008 authored Jan 7, 2025
1 parent 8468aa9 commit 35d5ac7
Show file tree
Hide file tree
Showing 61 changed files with 2,569 additions and 2,835 deletions.
30 changes: 30 additions & 0 deletions pkg/controllers/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type (
}
)

type GroupStateInitializer[G runtime.GroupSet] interface {
GroupInitializer() ResourceInitializer[G]
}

type GroupState[G runtime.Group] interface {
Group() G
}
Expand All @@ -79,6 +83,32 @@ type GroupAndInstanceSliceState[
InstanceSliceState[I]
}

type RevisionStateInitializer interface {
RevisionInitializer() RevisionInitializer
}

type RevisionState interface {
Revision() (update, current string, collisionCount int32)
}

type GroupAndInstanceSliceAndRevisionState[
G runtime.Group,
I runtime.Instance,
] interface {
GroupState[G]
InstanceSliceState[I]
RevisionState
}

type ClusterAndGroupAndInstanceSliceState[
G runtime.Group,
I runtime.Instance,
] interface {
ClusterState
GroupState[G]
InstanceSliceState[I]
}

type (
PDGroupStateInitializer interface {
PDGroupInitializer() PDGroupInitializer
Expand Down
50 changes: 50 additions & 0 deletions pkg/controllers/common/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,53 @@ func FakeGroupAndInstanceSliceState[
InstanceSliceState: FakeInstanceSliceState[RI](s),
}
}

type fakeRevisionState struct {
updateRevision string
currentRevision string
collisionCount int32
}

func (f *fakeRevisionState) Set(update, current string, collisionCount int32) {
f.updateRevision = update
f.currentRevision = current
f.collisionCount = collisionCount
}

func (f *fakeRevisionState) Revision() (update, current string, collisionCount int32) {
return f.updateRevision, f.currentRevision, f.collisionCount
}

func FakeRevisionState(update, current string, collisionCount int32) RevisionState {
return &fakeRevisionState{
updateRevision: update,
currentRevision: current,
collisionCount: collisionCount,
}
}

type fakeGroupAndInstanceSliceAndRevisionState[
RG runtime.Group,
RI runtime.Instance,
] struct {
GroupState[RG]
InstanceSliceState[RI]
RevisionState
}

func FakeGroupAndInstanceSliceAndRevisionState[
RG runtime.Group,
RI runtime.Instance,
](
update string,
current string,
collisionCount int32,
g RG,
s ...RI,
) GroupAndInstanceSliceAndRevisionState[RG, RI] {
return &fakeGroupAndInstanceSliceAndRevisionState[RG, RI]{
GroupState: FakeGroupState[RG](g),
InstanceSliceState: FakeInstanceSliceState[RI](s),
RevisionState: FakeRevisionState(update, current, collisionCount),
}
}
38 changes: 35 additions & 3 deletions pkg/controllers/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package common

import "github.com/pingcap/tidb-operator/pkg/client"

type Setter[T any] interface {
Set(T)
}
Expand All @@ -40,13 +42,43 @@ type ListOptions interface {
LabelsOption
}

type NameFunc func() string
type CurrentRevisionOption interface {
CurrentRevision() string
}

type CollisionCountOption interface {
CollisionCount() *int32
}

type ParentOption interface {
Parent() client.Object
}

type (
Lazy[T any] func() T
)

func (f Lazy[T]) Namespace() T {
return f()
}

func (f Lazy[T]) Name() T {
return f()
}

func (f Lazy[T]) CurrentRevision() T {
return f()
}

func (f Lazy[T]) CollisionCount() T {
return f()
}

func (f NameFunc) Namespace() string {
func (f Lazy[T]) Labels() T {
return f()
}

func (f NameFunc) Name() string {
func (f Lazy[T]) Parent() T {
return f()
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/common/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestResource(t *testing.T) {
},
{
desc: "use name func",
ns: NameFunc(func() string { return "aaa" }),
name: NameFunc(func() string { return "bbb" }),
ns: Lazy[string](func() string { return "aaa" }),
name: Lazy[string](func() string { return "bbb" }),
obj: 42,
expectedNs: "aaa",
expectedName: "bbb",
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestResourceSlice(t *testing.T) {
},
{
desc: "use func",
ns: NameFunc(func() string { return "aaa" }),
ns: Lazy[string](func() string { return "aaa" }),
labels: LabelsFunc(func() map[string]string { return map[string]string{"xxx": "yyy"} }),
objs: []*int{ptr.To(42)},
expectedNs: "aaa",
Expand Down
141 changes: 141 additions & 0 deletions pkg/controllers/common/revision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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 common

import (
"context"

"k8s.io/apimachinery/pkg/labels"

"github.com/pingcap/tidb-operator/pkg/client"
revisionutil "github.com/pingcap/tidb-operator/pkg/utils/k8s/revision"
"github.com/pingcap/tidb-operator/pkg/utils/task/v3"
"github.com/pingcap/tidb-operator/third_party/kubernetes/pkg/controller/history"
)

type RevisionSetter interface {
Set(update, current string, collisionCount int32)
}

type RevisionSetterFunc func(update, current string, collisionCount int32)

func (f RevisionSetterFunc) Set(update, current string, collisionCount int32) {
f(update, current, collisionCount)
}

type Revision interface {
WithCurrentRevision(CurrentRevisionOption) Revision
WithCollisionCount(CollisionCountOption) Revision
WithParent(ParentOption) Revision
WithLabels(LabelsOption) Revision
Initializer() RevisionInitializer
}

type RevisionInitializer interface {
LabelsOption
ParentOption
CurrentRevision() string
CollisionCount() *int32
RevisionSetter
}

type revision struct {
RevisionSetter

parent ParentOption
currentRevision CurrentRevisionOption
collisionCount CollisionCountOption
labels LabelsOption
}

func NewRevision(setter RevisionSetter) Revision {
return &revision{
RevisionSetter: setter,
}
}

func (r *revision) WithParent(parent ParentOption) Revision {
r.parent = parent
return r
}

func (r *revision) WithCurrentRevision(rev CurrentRevisionOption) Revision {
r.currentRevision = rev
return r
}

func (r *revision) WithCollisionCount(collisionCount CollisionCountOption) Revision {
r.collisionCount = collisionCount
return r
}

func (r *revision) WithLabels(ls LabelsOption) Revision {
r.labels = ls
return r
}

func (r *revision) Initializer() RevisionInitializer {
return r
}

func (r *revision) Parent() client.Object {
return r.parent.Parent()
}

func (r *revision) CurrentRevision() string {
return r.currentRevision.CurrentRevision()
}

func (r *revision) CollisionCount() *int32 {
return r.collisionCount.CollisionCount()
}

func (r *revision) Labels() map[string]string {
return r.labels.Labels()
}

func TaskRevision(state RevisionStateInitializer, c client.Client) task.Task {
w := state.RevisionInitializer()
return task.NameTaskFunc("ContextRevision", func(ctx context.Context) task.Result {
historyCli := history.NewClient(c)
parent := w.Parent()

lbs := w.Labels()
selector := labels.SelectorFromSet(labels.Set(lbs))

revisions, err := historyCli.ListControllerRevisions(parent, selector)
if err != nil {
return task.Fail().With("cannot list controller revisions: %w", err)
}
history.SortControllerRevisions(revisions)

// Get the current(old) and update(new) ControllerRevisions.
currentRevision, updateRevision, collisionCount, err := revisionutil.GetCurrentAndUpdate(
ctx,
parent,
lbs,
revisions,
historyCli,
w.CurrentRevision(),
w.CollisionCount(),
)
if err != nil {
return task.Fail().With("cannot get revisions: %w", err)
}

w.Set(updateRevision.Name, currentRevision.Name, collisionCount)
return task.Complete().With("revision is set")
})
}
Loading

0 comments on commit 35d5ac7

Please sign in to comment.