blob: 0c5f9720104143d3607a52f521828e53e9796a8e [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
Lorenz Brunb15abad2020-04-16 11:17:12 +020069 claimQueue workqueue.RateLimitingInterface
70 pvQueue workqueue.RateLimitingInterface
71 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
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020096 p.claimQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
97 p.pvQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
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.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200189func (p *csiProvisionerServer) processQueueItems(queue workqueue.RateLimitingInterface, 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
196 func(obj interface{}) {
197 defer queue.Done(obj)
198 key, ok := obj.(string)
199 if !ok {
200 queue.Forget(obj)
Serge Bazanskic7359672020-10-30 16:38:57 +0100201 p.logger.Errorf("Expected string in workqueue, got %+v", obj)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200202 return
203 }
204
205 if err := process(key); err != nil {
Serge Bazanskic7359672020-10-30 16:38:57 +0100206 p.logger.Warningf("Failed processing item %q, requeueing (numrequeues: %d): %v", key, queue.NumRequeues(obj), err)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200207 queue.AddRateLimited(obj)
208 }
209
210 queue.Forget(obj)
211 }(obj)
212 }
213}
214
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200215// volumePath gets the path where the volume is stored.
216func (p *csiProvisionerServer) volumePath(volumeID string) string {
217 return filepath.Join(p.VolumesDirectory.FullPath(), volumeID)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200218}
219
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200220// processPVC looks at a single PVC item from the queue, determines if it needs
221// to be provisioned and logs the provisioning result to the recorder
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200222func (p *csiProvisionerServer) processPVC(key string) error {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200223 namespace, name, err := cache.SplitMetaNamespaceKey(key)
224 if err != nil {
225 return fmt.Errorf("invalid resource key: %s", key)
226 }
227 pvc, err := p.pvcInformer.Lister().PersistentVolumeClaims(namespace).Get(name)
228 if apierrs.IsNotFound(err) {
229 return nil // nothing to do, no error
230 } else if err != nil {
231 return fmt.Errorf("failed to get PVC for processing: %w", err)
232 }
233
234 if !p.isOurPVC(pvc) {
235 return nil
236 }
237
238 if pvc.Status.Phase != "Pending" {
239 // If the PVC is not pending, we don't need to provision anything
240 return nil
241 }
242
243 storageClass, err := p.storageClassInformer.Lister().Get(*pvc.Spec.StorageClassName)
244 if err != nil {
Serge Bazanskice19acc2023-03-21 16:28:07 +0100245 return fmt.Errorf("could not get storage class: %w", err)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200246 }
247
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200248 if storageClass.Provisioner != csiProvisionerServerName {
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200249 // We're not responsible for this PVC. Can only happen if
250 // controller-manager makes a mistake setting the annotations, but
251 // we're bailing here anyways for safety.
Lorenz Brunb15abad2020-04-16 11:17:12 +0200252 return nil
253 }
254
255 err = p.provisionPVC(pvc, storageClass)
256
257 if err != nil {
258 p.recorder.Eventf(pvc, v1.EventTypeWarning, "ProvisioningFailed", "Failed to provision PV: %v", err)
259 return err
260 }
261 p.recorder.Eventf(pvc, v1.EventTypeNormal, "Provisioned", "Successfully provisioned PV")
262
263 return nil
264}
265
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200266// provisionPVC creates the directory where the volume lives, sets a quota for
267// the requested amount of storage and creates the PV object representing this
268// new volume
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200269func (p *csiProvisionerServer) provisionPVC(pvc *v1.PersistentVolumeClaim, storageClass *storagev1.StorageClass) error {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200270 claimRef, err := ref.GetReference(scheme.Scheme, pvc)
271 if err != nil {
272 return fmt.Errorf("failed to get reference to PVC: %w", err)
273 }
274
275 storageReq := pvc.Spec.Resources.Requests[v1.ResourceStorage]
276 if storageReq.IsZero() {
277 return fmt.Errorf("PVC is not requesting any storage, this is not supported")
278 }
279 capacity, ok := storageReq.AsInt64()
280 if !ok {
281 return fmt.Errorf("PVC requesting more than 2^63 bytes of storage, this is not supported")
282 }
283
Lorenz Brunb15abad2020-04-16 11:17:12 +0200284 volumeID := "pvc-" + string(pvc.ObjectMeta.UID)
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200285 volumePath := p.volumePath(volumeID)
Jan Schär652c2ad2024-11-19 17:40:50 +0100286 var mountOptions []string
Lorenz Brunb15abad2020-04-16 11:17:12 +0200287
Serge Bazanskic7359672020-10-30 16:38:57 +0100288 p.logger.Infof("Creating local PV %s", volumeID)
Lorenz Brun37050122021-03-30 14:00:27 +0200289
290 switch *pvc.Spec.VolumeMode {
291 case "", v1.PersistentVolumeFilesystem:
292 if err := os.Mkdir(volumePath, 0644); err != nil && !os.IsExist(err) {
293 return fmt.Errorf("failed to create volume directory: %w", err)
294 }
Lorenz Brun764a2de2021-11-22 16:26:36 +0100295 files, err := os.ReadDir(volumePath)
Lorenz Brun37050122021-03-30 14:00:27 +0200296 if err != nil {
297 return fmt.Errorf("failed to list files in newly-created volume: %w", err)
298 }
299 if len(files) > 0 {
300 return errors.New("newly-created volume already contains data, bailing")
301 }
Lorenz Brun397f7ea2024-08-20 21:26:06 +0200302 if err := fsquota.SetQuota(volumePath, uint64(capacity), uint64(capacity)/inodeCapacityRatio); err != nil {
Serge Bazanskice19acc2023-03-21 16:28:07 +0100303 return fmt.Errorf("failed to update quota: %w", err)
Lorenz Brun37050122021-03-30 14:00:27 +0200304 }
Jan Schär652c2ad2024-11-19 17:40:50 +0100305 mountOptions = storageClass.MountOptions
Lorenz Brun37050122021-03-30 14:00:27 +0200306 case v1.PersistentVolumeBlock:
307 imageFile, err := os.OpenFile(volumePath, os.O_CREATE|os.O_RDWR, 0644)
308 if err != nil {
309 return fmt.Errorf("failed to create volume image: %w", err)
310 }
311 defer imageFile.Close()
312 if err := unix.Fallocate(int(imageFile.Fd()), 0, 0, capacity); err != nil {
313 return fmt.Errorf("failed to fallocate() volume image: %w", err)
314 }
315 default:
316 return fmt.Errorf("VolumeMode \"%s\" is unsupported", *pvc.Spec.VolumeMode)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200317 }
318
319 vol := &v1.PersistentVolume{
320 ObjectMeta: metav1.ObjectMeta{
321 Name: volumeID,
322 Annotations: map[string]string{
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200323 "pv.kubernetes.io/provisioned-by": csiProvisionerServerName},
Lorenz Brunb15abad2020-04-16 11:17:12 +0200324 },
325 Spec: v1.PersistentVolumeSpec{
326 AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
327 Capacity: v1.ResourceList{
328 v1.ResourceStorage: storageReq, // We're always giving the exact amount
329 },
330 PersistentVolumeSource: v1.PersistentVolumeSource{
331 CSI: &v1.CSIPersistentVolumeSource{
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200332 Driver: csiProvisionerServerName,
Lorenz Brunb15abad2020-04-16 11:17:12 +0200333 VolumeHandle: volumeID,
334 },
335 },
Lorenz Brun37050122021-03-30 14:00:27 +0200336 ClaimRef: claimRef,
337 VolumeMode: pvc.Spec.VolumeMode,
Lorenz Brunb15abad2020-04-16 11:17:12 +0200338 NodeAffinity: &v1.VolumeNodeAffinity{
339 Required: &v1.NodeSelector{
340 NodeSelectorTerms: []v1.NodeSelectorTerm{
341 {
342 MatchExpressions: []v1.NodeSelectorRequirement{
343 {
344 Key: "kubernetes.io/hostname",
345 Operator: v1.NodeSelectorOpIn,
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200346 Values: []string{p.NodeName},
Lorenz Brunb15abad2020-04-16 11:17:12 +0200347 },
348 },
349 },
350 },
351 },
352 },
353 StorageClassName: *pvc.Spec.StorageClassName,
Jan Schär652c2ad2024-11-19 17:40:50 +0100354 MountOptions: mountOptions,
Lorenz Brunb15abad2020-04-16 11:17:12 +0200355 PersistentVolumeReclaimPolicy: *storageClass.ReclaimPolicy,
356 },
357 }
358
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200359 _, err = p.Kubernetes.CoreV1().PersistentVolumes().Create(context.Background(), vol, metav1.CreateOptions{})
360 if err != nil && !apierrs.IsAlreadyExists(err) {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200361 return fmt.Errorf("failed to create PV object: %w", err)
362 }
363 return nil
364}
365
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200366// processPV looks at a single PV item from the queue and checks if it has been
367// released and needs to be deleted. If yes it deletes the associated quota,
368// directory and the PV object and logs the result to the recorder.
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200369func (p *csiProvisionerServer) processPV(key string) error {
Lorenz Brunb15abad2020-04-16 11:17:12 +0200370 _, name, err := cache.SplitMetaNamespaceKey(key)
371 if err != nil {
372 return fmt.Errorf("invalid resource key: %s", key)
373 }
374 pv, err := p.pvInformer.Lister().Get(name)
375 if apierrs.IsNotFound(err) {
376 return nil // nothing to do, no error
377 } else if err != nil {
378 return fmt.Errorf("failed to get PV for processing: %w", err)
379 }
380
381 if !p.isOurPV(pv) {
382 return nil
383 }
384 if pv.Spec.PersistentVolumeReclaimPolicy != v1.PersistentVolumeReclaimDelete || pv.Status.Phase != "Released" {
385 return nil
386 }
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200387 volumePath := p.volumePath(pv.Spec.CSI.VolumeHandle)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200388
389 // Log deletes for auditing purposes
Serge Bazanskic7359672020-10-30 16:38:57 +0100390 p.logger.Infof("Deleting persistent volume %s", pv.Spec.CSI.VolumeHandle)
Lorenz Brun37050122021-03-30 14:00:27 +0200391 switch *pv.Spec.VolumeMode {
392 case "", v1.PersistentVolumeFilesystem:
393 if err := fsquota.SetQuota(volumePath, 0, 0); err != nil {
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200394 // We record these here manually since a successful deletion
395 // removes the PV we'd be attaching them to.
Lorenz Brun37050122021-03-30 14:00:27 +0200396 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to remove quota: %v", err)
397 return fmt.Errorf("failed to remove quota: %w", err)
398 }
399 if err := os.RemoveAll(volumePath); err != nil && !os.IsNotExist(err) {
400 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to delete volume: %v", err)
401 return fmt.Errorf("failed to delete volume: %w", err)
402 }
403 case v1.PersistentVolumeBlock:
404 if err := os.Remove(volumePath); err != nil && !os.IsNotExist(err) {
405 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to delete volume: %v", err)
406 return fmt.Errorf("failed to delete volume: %w", err)
407 }
408 default:
409 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Invalid volume mode \"%v\"", *pv.Spec.VolumeMode)
410 return fmt.Errorf("invalid volume mode \"%v\"", *pv.Spec.VolumeMode)
Lorenz Brunb15abad2020-04-16 11:17:12 +0200411 }
412
Serge Bazanskic2c7ad92020-07-13 17:20:09 +0200413 err = p.Kubernetes.CoreV1().PersistentVolumes().Delete(context.Background(), pv.Name, metav1.DeleteOptions{})
Lorenz Brunb15abad2020-04-16 11:17:12 +0200414 if err != nil && !apierrs.IsNotFound(err) {
415 p.recorder.Eventf(pv, v1.EventTypeWarning, "DeprovisioningFailed", "Failed to delete PV object from K8s API: %v", err)
416 return fmt.Errorf("failed to delete PV object: %w", err)
417 }
418 return nil
419}