blob: f92acf22180495dc1c295bb280a8b1e38e07a6a1 [file] [log] [blame]
Lorenz Brunb15abad2020-04-16 11:17:12 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package kubernetes
18
19import (
20 "context"
21 "errors"
22 "fmt"
Lorenz Brunb15abad2020-04-16 11:17:12 +020023 "os"
24 "path/filepath"
25
Lorenz Brun37050122021-03-30 14:00:27 +020026 "golang.org/x/sys/unix"
Lorenz Brunb15abad2020-04-16 11:17:12 +020027 v1 "k8s.io/api/core/v1"
28 storagev1 "k8s.io/api/storage/v1"
29 apierrs "k8s.io/apimachinery/pkg/api/errors"
30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31 "k8s.io/client-go/informers"
32 coreinformers "k8s.io/client-go/informers/core/v1"
33 storageinformers "k8s.io/client-go/informers/storage/v1"
34 "k8s.io/client-go/kubernetes"
35 "k8s.io/client-go/kubernetes/scheme"
36 typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
37 "k8s.io/client-go/tools/cache"
38 "k8s.io/client-go/tools/record"
39 ref "k8s.io/client-go/tools/reference"
40 "k8s.io/client-go/util/workqueue"
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020041
Serge Bazanski3c5d0632024-09-12 10:49:12 +000042 "source.monogon.dev/go/logging"
Serge Bazanski31370b02021-01-07 16:31:14 +010043 "source.monogon.dev/metropolis/node/core/localstorage"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020044 "source.monogon.dev/osbase/fsquota"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020045 "source.monogon.dev/osbase/supervisor"
Lorenz Brunb15abad2020-04-16 11:17:12 +020046)
47
Lorenz Brun397f7ea2024-08-20 21:26:06 +020048// inodeCapacityRatio describes the ratio between the byte capacity of a volume
49// and its inode capacity. One inode on XFS is 512 bytes and by default 25%
50// (1/4) of capacity can be used for metadata.
51const inodeCapacityRatio = 4 * 512
52
Serge Bazanski216fe7b2021-05-21 18:36:16 +020053// ONCHANGE(//metropolis/node/kubernetes/reconciler:resources_csi.go): needs to
54// match csiProvisionerServerName declared.
Serge Bazanski662b5b32020-12-21 13:49:00 +010055const csiProvisionerServerName = "dev.monogon.metropolis.vfs"
Lorenz Brunb15abad2020-04-16 11:17:12 +020056
Serge Bazanski216fe7b2021-05-21 18:36:16 +020057// csiProvisionerServer is responsible for the provisioning and deprovisioning
58// of CSI-based container volumes. It runs on all nodes and watches PVCs for
59// ones assigned to the node it's running on and fulfills the provisioning
60// request by creating a directory, applying a quota and creating the
61// corresponding PV. When the PV is released and its retention policy is
62// Delete, the directory and the PV resource are deleted.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020063type csiProvisionerServer struct {
64 NodeName string
65 Kubernetes kubernetes.Interface
66 InformerFactory informers.SharedInformerFactory
67 VolumesDirectory *localstorage.DataVolumesDirectory
68
Jan Schär896b1382025-01-15 13:54:26 +010069 claimQueue workqueue.TypedRateLimitingInterface[string]
70 pvQueue workqueue.TypedRateLimitingInterface[string]
Lorenz Brunb15abad2020-04-16 11:17:12 +020071 recorder record.EventRecorder
72 pvcInformer coreinformers.PersistentVolumeClaimInformer
73 pvInformer coreinformers.PersistentVolumeInformer
74 storageClassInformer storageinformers.StorageClassInformer
Serge Bazanski3c5d0632024-09-12 10:49:12 +000075 logger logging.Leveled
Lorenz Brunb15abad2020-04-16 11:17:12 +020076}
77
Serge Bazanski216fe7b2021-05-21 18:36:16 +020078// runCSIProvisioner runs the main provisioning machinery. It consists of a
79// bunch of informers which keep track of the events happening on the
80// Kubernetes control plane and informs us when something happens. If anything
81// happens to PVCs or PVs, we enqueue the identifier of that resource in a work
82// queue. Queues are being worked on by only one worker to limit load and avoid
83// complicated locking infrastructure. Failed items are requeued.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020084func (p *csiProvisionerServer) Run(ctx context.Context) error {
Serge Bazanski216fe7b2021-05-21 18:36:16 +020085 // The recorder is used to log Kubernetes events for successful or failed
86 // volume provisions. These events then show up in `kubectl describe pvc`
87 // and can be used by admins to debug issues with this provisioner.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020088 eventBroadcaster := record.NewBroadcaster()
89 eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: p.Kubernetes.CoreV1().Events("")})
90 p.recorder = eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: csiProvisionerServerName, Host: p.NodeName})
Lorenz Brunb15abad2020-04-16 11:17:12 +020091
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020092 p.pvInformer = p.InformerFactory.Core().V1().PersistentVolumes()
93 p.pvcInformer = p.InformerFactory.Core().V1().PersistentVolumeClaims()
94 p.storageClassInformer = p.InformerFactory.Storage().V1().StorageClasses()
Lorenz Brunb15abad2020-04-16 11:17:12 +020095
Jan Schär896b1382025-01-15 13:54:26 +010096 p.claimQueue = workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[string]())
97 p.pvQueue = workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[string]())
Lorenz Brunb15abad2020-04-16 11:17:12 +020098
Serge Bazanskice19acc2023-03-21 16:28:07 +010099 p.logger = supervisor.Logger(ctx)
100
101 p.pvcInformer.Informer().SetWatchErrorHandler(func(_ *cache.Reflector, err error) {
102 p.logger.Errorf("pvcInformer watch error: %v", err)
103 })
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200104 p.pvcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
105 AddFunc: p.enqueueClaim,
106 UpdateFunc: func(old, new interface{}) {
107 p.enqueueClaim(new)
108 },
109 })
110 p.pvInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
111 AddFunc: p.enqueuePV,
112 UpdateFunc: func(old, new interface{}) {
113 p.enqueuePV(new)
114 },
115 })
Serge Bazanskice19acc2023-03-21 16:28:07 +0100116 p.pvInformer.Informer().SetWatchErrorHandler(func(_ *cache.Reflector, err error) {
117 p.logger.Errorf("pvInformer watch error: %v", err)
118 })
119
120 p.storageClassInformer.Informer().SetWatchErrorHandler(func(_ *cache.Reflector, err error) {
121 p.logger.Errorf("storageClassInformer watch error: %v", err)
122 })
Lorenz Brunb15abad2020-04-16 11:17:12 +0200123
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200124 go p.pvcInformer.Informer().Run(ctx.Done())
125 go p.pvInformer.Informer().Run(ctx.Done())
126 go p.storageClassInformer.Informer().Run(ctx.Done())
Lorenz Brunb15abad2020-04-16 11:17:12 +0200127
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200128 // These will self-terminate once the queues are shut down
129 go p.processQueueItems(p.claimQueue, func(key string) error {
130 return p.processPVC(key)
131 })
132 go p.processQueueItems(p.pvQueue, func(key string) error {
133 return p.processPV(key)
134 })
Lorenz Brunb15abad2020-04-16 11:17:12 +0200135
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200136 supervisor.Signal(ctx, supervisor.SignalHealthy)
137 <-ctx.Done()
138 p.claimQueue.ShutDown()
139 p.pvQueue.ShutDown()
140 return nil
Lorenz Brunb15abad2020-04-16 11:17:12 +0200141}
142
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200143// isOurPVC checks if the given PVC is is to be provisioned by this provisioner
144// and has been scheduled onto this node
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200145func (p *csiProvisionerServer) isOurPVC(pvc *v1.PersistentVolumeClaim) bool {
146 if pvc.ObjectMeta.Annotations["volume.beta.kubernetes.io/storage-provisioner"] != csiProvisionerServerName {
147 return false
148 }
149 if pvc.ObjectMeta.Annotations["volume.kubernetes.io/selected-node"] != p.NodeName {
150 return false
151 }
152 return true
Lorenz Brunb15abad2020-04-16 11:17:12 +0200153}
154
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200155// isOurPV checks if the given PV has been provisioned by this provisioner and
156// has been scheduled onto this node
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200157func (p *csiProvisionerServer) isOurPV(pv *v1.PersistentVolume) bool {
158 if pv.ObjectMeta.Annotations["pv.kubernetes.io/provisioned-by"] != csiProvisionerServerName {
159 return false
160 }
161 if pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values[0] != p.NodeName {
162 return false
163 }
164 return true
Lorenz Brunb15abad2020-04-16 11:17:12 +0200165}
166
167// enqueueClaim adds an added/changed PVC to the work queue
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200168func (p *csiProvisionerServer) enqueueClaim(obj interface{}) {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200169 key, err := cache.MetaNamespaceKeyFunc(obj)
170 if err != nil {
Serge Bazanskic7359672020-10-30 16:38:57 +0100171 p.logger.Errorf("Not queuing PVC because key could not be derived: %v", err)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200172 return
173 }
174 p.claimQueue.Add(key)
175}
176
177// enqueuePV adds an added/changed PV to the work queue
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200178func (p *csiProvisionerServer) enqueuePV(obj interface{}) {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200179 key, err := cache.MetaNamespaceKeyFunc(obj)
180 if err != nil {
Serge Bazanskic7359672020-10-30 16:38:57 +0100181 p.logger.Errorf("Not queuing PV because key could not be derived: %v", err)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200182 return
183 }
184 p.pvQueue.Add(key)
185}
186
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200187// processQueueItems gets items from the given work queue and calls the process
188// function for each of them. It self- terminates once the queue is shut down.
Jan Schär896b1382025-01-15 13:54:26 +0100189func (p *csiProvisionerServer) processQueueItems(queue workqueue.TypedRateLimitingInterface[string], process func(key string) error) {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200190 for {
191 obj, shutdown := queue.Get()
192 if shutdown {
193 return
194 }
195
Jan Schär896b1382025-01-15 13:54:26 +0100196 func(obj string) {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200197 defer queue.Done(obj)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200198
Jan Schär896b1382025-01-15 13:54:26 +0100199 if err := process(obj); err != nil {
200 p.logger.Warningf("Failed processing item %q, requeueing (numrequeues: %d): %v", obj, queue.NumRequeues(obj), err)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200201 queue.AddRateLimited(obj)
202 }
203
204 queue.Forget(obj)
205 }(obj)
206 }
207}
208
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200209// volumePath gets the path where the volume is stored.
210func (p *csiProvisionerServer) volumePath(volumeID string) string {
211 return filepath.Join(p.VolumesDirectory.FullPath(), volumeID)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200212}
213
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200214// processPVC looks at a single PVC item from the queue, determines if it needs
215// to be provisioned and logs the provisioning result to the recorder
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200216func (p *csiProvisionerServer) processPVC(key string) error {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200217 namespace, name, err := cache.SplitMetaNamespaceKey(key)
218 if err != nil {
219 return fmt.Errorf("invalid resource key: %s", key)
220 }
221 pvc, err := p.pvcInformer.Lister().PersistentVolumeClaims(namespace).Get(name)
222 if apierrs.IsNotFound(err) {
223 return nil // nothing to do, no error
224 } else if err != nil {
225 return fmt.Errorf("failed to get PVC for processing: %w", err)
226 }
227
228 if !p.isOurPVC(pvc) {
229 return nil
230 }
231
232 if pvc.Status.Phase != "Pending" {
233 // If the PVC is not pending, we don't need to provision anything
234 return nil
235 }
236
237 storageClass, err := p.storageClassInformer.Lister().Get(*pvc.Spec.StorageClassName)
238 if err != nil {
Serge Bazanskice19acc2023-03-21 16:28:07 +0100239 return fmt.Errorf("could not get storage class: %w", err)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200240 }
241
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200242 if storageClass.Provisioner != csiProvisionerServerName {
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200243 // We're not responsible for this PVC. Can only happen if
244 // controller-manager makes a mistake setting the annotations, but
245 // we're bailing here anyways for safety.
Lorenz Brunb15abad2020-04-16 11:17:12 +0200246 return nil
247 }
248
249 err = p.provisionPVC(pvc, storageClass)
250
251 if err != nil {
252 p.recorder.Eventf(pvc, v1.EventTypeWarning, "ProvisioningFailed", "Failed to provision PV: %v", err)
253 return err
254 }
255 p.recorder.Eventf(pvc, v1.EventTypeNormal, "Provisioned", "Successfully provisioned PV")
256
257 return nil
258}
259
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200260// provisionPVC creates the directory where the volume lives, sets a quota for
261// the requested amount of storage and creates the PV object representing this
262// new volume
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200263func (p *csiProvisionerServer) provisionPVC(pvc *v1.PersistentVolumeClaim, storageClass *storagev1.StorageClass) error {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200264 claimRef, err := ref.GetReference(scheme.Scheme, pvc)
265 if err != nil {
266 return fmt.Errorf("failed to get reference to PVC: %w", err)
267 }
268
269 storageReq := pvc.Spec.Resources.Requests[v1.ResourceStorage]
270 if storageReq.IsZero() {
271 return fmt.Errorf("PVC is not requesting any storage, this is not supported")
272 }
273 capacity, ok := storageReq.AsInt64()
274 if !ok {
275 return fmt.Errorf("PVC requesting more than 2^63 bytes of storage, this is not supported")
276 }
277
Lorenz Brunb15abad2020-04-16 11:17:12 +0200278 volumeID := "pvc-" + string(pvc.ObjectMeta.UID)
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200279 volumePath := p.volumePath(volumeID)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200280
Serge Bazanskic7359672020-10-30 16:38:57 +0100281 p.logger.Infof("Creating local PV %s", volumeID)
Lorenz Brun37050122021-03-30 14:00:27 +0200282
283 switch *pvc.Spec.VolumeMode {
284 case "", v1.PersistentVolumeFilesystem:
285 if err := os.Mkdir(volumePath, 0644); err != nil && !os.IsExist(err) {
286 return fmt.Errorf("failed to create volume directory: %w", err)
287 }
Lorenz Brun764a2de2021-11-22 16:26:36 +0100288 files, err := os.ReadDir(volumePath)
Lorenz Brun37050122021-03-30 14:00:27 +0200289 if err != nil {
290 return fmt.Errorf("failed to list files in newly-created volume: %w", err)
291 }
292 if len(files) > 0 {
293 return errors.New("newly-created volume already contains data, bailing")
294 }
Lorenz Brun397f7ea2024-08-20 21:26:06 +0200295 if err := fsquota.SetQuota(volumePath, uint64(capacity), uint64(capacity)/inodeCapacityRatio); err != nil {
Serge Bazanskice19acc2023-03-21 16:28:07 +0100296 return fmt.Errorf("failed to update quota: %w", err)
Lorenz Brun37050122021-03-30 14:00:27 +0200297 }
298 case v1.PersistentVolumeBlock:
299 imageFile, err := os.OpenFile(volumePath, os.O_CREATE|os.O_RDWR, 0644)
300 if err != nil {
301 return fmt.Errorf("failed to create volume image: %w", err)
302 }
303 defer imageFile.Close()
304 if err := unix.Fallocate(int(imageFile.Fd()), 0, 0, capacity); err != nil {
305 return fmt.Errorf("failed to fallocate() volume image: %w", err)
306 }
307 default:
308 return fmt.Errorf("VolumeMode \"%s\" is unsupported", *pvc.Spec.VolumeMode)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200309 }
310
311 vol := &v1.PersistentVolume{
312 ObjectMeta: metav1.ObjectMeta{
313 Name: volumeID,
314 Annotations: map[string]string{
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200315 "pv.kubernetes.io/provisioned-by": csiProvisionerServerName},
Lorenz Brunb15abad2020-04-16 11:17:12 +0200316 },
317 Spec: v1.PersistentVolumeSpec{
318 AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
319 Capacity: v1.ResourceList{
320 v1.ResourceStorage: storageReq, // We're always giving the exact amount
321 },
322 PersistentVolumeSource: v1.PersistentVolumeSource{
323 CSI: &v1.CSIPersistentVolumeSource{
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200324 Driver: csiProvisionerServerName,
Lorenz Brunb15abad2020-04-16 11:17:12 +0200325 VolumeHandle: volumeID,
326 },
327 },
Lorenz Brun37050122021-03-30 14:00:27 +0200328 ClaimRef: claimRef,
329 VolumeMode: pvc.Spec.VolumeMode,
Lorenz Brunb15abad2020-04-16 11:17:12 +0200330 NodeAffinity: &v1.VolumeNodeAffinity{
331 Required: &v1.NodeSelector{
332 NodeSelectorTerms: []v1.NodeSelectorTerm{
333 {
334 MatchExpressions: []v1.NodeSelectorRequirement{
335 {
336 Key: "kubernetes.io/hostname",
337 Operator: v1.NodeSelectorOpIn,
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200338 Values: []string{p.NodeName},
Lorenz Brunb15abad2020-04-16 11:17:12 +0200339 },
340 },
341 },
342 },
343 },
344 },
345 StorageClassName: *pvc.Spec.StorageClassName,
346 PersistentVolumeReclaimPolicy: *storageClass.ReclaimPolicy,
347 },
348 }
349
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200350 _, err = p.Kubernetes.CoreV1().PersistentVolumes().Create(context.Background(), vol, metav1.CreateOptions{})
351 if err != nil && !apierrs.IsAlreadyExists(err) {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200352 return fmt.Errorf("failed to create PV object: %w", err)
353 }
354 return nil
355}
356
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200357// processPV looks at a single PV item from the queue and checks if it has been
358// released and needs to be deleted. If yes it deletes the associated quota,
359// directory and the PV object and logs the result to the recorder.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200360func (p *csiProvisionerServer) processPV(key string) error {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200361 _, name, err := cache.SplitMetaNamespaceKey(key)
362 if err != nil {
363 return fmt.Errorf("invalid resource key: %s", key)
364 }
365 pv, err := p.pvInformer.Lister().Get(name)
366 if apierrs.IsNotFound(err) {
367 return nil // nothing to do, no error
368 } else if err != nil {
369 return fmt.Errorf("failed to get PV for processing: %w", err)
370 }
371
372 if !p.isOurPV(pv) {
373 return nil
374 }
375 if pv.Spec.PersistentVolumeReclaimPolicy != v1.PersistentVolumeReclaimDelete || pv.Status.Phase != "Released" {
376 return nil
377 }
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200378 volumePath := p.volumePath(pv.Spec.CSI.VolumeHandle)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200379
380 // Log deletes for auditing purposes
Serge Bazanskic7359672020-10-30 16:38:57 +0100381 p.logger.Infof("Deleting persistent volume %s", pv.Spec.CSI.VolumeHandle)
Lorenz Brun37050122021-03-30 14:00:27 +0200382 switch *pv.Spec.VolumeMode {
383 case "", v1.PersistentVolumeFilesystem:
384 if err := fsquota.SetQuota(volumePath, 0, 0); err != nil {
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200385 // We record these here manually since a successful deletion
386 // removes the PV we'd be attaching them to.
Lorenz Brun37050122021-03-30 14:00:27 +0200387 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to remove quota: %v", err)
388 return fmt.Errorf("failed to remove quota: %w", err)
389 }
390 if err := os.RemoveAll(volumePath); err != nil && !os.IsNotExist(err) {
391 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to delete volume: %v", err)
392 return fmt.Errorf("failed to delete volume: %w", err)
393 }
394 case v1.PersistentVolumeBlock:
395 if err := os.Remove(volumePath); err != nil && !os.IsNotExist(err) {
396 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to delete volume: %v", err)
397 return fmt.Errorf("failed to delete volume: %w", err)
398 }
399 default:
400 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Invalid volume mode \"%v\"", *pv.Spec.VolumeMode)
401 return fmt.Errorf("invalid volume mode \"%v\"", *pv.Spec.VolumeMode)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200402 }
403
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200404 err = p.Kubernetes.CoreV1().PersistentVolumes().Delete(context.Background(), pv.Name, metav1.DeleteOptions{})
Lorenz Brunb15abad2020-04-16 11:17:12 +0200405 if err != nil && !apierrs.IsNotFound(err) {
406 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to delete PV object from K8s API: %v", err)
407 return fmt.Errorf("failed to delete PV object: %w", err)
408 }
409 return nil
410}