Skip to content

Commit acc02b3

Browse files
committed
update
1 parent 2cb78a8 commit acc02b3

File tree

13 files changed

+230
-196
lines changed

13 files changed

+230
-196
lines changed

apis/workloads/v1/instanceset_types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,20 +533,20 @@ type InstanceStatus struct {
533533
// +optional
534534
Role string `json:"role,omitempty"`
535535

536-
// Represents whether the instance is in volume expansion.
537-
//
538-
// +optional
539-
VolumeExpansion bool `json:"volumeExpansion,omitempty"`
540-
541536
// The status of configs.
542537
//
543538
// +optional
544539
Configs []InstanceConfigStatus `json:"configs,omitempty"`
545540

546-
// Joined indicates whether the instance is joined.
541+
// Represents whether the instance is joined the cluster.
547542
//
548543
// +optional
549544
Joined *bool `json:"joined,omitempty"`
545+
546+
// Represents whether the instance is in volume expansion.
547+
//
548+
// +optional
549+
VolumeExpansion bool `json:"volumeExpansion,omitempty"`
550550
}
551551

552552
type InstanceConfigStatus struct {

config/crd/bases/workloads.kubeblocks.io_instancesets.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13439,7 +13439,7 @@ spec:
1343913439
type: object
1344013440
type: array
1344113441
joined:
13442-
description: Joined indicates whether the instance is joined.
13442+
description: Represents whether the instance is joined the cluster.
1344313443
type: boolean
1344413444
podName:
1344513445
default: Unknown

deploy/helm/crds/workloads.kubeblocks.io_instancesets.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13439,7 +13439,7 @@ spec:
1343913439
type: object
1344013440
type: array
1344113441
joined:
13442-
description: Joined indicates whether the instance is joined.
13442+
description: Represents whether the instance is joined the cluster.
1344313443
type: boolean
1344413444
podName:
1344513445
default: Unknown

docs/developer_docs/api-reference/cluster.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33335,40 +33335,40 @@ string
3333533335
</tr>
3333633336
<tr>
3333733337
<td>
33338-
<code>volumeExpansion</code><br/>
33338+
<code>configs</code><br/>
3333933339
<em>
33340-
bool
33340+
<a href="#workloads.kubeblocks.io/v1.InstanceConfigStatus">
33341+
[]InstanceConfigStatus
33342+
</a>
3334133343
</em>
3334233344
</td>
3334333345
<td>
3334433346
<em>(Optional)</em>
33345-
<p>Represents whether the instance is in volume expansion.</p>
33347+
<p>The status of configs.</p>
3334633348
</td>
3334733349
</tr>
3334833350
<tr>
3334933351
<td>
33350-
<code>configs</code><br/>
33352+
<code>joined</code><br/>
3335133353
<em>
33352-
<a href="#workloads.kubeblocks.io/v1.InstanceConfigStatus">
33353-
[]InstanceConfigStatus
33354-
</a>
33354+
bool
3335533355
</em>
3335633356
</td>
3335733357
<td>
3335833358
<em>(Optional)</em>
33359-
<p>The status of configs.</p>
33359+
<p>Represents whether the instance is joined the cluster.</p>
3336033360
</td>
3336133361
</tr>
3336233362
<tr>
3336333363
<td>
33364-
<code>joined</code><br/>
33364+
<code>volumeExpansion</code><br/>
3336533365
<em>
3336633366
bool
3336733367
</em>
3336833368
</td>
3336933369
<td>
3337033370
<em>(Optional)</em>
33371-
<p>Joined indicates whether the instance is joined.</p>
33371+
<p>Represents whether the instance is in volume expansion.</p>
3337233372
</td>
3337333373
</tr>
3337433374
</tbody>

pkg/controller/instanceset/reconciler_instance_alignment.go

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package instanceset
2121

2222
import (
2323
"errors"
24+
"slices"
2425

2526
appsv1 "k8s.io/api/apps/v1"
2627
corev1 "k8s.io/api/core/v1"
@@ -145,7 +146,7 @@ func (r *instanceAlignmentReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (
145146
if err != nil {
146147
return kubebuilderx.Continue, err
147148
}
148-
if err := tree.AddWithOption(newPod, r.joinMemberHook(tree, its, oldInstanceList, newPod)); err != nil {
149+
if err := tree.AddWithOption(newPod, r.createInstance(tree, its, oldInstanceList, newPod)); err != nil {
149150
return kubebuilderx.Continue, err
150151
}
151152
currentAlignedNameList = append(currentAlignedNameList, name)
@@ -198,7 +199,7 @@ func (r *instanceAlignmentReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (
198199
its.Name,
199200
pod.Name)
200201
}
201-
if err := tree.DeleteWithOption(pod, r.leaveMemberHook(tree, its, oldInstanceList, pod)); err != nil {
202+
if err := tree.DeleteWithOption(pod, r.deleteInstance(tree, its, oldInstanceList, pod)); err != nil {
202203
return kubebuilderx.Continue, err
203204
}
204205

@@ -224,37 +225,30 @@ func (r *instanceAlignmentReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (
224225
return kubebuilderx.Continue, nil
225226
}
226227

227-
func (r *instanceAlignmentReconciler) joinMemberHook(tree *kubebuilderx.ObjectTree,
228-
its *workloads.InstanceSet, pods []client.Object, pod *corev1.Pod) kubebuilderx.WithHook {
228+
func (r *instanceAlignmentReconciler) createInstance(tree *kubebuilderx.ObjectTree,
229+
its *workloads.InstanceSet, pods []client.Object, pod *corev1.Pod) kubebuilderx.WithPrevHook {
229230
return func(obj client.Object) error {
230-
if its.Status.InitReplicas == nil || its.Status.ReadyInitReplicas == nil ||
231-
*its.Status.InitReplicas != *its.Status.ReadyInitReplicas {
232-
return nil // init replicas
233-
}
234-
if its.Spec.LifecycleActions == nil || its.Spec.LifecycleActions.MemberJoin == nil {
235-
return nil // member join not defined
236-
}
237-
joined := false
238-
for _, inst := range its.Status.InstanceStatus {
239-
if inst.PodName == pod.Name {
240-
joined = ptr.Deref(inst.Joined, false)
231+
joinMember := func(inst workloads.InstanceStatus) error {
232+
if its.Spec.LifecycleActions == nil || its.Spec.LifecycleActions.MemberJoin == nil {
233+
return nil
241234
}
235+
if ptr.Deref(inst.Joined, false) {
236+
return nil
237+
}
238+
// TODO: should wait for the data to be loaded before joining the member?
239+
return r.joinMember(tree, its, pods, pod)
242240
}
243-
if joined {
244-
return nil
245-
}
246-
247-
// TODO: should wait for the data to be loaded before joining the member?
248-
249-
if err := r.joinMember(tree, its, pods, pod); err != nil {
250-
return err
241+
idx := slices.IndexFunc(its.Status.InstanceStatus, func(status workloads.InstanceStatus) bool {
242+
return status.PodName == pod.Name
243+
})
244+
var err error
245+
if idx >= 0 {
246+
err = joinMember(its.Status.InstanceStatus[idx])
251247
}
252-
for i, inst := range its.Status.InstanceStatus {
253-
if inst.PodName == pod.Name {
254-
its.Status.InstanceStatus[i].Joined = ptr.To(true)
255-
}
248+
if err == nil {
249+
its.Status.InstanceStatus[idx].Joined = ptr.To(true)
256250
}
257-
return nil
251+
return err
258252
}
259253
}
260254

@@ -273,22 +267,29 @@ func (r *instanceAlignmentReconciler) joinMember(tree *kubebuilderx.ObjectTree,
273267
return nil
274268
}
275269

276-
func (r *instanceAlignmentReconciler) leaveMemberHook(tree *kubebuilderx.ObjectTree,
277-
its *workloads.InstanceSet, pods []client.Object, pod *corev1.Pod) kubebuilderx.WithHook {
270+
func (r *instanceAlignmentReconciler) deleteInstance(tree *kubebuilderx.ObjectTree,
271+
its *workloads.InstanceSet, pods []client.Object, pod *corev1.Pod) kubebuilderx.WithPostHook {
278272
return func(obj client.Object) error {
279-
hasMemberLeaveDefined := its.Spec.LifecycleActions != nil && its.Spec.LifecycleActions.MemberLeave != nil
280-
joined := func() bool {
281-
for _, inst := range its.Status.InstanceStatus {
282-
if inst.PodName == pod.Name {
283-
return ptr.Deref(inst.Joined, true)
284-
}
273+
leaveMember := func(inst workloads.InstanceStatus) error {
274+
if its.Spec.LifecycleActions == nil || its.Spec.LifecycleActions.MemberLeave == nil {
275+
return nil
285276
}
286-
return false
287-
}()
288-
if !hasMemberLeaveDefined || !joined {
289-
return nil
277+
if !ptr.Deref(inst.Joined, false) {
278+
return nil
279+
}
280+
return r.leaveMember(tree, its, pods, pod)
281+
}
282+
idx := slices.IndexFunc(its.Status.InstanceStatus, func(status workloads.InstanceStatus) bool {
283+
return status.PodName == pod.Name
284+
})
285+
var err error
286+
if idx >= 0 {
287+
err = leaveMember(its.Status.InstanceStatus[idx])
290288
}
291-
return r.leaveMember(tree, its, pods, pod)
289+
if err == nil {
290+
its.Status.InstanceStatus = slices.Delete(its.Status.InstanceStatus, idx, idx+1)
291+
}
292+
return err
292293
}
293294
}
294295

pkg/controller/instanceset/reconciler_revision_update.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package instanceset
2121

2222
import (
23+
"slices"
24+
2325
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/utils/ptr"
2427
"sigs.k8s.io/controller-runtime/pkg/client"
2528

2629
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
@@ -29,18 +32,20 @@ import (
2932
"github.com/apecloud/kubeblocks/pkg/controller/model"
3033
)
3134

35+
func NewRevisionUpdateReconciler() kubebuilderx.Reconciler {
36+
return &revisionUpdateReconciler{}
37+
}
38+
3239
// revisionUpdateReconciler is responsible for updating the expected instance names and their corresponding revisions in the status when there are changes in the spec.
3340
type revisionUpdateReconciler struct{}
3441

42+
var _ kubebuilderx.Reconciler = &revisionUpdateReconciler{}
43+
3544
type instanceRevision struct {
3645
name string
3746
revision string
3847
}
3948

40-
func NewRevisionUpdateReconciler() kubebuilderx.Reconciler {
41-
return &revisionUpdateReconciler{}
42-
}
43-
4449
func (r *revisionUpdateReconciler) PreCondition(tree *kubebuilderx.ObjectTree) *kubebuilderx.CheckResult {
4550
if tree.GetRoot() == nil || !model.IsObjectUpdating(tree.GetRoot()) {
4651
return kubebuilderx.ConditionUnsatisfied
@@ -89,11 +94,14 @@ func (r *revisionUpdateReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kub
8994
updateRevision = instanceRevisionList[len(instanceRevisionList)-1].revision
9095
}
9196
its.Status.UpdateRevision = updateRevision
92-
updatedReplicas, err := calculateUpdatedReplicas(its, tree.List(&corev1.Pod{}))
97+
98+
updatedReplicas, err := r.calculateUpdatedReplicas(its, tree.List(&corev1.Pod{}))
9399
if err != nil {
94100
return kubebuilderx.Continue, err
95101
}
96102
its.Status.UpdatedReplicas = updatedReplicas
103+
r.initReplicasNInstanceStatus(its, instanceRevisionList)
104+
97105
// The 'ObservedGeneration' field is used to indicate whether the revisions have been updated.
98106
// Computing these revisions in each reconciliation loop can be time-consuming, so we optimize it by
99107
// performing the computation only when the 'spec' is updated.
@@ -102,7 +110,7 @@ func (r *revisionUpdateReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kub
102110
return kubebuilderx.Continue, nil
103111
}
104112

105-
func calculateUpdatedReplicas(its *workloads.InstanceSet, pods []client.Object) (int32, error) {
113+
func (r *revisionUpdateReconciler) calculateUpdatedReplicas(its *workloads.InstanceSet, pods []client.Object) (int32, error) {
106114
updatedReplicas := int32(0)
107115
for i := range pods {
108116
pod, _ := pods[i].(*corev1.Pod)
@@ -113,9 +121,38 @@ func calculateUpdatedReplicas(its *workloads.InstanceSet, pods []client.Object)
113121
if updated {
114122
updatedReplicas++
115123
}
116-
117124
}
118125
return updatedReplicas, nil
119126
}
120127

121-
var _ kubebuilderx.Reconciler = &revisionUpdateReconciler{}
128+
func (r *revisionUpdateReconciler) initReplicasNInstanceStatus(its *workloads.InstanceSet, instances []instanceRevision) {
129+
if its.Status.InitReplicas == nil && ptr.Deref(its.Spec.Replicas, 0) > 0 {
130+
its.Status.InitReplicas = its.Spec.Replicas
131+
}
132+
if its.Status.InitReplicas == nil {
133+
return // init replicas is not set or set to 0
134+
}
135+
136+
init := false
137+
if *its.Status.InitReplicas != ptr.Deref(its.Status.ReadyInitReplicas, 0) { // in init phase
138+
init = true
139+
// in case the replicas is changed in the middle of init phase
140+
if ptr.Deref(its.Spec.Replicas, 0) == 0 {
141+
its.Status.InitReplicas = nil
142+
} else {
143+
its.Status.InitReplicas = its.Spec.Replicas
144+
}
145+
}
146+
147+
for _, inst := range instances {
148+
exist := slices.ContainsFunc(its.Status.InstanceStatus, func(status workloads.InstanceStatus) bool {
149+
return status.PodName == inst.name
150+
})
151+
if !exist {
152+
its.Status.InstanceStatus = append(its.Status.InstanceStatus, workloads.InstanceStatus{
153+
PodName: inst.name,
154+
Joined: ptr.To(init),
155+
})
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)