@@ -13,9 +13,9 @@ import (
13
13
14
14
"github.com/aws/aws-sdk-go/aws"
15
15
"github.com/zalando/postgres-operator/pkg/spec"
16
- "github.com/zalando/postgres-operator/pkg/util"
17
16
"github.com/zalando/postgres-operator/pkg/util/constants"
18
17
"github.com/zalando/postgres-operator/pkg/util/filesystems"
18
+ "github.com/zalando/postgres-operator/pkg/util/k8sutil"
19
19
"github.com/zalando/postgres-operator/pkg/util/volumes"
20
20
)
21
21
@@ -185,7 +185,7 @@ func (c *Cluster) syncVolumeClaims() error {
185
185
186
186
if c .OpConfig .StorageResizeMode == "off" || c .OpConfig .StorageResizeMode == "ebs" {
187
187
ignoreResize = true
188
- c .logger .Debugf ("Storage resize mode is set to %q. Skipping volume size sync of PVCs ." , c .OpConfig .StorageResizeMode )
188
+ c .logger .Debugf ("Storage resize mode is set to %q. Skipping volume size sync of persistent volume claims ." , c .OpConfig .StorageResizeMode )
189
189
}
190
190
191
191
newSize , err := resource .ParseQuantity (c .Spec .Volume .Size )
@@ -196,9 +196,10 @@ func (c *Cluster) syncVolumeClaims() error {
196
196
197
197
pvcs , err := c .listPersistentVolumeClaims ()
198
198
if err != nil {
199
- return fmt .Errorf ("could not receive persistent volume claims: %v" , err )
199
+ return fmt .Errorf ("could not list persistent volume claims: %v" , err )
200
200
}
201
201
for _ , pvc := range pvcs {
202
+ c .VolumeClaims [pvc .UID ] = & pvc
202
203
needsUpdate := false
203
204
currentSize := quantityToGigabyte (pvc .Spec .Resources .Requests [v1 .ResourceStorage ])
204
205
if ! ignoreResize && currentSize != manifestSize {
@@ -213,9 +214,11 @@ func (c *Cluster) syncVolumeClaims() error {
213
214
214
215
if needsUpdate {
215
216
c .logger .Infof ("updating persistent volume claim definition for volume %q" , pvc .Name )
216
- if _ , err := c .KubeClient .PersistentVolumeClaims (pvc .Namespace ).Update (context .TODO (), & pvc , metav1.UpdateOptions {}); err != nil {
217
+ updatedPvc , err := c .KubeClient .PersistentVolumeClaims (pvc .Namespace ).Update (context .TODO (), & pvc , metav1.UpdateOptions {})
218
+ if err != nil {
217
219
return fmt .Errorf ("could not update persistent volume claim: %q" , err )
218
220
}
221
+ c .VolumeClaims [pvc .UID ] = updatedPvc
219
222
c .logger .Infof ("successfully updated persistent volume claim %q" , pvc .Name )
220
223
} else {
221
224
c .logger .Debugf ("volume claim for volume %q do not require updates" , pvc .Name )
@@ -227,10 +230,11 @@ func (c *Cluster) syncVolumeClaims() error {
227
230
if err != nil {
228
231
return fmt .Errorf ("could not form patch for the persistent volume claim for volume %q: %v" , pvc .Name , err )
229
232
}
230
- _ , err = c .KubeClient .PersistentVolumeClaims (pvc .Namespace ).Patch (context .TODO (), pvc .Name , types .MergePatchType , []byte (patchData ), metav1.PatchOptions {})
233
+ patchedPvc , err : = c .KubeClient .PersistentVolumeClaims (pvc .Namespace ).Patch (context .TODO (), pvc .Name , types .MergePatchType , []byte (patchData ), metav1.PatchOptions {})
231
234
if err != nil {
232
235
return fmt .Errorf ("could not patch annotations of the persistent volume claim for volume %q: %v" , pvc .Name , err )
233
236
}
237
+ c .VolumeClaims [pvc .UID ] = patchedPvc
234
238
}
235
239
}
236
240
@@ -268,28 +272,40 @@ func (c *Cluster) listPersistentVolumeClaims() ([]v1.PersistentVolumeClaim, erro
268
272
269
273
pvcs , err := c .KubeClient .PersistentVolumeClaims (ns ).List (context .TODO (), listOptions )
270
274
if err != nil {
271
- return nil , fmt .Errorf ("could not list of PersistentVolumeClaims : %v" , err )
275
+ return nil , fmt .Errorf ("could not list of persistent volume claims : %v" , err )
272
276
}
273
277
return pvcs .Items , nil
274
278
}
275
279
276
280
func (c * Cluster ) deletePersistentVolumeClaims () error {
277
- c .logger .Debug ("deleting PVCs" )
278
- pvcs , err := c .listPersistentVolumeClaims ()
279
- if err != nil {
280
- return err
281
- }
282
- for _ , pvc := range pvcs {
283
- c .logger .Debugf ("deleting PVC %q" , util .NameFromMeta (pvc .ObjectMeta ))
284
- if err := c .KubeClient .PersistentVolumeClaims (pvc .Namespace ).Delete (context .TODO (), pvc .Name , c .deleteOptions ); err != nil {
285
- c .logger .Warningf ("could not delete PersistentVolumeClaim: %v" , err )
281
+ c .setProcessName ("deleting persistent volume claims" )
282
+ errors := make ([]string , 0 )
283
+ for uid := range c .VolumeClaims {
284
+ err := c .deletePersistentVolumeClaim (uid )
285
+ if err != nil {
286
+ errors = append (errors , fmt .Sprintf ("%v" , err ))
286
287
}
287
288
}
288
- if len (pvcs ) > 0 {
289
- c .logger .Debug ("PVCs have been deleted" )
290
- } else {
291
- c .logger .Debug ("no PVCs to delete" )
289
+
290
+ if len (errors ) > 0 {
291
+ c .logger .Warningf ("could not delete all persistent volume claims: %v" , strings .Join (errors , `', '` ))
292
+ }
293
+
294
+ return nil
295
+ }
296
+
297
+ func (c * Cluster ) deletePersistentVolumeClaim (uid types.UID ) error {
298
+ c .setProcessName ("deleting persistent volume claim" )
299
+ pvc := c .VolumeClaims [uid ]
300
+ c .logger .Debugf ("deleting persistent volume claim %q" , pvc .Name )
301
+ err := c .KubeClient .PersistentVolumeClaims (pvc .Namespace ).Delete (context .TODO (), pvc .Name , c .deleteOptions )
302
+ if k8sutil .ResourceNotFound (err ) {
303
+ c .logger .Debugf ("persistent volume claim %q has already been deleted" , pvc .Name )
304
+ } else if err != nil {
305
+ return fmt .Errorf ("could not delete persistent volume claim %q: %v" , pvc .Name , err )
292
306
}
307
+ c .logger .Infof ("persistent volume claim %q has been deleted" , pvc .Name )
308
+ delete (c .VolumeClaims , uid )
293
309
294
310
return nil
295
311
}
@@ -299,7 +315,7 @@ func (c *Cluster) listPersistentVolumes() ([]*v1.PersistentVolume, error) {
299
315
300
316
pvcs , err := c .listPersistentVolumeClaims ()
301
317
if err != nil {
302
- return nil , fmt .Errorf ("could not list cluster's PersistentVolumeClaims : %v" , err )
318
+ return nil , fmt .Errorf ("could not list cluster's persistent volume claims : %v" , err )
303
319
}
304
320
305
321
pods , err := c .listPods ()
0 commit comments