namespace_controller.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2016 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 services
  14. import (
  15. "time"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/client-go/informers"
  18. clientset "k8s.io/client-go/kubernetes"
  19. "k8s.io/client-go/metadata"
  20. restclient "k8s.io/client-go/rest"
  21. namespacecontroller "k8s.io/kubernetes/pkg/controller/namespace"
  22. )
  23. const (
  24. // ncName is the name of namespace controller
  25. ncName = "namespace-controller"
  26. // ncResyncPeriod is resync period of the namespace controller
  27. ncResyncPeriod = 5 * time.Minute
  28. // ncConcurrency is concurrency of the namespace controller
  29. ncConcurrency = 2
  30. )
  31. // NamespaceController is a server which manages namespace controller.
  32. type NamespaceController struct {
  33. host string
  34. stopCh chan struct{}
  35. }
  36. // NewNamespaceController creates a new namespace controller.
  37. func NewNamespaceController(host string) *NamespaceController {
  38. return &NamespaceController{host: host, stopCh: make(chan struct{})}
  39. }
  40. // Start starts the namespace controller.
  41. func (n *NamespaceController) Start() error {
  42. config := restclient.AddUserAgent(&restclient.Config{Host: n.host}, ncName)
  43. // the namespace cleanup controller is very chatty. It makes lots of discovery calls and then it makes lots of delete calls.
  44. config.QPS = 50
  45. config.Burst = 200
  46. client, err := clientset.NewForConfig(config)
  47. if err != nil {
  48. return err
  49. }
  50. metadataClient, err := metadata.NewForConfig(config)
  51. if err != nil {
  52. return err
  53. }
  54. discoverResourcesFn := client.Discovery().ServerPreferredNamespacedResources
  55. informerFactory := informers.NewSharedInformerFactory(client, ncResyncPeriod)
  56. nc := namespacecontroller.NewNamespaceController(
  57. client,
  58. metadataClient,
  59. discoverResourcesFn,
  60. informerFactory.Core().V1().Namespaces(),
  61. ncResyncPeriod, v1.FinalizerKubernetes,
  62. )
  63. informerFactory.Start(n.stopCh)
  64. go nc.Run(ncConcurrency, n.stopCh)
  65. return nil
  66. }
  67. // Stop stops the namespace controller.
  68. func (n *NamespaceController) Stop() error {
  69. close(n.stopCh)
  70. return nil
  71. }
  72. // Name returns the name of namespace controller.
  73. func (n *NamespaceController) Name() string {
  74. return ncName
  75. }