Skip to content

Commit ef00ae4

Browse files
cjc7373leon-inf
authored andcommitted
fix: adopt restored pvc in instanceset (#9882)
Co-authored-by: Leon <[email protected]> (cherry picked from commit 8217833)
1 parent 481bdb1 commit ef00ae4

File tree

9 files changed

+105
-27
lines changed

9 files changed

+105
-27
lines changed

apis/apps/v1/cluster_types.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,20 @@ type ClusterComponentSpec struct {
355355
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
356356

357357
// Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
358-
// Each template specifies the desired characteristics of a persistent volume, such as storage class,
359-
// size, and access modes.
360-
// These templates are used to dynamically provision persistent volumes for the Component.
358+
//
359+
// Each template defines the desired characteristics of a persistent volume, such as storage class,
360+
// size, and access modes, used for dynamic provisioning.
361+
//
362+
// PVC Adoption Mechanism:
363+
// KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
364+
// before the Cluster is created:
365+
// 1. Naming: The PVC name must follow the KubeBlocks naming convention:
366+
// $(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
367+
// 2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
368+
// 3. Ownership: The PVC must not have any existing controller reference.
369+
//
370+
// If these conditions are met, KubeBlocks will automatically take over the PVCs and
371+
// set the Component (or its controlled resources) as the owner/controller reference.
361372
//
362373
// +patchMergeKey=name
363374
// +patchStrategy=merge,retainKeys

config/crd/bases/apps.kubeblocks.io_clusters.yaml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,9 +4201,23 @@ spec:
42014201
volumeClaimTemplates:
42024202
description: |-
42034203
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
4204-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
4205-
size, and access modes.
4206-
These templates are used to dynamically provision persistent volumes for the Component.
4204+
4205+
4206+
Each template defines the desired characteristics of a persistent volume, such as storage class,
4207+
size, and access modes, used for dynamic provisioning.
4208+
4209+
4210+
PVC Adoption Mechanism:
4211+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
4212+
before the Cluster is created:
4213+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
4214+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
4215+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
4216+
3. Ownership: The PVC must not have any existing controller reference.
4217+
4218+
4219+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
4220+
set the Component (or its controlled resources) as the owner/controller reference.
42074221
items:
42084222
properties:
42094223
annotations:
@@ -11731,9 +11745,23 @@ spec:
1173111745
volumeClaimTemplates:
1173211746
description: |-
1173311747
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
11734-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
11735-
size, and access modes.
11736-
These templates are used to dynamically provision persistent volumes for the Component.
11748+
11749+
11750+
Each template defines the desired characteristics of a persistent volume, such as storage class,
11751+
size, and access modes, used for dynamic provisioning.
11752+
11753+
11754+
PVC Adoption Mechanism:
11755+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
11756+
before the Cluster is created:
11757+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
11758+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
11759+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
11760+
3. Ownership: The PVC must not have any existing controller reference.
11761+
11762+
11763+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
11764+
set the Component (or its controlled resources) as the owner/controller reference.
1173711765
items:
1173811766
properties:
1173911767
annotations:

deploy/helm/crds/apps.kubeblocks.io_clusters.yaml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,9 +4201,23 @@ spec:
42014201
volumeClaimTemplates:
42024202
description: |-
42034203
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
4204-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
4205-
size, and access modes.
4206-
These templates are used to dynamically provision persistent volumes for the Component.
4204+
4205+
4206+
Each template defines the desired characteristics of a persistent volume, such as storage class,
4207+
size, and access modes, used for dynamic provisioning.
4208+
4209+
4210+
PVC Adoption Mechanism:
4211+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
4212+
before the Cluster is created:
4213+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
4214+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
4215+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
4216+
3. Ownership: The PVC must not have any existing controller reference.
4217+
4218+
4219+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
4220+
set the Component (or its controlled resources) as the owner/controller reference.
42074221
items:
42084222
properties:
42094223
annotations:
@@ -11731,9 +11745,23 @@ spec:
1173111745
volumeClaimTemplates:
1173211746
description: |-
1173311747
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
11734-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
11735-
size, and access modes.
11736-
These templates are used to dynamically provision persistent volumes for the Component.
11748+
11749+
11750+
Each template defines the desired characteristics of a persistent volume, such as storage class,
11751+
size, and access modes, used for dynamic provisioning.
11752+
11753+
11754+
PVC Adoption Mechanism:
11755+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
11756+
before the Cluster is created:
11757+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
11758+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
11759+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
11760+
3. Ownership: The PVC must not have any existing controller reference.
11761+
11762+
11763+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
11764+
set the Component (or its controlled resources) as the owner/controller reference.
1173711765
items:
1173811766
properties:
1173911767
annotations:

docs/developer_docs/api-reference/cluster.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,10 +3135,18 @@ It allows defining the CPU, memory requirements and limits for the Component&rsq
31353135
</td>
31363136
<td>
31373137
<em>(Optional)</em>
3138-
<p>Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
3139-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
3140-
size, and access modes.
3141-
These templates are used to dynamically provision persistent volumes for the Component.</p>
3138+
<p>Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.</p>
3139+
<p>Each template defines the desired characteristics of a persistent volume, such as storage class,
3140+
size, and access modes, used for dynamic provisioning.</p>
3141+
<p>PVC Adoption Mechanism:
3142+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
3143+
before the Cluster is created:
3144+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
3145+
$(vct-name)-$(pod-name) (e.g., &ldquo;data-mycluster-mysql-0&rdquo;).
3146+
2. Labeling: The PVC must carry the label &ldquo;app.kubernetes.io/managed-by=kubeblocks&rdquo;.
3147+
3. Ownership: The PVC must not have any existing controller reference.</p>
3148+
<p>If these conditions are met, KubeBlocks will automatically take over the PVCs and
3149+
set the Component (or its controlled resources) as the owner/controller reference.</p>
31423150
</td>
31433151
</tr>
31443152
<tr>

pkg/controller/instanceset/instance_util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,13 @@ func buildInstancePVCByTemplate(name string, template *instanceTemplateExt, pare
613613
return pvcs
614614
}
615615

616+
func tryTakeOverExternalPVC(its *workloads.InstanceSet, pvc *corev1.PersistentVolumeClaim) error {
617+
if controllerutil.HasControllerReference(pvc) {
618+
return nil
619+
}
620+
return controllerutil.SetControllerReference(its, pvc, model.GetScheme())
621+
}
622+
616623
// copyAndMerge merges two objects for updating:
617624
// 1. new an object targetObj by copying from oldObj
618625
// 2. merge all fields can be updated from newObj into targetObj

pkg/controller/instanceset/reconciler_instance_alignment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ func (r *instanceAlignmentReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (
162162
default:
163163
pvcObj := copyAndMerge(oldPvc, pvc)
164164
if pvcObj != nil {
165+
if err := tryTakeOverExternalPVC(its, pvcObj.(*corev1.PersistentVolumeClaim)); err != nil {
166+
return kubebuilderx.Continue, err
167+
}
165168
if err = tree.Update(pvcObj); err != nil {
166169
return kubebuilderx.Continue, err
167170
}

pkg/controller/instanceset/tree_loader.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"context"
2424

2525
"github.com/go-logr/logr"
26-
batchv1 "k8s.io/api/batch/v1"
2726
corev1 "k8s.io/api/core/v1"
2827
"k8s.io/apimachinery/pkg/types"
2928
"k8s.io/client-go/tools/record"
@@ -85,7 +84,6 @@ func ownedKinds() []client.ObjectList {
8584
&corev1.ConfigMapList{},
8685
&corev1.PodList{},
8786
&corev1.PersistentVolumeClaimList{},
88-
&batchv1.JobList{},
8987
}
9088
}
9189

pkg/controller/instanceset/tree_loader_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
. "github.com/onsi/gomega"
2727

2828
"github.com/golang/mock/gomock"
29-
batchv1 "k8s.io/api/batch/v1"
3029
corev1 "k8s.io/api/core/v1"
3130
ctrl "sigs.k8s.io/controller-runtime"
3231
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -84,11 +83,6 @@ var _ = Describe("tree loader test", func() {
8483
DoAndReturn(func(_ context.Context, list *corev1.PersistentVolumeClaimList, _ ...client.ListOption) error {
8584
return nil
8685
}).Times(1)
87-
k8sMock.EXPECT().
88-
List(gomock.Any(), &batchv1.JobList{}, gomock.Any()).
89-
DoAndReturn(func(_ context.Context, list *batchv1.JobList, _ ...client.ListOption) error {
90-
return nil
91-
}).Times(1)
9286
k8sMock.EXPECT().
9387
Get(gomock.Any(), gomock.Any(), &corev1.ConfigMap{}, gomock.Any()).
9488
DoAndReturn(func(_ context.Context, objKey client.ObjectKey, obj *corev1.ConfigMap, _ ...client.GetOption) error {

pkg/controller/kubebuilderx/plan_builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ func (b *PlanBuilder) defaultWalkFunc(v graph.Vertex) error {
236236
if vertex.Action == nil {
237237
return errors.New("vertex action can't be nil")
238238
}
239+
b.transCtx.logger.V(5).Info("action for vertex", "vertex", vertex.String())
239240
ctx := b.transCtx.ctx
240241
switch *vertex.Action {
241242
case model.CREATE:

0 commit comments

Comments
 (0)