admission.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 autoprovision
  14. import (
  15. "fmt"
  16. "io"
  17. corev1 "k8s.io/api/core/v1"
  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. "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 = "NamespaceAutoProvision"
  29. // Register registers a plugin
  30. func Register(plugins *admission.Plugins) {
  31. plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
  32. return NewProvision(), nil
  33. })
  34. }
  35. // Provision is an implementation of admission.Interface.
  36. // It looks at all incoming requests in a namespace context, and if the namespace does not exist, it creates one.
  37. // It is useful in deployments that do not want to restrict creation of a namespace prior to its usage.
  38. type Provision struct {
  39. *admission.Handler
  40. client kubernetes.Interface
  41. namespaceLister corev1listers.NamespaceLister
  42. }
  43. var _ admission.MutationInterface = &Provision{}
  44. var _ = genericadmissioninitializer.WantsExternalKubeInformerFactory(&Provision{})
  45. var _ = genericadmissioninitializer.WantsExternalKubeClientSet(&Provision{})
  46. // Admit makes an admission decision based on the request attributes
  47. func (p *Provision) Admit(a admission.Attributes, o admission.ObjectInterfaces) error {
  48. // Don't create a namespace if the request is for a dry-run.
  49. if a.IsDryRun() {
  50. return nil
  51. }
  52. // if we're here, then we've already passed authentication, so we're allowed to do what we're trying to do
  53. // if we're here, then the API server has found a route, which means that if we have a non-empty namespace
  54. // its a namespaced resource.
  55. if len(a.GetNamespace()) == 0 || a.GetKind().GroupKind() == api.Kind("Namespace") {
  56. return nil
  57. }
  58. // we need to wait for our caches to warm
  59. if !p.WaitForReady() {
  60. return admission.NewForbidden(a, fmt.Errorf("not yet ready to handle request"))
  61. }
  62. _, err := p.namespaceLister.Get(a.GetNamespace())
  63. if err == nil {
  64. return nil
  65. }
  66. if !errors.IsNotFound(err) {
  67. return admission.NewForbidden(a, err)
  68. }
  69. namespace := &corev1.Namespace{
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: a.GetNamespace(),
  72. Namespace: "",
  73. },
  74. Status: corev1.NamespaceStatus{},
  75. }
  76. _, err = p.client.CoreV1().Namespaces().Create(namespace)
  77. if err != nil && !errors.IsAlreadyExists(err) {
  78. return admission.NewForbidden(a, err)
  79. }
  80. return nil
  81. }
  82. // NewProvision creates a new namespace provision admission control handler
  83. func NewProvision() *Provision {
  84. return &Provision{
  85. Handler: admission.NewHandler(admission.Create),
  86. }
  87. }
  88. // SetExternalKubeClientSet implements the WantsExternalKubeClientSet interface.
  89. func (p *Provision) SetExternalKubeClientSet(client kubernetes.Interface) {
  90. p.client = client
  91. }
  92. // SetExternalKubeInformerFactory implements the WantsExternalKubeInformerFactory interface.
  93. func (p *Provision) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
  94. namespaceInformer := f.Core().V1().Namespaces()
  95. p.namespaceLister = namespaceInformer.Lister()
  96. p.SetReadyFunc(namespaceInformer.Informer().HasSynced)
  97. }
  98. // ValidateInitialization implements the InitializationValidator interface.
  99. func (p *Provision) ValidateInitialization() error {
  100. if p.namespaceLister == nil {
  101. return fmt.Errorf("missing namespaceLister")
  102. }
  103. if p.client == nil {
  104. return fmt.Errorf("missing client")
  105. }
  106. return nil
  107. }