mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
fix(kubernetes): detach volumes when the infra PVC is missing
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Assisted-by: LLM
This commit is contained in:
1 parent
da7f2c2f50
commit
e623530c47
2 files changed
+71
-9
No files matched your search
@@ -424,7 +424,8 @@ func (w *WrappedControllerService) ControllerUnpublishVolume(ctx context.Context
|
||||
pvc, err := w.infraClient.CoreV1().PersistentVolumeClaims(w.infraNamespace).Get(ctx, dvName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return &csi.ControllerUnpublishVolumeResponse{}, nil
|
||||
// A failed PVC creation can still leave a hotplug entry in the VM.
|
||||
return w.ControllerService.ControllerUnpublishVolume(ctx, req)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -310,13 +310,14 @@ const (
|
||||
testVolumeName = "pvc-0"
|
||||
)
|
||||
|
||||
// teardownClient models the CAPI/CAPK teardown race: the VM is gone while its
|
||||
// VMI still reports the hot-plugged volume. The VMI drops the volume once
|
||||
// RemoveVolumeFromVMI is called, or when the garbage collector deletes a
|
||||
// VMI whose controlling VM is gone.
|
||||
// teardownClient keeps hotplug state until removal or garbage collection,
|
||||
// including when the VMI outlives its VM during CAPI/CAPK teardown.
|
||||
type teardownClient struct {
|
||||
kubevirtclient.Client
|
||||
vm *kubevirtv1.VirtualMachine
|
||||
vmiOwners []metav1.OwnerReference
|
||||
detached bool
|
||||
vmRemovals int
|
||||
vmiRemovals int
|
||||
lookups int
|
||||
}
|
||||
@@ -328,6 +329,9 @@ func (c *teardownClient) vmiGarbageCollected() bool {
|
||||
|
||||
func (c *teardownClient) GetWorkloadManagingVirtualMachine(context.Context, string, string) (*kubevirtv1.VirtualMachine, error) {
|
||||
c.lookups++
|
||||
if c.vm != nil {
|
||||
return c.vm, nil
|
||||
}
|
||||
return nil, k8serrors.NewNotFound(schema.GroupResource{Group: "kubevirt.io", Resource: "virtualmachines"}, testVMName)
|
||||
}
|
||||
|
||||
@@ -336,14 +340,27 @@ func (c *teardownClient) GetVirtualMachine(context.Context, string, string) (*ku
|
||||
vmi := &kubevirtv1.VirtualMachineInstance{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: testVMName, Namespace: testInfraNamespace, OwnerReferences: c.vmiOwners},
|
||||
}
|
||||
if c.vmiRemovals == 0 {
|
||||
vmi.Status.VolumeStatus = []kubevirtv1.VolumeStatus{{Name: testVolumeName, HotplugVolume: &kubevirtv1.HotplugVolumeStatus{}}}
|
||||
if !c.detached && c.vmRemovals == 0 && c.vmiRemovals == 0 {
|
||||
vmi.Status.VolumeStatus = []kubevirtv1.VolumeStatus{{Name: testVolumeName, Phase: kubevirtv1.VolumePending, Reason: "FailedPvcNotFound", HotplugVolume: &kubevirtv1.HotplugVolumeStatus{}}}
|
||||
}
|
||||
return vmi, nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) EnsureVolumeRemovedVM(context.Context, string, string, string) (bool, error) {
|
||||
return c.vm == nil || len(c.vm.Spec.Template.Spec.Volumes) == 0, nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) EnsureVolumeRemovedVMI(context.Context, string, string, string) (bool, error) {
|
||||
return c.vmiRemovals > 0, nil
|
||||
return c.detached || c.vmRemovals > 0 || c.vmiRemovals > 0, nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) RemoveVolumeFromVM(_ context.Context, namespace, name string, opts *kubevirtv1.RemoveVolumeOptions) error {
|
||||
if namespace != testInfraNamespace || name != testVMName || opts.Name != testVolumeName {
|
||||
return errors.New("unexpected hotplug removal target")
|
||||
}
|
||||
c.vmRemovals++
|
||||
c.vm.Spec.Template.Spec.Volumes = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) RemoveVolumeFromVMI(context.Context, string, string, *kubevirtv1.RemoveVolumeOptions) error {
|
||||
@@ -352,7 +369,7 @@ func (c *teardownClient) RemoveVolumeFromVMI(context.Context, string, string, *k
|
||||
}
|
||||
|
||||
func (c *teardownClient) EnsureVolumeRemoved(context.Context, string, string, string, time.Duration) error {
|
||||
if c.vmiRemovals > 0 || c.vmiGarbageCollected() {
|
||||
if c.detached || c.vmRemovals > 0 || c.vmiRemovals > 0 || c.vmiGarbageCollected() {
|
||||
return nil
|
||||
}
|
||||
return errors.New("timed out waiting for the VMI to drop the volume")
|
||||
@@ -410,6 +427,50 @@ func TestControllerUnpublishVolumeHonoursVMIHotplugFallback(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerUnpublishVolumeMissingPVC(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
vmGone bool
|
||||
detached bool
|
||||
wantVMCalls int
|
||||
wantVMICalls int
|
||||
}{
|
||||
{name: "pending hotplug volume", wantVMCalls: 1},
|
||||
{name: "already detached", detached: true},
|
||||
{name: "VM gone, VMI still has volume", vmGone: true, wantVMICalls: 1},
|
||||
{name: "VM gone, no attached volume", vmGone: true, detached: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
client := &teardownClient{detached: tt.detached}
|
||||
if !tt.vmGone {
|
||||
client.vm = &kubevirtv1.VirtualMachine{Spec: kubevirtv1.VirtualMachineSpec{Template: &kubevirtv1.VirtualMachineInstanceTemplateSpec{}}}
|
||||
if !tt.detached {
|
||||
client.vm.Spec.Template.Spec.Volumes = []kubevirtv1.Volume{{
|
||||
Name: testVolumeName,
|
||||
VolumeSource: kubevirtv1.VolumeSource{DataVolume: &kubevirtv1.DataVolumeSource{Name: testVolumeName, Hotpluggable: true}},
|
||||
}}
|
||||
}
|
||||
}
|
||||
w := &WrappedControllerService{
|
||||
ControllerService: service.NewControllerService(client, testInfraNamespace, nil, util.StorageClassEnforcement{AllowAll: true}, true),
|
||||
infraClient: fake.NewClientset(),
|
||||
infraNamespace: testInfraNamespace,
|
||||
}
|
||||
req := &csi.ControllerUnpublishVolumeRequest{VolumeId: testVolumeName, NodeId: testInfraNamespace + "/" + testVMName}
|
||||
if _, err := w.ControllerUnpublishVolume(context.Background(), req); err != nil {
|
||||
t.Fatalf("ControllerUnpublishVolume() error = %v", err)
|
||||
}
|
||||
if client.vmRemovals != tt.wantVMCalls || client.vmiRemovals != tt.wantVMICalls {
|
||||
t.Errorf("VM/VMI removevolume calls = %d/%d, want %d/%d", client.vmRemovals, client.vmiRemovals, tt.wantVMCalls, tt.wantVMICalls)
|
||||
}
|
||||
if client.vm != nil && len(client.vm.Spec.Template.Spec.Volumes) != 0 {
|
||||
t.Error("hotplug volume remains in VM spec")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func nfsCNP(owners ...string) *unstructured.Unstructured {
|
||||
refs := make([]interface{}, 0, len(owners))
|
||||
for _, owner := range owners {
|
||||
|
||||
Reference in new issue
Block a user