admission.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2014 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 exists
  14. import (
  15. "fmt"
  16. "io"
  17. "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apiserver/pkg/admission"
  20. genericadmissioninitializer "k8s.io/apiserver/pkg/admission/initializer"
  21. informers "k8s.io/client-go/informers"
  22. "k8s.io/client-go/kubernetes"
  23. corev1listers "k8s.io/client-go/listers/core/v1"
  24. api "k8s.io/kubernetes/pkg/apis/core"
  25. )
  26. // PluginName indicates name of admission plugin.
  27. const PluginName = "NamespaceExists"
  28. // Register registers a plugin
  29. func Register(plugins *admission.Plugins) {
  30. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  31. return NewExists(), nil
  32. })
  33. }
  34. // Exists is an implementation of admission.Interface.
  35. // It rejects all incoming requests in a namespace context if the namespace does not exist.
  36. // It is useful in deployments that want to enforce pre-declaration of a Namespace resource.
  37. type Exists struct {
  38. *admission.Handler
  39. client kubernetes.Interface
  40. namespaceLister corev1listers.NamespaceLister
  41. }
  42. var _ admission.ValidationInterface = &Exists{}
  43. var _ = genericadmissioninitializer.WantsExternalKubeInformerFactory(&Exists{})
  44. var _ = genericadmissioninitializer.WantsExternalKubeClientSet(&Exists{})
  45. // Validate makes an admission decision based on the request attributes
  46. func (e *Exists) Validate(a admission.Attributes, o admission.ObjectInterfaces) error {
  47. // if we're here, then we've already passed authentication, so we're allowed to do what we're trying to do
  48. // if we're here, then the API server has found a route, which means that if we have a non-empty namespace
  49. // its a namespaced resource.
  50. if len(a.GetNamespace()) == 0 || a.GetKind().GroupKind() == api.Kind("Namespace") {
  51. return nil
  52. }
  53. // we need to wait for our caches to warm
  54. if !e.WaitForReady() {
  55. return admission.NewForbidden(a, fmt.Errorf("not yet ready to handle request"))
  56. }
  57. _, err := e.namespaceLister.Get(a.GetNamespace())
  58. if err == nil {
  59. return nil
  60. }
  61. if !errors.IsNotFound(err) {
  62. return errors.NewInternalError(err)
  63. }
  64. // in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
  65. _, err = e.client.CoreV1().Namespaces().Get(a.GetNamespace(), metav1.GetOptions{})
  66. if err != nil {
  67. if errors.IsNotFound(err) {
  68. return err
  69. }
  70. return errors.NewInternalError(err)
  71. }
  72. return nil
  73. }
  74. // NewExists creates a new namespace exists admission control handler
  75. func NewExists() *Exists {
  76. return &Exists{
  77. Handler: admission.NewHandler(admission.Create, admission.Update, admission.Delete),
  78. }
  79. }
  80. // SetExternalKubeClientSet implements the WantsExternalKubeClientSet interface.
  81. func (e *Exists) SetExternalKubeClientSet(client kubernetes.Interface) {
  82. e.client = client
  83. }
  84. // SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
  85. func (e *Exists) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
  86. namespaceInformer := f.Core().V1().Namespaces()
  87. e.namespaceLister = namespaceInformer.Lister()
  88. e.SetReadyFunc(namespaceInformer.Informer().HasSynced)
  89. }
  90. // ValidateInitialization implements the InitializationValidator interface.
  91. func (e *Exists) ValidateInitialization() error {
  92. if e.namespaceLister == nil {
  93. return fmt.Errorf("missing namespaceLister")
  94. }
  95. if e.client == nil {
  96. return fmt.Errorf("missing client")
  97. }
  98. return nil
  99. }