Skip to content

Commit 532f11a

Browse files
committed
fix
1 parent 2c6a10a commit 532f11a

File tree

11 files changed

+519
-257
lines changed

11 files changed

+519
-257
lines changed

apis/workloads/v1/instance_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ type InstanceAssistantObject struct {
189189
ConfigMap *corev1.ConfigMap `json:"configMap,omitempty"`
190190
Secret *corev1.Secret `json:"secret,omitempty"`
191191
ServiceAccount *corev1.ServiceAccount `json:"serviceAccount,omitempty"`
192-
Role *rbacv1.Role `json:"clusterRole,omitempty"`
192+
Role *rbacv1.Role `json:"role,omitempty"`
193193
RoleBinding *rbacv1.RoleBinding `json:"roleBinding,omitempty"`
194194
}

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

Lines changed: 234 additions & 73 deletions
Large diffs are not rendered by default.

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

Lines changed: 234 additions & 73 deletions
Large diffs are not rendered by default.

docs/developer_docs/api-reference/cluster.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32276,7 +32276,7 @@ Kubernetes core/v1.ServiceAccount
3227632276
</tr>
3227732277
<tr>
3227832278
<td>
32279-
<code>clusterRole</code><br/>
32279+
<code>role</code><br/>
3228032280
<em>
3228132281
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#role-v1-rbac">
3228232282
Kubernetes rbac/v1.Role

pkg/controller/instance/pod_role_event_handler.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package instance
2222
import (
2323
"encoding/json"
2424
"fmt"
25-
"strconv"
2625
"strings"
2726

2827
corev1 "k8s.io/api/core/v1"
@@ -57,7 +56,7 @@ func (h *PodRoleEventHandler) Handle(cli client.Client, reqCtx intctrlutil.Reque
5756
return err
5857
}
5958

60-
// event order is crucial in role probing, but it's not guaranteed when controller restarted, so we have to mark them to be filtered
59+
// event order is crucial in role probing, but it's not guaranteed when controller restarted, so we have to mark them to be handled
6160
patch := client.MergeFrom(event.DeepCopy())
6261
if event.Annotations == nil {
6362
event.Annotations = make(map[string]string)
@@ -97,15 +96,6 @@ func (h *PodRoleEventHandler) handleRoleChangedEvent(cli client.Client, reqCtx i
9796
return nil
9897
}
9998

100-
snapshotVersion := strconv.FormatInt(event.EventTime.UnixMicro(), 10)
101-
lastSnapshotVersion, ok := pod.Annotations[constant.LastRoleSnapshotVersionAnnotationKey]
102-
if ok {
103-
if snapshotVersion <= lastSnapshotVersion && !strings.Contains(lastSnapshotVersion, ":") {
104-
reqCtx.Log.Info("stale role probe event received, ignore it")
105-
return nil
106-
}
107-
}
108-
10999
var instName string
110100
if pod.Labels != nil {
111101
if n, ok := pod.Labels[constant.KBAppInstanceNameLabelKey]; ok {
@@ -119,11 +109,11 @@ func (h *PodRoleEventHandler) handleRoleChangedEvent(cli client.Client, reqCtx i
119109

120110
role := strings.ToLower(string(probeEvent.Output))
121111
reqCtx.Log.Info("handle role change event", "pod", pod.Name, "role", role)
122-
return h.updatePodRoleLabel(cli, reqCtx, inst, pod, role, snapshotVersion)
112+
return h.updatePodRoleLabel(cli, reqCtx, inst, pod, role)
123113
}
124114

125115
func (h *PodRoleEventHandler) updatePodRoleLabel(cli client.Client, reqCtx intctrlutil.RequestCtx,
126-
inst *workloads.Instance, pod *corev1.Pod, roleName string, snapshotVersion string) error {
116+
inst *workloads.Instance, pod *corev1.Pod, roleName string) error {
127117
var (
128118
newPod = pod.DeepCopy()
129119
roleMap = composeRoleMap(inst)
@@ -136,10 +126,5 @@ func (h *PodRoleEventHandler) updatePodRoleLabel(cli client.Client, reqCtx intct
136126
case false:
137127
delete(newPod.Labels, constant.RoleLabelKey)
138128
}
139-
140-
if newPod.Annotations == nil {
141-
newPod.Annotations = map[string]string{}
142-
}
143-
newPod.Annotations[constant.LastRoleSnapshotVersionAnnotationKey] = snapshotVersion
144129
return cli.Update(reqCtx.Ctx, newPod)
145130
}

pkg/controller/instance/reconciler_alignment.go

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import (
2323
corev1 "k8s.io/api/core/v1"
2424
"k8s.io/apimachinery/pkg/util/sets"
2525

26-
kbappsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
2726
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
28-
"github.com/apecloud/kubeblocks/pkg/constant"
2927
"github.com/apecloud/kubeblocks/pkg/controller/kubebuilderx"
3028
"github.com/apecloud/kubeblocks/pkg/controller/model"
3129
)
@@ -51,75 +49,59 @@ func (r *alignmentReconciler) PreCondition(tree *kubebuilderx.ObjectTree) *kubeb
5149
func (r *alignmentReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilderx.Result, error) {
5250
inst := tree.GetRoot().(*workloads.Instance)
5351

54-
newNameSet := sets.New[string](podName(inst))
52+
// create pod
5553
obj, err := tree.Get(podObj(inst))
5654
if err != nil {
5755
return kubebuilderx.Continue, err
5856
}
59-
60-
oldNameSet := sets.New[string]()
61-
oldPodList := tree.List(&corev1.Pod{})
62-
oldPVCList := tree.List(&corev1.PersistentVolumeClaim{})
63-
for _, object := range oldPodList {
64-
oldNameSet.Insert(object.GetName())
65-
}
66-
deleteNameSet := oldNameSet.Difference(newNameSet)
67-
6857
if obj == nil {
69-
// create pod
7058
newPod, err := buildInstancePod(inst, "")
7159
if err != nil {
7260
return kubebuilderx.Continue, err
7361
}
7462
if err := tree.Add(newPod); err != nil {
7563
return kubebuilderx.Continue, err
7664
}
65+
}
7766

78-
// create PVCs
79-
pvcs, err := buildInstancePVCs(inst)
80-
if err != nil {
81-
return kubebuilderx.Continue, err
82-
}
83-
for _, pvc := range pvcs {
84-
switch oldPvc, err := tree.Get(pvc); {
85-
case err != nil:
86-
return kubebuilderx.Continue, err
87-
case oldPvc == nil:
88-
if err = tree.Add(pvc); err != nil {
89-
return kubebuilderx.Continue, err
90-
}
91-
default:
92-
// TODO: do not update PVC here
93-
pvcObj := copyAndMerge(oldPvc, pvc)
94-
if pvcObj != nil {
95-
if err = tree.Update(pvcObj); err != nil {
96-
return kubebuilderx.Continue, err
97-
}
98-
}
99-
}
100-
}
67+
// handle pvcs
68+
newPVCList, err := buildInstancePVCs(inst)
69+
if err != nil {
70+
return kubebuilderx.Continue, err
71+
}
72+
oldPVCList := tree.List(&corev1.PersistentVolumeClaim{})
73+
74+
newPVCs, oldPVCs := map[string]*corev1.PersistentVolumeClaim{}, map[string]*corev1.PersistentVolumeClaim{}
75+
newPVCNameSet, oldPVCNameSet := sets.New[string](), sets.New[string]()
76+
for i, pvc := range newPVCList {
77+
newPVCs[pvc.Name] = newPVCList[i]
78+
newPVCNameSet.Insert(pvc.Name)
79+
}
80+
for i, pvc := range oldPVCList {
81+
oldPVCs[pvc.GetName()] = oldPVCList[i].(*corev1.PersistentVolumeClaim)
82+
oldPVCNameSet.Insert(pvc.GetName())
10183
}
10284

103-
// delete useless pod & PVCs
104-
for _, object := range oldPodList {
105-
pod, _ := object.(*corev1.Pod)
106-
if _, ok := deleteNameSet[pod.Name]; !ok {
107-
continue
85+
createSet := newPVCNameSet.Difference(oldPVCNameSet)
86+
deleteSet := oldPVCNameSet.Difference(newPVCNameSet)
87+
updateSet := newPVCNameSet.Intersection(oldPVCNameSet)
88+
89+
for pvcName := range deleteSet {
90+
if err = tree.Delete(oldPVCs[pvcName]); err != nil {
91+
return kubebuilderx.Continue, err
10892
}
109-
if err = tree.Delete(pod); err != nil {
93+
}
94+
for pvcName := range createSet {
95+
if err = tree.Add(newPVCs[pvcName]); err != nil {
11096
return kubebuilderx.Continue, err
11197
}
112-
113-
retentionPolicy := inst.Spec.PersistentVolumeClaimRetentionPolicy
114-
// the default policy is `Delete`
115-
if retentionPolicy == nil || retentionPolicy.WhenScaled != kbappsv1.RetainPersistentVolumeClaimRetentionPolicyType {
116-
for _, pobj := range oldPVCList {
117-
pvc := pobj.(*corev1.PersistentVolumeClaim)
118-
if pvc.Labels != nil && pvc.Labels[constant.KBAppPodNameLabelKey] == pod.Name {
119-
if err = tree.Delete(pvc); err != nil {
120-
return kubebuilderx.Continue, err
121-
}
122-
}
98+
}
99+
for pvcName := range updateSet {
100+
// TODO: do not update PVC here
101+
pvcObj := copyAndMerge(oldPVCs[pvcName], newPVCs[pvcName])
102+
if pvcObj != nil {
103+
if err = tree.Update(pvcObj); err != nil {
104+
return kubebuilderx.Continue, err
123105
}
124106
}
125107
}

pkg/controller/instance/reconciler_assistant_object.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ func (r *assistantObjectReconciler) createOrUpdate(tree *kubebuilderx.ObjectTree
7171
}
7272
if err != nil || robj == nil {
7373
labels := obj.GetLabels()
74-
maps.Copy(labels, getMatchLabels(inst.Name))
74+
if labels == nil {
75+
labels = getMatchLabels(inst.Name)
76+
} else {
77+
maps.Copy(labels, getMatchLabels(inst.Name))
78+
}
7579
obj.SetLabels(labels)
7680
if err := controllerutil.SetControllerReference(inst, obj, model.GetScheme()); err != nil {
7781
return false, err

pkg/controller/instance/reconciler_update.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ import (
3838
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
3939
)
4040

41-
// updateReconciler handles the updates of instances based on the UpdateStrategy.
42-
// Currently, two update strategies are supported: 'OnDelete' and 'RollingUpdate'.
4341
type updateReconciler struct{}
4442

4543
var _ kubebuilderx.Reconciler = &updateReconciler{}

pkg/controller/instance/revision_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const (
4848
var (
4949
codecs = serializer.NewCodecFactory(model.GetScheme())
5050
patchCodec = codecs.LegacyCodec(workloads.SchemeGroupVersion)
51-
controllerKind = appsv1.SchemeGroupVersion.WithKind("StatefulSet")
51+
controllerKind = appsv1.SchemeGroupVersion.WithKind("Instance")
5252
)
5353

5454
func newRevision(inst *workloads.Instance) (*appsv1.ControllerRevision, error) {

pkg/controller/instanceset2/instance_util.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,31 +220,7 @@ func copyAndMergeInstance(oldInst, newInst *workloads.Instance) *workloads.Insta
220220
targetInst.Spec.MinReadySeconds = newInst.Spec.MinReadySeconds
221221

222222
// merge pvcs
223-
for i := range newInst.Spec.VolumeClaimTemplates {
224-
newPVC := &newInst.Spec.VolumeClaimTemplates[i]
225-
oldPVC := &targetInst.Spec.VolumeClaimTemplates[i]
226-
mergeMap(&newPVC.Labels, &oldPVC.Labels)
227-
mergeMap(&newPVC.Annotations, &oldPVC.Annotations)
228-
// resources.request.storage and accessModes support in-place update.
229-
// resources.request.storage only supports volume expansion.
230-
if reflect.DeepEqual(oldPVC.Spec.AccessModes, newPVC.Spec.AccessModes) &&
231-
oldPVC.Spec.Resources.Requests.Storage().Cmp(*newPVC.Spec.Resources.Requests.Storage()) >= 0 {
232-
continue
233-
}
234-
oldPVC.Spec.AccessModes = newPVC.Spec.AccessModes
235-
if newPVC.Spec.Resources.Requests == nil {
236-
continue
237-
}
238-
if _, ok := newPVC.Spec.Resources.Requests[corev1.ResourceStorage]; !ok {
239-
continue
240-
}
241-
requests := oldPVC.Spec.Resources.Requests
242-
if requests == nil {
243-
requests = make(corev1.ResourceList)
244-
}
245-
requests[corev1.ResourceStorage] = *newPVC.Spec.Resources.Requests.Storage()
246-
oldPVC.Spec.Resources.Requests = requests
247-
}
223+
targetInst.Spec.VolumeClaimTemplates = newInst.Spec.VolumeClaimTemplates
248224
targetInst.Spec.PersistentVolumeClaimRetentionPolicy = newInst.Spec.PersistentVolumeClaimRetentionPolicy
249225

250226
copyAndMergeService := func(old, new *corev1.Service) client.Object {

0 commit comments

Comments
 (0)