Skip to content

Commit

Permalink
feat(pdgroup): migrate to task v3
Browse files Browse the repository at this point in the history
Signed-off-by: liubo02 <[email protected]>
  • Loading branch information
liubog2008 committed Dec 26, 2024
1 parent 749fbbb commit 24f21f2
Show file tree
Hide file tree
Showing 24 changed files with 572 additions and 506 deletions.
8 changes: 4 additions & 4 deletions apis/core/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ type GroupStatus struct {
Replicas int32 `json:"replicas"`

// ReadyReplicas is the number of Instances created for this ComponentGroup with a Ready Condition.
ReadyReplicas int32 `json:"readyReplicas,omitempty"`
ReadyReplicas int32 `json:"readyReplicas"`

// CurrentReplicas is the number of Instances created by the Group controller from the Group version
// indicated by currentRevision.
CurrentReplicas int32 `json:"currentReplicas,omitempty"`
CurrentReplicas int32 `json:"currentReplicas"`

// UpdatedReplicas is the number of Instances created by the Group controller from the Group version
// UpdateReplicas is the number of Instances created by the Group controller from the Group version
// indicated by updateRevision.
UpdatedReplicas int32 `json:"updatedReplicas,omitempty"`
UpdateReplicas int32 `json:"updateReplicas"`
}

type UpdateStrategy struct {
Expand Down
15 changes: 9 additions & 6 deletions manifests/crd/core.pingcap.com_pdgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -383,23 +383,26 @@ spec:
description: Replicas is the number of Instance created by the controller.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
updatedReplicas:
updateReplicas:
description: |-
UpdatedReplicas is the number of Instances created by the Group controller from the Group version
UpdateReplicas is the number of Instances created by the Group controller from the Group version
indicated by updateRevision.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
version:
description: |-
Version is the version of all instances in the group.
It will be same as the `spec.version` only when all instances are upgraded to the desired version.
type: string
required:
- currentReplicas
- readyReplicas
- replicas
- updateReplicas
type: object
type: object
served: true
Expand Down
15 changes: 9 additions & 6 deletions manifests/crd/core.pingcap.com_tidbgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -522,23 +522,26 @@ spec:
description: Replicas is the number of Instance created by the controller.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
updatedReplicas:
updateReplicas:
description: |-
UpdatedReplicas is the number of Instances created by the Group controller from the Group version
UpdateReplicas is the number of Instances created by the Group controller from the Group version
indicated by updateRevision.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
version:
description: |-
Version is the version of all instances in the group.
It will be same as the `spec.version` only when all instances are upgraded to the desired version.
type: string
required:
- currentReplicas
- readyReplicas
- replicas
- updateReplicas
type: object
type: object
served: true
Expand Down
15 changes: 9 additions & 6 deletions manifests/crd/core.pingcap.com_tiflashgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -419,23 +419,26 @@ spec:
description: Replicas is the number of Instance created by the controller.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
updatedReplicas:
updateReplicas:
description: |-
UpdatedReplicas is the number of Instances created by the Group controller from the Group version
UpdateReplicas is the number of Instances created by the Group controller from the Group version
indicated by updateRevision.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
version:
description: |-
Version is the version of all instances in the group.
It will be same as the `spec.version` only when all instances are upgraded to the desired version.
type: string
required:
- currentReplicas
- readyReplicas
- replicas
- updateReplicas
type: object
type: object
served: true
Expand Down
15 changes: 9 additions & 6 deletions manifests/crd/core.pingcap.com_tikvgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -384,23 +384,26 @@ spec:
description: Replicas is the number of Instance created by the controller.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
updatedReplicas:
updateReplicas:
description: |-
UpdatedReplicas is the number of Instances created by the Group controller from the Group version
UpdateReplicas is the number of Instances created by the Group controller from the Group version
indicated by updateRevision.
format: int32
type: integer
updateRevision:
description: UpdateRevision is the revision of the Controller that
should modify the resource.
type: string
version:
description: |-
Version is the version of all instances in the group.
It will be same as the `spec.version` only when all instances are upgraded to the desired version.
type: string
required:
- currentReplicas
- readyReplicas
- replicas
- updateReplicas
type: object
type: object
served: true
Expand Down
12 changes: 12 additions & 0 deletions pkg/controllers/common/cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ func CondClusterIsPaused(ctx ClusterState) task.Condition {
return ctx.Cluster().ShouldPauseReconcile()
})
}

func CondPDGroupHasBeenDeleted(ctx PDGroupState) task.Condition {
return task.CondFunc(func() bool {
return ctx.PDGroup() == nil
})
}

func CondPDGroupIsDeleting(ctx PDGroupState) task.Condition {
return task.CondFunc(func() bool {
return !ctx.PDGroup().GetDeletionTimestamp().IsZero()
})
}
17 changes: 13 additions & 4 deletions pkg/controllers/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ type ObjectList[T any] interface {
}

type (
PDInitializer = ResourceInitializer[*v1alpha1.PD]
PDInitializer = ResourceInitializer[v1alpha1.PD]

ClusterInitializer = ResourceInitializer[*v1alpha1.Cluster]
ClusterInitializer = ResourceInitializer[v1alpha1.Cluster]

PodInitializer = ResourceInitializer[*corev1.Pod]
PDSliceInitializer = ResourceSliceInitializer[*v1alpha1.PD]
PodInitializer = ResourceInitializer[corev1.Pod]
PDSliceInitializer = ResourceSliceInitializer[v1alpha1.PD]
PDGroupInitializer = ResourceInitializer[v1alpha1.PDGroup]
)

type PDStateInitializer interface {
Expand All @@ -64,6 +65,14 @@ type PodState interface {
Pod() *corev1.Pod
}

type PDGroupStateInitializer interface {
PDGroupInitializer() PDGroupInitializer
}

type PDGroupState interface {
PDGroup() *v1alpha1.PDGroup
}

type PDSliceStateInitializer interface {
PDSliceInitializer() PDSliceInitializer
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/common/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (f *fakeState[T]) Object() *T {
return f.obj
}

func (f *fakeState[T]) Initializer() ResourceInitializer[*T] {
func (f *fakeState[T]) Initializer() ResourceInitializer[T] {
return NewResource(func(obj *T) { f.obj = obj }).
WithNamespace(Namespace(f.ns)).
WithName(Name(f.name)).
Expand All @@ -47,7 +47,7 @@ func (f *fakeSliceState[T]) Slice() []*T {
return f.objs
}

func (f *fakeSliceState[T]) Initializer() ResourceSliceInitializer[*T] {
func (f *fakeSliceState[T]) Initializer() ResourceSliceInitializer[T] {
return NewResourceSlice(func(objs []*T) { f.objs = objs }).
WithNamespace(Namespace(f.ns)).
WithLabels(Labels(f.labels)).
Expand Down
16 changes: 8 additions & 8 deletions pkg/controllers/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (f SetFunc[T]) Set(obj T) {

type ResourceInitializer[T any] interface {
GetOptions
Setter[T]
Setter[*T]
}

type Resource[T any] interface {
Expand All @@ -91,19 +91,19 @@ type Resource[T any] interface {
Initializer() ResourceInitializer[T]
}

func NewResource[T any](setter SetFunc[T]) Resource[T] {
func NewResource[T any](setter SetFunc[*T]) Resource[T] {
return &resource[T]{
setter: setter,
}
}

type resource[T any] struct {
setter Setter[T]
setter Setter[*T]
ns NamespaceOption
name NameOption
}

func (r *resource[T]) Set(obj T) {
func (r *resource[T]) Set(obj *T) {
r.setter.Set(obj)
}

Expand Down Expand Up @@ -131,7 +131,7 @@ func (r *resource[T]) Initializer() ResourceInitializer[T] {

type ResourceSliceInitializer[T any] interface {
ListOptions
Setter[[]T]
Setter[[]*T]
}

type ResourceSlice[T any] interface {
Expand All @@ -140,7 +140,7 @@ type ResourceSlice[T any] interface {
Initializer() ResourceSliceInitializer[T]
}

func NewResourceSlice[T any](setter SetFunc[[]T]) ResourceSlice[T] {
func NewResourceSlice[T any](setter SetFunc[[]*T]) ResourceSlice[T] {
return &resourceSlice[T]{
setter: setter,
}
Expand All @@ -149,7 +149,7 @@ func NewResourceSlice[T any](setter SetFunc[[]T]) ResourceSlice[T] {
type resourceSlice[T any] struct {
ns NamespaceOption
labels LabelsOption
setter Setter[[]T]
setter Setter[[]*T]
}

func (r *resourceSlice[T]) Namespace() string {
Expand All @@ -160,7 +160,7 @@ func (r *resourceSlice[T]) Labels() map[string]string {
return r.labels.Labels()
}

func (r *resourceSlice[T]) Set(objs []T) {
func (r *resourceSlice[T]) Set(objs []*T) {
r.setter.Set(objs)
}

Expand Down
23 changes: 12 additions & 11 deletions pkg/controllers/common/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"
)

func TestResource(t *testing.T) {
Expand Down Expand Up @@ -56,14 +57,14 @@ func TestResource(t *testing.T) {
tt.Parallel()

var obj int
r := NewResource(func(t int) {
obj = t
r := NewResource(func(t *int) {
obj = *t
}).
WithNamespace(c.ns).
WithName(c.name).
Initializer()

r.Set(c.obj)
r.Set(&c.obj)

assert.Equal(tt, c.expectedNs, r.Namespace(), c.desc)
assert.Equal(tt, c.expectedName, r.Name(), c.desc)
Expand All @@ -77,32 +78,32 @@ func TestResourceSlice(t *testing.T) {
desc string
ns NamespaceOption
labels LabelsOption
objs []int
objs []*int
expectedNs string
expectedLabels map[string]string
expectedObjs []int
expectedObjs []*int
}{
{
desc: "normal",
ns: Namespace("aaa"),
labels: Labels(map[string]string{"xxx": "yyy"}),
objs: []int{42},
objs: []*int{ptr.To(42)},
expectedNs: "aaa",
expectedLabels: map[string]string{
"xxx": "yyy",
},
expectedObjs: []int{42},
expectedObjs: []*int{ptr.To(42)},
},
{
desc: "use func",
ns: NameFunc(func() string { return "aaa" }),
labels: LabelsFunc(func() map[string]string { return map[string]string{"xxx": "yyy"} }),
objs: []int{42},
objs: []*int{ptr.To(42)},
expectedNs: "aaa",
expectedLabels: map[string]string{
"xxx": "yyy",
},
expectedObjs: []int{42},
expectedObjs: []*int{ptr.To(42)},
},
}

Expand All @@ -111,8 +112,8 @@ func TestResourceSlice(t *testing.T) {
t.Run(c.desc, func(tt *testing.T) {
tt.Parallel()

var objs []int
r := NewResourceSlice(func(t []int) {
var objs []*int
r := NewResourceSlice(func(t []*int) {
objs = t
}).
WithNamespace(c.ns).
Expand Down
Loading

0 comments on commit 24f21f2

Please sign in to comment.