@@ -20,7 +20,6 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"slices"
23
- "time"
24
23
25
24
corev1 "k8s.io/api/core/v1"
26
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -30,6 +29,7 @@ import (
30
29
"sigs.k8s.io/controller-runtime/pkg/client"
31
30
"sigs.k8s.io/controller-runtime/pkg/reconcile"
32
31
32
+ "github.com/deckhouse/virtualization-controller/pkg/common/annotations"
33
33
"github.com/deckhouse/virtualization-controller/pkg/common/object"
34
34
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
35
35
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
@@ -49,59 +49,50 @@ func NewInUseHandler(client client.Client) *InUseHandler {
49
49
}
50
50
51
51
func (h InUseHandler ) Handle (ctx context.Context , vd * virtv2.VirtualDisk ) (reconcile.Result , error ) {
52
- inUseCondition , ok := conditions .GetCondition (vdcondition .InUseType , vd .Status .Conditions )
53
- if ! ok {
54
- cb := conditions .NewConditionBuilder (vdcondition .InUseType ).
55
- Status (metav1 .ConditionUnknown ).
56
- Reason (conditions .ReasonUnknown ).
57
- Generation (vd .Generation )
58
- conditions .SetCondition (cb , & vd .Status .Conditions )
59
- inUseCondition = cb .Condition ()
60
- }
61
-
62
52
err := h .updateAttachedVirtualMachines (ctx , vd )
63
53
if err != nil {
64
54
return reconcile.Result {}, err
65
55
}
66
56
67
- usedByVM , usedByImage := false , false
57
+ var (
58
+ usedByVM bool
59
+ usedByImage bool
60
+ usedByDataExport bool
61
+ )
68
62
69
- if inUseCondition .Reason != vdcondition .UsedForImageCreation .String () {
70
- usedByVM = h .checkUsageByVM (vd )
71
-
72
- if ! usedByVM {
73
- usedByImage , err = h .checkImageUsage (ctx , vd )
74
- if err != nil {
75
- return reconcile.Result {}, err
76
- }
77
- }
78
- } else {
63
+ usedByVM = h .checkUsageByVM (vd )
64
+ if ! usedByVM {
79
65
usedByImage , err = h .checkImageUsage (ctx , vd )
80
66
if err != nil {
81
67
return reconcile.Result {}, err
82
68
}
83
-
84
- if ! usedByImage {
85
- usedByVM = h .checkUsageByVM (vd )
69
+ }
70
+ if ! usedByVM && ! usedByImage {
71
+ usedByDataExport , err = h .checkDataExportUsage (ctx , vd )
72
+ if err != nil {
73
+ return reconcile.Result {}, err
86
74
}
87
75
}
88
76
89
- cb := conditions .NewConditionBuilder (vdcondition .InUseType )
77
+ cb := conditions .NewConditionBuilder (vdcondition .InUseType ). Generation ( vd . Generation )
90
78
switch {
91
79
case usedByVM :
92
- cb .Generation ( vd . Generation ).
80
+ cb .
93
81
Status (metav1 .ConditionTrue ).
94
82
Reason (vdcondition .AttachedToVirtualMachine ).
95
- Message ("" ).
96
- LastTransitionTime (time .Now ())
83
+ Message ("" )
97
84
case usedByImage :
98
- cb .Generation ( vd . Generation ).
85
+ cb .
99
86
Status (metav1 .ConditionTrue ).
100
87
Reason (vdcondition .UsedForImageCreation ).
101
- Message ("" ).
102
- LastTransitionTime (time .Now ())
88
+ Message ("" )
89
+ case usedByDataExport :
90
+ cb .
91
+ Status (metav1 .ConditionTrue ).
92
+ Reason (vdcondition .UsedForDataExport ).
93
+ Message ("" )
103
94
default :
104
- cb .Generation ( vd . Generation ).
95
+ cb .
105
96
Status (metav1 .ConditionFalse ).
106
97
Reason (vdcondition .NotInUse ).
107
98
Message ("" )
@@ -121,6 +112,23 @@ func (h InUseHandler) isVDAttachedToVM(vdName string, vm virtv2.VirtualMachine)
121
112
return false
122
113
}
123
114
115
+ func (h InUseHandler ) checkDataExportUsage (ctx context.Context , vd * virtv2.VirtualDisk ) (bool , error ) {
116
+ pvcName := vd .Status .Target .PersistentVolumeClaim
117
+ if pvcName == "" {
118
+ return false , nil
119
+ }
120
+
121
+ pvc , err := object .FetchObject (ctx , types.NamespacedName {Name : pvcName , Namespace : vd .Namespace }, h .client , & corev1.PersistentVolumeClaim {})
122
+ if err != nil {
123
+ return false , fmt .Errorf ("fetch pvc: %w" , err )
124
+ }
125
+ if pvc == nil {
126
+ return false , nil
127
+ }
128
+
129
+ return pvc .GetAnnotations ()[annotations .AnnDataExportRequest ] == "true" , nil
130
+ }
131
+
124
132
func (h InUseHandler ) checkImageUsage (ctx context.Context , vd * virtv2.VirtualDisk ) (bool , error ) {
125
133
// If disk is not ready, it cannot be used for create image
126
134
if vd .Status .Phase != virtv2 .DiskReady {
0 commit comments