admission.go 4.1 KB

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