k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e_node/services/namespace_controller.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package services 18 19 import ( 20 "context" 21 "time" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/client-go/informers" 25 clientset "k8s.io/client-go/kubernetes" 26 "k8s.io/client-go/metadata" 27 restclient "k8s.io/client-go/rest" 28 namespacecontroller "k8s.io/kubernetes/pkg/controller/namespace" 29 "k8s.io/kubernetes/test/e2e/framework" 30 ) 31 32 const ( 33 // ncName is the name of namespace controller 34 ncName = "namespace-controller" 35 // ncResyncPeriod is resync period of the namespace controller 36 ncResyncPeriod = 5 * time.Minute 37 // ncConcurrency is concurrency of the namespace controller 38 ncConcurrency = 2 39 ) 40 41 // NamespaceController is a server which manages namespace controller. 42 type NamespaceController struct { 43 host string 44 stopCh chan struct{} 45 } 46 47 // NewNamespaceController creates a new namespace controller. 48 func NewNamespaceController(host string) *NamespaceController { 49 return &NamespaceController{host: host, stopCh: make(chan struct{})} 50 } 51 52 // Start starts the namespace controller. 53 func (n *NamespaceController) Start(ctx context.Context) error { 54 config := restclient.AddUserAgent(&restclient.Config{ 55 Host: n.host, 56 BearerToken: framework.TestContext.BearerToken, 57 TLSClientConfig: restclient.TLSClientConfig{ 58 Insecure: true, 59 }, 60 }, ncName) 61 62 // the namespace cleanup controller is very chatty. It makes lots of discovery calls and then it makes lots of delete calls. 63 config.QPS = 50 64 config.Burst = 200 65 66 client, err := clientset.NewForConfig(config) 67 if err != nil { 68 return err 69 } 70 metadataClient, err := metadata.NewForConfig(config) 71 if err != nil { 72 return err 73 } 74 discoverResourcesFn := client.Discovery().ServerPreferredNamespacedResources 75 informerFactory := informers.NewSharedInformerFactory(client, ncResyncPeriod) 76 77 nc := namespacecontroller.NewNamespaceController( 78 ctx, 79 client, 80 metadataClient, 81 discoverResourcesFn, 82 informerFactory.Core().V1().Namespaces(), 83 ncResyncPeriod, v1.FinalizerKubernetes, 84 ) 85 informerFactory.Start(n.stopCh) 86 go nc.Run(ctx, ncConcurrency) 87 return nil 88 } 89 90 // Stop stops the namespace controller. 91 func (n *NamespaceController) Stop() error { 92 close(n.stopCh) 93 return nil 94 } 95 96 // Name returns the name of namespace controller. 97 func (n *NamespaceController) Name() string { 98 return ncName 99 }