admission.go 3.9 KB

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