pv_protection_controller.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pvprotection
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. coreinformers "k8s.io/client-go/informers/core/v1"
  24. clientset "k8s.io/client-go/kubernetes"
  25. corelisters "k8s.io/client-go/listers/core/v1"
  26. "k8s.io/client-go/tools/cache"
  27. "k8s.io/client-go/util/workqueue"
  28. "k8s.io/component-base/metrics/prometheus/ratelimiter"
  29. "k8s.io/klog"
  30. "k8s.io/kubernetes/pkg/controller/volume/protectionutil"
  31. "k8s.io/kubernetes/pkg/util/slice"
  32. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  33. )
  34. // Controller is controller that removes PVProtectionFinalizer
  35. // from PVs that are not bound to PVCs.
  36. type Controller struct {
  37. client clientset.Interface
  38. pvLister corelisters.PersistentVolumeLister
  39. pvListerSynced cache.InformerSynced
  40. queue workqueue.RateLimitingInterface
  41. // allows overriding of StorageObjectInUseProtection feature Enabled/Disabled for testing
  42. storageObjectInUseProtectionEnabled bool
  43. }
  44. // NewPVProtectionController returns a new *Controller.
  45. func NewPVProtectionController(pvInformer coreinformers.PersistentVolumeInformer, cl clientset.Interface, storageObjectInUseProtectionFeatureEnabled bool) *Controller {
  46. e := &Controller{
  47. client: cl,
  48. queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "pvprotection"),
  49. storageObjectInUseProtectionEnabled: storageObjectInUseProtectionFeatureEnabled,
  50. }
  51. if cl != nil && cl.CoreV1().RESTClient().GetRateLimiter() != nil {
  52. ratelimiter.RegisterMetricAndTrackRateLimiterUsage("persistentvolume_protection_controller", cl.CoreV1().RESTClient().GetRateLimiter())
  53. }
  54. e.pvLister = pvInformer.Lister()
  55. e.pvListerSynced = pvInformer.Informer().HasSynced
  56. pvInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
  57. AddFunc: e.pvAddedUpdated,
  58. UpdateFunc: func(old, new interface{}) {
  59. e.pvAddedUpdated(new)
  60. },
  61. })
  62. return e
  63. }
  64. // Run runs the controller goroutines.
  65. func (c *Controller) Run(workers int, stopCh <-chan struct{}) {
  66. defer utilruntime.HandleCrash()
  67. defer c.queue.ShutDown()
  68. klog.Infof("Starting PV protection controller")
  69. defer klog.Infof("Shutting down PV protection controller")
  70. if !cache.WaitForNamedCacheSync("PV protection", stopCh, c.pvListerSynced) {
  71. return
  72. }
  73. for i := 0; i < workers; i++ {
  74. go wait.Until(c.runWorker, time.Second, stopCh)
  75. }
  76. <-stopCh
  77. }
  78. func (c *Controller) runWorker() {
  79. for c.processNextWorkItem() {
  80. }
  81. }
  82. // processNextWorkItem deals with one pvcKey off the queue. It returns false when it's time to quit.
  83. func (c *Controller) processNextWorkItem() bool {
  84. pvKey, quit := c.queue.Get()
  85. if quit {
  86. return false
  87. }
  88. defer c.queue.Done(pvKey)
  89. pvName := pvKey.(string)
  90. err := c.processPV(pvName)
  91. if err == nil {
  92. c.queue.Forget(pvKey)
  93. return true
  94. }
  95. utilruntime.HandleError(fmt.Errorf("PV %v failed with : %v", pvKey, err))
  96. c.queue.AddRateLimited(pvKey)
  97. return true
  98. }
  99. func (c *Controller) processPV(pvName string) error {
  100. klog.V(4).Infof("Processing PV %s", pvName)
  101. startTime := time.Now()
  102. defer func() {
  103. klog.V(4).Infof("Finished processing PV %s (%v)", pvName, time.Since(startTime))
  104. }()
  105. pv, err := c.pvLister.Get(pvName)
  106. if apierrors.IsNotFound(err) {
  107. klog.V(4).Infof("PV %s not found, ignoring", pvName)
  108. return nil
  109. }
  110. if err != nil {
  111. return err
  112. }
  113. if protectionutil.IsDeletionCandidate(pv, volumeutil.PVProtectionFinalizer) {
  114. // PV should be deleted. Check if it's used and remove finalizer if
  115. // it's not.
  116. isUsed := c.isBeingUsed(pv)
  117. if !isUsed {
  118. return c.removeFinalizer(pv)
  119. }
  120. }
  121. if protectionutil.NeedToAddFinalizer(pv, volumeutil.PVProtectionFinalizer) {
  122. // PV is not being deleted -> it should have the finalizer. The
  123. // finalizer should be added by admission plugin, this is just to add
  124. // the finalizer to old PVs that were created before the admission
  125. // plugin was enabled.
  126. return c.addFinalizer(pv)
  127. }
  128. return nil
  129. }
  130. func (c *Controller) addFinalizer(pv *v1.PersistentVolume) error {
  131. // Skip adding Finalizer in case the StorageObjectInUseProtection feature is not enabled
  132. if !c.storageObjectInUseProtectionEnabled {
  133. return nil
  134. }
  135. pvClone := pv.DeepCopy()
  136. pvClone.ObjectMeta.Finalizers = append(pvClone.ObjectMeta.Finalizers, volumeutil.PVProtectionFinalizer)
  137. _, err := c.client.CoreV1().PersistentVolumes().Update(context.TODO(), pvClone, metav1.UpdateOptions{})
  138. if err != nil {
  139. klog.V(3).Infof("Error adding protection finalizer to PV %s: %v", pv.Name, err)
  140. return err
  141. }
  142. klog.V(3).Infof("Added protection finalizer to PV %s", pv.Name)
  143. return nil
  144. }
  145. func (c *Controller) removeFinalizer(pv *v1.PersistentVolume) error {
  146. pvClone := pv.DeepCopy()
  147. pvClone.ObjectMeta.Finalizers = slice.RemoveString(pvClone.ObjectMeta.Finalizers, volumeutil.PVProtectionFinalizer, nil)
  148. _, err := c.client.CoreV1().PersistentVolumes().Update(context.TODO(), pvClone, metav1.UpdateOptions{})
  149. if err != nil {
  150. klog.V(3).Infof("Error removing protection finalizer from PV %s: %v", pv.Name, err)
  151. return err
  152. }
  153. klog.V(3).Infof("Removed protection finalizer from PV %s", pv.Name)
  154. return nil
  155. }
  156. func (c *Controller) isBeingUsed(pv *v1.PersistentVolume) bool {
  157. // check if PV is being bound to a PVC by its status
  158. // the status will be updated by PV controller
  159. if pv.Status.Phase == v1.VolumeBound {
  160. // the PV is being used now
  161. return true
  162. }
  163. return false
  164. }
  165. // pvAddedUpdated reacts to pv added/updated events
  166. func (c *Controller) pvAddedUpdated(obj interface{}) {
  167. pv, ok := obj.(*v1.PersistentVolume)
  168. if !ok {
  169. utilruntime.HandleError(fmt.Errorf("PV informer returned non-PV object: %#v", obj))
  170. return
  171. }
  172. klog.V(4).Infof("Got event on PV %s", pv.Name)
  173. if protectionutil.NeedToAddFinalizer(pv, volumeutil.PVProtectionFinalizer) || protectionutil.IsDeletionCandidate(pv, volumeutil.PVProtectionFinalizer) {
  174. c.queue.Add(pv.Name)
  175. }
  176. }