pv_protection_controller.go 6.3 KB

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