作用
当pv删除时候,如果存在finalizer则移除
当pv添加或者更新时候,如果不存在finalizer则添加
图

相关代码
前置相关代码
cmd/kube-controller-manager/app/controllermanager.go
func NewControllerInitializers(loopMode ControllerLoopMode) map[string]InitFunc {
...
controllers["pv-protection"] = startPVProtectionController
...
return controllers
}
cmd/kube-controller-manager/app/core.go
func startPVProtectionController(ctx context.Context, controllerContext ControllerContext) (controller.Interface, bool, error) {
go pvprotection.NewPVProtectionController(
controllerContext.InformerFactory.Core().V1().PersistentVolumes(),
controllerContext.ClientBuilder.ClientOrDie("pv-protection-controller"),
).Run(ctx, 1)
return nil, true, nil
}
pkg/controller/volume/pvprotection/pv_protection_controller.go
func NewPVProtectionController(pvInformer coreinformers.PersistentVolumeInformer, cl clientset.Interface) *Controller {
e := &Controller{
client: cl,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "pvprotection"),
}
if cl != nil && cl.CoreV1().RESTClient().GetRateLimiter() != nil {
ratelimiter.RegisterMetricAndTrackRateLimiterUsage("persistentvolume_protection_controller", cl.CoreV1().RESTClient().GetRateLimiter())
}
e.pvLister = pvInformer.Lister()
e.pvListerSynced = pvInformer.Informer().HasSynced
pvInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: e.pvAddedUpdated,
UpdateFunc: func(old, new interface{}) {
e.pvAddedUpdated(new)
},
})
return e
}
func (c *Controller) Run(ctx context.Context, workers int) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting PV protection controller")
defer klog.Infof("Shutting down PV protection controller")
if !cache.WaitForNamedCacheSync("PV protection", ctx.Done(), c.pvListerSynced) {
return
}
for i := 0; i < workers; i++ {
go wait.UntilWithContext(ctx, c.runWorker, time.Second)
}
<-ctx.Done()
}
主要代码
pkg/controller/volume/pvprotection/pv_protection_controller.go
func (c *Controller) processPV(ctx context.Context, pvName string) error {
klog.V(4).Infof("Processing PV %s", pvName)
startTime := time.Now()
defer func() {
klog.V(4).Infof("Finished processing PV %s (%v)", pvName, time.Since(startTime))
}()
pv, err := c.pvLister.Get(pvName)
if apierrors.IsNotFound(err) {
klog.V(4).Infof("PV %s not found, ignoring", pvName)
return nil
}
if err != nil {
return err
}
if protectionutil.IsDeletionCandidate(pv, volumeutil.PVProtectionFinalizer) {
// PV should be deleted. Check if it's used and remove finalizer if
// it's not.
isUsed := c.isBeingUsed(pv)
if !isUsed {
return c.removeFinalizer(ctx, pv)
}
klog.V(4).Infof("Keeping PV %s because it is being used", pvName)
}
if protectionutil.NeedToAddFinalizer(pv, volumeutil.PVProtectionFinalizer) {
// PV is not being deleted -> it should have the finalizer. The
// finalizer should be added by admission plugin, this is just to add
// the finalizer to old PVs that were created before the admission
// plugin was enabled.
return c.addFinalizer(ctx, pv)
}
return nil
}
网友评论