Skip to content

Commit 8217833

Browse files
cjc7373leon-inf
andauthored
fix: adopt restored pvc in instanceset (#9882)
Co-authored-by: Leon <[email protected]>
1 parent 68671b0 commit 8217833

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
@@ -4482,9 +4482,23 @@ spec:
44824482
volumeClaimTemplates:
44834483
description: |-
44844484
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
4485-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
4486-
size, and access modes.
4487-
These templates are used to dynamically provision persistent volumes for the Component.
4485+
4486+
4487+
Each template defines the desired characteristics of a persistent volume, such as storage class,
4488+
size, and access modes, used for dynamic provisioning.
4489+
4490+
4491+
PVC Adoption Mechanism:
4492+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
4493+
before the Cluster is created:
4494+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
4495+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
4496+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
4497+
3. Ownership: The PVC must not have any existing controller reference.
4498+
4499+
4500+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
4501+
set the Component (or its controlled resources) as the owner/controller reference.
44884502
items:
44894503
properties:
44904504
annotations:
@@ -15841,9 +15855,23 @@ spec:
1584115855
volumeClaimTemplates:
1584215856
description: |-
1584315857
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
15844-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
15845-
size, and access modes.
15846-
These templates are used to dynamically provision persistent volumes for the Component.
15858+
15859+
15860+
Each template defines the desired characteristics of a persistent volume, such as storage class,
15861+
size, and access modes, used for dynamic provisioning.
15862+
15863+
15864+
PVC Adoption Mechanism:
15865+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
15866+
before the Cluster is created:
15867+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
15868+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
15869+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
15870+
3. Ownership: The PVC must not have any existing controller reference.
15871+
15872+
15873+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
15874+
set the Component (or its controlled resources) as the owner/controller reference.
1584715875
items:
1584815876
properties:
1584915877
annotations:

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4482,9 +4482,23 @@ spec:
44824482
volumeClaimTemplates:
44834483
description: |-
44844484
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
4485-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
4486-
size, and access modes.
4487-
These templates are used to dynamically provision persistent volumes for the Component.
4485+
4486+
4487+
Each template defines the desired characteristics of a persistent volume, such as storage class,
4488+
size, and access modes, used for dynamic provisioning.
4489+
4490+
4491+
PVC Adoption Mechanism:
4492+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
4493+
before the Cluster is created:
4494+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
4495+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
4496+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
4497+
3. Ownership: The PVC must not have any existing controller reference.
4498+
4499+
4500+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
4501+
set the Component (or its controlled resources) as the owner/controller reference.
44884502
items:
44894503
properties:
44904504
annotations:
@@ -15841,9 +15855,23 @@ spec:
1584115855
volumeClaimTemplates:
1584215856
description: |-
1584315857
Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
15844-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
15845-
size, and access modes.
15846-
These templates are used to dynamically provision persistent volumes for the Component.
15858+
15859+
15860+
Each template defines the desired characteristics of a persistent volume, such as storage class,
15861+
size, and access modes, used for dynamic provisioning.
15862+
15863+
15864+
PVC Adoption Mechanism:
15865+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
15866+
before the Cluster is created:
15867+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
15868+
$(vct-name)-$(pod-name) (e.g., "data-mycluster-mysql-0").
15869+
2. Labeling: The PVC must carry the label "app.kubernetes.io/managed-by=kubeblocks".
15870+
3. Ownership: The PVC must not have any existing controller reference.
15871+
15872+
15873+
If these conditions are met, KubeBlocks will automatically take over the PVCs and
15874+
set the Component (or its controlled resources) as the owner/controller reference.
1584715875
items:
1584815876
properties:
1584915877
annotations:

docs/developer_docs/api-reference/cluster.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,10 +3163,18 @@ It allows defining the CPU, memory requirements and limits for the Component&rsq
31633163
</td>
31643164
<td>
31653165
<em>(Optional)</em>
3166-
<p>Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.
3167-
Each template specifies the desired characteristics of a persistent volume, such as storage class,
3168-
size, and access modes.
3169-
These templates are used to dynamically provision persistent volumes for the Component.</p>
3166+
<p>Specifies a list of PersistentVolumeClaim templates that represent the storage requirements for the Component.</p>
3167+
<p>Each template defines the desired characteristics of a persistent volume, such as storage class,
3168+
size, and access modes, used for dynamic provisioning.</p>
3169+
<p>PVC Adoption Mechanism:
3170+
KubeBlocks supports adopting existing PVCs (static provisioning) if they meet the following criteria
3171+
before the Cluster is created:
3172+
1. Naming: The PVC name must follow the KubeBlocks naming convention:
3173+
$(vct-name)-$(pod-name) (e.g., &ldquo;data-mycluster-mysql-0&rdquo;).
3174+
2. Labeling: The PVC must carry the label &ldquo;app.kubernetes.io/managed-by=kubeblocks&rdquo;.
3175+
3. Ownership: The PVC must not have any existing controller reference.</p>
3176+
<p>If these conditions are met, KubeBlocks will automatically take over the PVCs and
3177+
set the Component (or its controlled resources) as the owner/controller reference.</p>
31703178
</td>
31713179
</tr>
31723180
<tr>

pkg/controller/instanceset/instance_util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ func buildInstancePVCByTemplate(name string, template *instancetemplate.Instance
515515
return pvcs, nil
516516
}
517517

518+
func tryTakeOverExternalPVC(its *workloads.InstanceSet, pvc *corev1.PersistentVolumeClaim) error {
519+
if controllerutil.HasControllerReference(pvc) {
520+
return nil
521+
}
522+
return controllerutil.SetControllerReference(its, pvc, model.GetScheme())
523+
}
524+
518525
// copyAndMerge merges two objects for updating:
519526
// 1. new an object targetObj by copying from oldObj
520527
// 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
@@ -168,6 +168,9 @@ func (r *instanceAlignmentReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (
168168
default:
169169
pvcObj := copyAndMerge(oldPvc, pvc)
170170
if pvcObj != nil {
171+
if err := tryTakeOverExternalPVC(its, pvcObj.(*corev1.PersistentVolumeClaim)); err != nil {
172+
return kubebuilderx.Continue, err
173+
}
171174
if err = tree.Update(pvcObj); err != nil {
172175
return kubebuilderx.Continue, err
173176
}

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
@@ -246,6 +246,7 @@ func (b *PlanBuilder) defaultWalkFunc(v graph.Vertex) error {
246246
if vertex.Action == nil {
247247
return errors.New("vertex action can't be nil")
248248
}
249+
b.transCtx.logger.V(5).Info("action for vertex", "vertex", vertex.String())
249250
ctx := b.transCtx.ctx
250251
switch *vertex.Action {
251252
case model.CREATE:

0 commit comments

Comments
 (0)