@@ -47,40 +47,34 @@ func NewLifeCycleHandler(snapshotter LifeCycleSnapshotter) *LifeCycleHandler {
47
47
func (h LifeCycleHandler ) Handle (ctx context.Context , vdSnapshot * virtv2.VirtualDiskSnapshot ) (reconcile.Result , error ) {
48
48
log := logger .FromContext (ctx ).With (logger .SlogHandler ("lifecycle" ))
49
49
50
- condition , ok := service .GetCondition (vdscondition .VirtualDiskSnapshotReadyType , vdSnapshot .Status .Conditions )
51
- if ! ok {
52
- condition = metav1.Condition {
53
- Type : vdscondition .VirtualDiskSnapshotReadyType ,
54
- Status : metav1 .ConditionUnknown ,
55
- Reason : conditions .ReasonUnknown .String (),
56
- }
57
- }
50
+ cb := conditions .NewConditionBuilder (vdscondition .VirtualDiskSnapshotReadyType ).Generation (vdSnapshot .Generation )
58
51
59
- defer func () { service .SetCondition (condition , & vdSnapshot .Status .Conditions ) }()
52
+ defer func () { conditions .SetCondition (cb , & vdSnapshot .Status .Conditions ) }()
60
53
61
54
vs , err := h .snapshotter .GetVolumeSnapshot (ctx , vdSnapshot .Name , vdSnapshot .Namespace )
62
55
if err != nil {
63
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
56
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
64
57
return reconcile.Result {}, err
65
58
}
66
59
67
60
vd , err := h .snapshotter .GetVirtualDisk (ctx , vdSnapshot .Spec .VirtualDiskName , vdSnapshot .Namespace )
68
61
if err != nil {
69
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
62
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
70
63
return reconcile.Result {}, err
71
64
}
72
65
73
66
vm , err := getVirtualMachine (ctx , vd , h .snapshotter )
74
67
if err != nil {
75
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
68
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
76
69
return reconcile.Result {}, err
77
70
}
78
71
79
72
if vdSnapshot .DeletionTimestamp != nil {
80
73
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseTerminating
81
- condition .Status = metav1 .ConditionUnknown
82
- condition .Reason = conditions .ReasonUnknown .String ()
83
- condition .Message = ""
74
+ cb .
75
+ Status (metav1 .ConditionUnknown ).
76
+ Reason (conditions .ReasonUnknown ).
77
+ Message ("" )
84
78
85
79
return reconcile.Result {}, nil
86
80
}
@@ -91,42 +85,46 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual
91
85
case virtv2 .VirtualDiskSnapshotPhaseReady :
92
86
if vs == nil || vs .Status == nil || vs .Status .ReadyToUse == nil || ! * vs .Status .ReadyToUse {
93
87
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseFailed
94
- condition .Status = metav1 .ConditionFalse
95
- condition .Reason = vdscondition .VolumeSnapshotLost
96
- condition .Message = fmt .Sprintf ("The underlieng volume snapshot %q is not ready to use." , vdSnapshot .Status .VolumeSnapshotName )
88
+ cb .
89
+ Status (metav1 .ConditionFalse ).
90
+ Reason (vdscondition .VolumeSnapshotLost ).
91
+ Message (fmt .Sprintf ("The underlieng volume snapshot %q is not ready to use." , vdSnapshot .Status .VolumeSnapshotName ))
97
92
return reconcile.Result {Requeue : true }, nil
98
93
}
99
94
100
95
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseReady
101
- condition .Status = metav1 .ConditionTrue
102
- condition .Reason = vdscondition .VirtualDiskSnapshotReady
103
- condition .Message = ""
96
+ cb .
97
+ Status (metav1 .ConditionTrue ).
98
+ Reason (vdscondition .VirtualDiskSnapshotReady ).
99
+ Message ("" )
104
100
return reconcile.Result {}, nil
105
101
}
106
102
107
- virtualDiskReadyCondition , _ := service .GetCondition (vdscondition .VirtualDiskReadyType , vdSnapshot .Status .Conditions )
103
+ virtualDiskReadyCondition , _ := conditions .GetCondition (vdscondition .VirtualDiskReadyType , vdSnapshot .Status .Conditions )
108
104
if vd == nil || virtualDiskReadyCondition .Status != metav1 .ConditionTrue {
109
105
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhasePending
110
- condition .Status = metav1 .ConditionFalse
111
- condition .Reason = vdscondition .WaitingForTheVirtualDisk
112
- condition .Message = fmt .Sprintf ("Waiting for the virtual disk %q to be ready for snapshotting." , vdSnapshot .Spec .VirtualDiskName )
106
+ cb .
107
+ Status (metav1 .ConditionFalse ).
108
+ Reason (vdscondition .WaitingForTheVirtualDisk ).
109
+ Message (fmt .Sprintf ("Waiting for the virtual disk %q to be ready for snapshotting." , vdSnapshot .Spec .VirtualDiskName ))
113
110
return reconcile.Result {}, nil
114
111
}
115
112
116
113
var pvc * corev1.PersistentVolumeClaim
117
114
if vd .Status .Target .PersistentVolumeClaim != "" {
118
115
pvc , err = h .snapshotter .GetPersistentVolumeClaim (ctx , vd .Status .Target .PersistentVolumeClaim , vd .Namespace )
119
116
if err != nil {
120
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
117
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
121
118
return reconcile.Result {}, err
122
119
}
123
120
}
124
121
125
122
if pvc == nil || pvc .Status .Phase != corev1 .ClaimBound {
126
123
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhasePending
127
- condition .Status = metav1 .ConditionFalse
128
- condition .Reason = vdscondition .WaitingForTheVirtualDisk
129
- condition .Message = "Waiting for the virtual disk's pvc to be in phase Bound."
124
+ cb .
125
+ Status (metav1 .ConditionFalse ).
126
+ Reason (vdscondition .WaitingForTheVirtualDisk ).
127
+ Message ("Waiting for the virtual disk's pvc to be in phase Bound." )
130
128
return reconcile.Result {}, nil
131
129
}
132
130
@@ -138,30 +136,32 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual
138
136
139
137
err = h .snapshotter .Freeze (ctx , vm .Name , vm .Namespace )
140
138
if err != nil {
141
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
139
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
142
140
return reconcile.Result {}, err
143
141
}
144
142
145
143
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseInProgress
146
- condition .Status = metav1 .ConditionFalse
147
- condition .Reason = vdscondition .FileSystemFreezing
148
- condition .Message = fmt .Sprintf (
149
- "The virtual machine %q with an attached virtual disk %q is in the process of being frozen for taking a snapshot." ,
150
- vm .Name , vdSnapshot .Spec .VirtualDiskName ,
151
- )
144
+ cb .
145
+ Status (metav1 .ConditionFalse ).
146
+ Reason (vdscondition .FileSystemFreezing ).
147
+ Message (fmt .Sprintf (
148
+ "The virtual machine %q with an attached virtual disk %q is in the process of being frozen for taking a snapshot." ,
149
+ vm .Name , vdSnapshot .Spec .VirtualDiskName ,
150
+ ))
152
151
return reconcile.Result {}, nil
153
152
}
154
153
155
154
if vdSnapshot .Spec .RequiredConsistency {
156
155
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhasePending
157
- condition .Status = metav1 .ConditionFalse
158
- condition .Reason = vdscondition .PotentiallyInconsistent
159
- condition .Message = fmt .Sprintf (
160
- "The virtual machine %q with an attached virtual disk %q is %s: " +
161
- "the snapshotting of virtual disk might result in an inconsistent snapshot: " +
162
- "waiting for the virtual machine to be %s or the disk to be detached" ,
163
- vm .Name , vd .Name , vm .Status .Phase , virtv2 .MachineStopped ,
164
- )
156
+ cb .
157
+ Status (metav1 .ConditionFalse ).
158
+ Reason (vdscondition .PotentiallyInconsistent ).
159
+ Message (fmt .Sprintf (
160
+ "The virtual machine %q with an attached virtual disk %q is %s: " +
161
+ "the snapshotting of virtual disk might result in an inconsistent snapshot: " +
162
+ "waiting for the virtual machine to be %s or the disk to be detached" ,
163
+ vm .Name , vd .Name , vm .Status .Phase , virtv2 .MachineStopped ,
164
+ ))
165
165
return reconcile.Result {}, nil
166
166
}
167
167
}
@@ -203,31 +203,34 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual
203
203
204
204
vs , err = h .snapshotter .CreateVolumeSnapshot (ctx , vs )
205
205
if err != nil {
206
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
206
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
207
207
return reconcile.Result {}, err
208
208
}
209
209
210
210
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseInProgress
211
211
vdSnapshot .Status .VolumeSnapshotName = vs .Name
212
- condition .Status = metav1 .ConditionFalse
213
- condition .Reason = vdscondition .Snapshotting
214
- condition .Message = fmt .Sprintf ("The snapshotting process for virtual disk %q has started." , vdSnapshot .Spec .VirtualDiskName )
212
+ cb .
213
+ Status (metav1 .ConditionFalse ).
214
+ Reason (vdscondition .Snapshotting ).
215
+ Message (fmt .Sprintf ("The snapshotting process for virtual disk %q has started." , vdSnapshot .Spec .VirtualDiskName ))
215
216
return reconcile.Result {}, nil
216
217
case vs .Status != nil && vs .Status .Error != nil && vs .Status .Error .Message != nil :
217
218
log .Debug ("The volume snapshot has an error" )
218
219
219
220
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseFailed
220
- condition .Status = metav1 .ConditionFalse
221
- condition .Reason = vdscondition .VirtualDiskSnapshotFailed
222
- condition .Message = fmt .Sprintf ("VolumeSnapshot %q has an error: %s." , vs .Name , * vs .Status .Error .Message )
221
+ cb .
222
+ Status (metav1 .ConditionFalse ).
223
+ Reason (vdscondition .VirtualDiskSnapshotFailed ).
224
+ Message (fmt .Sprintf ("VolumeSnapshot %q has an error: %s." , vs .Name , * vs .Status .Error .Message ))
223
225
return reconcile.Result {}, nil
224
226
case vs .Status == nil || vs .Status .ReadyToUse == nil || ! * vs .Status .ReadyToUse :
225
227
log .Debug ("Waiting for the volume snapshot to be ready to use" )
226
228
227
229
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseInProgress
228
- condition .Status = metav1 .ConditionFalse
229
- condition .Reason = vdscondition .Snapshotting
230
- condition .Message = fmt .Sprintf ("Waiting fot the volume snapshot %q to be ready to use." , vdSnapshot .Name )
230
+ cb .
231
+ Status (metav1 .ConditionFalse ).
232
+ Reason (vdscondition .Snapshotting ).
233
+ Message (fmt .Sprintf ("Waiting fot the volume snapshot %q to be ready to use." , vdSnapshot .Name ))
231
234
return reconcile.Result {}, nil
232
235
default :
233
236
log .Debug ("The volume snapshot is ready to use" )
@@ -241,7 +244,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual
241
244
var canUnfreeze bool
242
245
canUnfreeze , err = h .snapshotter .CanUnfreeze (ctx , vdSnapshot .Name , vm )
243
246
if err != nil {
244
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
247
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
245
248
return reconcile.Result {}, err
246
249
}
247
250
@@ -250,16 +253,17 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *virtv2.Virtual
250
253
251
254
err = h .snapshotter .Unfreeze (ctx , vm .Name , vm .Namespace )
252
255
if err != nil {
253
- setPhaseConditionToFailed (& condition , & vdSnapshot .Status .Phase , err )
256
+ setPhaseConditionToFailed (cb , & vdSnapshot .Status .Phase , err )
254
257
return reconcile.Result {}, err
255
258
}
256
259
}
257
260
}
258
261
259
262
vdSnapshot .Status .Phase = virtv2 .VirtualDiskSnapshotPhaseReady
260
- condition .Status = metav1 .ConditionTrue
261
- condition .Reason = vdscondition .VirtualDiskSnapshotReady
262
- condition .Message = ""
263
+ cb .
264
+ Status (metav1 .ConditionTrue ).
265
+ Reason (vdscondition .VirtualDiskSnapshotReady ).
266
+ Message ("" )
263
267
264
268
return reconcile.Result {}, nil
265
269
}
@@ -286,9 +290,10 @@ func getVirtualMachine(ctx context.Context, vd *virtv2.VirtualDisk, snapshotter
286
290
}
287
291
}
288
292
289
- func setPhaseConditionToFailed (cond * metav1. Condition , phase * virtv2.VirtualDiskSnapshotPhase , err error ) {
293
+ func setPhaseConditionToFailed (cb * conditions. ConditionBuilder , phase * virtv2.VirtualDiskSnapshotPhase , err error ) {
290
294
* phase = virtv2 .VirtualDiskSnapshotPhaseFailed
291
- cond .Status = metav1 .ConditionFalse
292
- cond .Reason = vdscondition .VirtualDiskSnapshotFailed
293
- cond .Message = service .CapitalizeFirstLetter (err .Error ())
295
+ cb .
296
+ Status (metav1 .ConditionFalse ).
297
+ Reason (vdscondition .VirtualDiskSnapshotFailed ).
298
+ Message (service .CapitalizeFirstLetter (err .Error ()))
294
299
}
0 commit comments