admission.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2017 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 storageobjectinuseprotection
  14. import (
  15. "io"
  16. "k8s.io/klog"
  17. "k8s.io/apiserver/pkg/admission"
  18. "k8s.io/apiserver/pkg/util/feature"
  19. api "k8s.io/kubernetes/pkg/apis/core"
  20. "k8s.io/kubernetes/pkg/features"
  21. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  22. )
  23. const (
  24. // PluginName is the name of this admission controller plugin
  25. PluginName = "StorageObjectInUseProtection"
  26. )
  27. // Register registers a plugin
  28. func Register(plugins *admission.Plugins) {
  29. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  30. plugin := newPlugin()
  31. return plugin, nil
  32. })
  33. }
  34. // storageProtectionPlugin holds state for and implements the admission plugin.
  35. type storageProtectionPlugin struct {
  36. *admission.Handler
  37. }
  38. var _ admission.Interface = &storageProtectionPlugin{}
  39. // newPlugin creates a new admission plugin.
  40. func newPlugin() *storageProtectionPlugin {
  41. return &storageProtectionPlugin{
  42. Handler: admission.NewHandler(admission.Create),
  43. }
  44. }
  45. var (
  46. pvResource = api.Resource("persistentvolumes")
  47. pvcResource = api.Resource("persistentvolumeclaims")
  48. )
  49. // Admit sets finalizer on all PVCs(PVs). The finalizer is removed by
  50. // PVCProtectionController(PVProtectionController) when it's not referenced.
  51. //
  52. // This prevents users from deleting a PVC that's used by a running pod.
  53. // This also prevents admin from deleting a PV that's bound by a PVC
  54. func (c *storageProtectionPlugin) Admit(a admission.Attributes, o admission.ObjectInterfaces) error {
  55. if !feature.DefaultFeatureGate.Enabled(features.StorageObjectInUseProtection) {
  56. return nil
  57. }
  58. switch a.GetResource().GroupResource() {
  59. case pvResource:
  60. return c.admitPV(a)
  61. case pvcResource:
  62. return c.admitPVC(a)
  63. default:
  64. return nil
  65. }
  66. }
  67. func (c *storageProtectionPlugin) admitPV(a admission.Attributes) error {
  68. if len(a.GetSubresource()) != 0 {
  69. return nil
  70. }
  71. pv, ok := a.GetObject().(*api.PersistentVolume)
  72. // if we can't convert the obj to PV, just return
  73. if !ok {
  74. return nil
  75. }
  76. for _, f := range pv.Finalizers {
  77. if f == volumeutil.PVProtectionFinalizer {
  78. // Finalizer is already present, nothing to do
  79. return nil
  80. }
  81. }
  82. klog.V(4).Infof("adding PV protection finalizer to %s", pv.Name)
  83. pv.Finalizers = append(pv.Finalizers, volumeutil.PVProtectionFinalizer)
  84. return nil
  85. }
  86. func (c *storageProtectionPlugin) admitPVC(a admission.Attributes) error {
  87. if len(a.GetSubresource()) != 0 {
  88. return nil
  89. }
  90. pvc, ok := a.GetObject().(*api.PersistentVolumeClaim)
  91. // if we can't convert the obj to PVC, just return
  92. if !ok {
  93. return nil
  94. }
  95. for _, f := range pvc.Finalizers {
  96. if f == volumeutil.PVCProtectionFinalizer {
  97. // Finalizer is already present, nothing to do
  98. return nil
  99. }
  100. }
  101. klog.V(4).Infof("adding PVC protection finalizer to %s/%s", pvc.Namespace, pvc.Name)
  102. pvc.Finalizers = append(pvc.Finalizers, volumeutil.PVCProtectionFinalizer)
  103. return nil
  104. }